-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·370 lines (316 loc) · 11 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·370 lines (316 loc) · 11 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
#!/bin/bash
# Orbit local CLI (`orbit`) installation script.
# Supports macOS (darwin) and Linux on x86_64 and aarch64.
#
# Usage:
# curl -fsSL https://gitlab.com/gitlab-org/orbit/knowledge-graph/-/raw/main/install.sh | bash
# curl -fsSL https://gitlab.com/gitlab-org/orbit/knowledge-graph/-/raw/main/install.sh | bash -s -- --version v0.51.0
# curl -fsSL https://gitlab.com/gitlab-org/orbit/knowledge-graph/-/raw/main/install.sh | bash -s -- --libc musl
# curl -fsSL https://gitlab.com/gitlab-org/orbit/knowledge-graph/-/raw/main/install.sh | bash -s -- --force
#
# Or, after downloading:
# bash install.sh
set -euo pipefail
INSTALL_DIR="${HOME}/.local/bin"
TEMP_DIR=$(mktemp -d)
VERSION=""
FORCE_INSTALL=false
LINUX_LIBC=""
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
cleanup() {
if [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
fi
}
trap cleanup EXIT
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
success() {
echo -e "${GREEN}$1${NC}"
}
warning() {
echo -e "${YELLOW}$1${NC}"
}
usage() {
cat <<EOF
Usage: $0 [OPTIONS]
OPTIONS:
--version VERSION Install a specific version (e.g., v0.51.0). Defaults to the latest release.
--libc LIBC Linux libc artifact to install: gnu or musl. Defaults to auto-detection.
--force Reinstall even if 'orbit' already exists in the install directory.
--help Show this help message.
EXAMPLES:
bash install.sh
bash install.sh --version v0.51.0
bash install.sh --libc musl
bash install.sh --force
EOF
}
while [[ $# -gt 0 ]]; do
case $1 in
--version)
if [[ -z "${2:-}" || "$2" == --* ]]; then
error "Missing value for --version"
fi
VERSION="$2"
shift 2
;;
--libc)
if [[ -z "${2:-}" || "$2" == --* ]]; then
error "Missing value for --libc"
fi
case "$2" in
gnu|musl) LINUX_LIBC="$2" ;;
*) error "Invalid --libc value: $2 (expected: gnu or musl)" ;;
esac
shift 2
;;
--force)
FORCE_INSTALL=true
shift
;;
--help)
usage
exit 0
;;
*)
error "Unknown option: $1"
;;
esac
done
if [ -n "$VERSION" ]; then
case "$VERSION" in
v*) : ;;
*) VERSION="v$VERSION" ;;
esac
if ! echo "$VERSION" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$'; then
error "Invalid version format: $VERSION (expected: vX.Y.Z or vX.Y.Z-suffix)"
fi
fi
detect_os() {
case "$(uname -s)" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
*) error "Unsupported operating system: $(uname -s)" ;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo "x86_64" ;;
arm64|aarch64) echo "aarch64" ;;
*) error "Unsupported architecture: $(uname -m)" ;;
esac
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
detect_linux_libc() {
if [ -n "$LINUX_LIBC" ]; then
echo "$LINUX_LIBC"
return
fi
if command_exists ldd && ldd --version 2>&1 | grep -qi musl; then
echo "musl"
elif ls /lib/ld-musl-*.so.* >/dev/null 2>&1 || ls /usr/lib/ld-musl-*.so.* >/dev/null 2>&1; then
echo "musl"
else
echo "gnu"
fi
}
download_file() {
local url="$1"
local output="$2"
if command_exists curl; then
curl -fsSL --progress-bar "$url" -o "$output" || return 1
elif command_exists wget; then
wget -q --show-progress "$url" -O "$output" || return 1
else
error "Neither curl nor wget found. Please install one of them."
fi
}
verify_checksum() {
local file="$1"
local checksum_url="$2"
local checksum_file="${TEMP_DIR}/checksum.sha256"
echo "Downloading checksum..."
if ! download_file "$checksum_url" "$checksum_file"; then
error "Checksum file not found at $checksum_url. Installation aborted for security reasons."
fi
echo "Verifying checksum..."
local expected_checksum
expected_checksum=$(awk '{print $1}' "$checksum_file")
local actual_checksum
if command_exists sha256sum; then
actual_checksum=$(sha256sum "$file" | awk '{print $1}')
elif command_exists shasum; then
actual_checksum=$(shasum -a 256 "$file" | awk '{print $1}')
else
error "No SHA256 tool found (sha256sum or shasum). Cannot verify download integrity."
fi
if [ "$expected_checksum" != "$actual_checksum" ]; then
error "Checksum verification failed for $file using $checksum_url
Expected: $expected_checksum
Actual: $actual_checksum"
fi
success "Checksum verified successfully."
}
install_orbit() {
local platform="$1"
local arch="$2"
local libc="${3:-}"
if [ "$FORCE_INSTALL" = false ] && [ -f "$INSTALL_DIR/orbit" ]; then
warning "Orbit local CLI is already installed at $INSTALL_DIR/orbit"
echo "To upgrade or reinstall:"
echo " - Reinstall same/latest: run with --force"
echo " - Install specific: run with --version vX.Y.Z [and optionally --force]"
exit 0
fi
mkdir -p "$INSTALL_DIR"
local project_id="77960826"
local artifact_name="orbit-local-${platform}-${arch}.tar.gz"
if [ "$platform" = "linux" ] && [ "$libc" = "musl" ]; then
artifact_name="orbit-local-${platform}-${libc}-${arch}.tar.gz"
fi
local resolved_tag
if [ -z "$VERSION" ]; then
echo "Resolving the latest Orbit release..."
local permalink="https://gitlab.com/api/v4/projects/${project_id}/releases/permalink/latest"
if command_exists curl; then
resolved_tag=$(curl -fsSL "$permalink" | sed -nE 's/.*"tag_name":[[:space:]]*"([^"\\]+)".*/\1/p' | head -n1)
else
resolved_tag=$(wget -qO- "$permalink" | sed -nE 's/.*"tag_name":[[:space:]]*"([^"\\]+)".*/\1/p' | head -n1)
fi
if [ -z "$resolved_tag" ]; then
error "Failed to resolve the latest release tag from $permalink"
fi
VERSION="$resolved_tag"
fi
echo "Installing the Orbit local CLI ${VERSION}..."
# Tarballs are published to the project's Generic Package Registry under
# 'orbit-local/<version>/<artifact>'. The version segment omits the leading 'v'.
local pkg_version="${VERSION#v}"
local pkg_base="https://gitlab.com/api/v4/projects/${project_id}/packages/generic/orbit-local/${pkg_version}"
local download_url="${pkg_base}/${artifact_name}"
local checksum_url="${pkg_base}/${artifact_name}.sha256"
local tarball="${TEMP_DIR}/${artifact_name}"
echo "Downloading the Orbit local CLI for ${platform}-${arch}${libc:+-${libc}}..."
if ! download_file "$download_url" "$tarball"; then
error "Failed to download the Orbit local CLI from $download_url. Check your internet connection and the version number."
fi
verify_checksum "$tarball" "$checksum_url"
echo "Extracting orbit..."
if ! tar -xzf "$tarball" -C "$TEMP_DIR"; then
error "Failed to extract the tarball."
fi
local orbit_binary="${TEMP_DIR}/orbit"
if [ ! -f "$orbit_binary" ]; then
orbit_binary=$(find "$TEMP_DIR" -maxdepth 2 -name "orbit" -type f -perm -u+x | head -n 1)
if [ -z "$orbit_binary" ]; then
error "orbit binary not found in the extracted files."
fi
fi
local real_binary real_temp
real_binary=$(realpath "$orbit_binary" 2>/dev/null || readlink -f "$orbit_binary" 2>/dev/null || echo "$orbit_binary")
real_temp=$(realpath "$TEMP_DIR" 2>/dev/null || readlink -f "$TEMP_DIR" 2>/dev/null || echo "$TEMP_DIR")
case "$real_binary" in
"${real_temp}"/*) : ;;
*) error "Extracted binary resolved outside temp directory: $real_binary" ;;
esac
echo "Installing orbit to $INSTALL_DIR..."
if command_exists install; then
install -m 0755 "$orbit_binary" "$INSTALL_DIR/orbit"
else
chmod +x "$orbit_binary"
mv "$orbit_binary" "$INSTALL_DIR/orbit"
fi
success "Orbit local CLI has been installed to $INSTALL_DIR/orbit"
}
update_path() {
local targets=()
local os_name
os_name="$(uname -s)"
if command_exists zsh || [ -x "/bin/zsh" ]; then
targets+=("${HOME}/.zshrc")
if [ "$os_name" = "Darwin" ]; then
targets+=("${HOME}/.zprofile")
fi
fi
if command_exists bash || [ -x "/bin/bash" ]; then
if [ "$os_name" = "Darwin" ]; then
targets+=("${HOME}/.bash_profile")
else
targets+=("${HOME}/.bashrc")
fi
fi
targets+=("${HOME}/.profile")
for shell_rc in "${targets[@]}"; do
echo "Updating PATH in $shell_rc..."
touch "$shell_rc"
local home_path="${HOME}/.local/bin"
if grep -Fq "# Added by Orbit local CLI installer" "$shell_rc"; then
echo "PATH export already exists in $shell_rc"
continue
fi
if grep -Eq '^[[:space:]]*(export[[:space:]]+)?PATH=.*((\$HOME|\${HOME}|~)/\.local/bin)' "$shell_rc" || \
grep -Fq "$home_path" "$shell_rc"; then
echo "PATH export already exists in $shell_rc"
else
echo "" >> "$shell_rc"
echo "# Added by Orbit local CLI installer" >> "$shell_rc"
echo "export PATH=\"\$HOME/.local/bin:\$PATH\"" >> "$shell_rc"
success "PATH has been updated in $shell_rc"
fi
done
}
ensure_dependencies() {
if ! command_exists tar; then
error "Required dependency 'tar' not found. Please install it and re-run the installer."
fi
if ! command_exists curl && ! command_exists wget; then
error "Neither 'curl' nor 'wget' found. Please install one of them and re-run the installer."
fi
if ! command_exists awk; then
error "Required dependency 'awk' not found. Please install it and re-run the installer."
fi
}
main() {
echo "=== Orbit local CLI installation ==="
echo
ensure_dependencies
local platform
local arch
local libc=""
platform=$(detect_os)
arch=$(detect_arch)
if [ "$platform" = "linux" ]; then
libc=$(detect_linux_libc)
elif [ -n "$LINUX_LIBC" ]; then
error "--libc is only supported on Linux"
fi
echo "Detected system: ${platform}-${arch}${libc:+-${libc}}"
echo
install_orbit "$platform" "$arch" "$libc"
update_path
echo
success "Installation complete."
echo
echo "To start using orbit in your terminal, run:"
if [ "$platform" = "darwin" ]; then
echo " - zsh: 'source ~/.zshrc' (login shells: 'source ~/.zprofile')"
echo " - bash: 'source ~/.bash_profile'"
else
echo " - zsh: 'source ~/.zshrc'"
echo " - bash: 'source ~/.bashrc'"
fi
echo " - Or open a new terminal."
echo
echo "If you use a different shell, add \$HOME/.local/bin to PATH manually."
echo
echo "Then verify the installation with: orbit --version"
}
main