forked from Tracer-Cloud/opensre
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
391 lines (311 loc) · 9.88 KB
/
install.sh
File metadata and controls
391 lines (311 loc) · 9.88 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
#!/usr/bin/env bash
[ -n "${BASH_VERSION:-}" ] || {
printf '%s\n' "Error: install.sh requires bash. Run 'bash install.sh' or pipe it into bash." >&2
exit 1
}
set -euo pipefail
REPO="${OPENSRE_INSTALL_REPO:-Tracer-Cloud/opensre}"
INSTALL_DIR="${OPENSRE_INSTALL_DIR:-$HOME/.local/bin}"
BIN_NAME="opensre"
log() {
printf '%s\n' "$*"
}
warn() {
printf 'Warning: %s\n' "$*" >&2
}
die() {
printf 'Error: %s\n' "$*" >&2
exit 1
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || die "'$1' is required but was not found in PATH."
}
need_cmd curl
need_cmd grep
need_cmd sed
need_cmd tr
need_cmd uname
CURL_FLAGS=(
--fail
--silent
--show-error
--location
--retry 3
--retry-delay 1
)
download_to() {
local url="$1"
local destination="$2"
curl "${CURL_FLAGS[@]}" -o "$destination" "$url"
}
download_text() {
local url="$1"
curl "${CURL_FLAGS[@]}" \
-H "Accept: application/vnd.github+json" \
-H "User-Agent: opensre-install-script" \
"$url"
}
fetch_release_json() {
local version="${1:-}"
local api_url
if [ -n "$version" ]; then
api_url="https://api.github.com/repos/${REPO}/releases/tags/v${version}"
else
api_url="https://api.github.com/repos/${REPO}/releases/latest"
fi
download_text "$api_url"
}
extract_tag_name() {
local release_json="$1"
printf '%s\n' "$release_json" | sed -n '/"tag_name"[[:space:]]*:/{
s/.*"tag_name":[[:space:]]*"v\{0,1\}\([^"]*\)".*/\1/p
q
}'
}
release_has_asset() {
local release_json="$1"
local asset_name="$2"
printf '%s' "$release_json" | tr -d '\r\n\t ' | grep -F "\"name\":\"${asset_name}\"" >/dev/null 2>&1
}
build_archive_name() {
local version="$1"
local asset_arch="$2"
if [ "$platform" = "windows" ]; then
printf 'opensre_%s_windows-%s.zip\n' "$version" "$asset_arch"
return
fi
printf 'opensre_%s_%s-%s.tar.gz\n' "$version" "$platform" "$asset_arch"
}
ps_escape() {
printf '%s' "$1" | sed "s/'/''/g"
}
to_windows_path() {
local posix_path="$1"
if command -v cygpath >/dev/null 2>&1; then
cygpath -w "$posix_path"
return
fi
die "PowerShell archive extraction requires 'cygpath' when 'unzip' is unavailable."
}
extract_zip() {
local archive_path="$1"
local destination_dir="$2"
local archive_for_ps
local destination_for_ps
if command -v unzip >/dev/null 2>&1; then
unzip -q "$archive_path" -d "$destination_dir"
return
fi
archive_for_ps="$(ps_escape "$(to_windows_path "$archive_path")")"
destination_for_ps="$(ps_escape "$(to_windows_path "$destination_dir")")"
if command -v powershell.exe >/dev/null 2>&1; then
powershell.exe -NoLogo -NoProfile -NonInteractive -Command \
"Expand-Archive -LiteralPath '$archive_for_ps' -DestinationPath '$destination_for_ps' -Force" \
>/dev/null
return
fi
if command -v pwsh >/dev/null 2>&1; then
pwsh -NoLogo -NoProfile -NonInteractive -Command \
"Expand-Archive -LiteralPath '$archive_for_ps' -DestinationPath '$destination_for_ps' -Force" \
>/dev/null
return
fi
die "A zip extractor is required on Windows. Install 'unzip' or run the PowerShell installer."
}
extract_archive() {
local archive_path="$1"
local destination_dir="$2"
if [ "$platform" = "windows" ]; then
extract_zip "$archive_path" "$destination_dir"
return
fi
need_cmd tar
tar -xzf "$archive_path" -C "$destination_dir"
}
verify_checksum() {
local checksum_path="$1"
local archive_path="$2"
local archive_dir
local checksum_name
local normalized_checksum_path
local expected
local actual
archive_dir="${archive_path%/*}"
checksum_name="${checksum_path##*/}"
normalized_checksum_path="${checksum_path}.normalized"
tr -d '\r' < "$checksum_path" > "$normalized_checksum_path"
checksum_path="$normalized_checksum_path"
checksum_name="${checksum_path##*/}"
if command -v sha256sum >/dev/null 2>&1; then
(cd "$archive_dir" && sha256sum -c "$checksum_name") >/dev/null \
|| die "Checksum verification failed for '${archive_path##*/}'."
return
fi
if command -v shasum >/dev/null 2>&1; then
(cd "$archive_dir" && shasum -a 256 -c "$checksum_name") >/dev/null \
|| die "Checksum verification failed for '${archive_path##*/}'."
return
fi
if command -v openssl >/dev/null 2>&1; then
expected="$(sed -n 's/^\([0-9A-Fa-f]\{64\}\)[[:space:]][[:space:]]*.*/\1/p' "$checksum_path")"
[ -n "$expected" ] || die "Checksum file '${checksum_name}' is malformed."
actual="$(openssl dgst -sha256 "$archive_path" | sed 's/^.*= //')"
[ "$expected" = "$actual" ] || die "Checksum verification failed for '${archive_path##*/}'."
return
fi
warn "No checksum verifier found (sha256sum, shasum, or openssl). Skipping checksum verification."
}
install_binary() {
local source_path="$1"
local destination_path="$2"
if command -v install >/dev/null 2>&1; then
install -m 0755 "$source_path" "$destination_path"
return
fi
cp "$source_path" "$destination_path"
chmod 0755 "$destination_path" 2>/dev/null || true
}
get_binary_path_from_archive() {
local extraction_root="$1"
local binary_name="$2"
local direct_binary_path
local binary_candidates=()
local binary_locations
direct_binary_path="${extraction_root}/${binary_name}"
if [ -f "$direct_binary_path" ]; then
printf '%s\n' "$direct_binary_path"
return
fi
need_cmd find
while IFS= read -r candidate; do
binary_candidates+=("$candidate")
done < <(find "$extraction_root" -type f -name "$binary_name")
case "${#binary_candidates[@]}" in
1)
printf '%s\n' "${binary_candidates[0]}"
;;
0)
die "Archive '${archive}' did not contain '${binary_name}'."
;;
*)
binary_locations="$(printf '%s, ' "${binary_candidates[@]}")"
binary_locations="${binary_locations%, }"
die "Found multiple '${binary_name}' files after extraction: ${binary_locations}"
;;
esac
}
verify_binary_version() {
local binary_path="$1"
local expected_version="$2"
local version_output
local actual_version
if ! version_output="$("$binary_path" --version 2>&1)"; then
die "Failed to execute '${binary_path##*/} --version': ${version_output}"
fi
actual_version="$(printf '%s\n' "$version_output" | sed -n 's/.*\([0-9][0-9][0-9][0-9]\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' | head -n 1)"
case "$version_output" in
*"$expected_version"*)
printf '%s\n' "$expected_version"
;;
*)
if [ -n "${OPENSRE_VERSION:-}" ] || [ -z "$actual_version" ]; then
die "Downloaded binary version mismatch. Expected '${expected_version}' but got: ${version_output}"
fi
warn "Latest release metadata reports v${expected_version}, but the downloaded binary reports v${actual_version}. Installing the verified binary anyway."
printf '%s\n' "$actual_version"
;;
esac
}
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux)
platform="linux"
;;
Darwin)
platform="darwin"
;;
MINGW*|MSYS*|CYGWIN*)
platform="windows"
BIN_NAME="opensre.exe"
log "Detected Windows environment (${os})."
;;
*)
die "Unsupported operating system: $os"
;;
esac
case "$arch" in
x86_64|amd64)
target_arch="x64"
;;
arm64|aarch64)
target_arch="arm64"
;;
*)
die "Unsupported architecture: $arch"
;;
esac
version="${OPENSRE_VERSION:-}"
version="${version#v}"
if [ -z "$version" ]; then
log "Fetching latest release version..."
fi
release_json="$(fetch_release_json "$version")" || die "Failed to query release metadata from GitHub."
if [ -z "$version" ]; then
version="$(extract_tag_name "$release_json")"
fi
[ -n "$version" ] || die "Failed to determine the release version."
asset_arch="$target_arch"
archive="$(build_archive_name "$version" "$asset_arch")"
if [ "$platform" = "windows" ] && [ "$target_arch" = "arm64" ] && ! release_has_asset "$release_json" "$archive"; then
fallback_archive="$(build_archive_name "$version" "x64")"
if release_has_asset "$release_json" "$fallback_archive"; then
asset_arch="x64"
archive="$fallback_archive"
warn "Windows ARM64 artifact is not published for v${version}; falling back to the x64 build."
fi
fi
release_has_asset "$release_json" "$archive" || die "Release v${version} does not include asset '${archive}'."
download_url="https://github.com/${REPO}/releases/download/v${version}/${archive}"
checksum_asset="${archive}.sha256"
checksum_url="${download_url}.sha256"
log "Installing opensre v${version} (${platform}/${target_arch})..."
if [ "$asset_arch" != "$target_arch" ]; then
log "Using release asset built for ${platform}/${asset_arch}."
fi
log "Downloading ${download_url}"
need_cmd mktemp
tmp_dir="$(mktemp -d)"
cleanup() {
if [ -n "${tmp_dir:-}" ] && [ -d "$tmp_dir" ]; then
rm -rf "$tmp_dir"
fi
}
trap cleanup EXIT
archive_path="${tmp_dir}/${archive}"
download_to "$download_url" "$archive_path" || die "Failed to download '${archive}'."
if release_has_asset "$release_json" "$checksum_asset"; then
checksum_path="${tmp_dir}/${checksum_asset}"
download_to "$checksum_url" "$checksum_path" || die "Failed to download checksum '${checksum_asset}'."
verify_checksum "$checksum_path" "$archive_path"
else
warn "Release v${version} is missing checksum asset '${checksum_asset}'."
fi
mkdir -p "$INSTALL_DIR"
extract_archive "$archive_path" "$tmp_dir"
binary_path="$(get_binary_path_from_archive "$tmp_dir" "$BIN_NAME")"
installed_version="$(verify_binary_version "$binary_path" "$version")"
install_binary "$binary_path" "${INSTALL_DIR}/${BIN_NAME}"
log "Installed ${BIN_NAME} v${installed_version} to ${INSTALL_DIR}/${BIN_NAME}"
case ":$PATH:" in
*":${INSTALL_DIR}:"*)
;;
*)
if [ "$platform" = "windows" ]; then
warn "'${INSTALL_DIR}' is not in PATH for this shell. Add it to Git Bash or Windows PATH to run ${BIN_NAME} from any terminal."
else
warn "'${INSTALL_DIR}' is not in PATH. Add this line to your shell profile:"
warn " export PATH=\"\$PATH:${INSTALL_DIR}\""
fi
;;
esac