-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·424 lines (360 loc) · 9.02 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·424 lines (360 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env sh
set -eu
BINARY_NAME="ucloud-sandbox-cli"
DEFAULT_BIN_DIR="/usr/local/bin"
DEFAULT_BASE_URL="https://github.com/ucloud/ucloud-sandbox-cli/releases"
DOCS_URL="https://astraflow.ucloud.cn/docs/agent-sandbox/product/cli"
BOLD="$(tput bold 2>/dev/null || printf '')"
GREEN="$(tput setaf 2 2>/dev/null || printf '')"
YELLOW="$(tput setaf 3 2>/dev/null || printf '')"
BLUE="$(tput setaf 4 2>/dev/null || printf '')"
RED="$(tput setaf 1 2>/dev/null || printf '')"
NO_COLOR="$(tput sgr0 2>/dev/null || printf '')"
BIN_DIR="${BIN_DIR:-$DEFAULT_BIN_DIR}"
VERSION="${VERSION:-latest}"
BASE_URL="${BASE_URL:-$DEFAULT_BASE_URL}"
YES=0
TMP_DIR=""
info() {
printf '%s\n' "${BOLD}>${NO_COLOR} $*"
}
warn() {
printf '%s\n' "${YELLOW}! $*${NO_COLOR}"
}
error() {
printf '%s\n' "${RED}x $*${NO_COLOR}" >&2
}
success() {
printf '%s\n' "${GREEN}+${NO_COLOR} $*"
}
has() {
command -v "$1" >/dev/null 2>&1
}
usage() {
cat <<EOF
install.sh [options]
Download and install ${BINARY_NAME}.
Options:
-y, --yes Skip confirmation prompts
-p, --path <dir> Installation directory [default: ${DEFAULT_BIN_DIR}]
-v, --version <ver> Version to install [default: latest]
-u, --url <url> Release download base URL [default: ${DEFAULT_BASE_URL}]
-h, --help Show this help message
Examples:
sh install.sh
sh install.sh -y -p "\$HOME/.local/bin"
sh install.sh -v v1.2.3
When running through curl:
curl -sS https://raw.githubusercontent.com/ucloud/ucloud-sandbox-cli/main/install.sh | sh -s -- -y
EOF
}
need_value() {
if [ "$#" -lt 2 ] || [ -z "$2" ]; then
error "Option $1 requires a value."
exit 1
fi
}
read_from_tty() {
if [ -r /dev/tty ]; then
IFS= read -r REPLY </dev/tty
else
IFS= read -r REPLY
fi
}
choose_install_dir() {
if [ "$YES" -eq 1 ]; then
return 0
fi
printf 'Installation directory [%s]: ' "$BIN_DIR" >/dev/tty 2>/dev/null || printf 'Installation directory [%s]: ' "$BIN_DIR"
if ! read_from_tty; then
error "Unable to read installation directory. Re-run with -y or pass -p <dir>."
exit 1
fi
if [ -n "$REPLY" ]; then
BIN_DIR="$REPLY"
fi
if [ -z "$BIN_DIR" ]; then
error "Installation directory cannot be empty."
exit 1
fi
}
detect_os() {
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$os" in
linux)
printf 'linux'
;;
darwin)
printf 'darwin'
;;
*)
error "Unsupported operating system: $os"
info "Supported operating systems are: linux, darwin."
exit 1
;;
esac
}
detect_arch() {
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
case "$arch" in
x86_64 | amd64)
printf 'amd64'
;;
arm64 | aarch64)
printf 'arm64'
;;
*)
error "Unsupported architecture: $arch"
info "Supported architectures are: amd64, arm64."
exit 1
;;
esac
}
build_url() {
asset="${BINARY_NAME}_${OS}_${ARCH}.tar.gz"
base="${BASE_URL%/}"
if [ "$VERSION" = "latest" ]; then
printf '%s/latest/download/%s' "$base" "$asset"
else
printf '%s/download/%s/%s' "$base" "$VERSION" "$asset"
fi
}
download() {
file="$1"
url="$2"
if has curl; then
curl --fail --location --progress-bar --output "$file" "$url"
elif has wget; then
wget --output-document="$file" "$url"
else
error "Neither curl nor wget was found."
info "Please install curl or wget first, then run this installer again."
exit 1
fi
}
verify_sha256() {
file="$1"
checksum_file="$2"
expected="$(tr -d '[:space:]' <"$checksum_file" | tr '[:upper:]' '[:lower:]')"
case "$expected" in
*[!0-9a-f]* | "")
error "Release checksum is invalid."
exit 1
;;
esac
if [ "${#expected}" -ne 64 ]; then
error "Release checksum must contain exactly 64 hexadecimal characters."
exit 1
fi
if has sha256sum; then
actual="$(sha256sum "$file" | awk '{ print $1 }')"
elif has shasum; then
actual="$(shasum -a 256 "$file" | awk '{ print $1 }')"
elif has openssl; then
actual="$(openssl dgst -sha256 "$file" | awk '{ print $NF }')"
else
error "No SHA256 implementation was found (sha256sum, shasum, or openssl)."
exit 1
fi
actual="$(printf '%s' "$actual" | tr '[:upper:]' '[:lower:]')"
if [ "$actual" != "$expected" ]; then
error "SHA256 verification failed for the downloaded release asset."
exit 1
fi
}
make_tmp_dir() {
if ! has mktemp; then
error "mktemp was not found."
info "Install mktemp, then run this installer again."
exit 1
fi
TMPDIR="${TMPDIR:-/tmp}"
if [ ! -d "$TMPDIR" ] || [ ! -w "$TMPDIR" ]; then
error "Temporary directory is unavailable: $TMPDIR"
exit 1
fi
# Pass an explicit template so both BSD/macOS and GNU mktemp create the
# directory under the caller's temporary root. The bare `mktemp -d` form
# can ignore TMPDIR on macOS and land in a sandbox-denied system directory.
TMP_DIR="$(mktemp -d "${TMPDIR%/}/ucloud-sandbox-cli.XXXXXXXX")"
}
cleanup() {
if [ -n "$TMP_DIR" ] && [ -d "$TMP_DIR" ]; then
rm -rf "$TMP_DIR"
fi
}
test_writable() {
dir="$1"
test_file="${dir}/.ucloud-sandbox-cli-install-test.$$"
if touch "$test_file" 2>/dev/null; then
rm -f "$test_file"
return 0
fi
return 1
}
elevate_privileges() {
if ! has sudo; then
error "The installation directory is not writable and sudo is not available."
info "Install sudo, run this installer as root, or pass -p with a writable directory."
exit 1
fi
if ! sudo -v; then
error "Could not obtain sudo permissions."
exit 1
fi
}
prepare_install_dir() {
SUDO=""
if [ -d "$BIN_DIR" ]; then
if test_writable "$BIN_DIR"; then
return 0
fi
warn "Escalated permissions are required to write to ${BIN_DIR}."
elevate_privileges
SUDO="sudo"
return 0
fi
if mkdir -p "$BIN_DIR" 2>/dev/null && test_writable "$BIN_DIR"; then
return 0
fi
warn "Escalated permissions are required to create ${BIN_DIR}."
elevate_privileges
SUDO="sudo"
$SUDO mkdir -p "$BIN_DIR"
}
install_binary() {
archive="$1"
extract_dir="${TMP_DIR}/extract"
mkdir -p "$extract_dir"
tar -xzf "$archive" -C "$extract_dir"
if [ ! -f "${extract_dir}/${BINARY_NAME}" ]; then
error "Archive did not contain ${BINARY_NAME}."
exit 1
fi
chmod +x "${extract_dir}/${BINARY_NAME}"
$SUDO cp "${extract_dir}/${BINARY_NAME}" "${BIN_DIR}/${BINARY_NAME}"
$SUDO chmod 755 "${BIN_DIR}/${BINARY_NAME}"
}
verify_installation() {
printf '\n'
if [ -x "${BIN_DIR}/${BINARY_NAME}" ]; then
info "Installed binary version:"
"${BIN_DIR}/${BINARY_NAME}" version
fi
if has "$BINARY_NAME"; then
found_path="$(command -v "$BINARY_NAME")"
if [ "$found_path" != "${BIN_DIR}/${BINARY_NAME}" ]; then
warn "${BINARY_NAME} is available in PATH as ${found_path}, which is different from ${BIN_DIR}/${BINARY_NAME}."
fi
return 0
fi
warn "${BINARY_NAME} was installed to ${BIN_DIR}, but the command was not found in PATH."
info "Add the installation directory to PATH, for example:"
printf ' export PATH="%s:$PATH"\n' "$BIN_DIR"
}
parse_args() {
while [ "$#" -gt 0 ]; do
case "$1" in
-y | --yes)
YES=1
shift
;;
-p | --path)
need_value "$1" "${2-}"
BIN_DIR="$2"
shift 2
;;
-p=* | --path=*)
BIN_DIR="${1#*=}"
shift
;;
-v | --version)
need_value "$1" "${2-}"
VERSION="$2"
shift 2
;;
-v=* | --version=*)
VERSION="${1#*=}"
shift
;;
-u | --url)
need_value "$1" "${2-}"
BASE_URL="$2"
shift 2
;;
-u=* | --url=*)
BASE_URL="${1#*=}"
shift
;;
-h | --help)
usage
exit 0
;;
*)
error "Unknown option: $1"
usage
exit 1
;;
esac
done
if [ -z "$BIN_DIR" ]; then
error "Installation directory cannot be empty."
exit 1
fi
if [ -z "$VERSION" ]; then
error "Version cannot be empty."
exit 1
fi
if [ -z "$BASE_URL" ]; then
error "Download URL cannot be empty."
exit 1
fi
}
main() {
parse_args "$@"
printf '\n'
info "Welcome to the ${BINARY_NAME} installer."
if ! has tar; then
error "tar was not found."
info "Please install tar first, then run this installer again."
exit 1
fi
OS="$(detect_os)"
ARCH="$(detect_arch)"
URL="$(build_url)"
info "Installer configuration:"
info " Version: ${GREEN}${VERSION}${NO_COLOR}"
info " OS: ${GREEN}${OS}${NO_COLOR}"
info " Arch: ${GREEN}${ARCH}${NO_COLOR}"
info " Install dir: ${GREEN}${BIN_DIR}${NO_COLOR}"
info " Download URL: ${BLUE}${URL}${NO_COLOR}"
printf '\n'
choose_install_dir
prepare_install_dir
make_tmp_dir
trap cleanup EXIT INT TERM
archive="${TMP_DIR}/${BINARY_NAME}.tar.gz"
checksum="${archive}.sha256"
info "Downloading ${BINARY_NAME}..."
if ! download "$archive" "$URL"; then
error "Download failed: $URL"
if [ "$VERSION" != "latest" ]; then
info "Make sure the release tag matches exactly, for example v1.2.3 if tags use the v prefix."
fi
exit 1
fi
if ! download "$checksum" "${URL}.sha256"; then
error "Checksum download failed: ${URL}.sha256"
exit 1
fi
info "Verifying release SHA256..."
verify_sha256 "$archive" "$checksum"
info "Installing ${BINARY_NAME} to ${BIN_DIR}..."
install_binary "$archive"
success "${BINARY_NAME} installed successfully."
verify_installation
printf '\n'
info "Documentation: ${DOCS_URL}"
info "Run '${BINARY_NAME} login' first, then start using the CLI."
}
main "$@"