Repository navigation
Expand file tree
/
Copy pathinstall-from-github.sh
More file actions
executable file
·270 lines (254 loc) · 9.23 KB
/
Copy pathinstall-from-github.sh
File metadata and controls
executable file
·270 lines (254 loc) · 9.23 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
#!/usr/bin/env bash
set -euo pipefail
repo_explicit="${LOOPX_REPO:+1}"
repo="${LOOPX_REPO:-loopx-project/loopx}"
ref_explicit="${LOOPX_REF:+1}"
ref="${LOOPX_REF:-stable}"
archive_url_override="${LOOPX_ARCHIVE_URL:-}"
archive_url="$archive_url_override"
python_bin="${LOOPX_PYTHON:-python3}"
installer_timeout_seconds="${LOOPX_INSTALLER_TIMEOUT_SECONDS:-}"
export LOOPX_REPO="$repo"
export LOOPX_REF="$ref"
need() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "loopx installer error: missing required command: $1" >&2
exit 1
fi
}
need curl
need tar
need "$python_bin"
if [[ -n "${LOOPX_RESOLVED_SOURCE_GIT_COMMIT:-}" \
&& ! "$LOOPX_RESOLVED_SOURCE_GIT_COMMIT" =~ ^[0-9a-fA-F]{40}$ ]]; then
echo "loopx installer error: LOOPX_RESOLVED_SOURCE_GIT_COMMIT must be a full Git commit SHA" >&2
exit 2
fi
if [[ -n "$installer_timeout_seconds" \
&& ! "$installer_timeout_seconds" =~ ^[1-9][0-9]*$ ]]; then
echo "loopx installer error: LOOPX_INSTALLER_TIMEOUT_SECONDS must be a positive integer" >&2
exit 2
fi
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/loopx-install.XXXXXX")"
cleanup() {
rm -rf "$tmp_dir"
}
trap cleanup EXIT
# Default snapshots use a verified, prebuilt runtime. Developer source builds
# remain explicit; a missing release never silently starts npm or a whole-repo
# download. The bootstrap is published beside this script over the same TLS
# origin. Its stdlib transport preserves the operator's proxy environment.
if [[ "${LOOPX_BUILD_FROM_SOURCE:-0}" != "1" && -z "$archive_url_override" ]]; then
export LOOPX_UPDATE_REF_EXPLICIT="${ref_explicit:-0}"
export LOOPX_UPDATE_REPO_EXPLICIT="${repo_explicit:-0}"
curl -fsSL --connect-timeout 10 --max-time 45 --retry 2 --retry-max-time 60 \
https://loopx-project.github.io/loopx/update-artifact.py -o "$tmp_dir/update-artifact.py"
"$python_bin" "$tmp_dir/update-artifact.py"
exit $?
fi
if [[ -z "$archive_url" ]]; then
if [[ ! "$repo" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]]; then
echo "loopx installer error: LOOPX_REPO must use GitHub owner/name syntax" >&2
exit 2
fi
if [[ "$ref" =~ ^[0-9a-fA-F]{40}$ ]]; then
# Immutable inputs need no branch-resolution service (or GitHub API quota).
resolved_commit="$(printf '%s' "$ref" | tr '[:upper:]' '[:lower:]')"
if [[ -n "${LOOPX_RESOLVED_SOURCE_GIT_COMMIT:-}" \
&& "$(printf '%s' "$LOOPX_RESOLVED_SOURCE_GIT_COMMIT" | tr '[:upper:]' '[:lower:]')" != "$resolved_commit" ]]; then
echo "loopx installer error: resolved commit disagrees with the requested full SHA" >&2
exit 2
fi
else
commit_api_url="$("$python_bin" - "$repo" "$ref" <<'PY'
from urllib.parse import quote
import sys
repo, ref = sys.argv[1:]
owner, name = repo.split("/", 1)
print(
"https://api.github.com/repos/"
f"{quote(owner, safe='')}/{quote(name, safe='')}/commits/{quote(ref, safe='')}"
)
PY
)"
if ! curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-max-time 45 \
-H 'Accept: application/vnd.github+json' \
-H 'User-Agent: LoopX-installer' \
"$commit_api_url" -o "$tmp_dir/commit.json"; then
# Reuse an existing login when available; never request credentials or
# fall back to a different branch. Keep both transport and capture bounded.
echo "loopx installer: public commit lookup failed; trying existing GitHub CLI authentication" >&2
"$python_bin" - "$commit_api_url" "$tmp_dir/commit.json" <<'PY'
from pathlib import Path
import shutil
import subprocess
import sys
from urllib.parse import urlsplit
gh = shutil.which("gh")
if gh:
try:
with Path(sys.argv[2]).open("wb") as output:
result = subprocess.run(
[gh, "api", "--hostname", "github.com", urlsplit(sys.argv[1]).path],
stdout=output, stderr=subprocess.DEVNULL, timeout=30, check=False,
)
if result.returncode == 0:
raise SystemExit(0)
except (OSError, subprocess.TimeoutExpired):
pass
raise SystemExit("Commit lookup failed. Retry with LOOPX_REF=<verified full commit SHA>; "
"no runtime was installed and no alternate ref was selected.")
PY
fi
resolved_commit="$("$python_bin" - "$tmp_dir/commit.json" <<'PY'
import json
import re
import sys
with open(sys.argv[1], encoding="utf-8") as handle:
payload = json.load(handle)
sha = payload.get("sha")
if not isinstance(sha, str) or re.fullmatch(r"[0-9a-fA-F]{40}", sha) is None:
raise SystemExit("GitHub commit response did not include a full SHA")
print(sha.lower())
PY
)"
fi
export LOOPX_RESOLVED_SOURCE_GIT_COMMIT="$resolved_commit"
archive_url="https://codeload.github.com/$repo/tar.gz/$resolved_commit"
fi
export LOOPX_ARCHIVE_URL="$archive_url"
archive_path="$tmp_dir/loopx.tar.gz"
extract_dir="$tmp_dir/extract"
mkdir -p "$extract_dir"
echo "loopx installer: downloading $archive_url" >&2
archive_deadline=$((SECONDS + ${installer_timeout_seconds:-150}))
archive_attempt=1
archive_max_attempts=3
archive_attempts_completed=0
archive_downloaded=0
last_curl_code=28
last_http_status=0
download_source_archive() {
local curl_pid curl_result
local -a arguments=(--silent --show-error --fail --location
--connect-timeout 10 --max-time "$attempt_timeout"
--continue-at - --write-out '%{http_code}' "$archive_url" -o "$archive_path")
if [[ "${LOOPX_UPDATE_EVENTS:-0}" != "1" ]]; then
curl "${arguments[@]}"
return $?
fi
curl "${arguments[@]}" >"$tmp_dir/http-status" &
curl_pid=$!
while kill -0 "$curl_pid" 2>/dev/null; do
if [[ -f "$archive_path" ]]; then
local received
received="$(wc -c <"$archive_path" | tr -d ' ')"
printf 'LOOPX_UPDATE_EVENT {"stage":"download","received":%s,"total":null}\n' "$received" >&2
fi
sleep 2
done
if wait "$curl_pid"; then curl_result=0; else curl_result=$?; fi
cat "$tmp_dir/http-status"
return "$curl_result"
}
while [[ "$archive_attempt" -le "$archive_max_attempts" ]]; do
remaining=$((archive_deadline - SECONDS))
if [[ "$remaining" -le 0 ]]; then
break
fi
attempt_timeout="$remaining"
if [[ "$archive_attempt" -lt "$archive_max_attempts" ]]; then
reserved_attempts=$((archive_max_attempts - archive_attempt))
attempt_timeout=$((remaining - reserved_attempts))
if [[ "$attempt_timeout" -gt 120 ]]; then
attempt_timeout=120
elif [[ "$attempt_timeout" -lt 1 ]]; then
attempt_timeout=1
fi
fi
archive_attempts_completed="$archive_attempt"
resuming_archive=0
if [[ -s "$archive_path" ]]; then
resuming_archive=1
fi
if http_status="$(download_source_archive)"; then
# curl can treat 416 as an already-complete resumed transfer. The local
# partial file is not proof of that; recover by downloading it afresh.
if [[ "$resuming_archive" -eq 1 && "$http_status" == 416 ]]; then
last_curl_code=0
else
archive_downloaded=1
break
fi
else
last_curl_code=$?
fi
if [[ "$http_status" =~ ^[0-9]{3}$ ]]; then
last_http_status="$http_status"
else
last_http_status=0
fi
retryable=0
# Some servers/proxies ignore Range (200) or reject its offset (416).
# curl reports these as 33, or 56 with HTTP 200, depending on its version.
# Restart only a rejected resume; ordinary transfer failures keep partial
# bytes. The restart uses the next attempt and the original deadline.
if [[ "$resuming_archive" -eq 1 ]]; then
case "$last_curl_code:$last_http_status" in
33:*|56:200|22:416|0:416)
rm -f "$archive_path"
retryable=1
echo "loopx installer: archive resume rejected; retrying a complete download within the remaining budget" >&2
;;
esac
fi
case "$last_curl_code" in
5|6|7|18|28|35|52|55|56)
retryable=1
;;
22)
case "$last_http_status" in
403|408|429|500|502|503|504)
retryable=1
;;
esac
;;
esac
if [[ "$retryable" -ne 1 ]]; then
break
fi
archive_attempt=$((archive_attempt + 1))
done
if [[ "$archive_downloaded" -ne 1 ]]; then
echo "loopx installer error: archive download failed after $archive_attempts_completed attempt(s) (curl $last_curl_code, HTTP $last_http_status)" >&2
if [[ "$last_curl_code" -eq 0 ]]; then
exit 1
fi
exit "$last_curl_code"
fi
archive_sha256="$("$python_bin" - "$archive_path" <<'PY'
from pathlib import Path
import hashlib
import sys
digest = hashlib.sha256()
with Path(sys.argv[1]).open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
print(digest.hexdigest())
PY
)"
export LOOPX_ARCHIVE_SHA256="$archive_sha256"
# Validate the complete compressed archive before extracting any members.
tar -tzf "$archive_path" >/dev/null
tar -xzf "$archive_path" -C "$extract_dir"
repo_root="$(find "$extract_dir" -mindepth 1 -maxdepth 1 -type d -print -quit)"
if [[ -z "$repo_root" || ! -x "$repo_root/scripts/install-local.sh" ]]; then
echo "loopx installer error: downloaded archive does not contain scripts/install-local.sh" >&2
exit 1
fi
# The downloaded checkout is temporary. Install a stable release snapshot and
# skip the live canary symlink unless the caller explicitly overrides it.
export LOOPX_INSTALL_CANARY="${LOOPX_INSTALL_CANARY:-0}"
export LOOPX_PROMOTE_DEFAULT="${LOOPX_PROMOTE_DEFAULT:-1}"
export LOOPX_PROMOTION_MODE="${LOOPX_PROMOTION_MODE:-trusted_github_archive}"
"$repo_root/scripts/install-local.sh"