-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·402 lines (338 loc) · 10.4 KB
/
install.sh
File metadata and controls
executable file
·402 lines (338 loc) · 10.4 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
#!/usr/bin/env sh
set -eu
CHANNEL="${UNIKRAFT_CLI_INSTALL_CHANNEL:-stable}"
VERSION="${UNIKRAFT_CLI_INSTALL_VERSION:-}"
BIN_DIR="${UNIKRAFT_CLI_INSTALL_BIN_DIR:-}"
# Override URLs for testing
BASE_URL="${UNIKRAFT_CLI_INSTALL_URL:-https://pkg.unikraft.com}"
# Spinner characters
SPINNER_CHARS="|/-\\"
SPINNER_PID=""
SPINNER_MSG=""
# Colors
CYAN=$(printf '\033[36m')
GREEN=$(printf '\033[32m')
RED=$(printf '\033[31m')
YELLOW=$(printf '\033[33m')
RESET=$(printf '\033[0m')
# Clean up spinner on exit
cleanup() {
stop_spinner
}
trap cleanup EXIT
start_spinner() {
SPINNER_MSG="$1"
# Only show spinner if stdout is a terminal
if [ ! -t 1 ]; then
return
fi
(
i=0
len=${#SPINNER_CHARS}
while :; do
idx=$((i % len + 1))
ch=$(printf "%s" "$SPINNER_CHARS" | cut -c "$idx")
printf "\r%s%s%s %s " "$CYAN" "$ch" "$RESET" "$SPINNER_MSG"
i=$((i + 1))
sleep 0.1
done
) &
SPINNER_PID=$!
}
stop_spinner() {
if [ -n "${SPINNER_PID:-}" ]; then
kill "$SPINNER_PID" 2>/dev/null || true
wait "$SPINNER_PID" 2>/dev/null || true
SPINNER_PID=""
# Clear the spinner line
if [ -t 1 ]; then
printf "\r\033[K"
fi
fi
}
step_done() {
msg="$1"
stop_spinner
printf "%s✓%s %s\n" "$GREEN" "$RESET" "$msg"
}
step_fail() {
msg="$1"
stop_spinner
printf "%s✗%s %s\n" "$RED" "$RESET" "$msg" >&2
}
usage() {
echo "Install the Unikraft CLI"
echo
echo "Usage: $0 [--channel <stable|staging>] [--version vX.Y.Z] [--bin-dir <dir>]"
echo
echo "Options:"
echo " --channel Release channel (default: stable). Ignored if --version is set."
echo " --version Install a specific version tag (e.g., v1.2.3)."
echo " --bin-dir Directory to install the unikraft CLI binary into (default: ~/.local/bin)."
echo " -h, --help Show this help message."
echo
echo "Influential environment variables:"
echo " UNIKRAFT_CLI_INSTALL_URL Override base download URL"
echo " UNIKRAFT_CLI_INSTALL_CHANNEL Override default channel"
echo " UNIKRAFT_CLI_INSTALL_VERSION Override version"
echo " UNIKRAFT_CLI_INSTALL_BIN_DIR Override install directory"
}
err() { echo "Error: $*" >&2; exit 1; }
need_cmd() { command -v "$1" >/dev/null 2>&1 || err "Required command not found: $1"; }
# HTTP download abstraction - uses curl if available, falls back to wget
# Usage: http_download <url> <output_file>
# Returns: HTTP status code (or approximation for wget)
# Sets: HTTP_CODE variable, SSL_ERROR variable ("1" if SSL error, "" otherwise)
http_download() {
url="$1"
output="$2"
SSL_ERROR=""
if command -v curl >/dev/null 2>&1; then
curl_exit=0
HTTP_CODE=$(curl -sSL -w '%{http_code}' -o "$output" "$url" 2>/dev/null) || curl_exit=$?
if [ "$curl_exit" -ne 0 ]; then
# curl SSL-related exit codes:
# 35: SSL connect error
# 51: Peer's SSL certificate was not OK
# 60: Peer certificate cannot be authenticated with known CA certificates
# 77: Problem reading SSL CA cert
case "$curl_exit" in
35|51|60|77) SSL_ERROR="1" ;;
esac
HTTP_CODE="000"
fi
elif command -v wget >/dev/null 2>&1; then
# wget doesn't easily return HTTP codes, so we parse server response
stderr_file=$(mktemp)
if wget -q -S -O "$output" "$url" 2>"$stderr_file"; then
wget_exit=0
else
wget_exit=$?
fi
# wget exit code 5 indicates SSL verification failure
if [ "$wget_exit" -eq 5 ]; then
SSL_ERROR="1"
fi
# Try to extract HTTP code from server response
HTTP_CODE=$(awk '{for (i=1; i<=NF; i++) if ($i ~ /^[0-9][0-9][0-9]$/) code=$i} END{print code}' "$stderr_file")
# If we couldn't parse it, use exit-code-based approximation
if [ -z "$HTTP_CODE" ]; then
if [ "$wget_exit" -eq 0 ]; then
HTTP_CODE="200"
else
HTTP_CODE="000"
fi
fi
rm -f "$stderr_file"
else
err "Neither curl nor wget found. Please install one of them."
fi
}
need_http_cmd() {
if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then
err "Neither curl nor wget found. Please install one of them."
fi
}
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
--channel)
[ -z "${2:-}" ] && err "Option --channel requires a value"
CHANNEL="$2"; shift 2 ;;
--version)
[ -z "${2:-}" ] && err "Option --version requires a value"
VERSION="$2"; shift 2 ;;
--bin-dir)
[ -z "${2:-}" ] && err "Option --bin-dir requires a value"
BIN_DIR="$2"; shift 2 ;;
-h|--help)
usage; exit 0 ;;
*)
err "Unknown argument: $1" ;;
esac
done
}
detect_platform() {
os=$(uname -s)
arch=$(uname -m)
case "$arch" in
x86_64|amd64) arch="amd64" ;;
aarch64|arm64) arch="arm64" ;;
*) err "Unsupported architecture: $arch" ;;
esac
case "$os" in
Linux) PLATFORM="linux" EXT="tar.gz" BIN_NAME="unikraft" ;;
Darwin) PLATFORM="darwin" EXT="tar.gz" BIN_NAME="unikraft" ;;
*) err "Unsupported OS: $os (use the PowerShell installer on Windows)" ;;
esac
ARCH="$arch"
}
resolve_prefix() {
# Determine S3 prefix where artifacts are stored
if [ -n "$VERSION" ]; then
PREFIX="endpoints/cli/content/${VERSION}/"
return
fi
need_http_cmd
start_spinner "Fetching latest version"
v=""
ch_file_url="${BASE_URL}/endpoints/cli/content/${CHANNEL}.txt"
# Try to fetch the channel file, capturing both output and HTTP code
tmpfile=$(mktemp)
http_download "$ch_file_url" "$tmpfile"
if [ "$HTTP_CODE" = "200" ]; then
v=$(tr -d '\r\n' < "$tmpfile")
fi
rm -f "$tmpfile"
if [ -z "$v" ]; then
step_fail "Fetching latest version"
if [ "$HTTP_CODE" = "000" ]; then
if [ -n "$SSL_ERROR" ]; then
err "SSL certificate verification failed connecting to $BASE_URL. Ensure CA certificates are installed (e.g., ca-certificates package)."
else
err "Network error: could not connect to $BASE_URL"
fi
elif [ "$HTTP_CODE" = "404" ]; then
err "Version file not found at ${ch_file_url} (HTTP 404)"
else
err "Failed to fetch version info (HTTP $HTTP_CODE)"
fi
fi
step_done "Fetching latest version ($v)"
VERSION="$v"
PREFIX="endpoints/cli/content/${VERSION}/"
}
dir_in_path() {
check_dir="$1"
# Normalize the directory path
if [ -d "$check_dir" ]; then
check_dir=$(cd "$check_dir" 2>/dev/null && pwd) || return 1
fi
case ":$PATH:" in
*":$check_dir:") return 0 ;;
*) return 1 ;;
esac
}
choose_bindir() {
default_bin_dir="${UNIKRAFT_CLI_INSTALL_DEFAULT_BIN_DIR:-$HOME/.local/bin}"
if [ -n "$BIN_DIR" ]; then
# User explicitly specified a directory - use it as-is
DEST_DIR="$BIN_DIR"
else
# Check common home bin directories in preference order
if [ -n "${UNIKRAFT_CLI_INSTALL_PREFERRED_DIRS:-}" ]; then
preferred_dirs="$UNIKRAFT_CLI_INSTALL_PREFERRED_DIRS"
else
preferred_dirs="$HOME/.local/bin:$HOME/bin:$HOME/.bin"
fi
DEST_DIR=""
old_ifs=$IFS
IFS=:
for dir in $preferred_dirs; do
if dir_in_path "$dir"; then
DEST_DIR="$dir"
break
fi
done
IFS=$old_ifs
# If none of the preferred dirs are in PATH, warn and use default
if [ -z "$DEST_DIR" ]; then
printf "%sWarning:%s None of the standard bin directories (~/.local/bin, ~/bin, ~/.bin) are in your PATH.\n" "$YELLOW" "$RESET"
printf " Using %s%s%s - you may need to add it to your PATH.\n" "$CYAN" "$default_bin_dir" "$RESET"
DEST_DIR="$default_bin_dir"
fi
fi
mkdir_err=$(mkdir -p "$DEST_DIR" 2>&1) || err "Could not create bin directory $DEST_DIR: $mkdir_err"
if [ ! -d "$DEST_DIR" ]; then
err "Could not create bin directory: $DEST_DIR"
fi
}
verify_checksum() {
archive_path="$1"
sha_url="$2"
start_spinner "Verifying checksum"
sha_tmp=$(mktemp)
http_download "$sha_url" "$sha_tmp"
if [ "$HTTP_CODE" != "200" ]; then
rm -f "$sha_tmp" || true
# Checksum file missing is a warning, not a fatal error
stop_spinner
printf "%s⚠%s Checksum file not available, skipping verification\n" "$YELLOW" "$RESET"
return
fi
expected=$(awk '{print $1; exit}' "$sha_tmp")
if command -v sha256sum >/dev/null 2>&1; then
actual=$(sha256sum "$archive_path") || err "Failed to calculate sha256"
actual=${actual%% *}
else
need_cmd shasum
actual=$(shasum -a 256 "$archive_path") || err "Failed to calculate sha256"
actual=${actual%% *}
fi
rm -f "$sha_tmp" || true
if [ "$actual" != "$expected" ]; then
step_fail "Verifying checksum"
err "Checksum mismatch: expected $expected, got $actual. The download may be corrupted."
fi
step_done "Verifying checksum"
}
extract_and_install() {
archive_path="$1"
start_spinner "Installing to $DEST_DIR"
extract_err=$(tar -xzf "$archive_path" "$BIN_NAME" 2>&1) || {
step_fail "Installing to $DEST_DIR"
err "Failed to extract archive: $extract_err"
}
install_err=$(install -m 0755 "$BIN_NAME" "$DEST_DIR/$BIN_NAME" 2>&1) || {
step_fail "Installing to $DEST_DIR"
err "Failed to install binary to $DEST_DIR: $install_err"
}
step_done "Installed to $DEST_DIR"
}
post_install_note() {
case ":$PATH:" in
*":$DEST_DIR:") return 0 ;;
esac
if [ -n "$DEST_DIR" ]; then
echo " Add to PATH: export PATH=\"$DEST_DIR:\$PATH\""
fi
}
configure_auth() {
printf "\nRun %sunikraft login%s to get started.\n" "$CYAN" "$RESET"
}
main() {
parse_args "$@"
detect_platform
resolve_prefix
choose_bindir
asset="unikraft-${PLATFORM}-${ARCH}.${EXT}"
url="${BASE_URL}/${PREFIX}${asset}"
sha_url="${url}.sha256"
need_http_cmd
need_cmd tar
tmpdir=$(mktemp -d)
archive_path="$tmpdir/$asset"
start_spinner "Downloading Unikraft CLI"
http_download "$url" "$archive_path"
if [ "$HTTP_CODE" != "200" ]; then
step_fail "Downloading Unikraft CLI"
if [ "$HTTP_CODE" = "000" ]; then
if [ -n "$SSL_ERROR" ]; then
err "SSL certificate verification failed. Ensure CA certificates are installed (e.g., ca-certificates package)."
else
err "Network error: could not connect to download server"
fi
elif [ "$HTTP_CODE" = "404" ]; then
err "Binary not found (HTTP 404). Version $VERSION may not exist for ${PLATFORM}/${ARCH}."
else
err "Download failed (HTTP $HTTP_CODE)"
fi
fi
step_done "Downloading Unikraft CLI"
verify_checksum "$archive_path" "$sha_url"
(cd "$tmpdir" && extract_and_install "$archive_path")
rm -rf "$tmpdir" || true
post_install_note
configure_auth
}
main "$@"