Repository navigation
Expand file tree
/
Copy pathinstall-cli.sh
More file actions
executable file
·307 lines (283 loc) · 10.9 KB
/
Copy pathinstall-cli.sh
File metadata and controls
executable file
·307 lines (283 loc) · 10.9 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
#!/bin/sh
# curl -fsSL https://levelrail.com/install-cli.sh | sh
#
# Installs levelrail-cli, the client for an already-running Levelrail
# control plane. This is the client, not the server: it needs no root,
# no systemd, no Docker. See install.sh for the server.
#
# Usage: install-cli.sh [--force]
# --force continue even if an existing levelrail-cli looks newer
#
# Env overrides:
# LEVELRAIL_VERSION release tag to install, e.g. v0.3.0
# LEVELRAIL_CHANNEL stable or beta, same meaning as install.sh
# LEVELRAIL_CLI_INSTALL_DIR where the binary goes (default: /usr/local/bin
# as root, otherwise $HOME/.local/bin, no root needed)
# LEVELRAIL_BINARY_FILE install this local binary instead of downloading
# LEVELRAIL_SKIP_CHECKSUM=1 install even when checksums.txt is missing or
# does not list the binary (unverified; opt-out only)
# APP_INSTALL_VERIFY signature check on checksums.txt (cosign keyless),
# same meaning as install.sh: auto, require, or off
# LEVELRAIL_RELEASE_BASE_URL download release assets from this https mirror
# instead of GitHub
# LEVELRAIL_MODIFY_PATH=1 when the install dir is not on PATH, append the
# export line to your shell profile (default: only
# print it, never edit files)
#
# Windows: use install-cli.ps1 (irm https://levelrail.com/install-cli.ps1 | iex)
set -eu
REPO="glincker/levelrail"
BINARY_NAME="levelrail-cli"
if [ "$(id -u)" -eq 0 ]; then
DEFAULT_INSTALL_DIR="/usr/local/bin"
else
DEFAULT_INSTALL_DIR="${HOME}/.local/bin"
fi
INSTALL_DIR="${LEVELRAIL_CLI_INSTALL_DIR:-${DEFAULT_INSTALL_DIR}}"
BIN_PATH="${INSTALL_DIR}/${BINARY_NAME}"
HTTPS_ONLY="=https"
VERIFY_MODE="${APP_INSTALL_VERIFY:-auto}"
COSIGN_IDENTITY_REGEXP="https://github.com/${REPO}/\\.github/workflows/release\\.yml@.*"
COSIGN_OIDC_ISSUER="https://token.actions.githubusercontent.com"
log() { printf '%s\n' "$*"; }
warn() { printf 'warning: %s\n' "$*" >&2; }
fatal() {
printf 'error: %s\n' "$*" >&2
exit 1
}
usage() {
cat <<'EOF'
Usage: install-cli.sh [--force]
--force continue even if an existing levelrail-cli looks newer
EOF
}
FORCE=0
for arg in "$@"; do
case "$arg" in
--force) FORCE=1 ;;
-h | --help)
usage
exit 0
;;
*) fatal "unknown argument: $arg" ;;
esac
done
detect_platform() {
os="$(uname -s)"
case "$os" in
Linux) GOOS="linux" ;;
Darwin) GOOS="darwin" ;;
MINGW* | MSYS* | CYGWIN*) fatal "this script is for Linux and macOS. On Windows run in PowerShell: irm https://levelrail.com/install-cli.ps1 | iex (or run this script from WSL)." ;;
*) fatal "unsupported OS: ${os}" ;;
esac
arch="$(uname -m)"
case "$arch" in
x86_64 | amd64) GOARCH="amd64" ;;
aarch64 | arm64) GOARCH="arm64" ;;
*) fatal "unsupported architecture: ${arch}" ;;
esac
}
github_api() {
curl -fsSL --proto "$HTTPS_ONLY" --tlsv1.2 --connect-timeout 10 --max-time 30 \
-H "Accept: application/vnd.github+json" "https://api.github.com/repos/${REPO}/$1" 2>/dev/null
}
# Same shape as install.sh's list_releases/resolve_version: picks the
# newest release on the channel that actually ships this platform's CLI
# asset, skipping releases with no assets yet.
list_releases() {
github_api "releases?per_page=30" | awk -v asset="\"name\": \"$1\"" '
/"tag_name":/ {
if (tag != "" && index(tag, "/") == 0) print tag, pre, has, created
tag = $0; sub(/.*"tag_name": *"/, "", tag); sub(/".*/, "", tag)
pre = "false"; has = 0; created = ""
}
/"prerelease": *true/ { pre = "true" }
/"created_at":/ && created == "" {
created = $0
sub(/.*"created_at": *"/, "", created); sub(/".*/, "", created)
}
index($0, asset) { has = 1 }
END { if (tag != "" && index(tag, "/") == 0) print tag, pre, has, created }' | sort -k4,4r
}
resolve_version() {
if [ -n "${LEVELRAIL_VERSION:-}" ]; then
VERSION="$LEVELRAIL_VERSION"
return
fi
channel="${LEVELRAIL_CHANNEL:-}"
case "$channel" in
"" | stable | beta) ;;
*) fatal "LEVELRAIL_CHANNEL must be stable or beta, got: $channel" ;;
esac
asset="${BINARY_NAME}-${GOOS}-${GOARCH}"
releases="$(list_releases "$asset" || true)"
[ -n "$releases" ] || fatal "could not list releases from GitHub. Set LEVELRAIL_VERSION=vX.Y.Z to pin one."
newest_stable="$(printf '%s\n' "$releases" | awk '$2 == "false" { print $1; exit }')"
stable="$(printf '%s\n' "$releases" | awk '$2 == "false" && $3 == 1 { print $1; exit }')"
newest_any="$(printf '%s\n' "$releases" | awk '{ print $1; exit }')"
any="$(printf '%s\n' "$releases" | awk '$3 == 1 { print $1; exit }')"
case "$channel" in
stable)
VERSION="$stable"
newest="$newest_stable"
;;
beta)
VERSION="$any"
newest="$newest_any"
;;
*)
VERSION="$stable"
newest="$newest_stable"
if [ -z "$VERSION" ]; then
VERSION="$any"
newest="$newest_any"
[ -z "$VERSION" ] || log "No stable release with a ${asset} binary yet, using pre-release ${VERSION}."
fi
;;
esac
[ -n "$VERSION" ] || fatal "no ${channel:-published} release ships ${asset}. Set LEVELRAIL_VERSION=vX.Y.Z to pin one."
if [ -n "$newest" ] && [ "$newest" != "$VERSION" ]; then
warn "${newest} has no ${asset} binary, installing ${VERSION} instead"
fi
}
# Same cosign keyless check install.sh runs, over the same checksums.txt
# a release publishes: the CLI binaries are checksummed and signed
# alongside the server binary by the one release workflow, nothing extra
# to set up here.
verify_signature() {
sums="$1"
base_url="$2"
proto="$3"
[ "$VERIFY_MODE" != "off" ] || return 0
if ! command -v cosign >/dev/null 2>&1; then
[ "$VERIFY_MODE" != "require" ] || fatal "APP_INSTALL_VERIFY=require but cosign is not installed. Install it from https://docs.sigstore.dev/cosign/system_config/installation/ or unset APP_INSTALL_VERIFY."
log "cosign not found: verifying the SHA-256 checksum only (install cosign to also verify the release signature)."
return 0
fi
bundle="${sums}.sigstore.json"
if ! curl -fsSL --proto "$proto" --tlsv1.2 --connect-timeout 10 --max-time 60 -o "$bundle" "${base_url}/checksums.txt.sigstore.json" 2>/dev/null; then
[ "$VERIFY_MODE" != "require" ] || fatal "APP_INSTALL_VERIFY=require but ${VERSION} publishes no checksums.txt.sigstore.json"
warn "${VERSION} publishes no signature for checksums.txt, verifying the SHA-256 checksum only"
return 0
fi
cosign verify-blob --bundle "$bundle" \
--certificate-identity-regexp "$COSIGN_IDENTITY_REGEXP" \
--certificate-oidc-issuer "$COSIGN_OIDC_ISSUER" "$sums" >/dev/null 2>&1 ||
fatal "cosign signature verification of checksums.txt failed for ${VERSION}, refusing to install"
log "Signature verified (cosign, keyless)."
}
checksum_unavailable() {
if [ "$VERIFY_MODE" = "require" ]; then
fatal "$1, and APP_INSTALL_VERIFY=require does not allow an unverified install"
fi
if [ "${LEVELRAIL_SKIP_CHECKSUM:-}" = "1" ]; then
warn "$1, skipping verification because LEVELRAIL_SKIP_CHECKSUM=1"
return 0
fi
fatal "$1, refusing to install an unverified binary. Pick another release with LEVELRAIL_VERSION, or set LEVELRAIL_SKIP_CHECKSUM=1 to install without verification."
}
fetch_binary() {
dest="$1"
if [ -n "${LEVELRAIL_BINARY_FILE:-}" ]; then
[ -f "$LEVELRAIL_BINARY_FILE" ] || fatal "LEVELRAIL_BINARY_FILE not found: $LEVELRAIL_BINARY_FILE"
log "Using local binary ${LEVELRAIL_BINARY_FILE}."
cp "$LEVELRAIL_BINARY_FILE" "$dest"
VERSION="${LEVELRAIL_VERSION:-local build}"
return
fi
resolve_version
asset="${BINARY_NAME}-${GOOS}-${GOARCH}"
base_url="${LEVELRAIL_RELEASE_BASE_URL:-https://github.com/${REPO}/releases/download/${VERSION}}"
proto="$HTTPS_ONLY"
case "$base_url" in
https://*) ;;
http://*)
[ "${LEVELRAIL_INSECURE_MIRROR:-}" = "1" ] || fatal "LEVELRAIL_RELEASE_BASE_URL must be https (LEVELRAIL_INSECURE_MIRROR=1 allows http for tests)"
proto="=http"
;;
*) fatal "LEVELRAIL_RELEASE_BASE_URL must start with https://" ;;
esac
log "Downloading ${asset} ${VERSION}..."
curl -fsSL --proto "$proto" --tlsv1.2 --connect-timeout 10 --max-time 120 -o "$dest" "${base_url}/${asset}" ||
fatal "download failed: ${base_url}/${asset}. The release may not ship this binary; pick another with LEVELRAIL_VERSION."
sums="${dest}.checksums"
if curl -fsSL --proto "$proto" --tlsv1.2 --connect-timeout 10 --max-time 60 -o "$sums" "${base_url}/checksums.txt" 2>/dev/null; then
verify_signature "$sums" "$base_url" "$proto"
expected="$(grep " ${asset}\$" "$sums" | awk '{ print $1 }')"
if [ -n "$expected" ]; then
actual="$(sha256sum "$dest" 2>/dev/null | awk '{ print $1 }')"
[ -n "$actual" ] || actual="$(shasum -a 256 "$dest" | awk '{ print $1 }')"
[ "$expected" = "$actual" ] || fatal "checksum mismatch for ${asset}: expected ${expected}, got ${actual}"
log "Checksum verified."
else
checksum_unavailable "${asset} is not listed in checksums.txt for ${VERSION}"
fi
else
checksum_unavailable "no checksums.txt published for ${VERSION}"
fi
}
# Picks the profile file and the exact line for the user's login shell.
# Prints by default; only writes when LEVELRAIL_MODIFY_PATH=1.
suggest_path() {
shell_name="$(basename "${SHELL:-sh}")"
case "$shell_name" in
zsh)
profile="${ZDOTDIR:-$HOME}/.zshrc"
line="export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
bash)
# macOS terminals start login shells, which read .bash_profile and not .bashrc.
if [ "${GOOS:-}" = "darwin" ]; then
profile="${HOME}/.bash_profile"
else
profile="${HOME}/.bashrc"
fi
line="export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
fish)
profile="${HOME}/.config/fish/config.fish"
line="fish_add_path \"${INSTALL_DIR}\""
;;
*)
profile="${HOME}/.profile"
line="export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
esac
if [ "${LEVELRAIL_MODIFY_PATH:-}" = "1" ]; then
if [ -f "$profile" ] && grep -qF -- "$line" "$profile"; then
log "${profile} already puts ${INSTALL_DIR} on PATH. Open a new terminal."
return
fi
mkdir -p "$(dirname "$profile")"
printf '\n# Added by the levelrail-cli installer\n%s\n' "$line" >>"$profile"
log "Added ${INSTALL_DIR} to PATH in ${profile}. Open a new terminal or run: ${line}"
return
fi
warn "${INSTALL_DIR} is not on your PATH. Add this line to ${profile}:"
log " ${line}"
log "Or re-run with LEVELRAIL_MODIFY_PATH=1 to have the installer add it."
}
main() {
detect_platform
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT
tmp_bin="${tmp_dir}/${BINARY_NAME}"
if [ -x "$BIN_PATH" ] && [ "$FORCE" -eq 0 ] && [ -z "${LEVELRAIL_VERSION:-}" ] && [ -z "${LEVELRAIL_BINARY_FILE:-}" ]; then
current="$("$BIN_PATH" version 2>/dev/null | head -1 || true)"
[ -z "$current" ] || log "Found existing ${BIN_PATH} (${current}), checking for a newer release..."
fi
fetch_binary "$tmp_bin"
chmod +x "$tmp_bin"
mkdir -p "$INSTALL_DIR"
mv "$tmp_bin" "$BIN_PATH"
log "Installed ${BIN_PATH} (${VERSION})."
case ":$PATH:" in
*":${INSTALL_DIR}:"*) ;;
*) suggest_path ;;
esac
log ""
log "Next: point it at your running instance and log in."
log " export APP_API_URL=https://your-dashboard-domain"
log " ${BINARY_NAME} auth login --device"
}
main