| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | cage_tool_version() { |
| 7 | awk '$1 ~ /^v?[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)?$/ && !found { print $1; found=1 }' |
| 8 | } |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | cage_curl_download() { |
| 14 | python3 - "$@" <<'CAGE_DOWNLOAD_IDLE' |
| 15 | import os |
| 16 | import signal |
| 17 | import subprocess |
| 18 | import sys |
| 19 | import time |
| 20 | |
| 21 | artifact = sys.argv[1] |
| 22 | idle_timeout = 60 |
| 23 | |
| 24 | def size(): |
| 25 | try: |
| 26 | return os.stat(artifact).st_size |
| 27 | except FileNotFoundError: |
| 28 | return 0 |
| 29 | |
| 30 | def stop(signum, frame): |
| 31 | sys.exit(128 + signum) |
| 32 | |
| 33 | signal.signal(signal.SIGTERM, stop) |
| 34 | received = size() |
| 35 | name = os.path.basename(artifact).removesuffix(".part").removesuffix(".mirror") |
| 36 | show_progress = os.environ.get("CAGE_SETUP_PROGRESS") == "1" |
| 37 | def report(count): |
| 38 | if show_progress: |
| 39 | print(f"CAGE_TOOL_BYTES {name} {count}", file=sys.stderr, flush=True) |
| 40 | report(received) |
| 41 | last_received = time.monotonic() |
| 42 | meter = ["--silent", "--show-error"] if show_progress else [] |
| 43 | process = subprocess.Popen(["curl", "--no-buffer", *meter, *sys.argv[2:]], stdin=subprocess.DEVNULL) |
| 44 | try: |
| 45 | while True: |
| 46 | try: |
| 47 | status = process.wait(timeout=min(1, idle_timeout)) |
| 48 | report(size()) |
| 49 | break |
| 50 | except subprocess.TimeoutExpired: |
| 51 | current = size() |
| 52 | if current > received: |
| 53 | report(current) |
| 54 | last_received = time.monotonic() |
| 55 | received = current |
| 56 | if time.monotonic() - last_received >= idle_timeout: |
| 57 | print("Download received no data for 60 seconds; retrying with saved partial data.", file=sys.stderr) |
| 58 | status = 28 |
| 59 | break |
| 60 | finally: |
| 61 | if process.poll() is None: |
| 62 | process.terminate() |
| 63 | try: |
| 64 | process.wait(timeout=5) |
| 65 | except subprocess.TimeoutExpired: |
| 66 | process.kill() |
| 67 | process.wait() |
| 68 | sys.exit(status if status >= 0 else 128 - status) |
| 69 | CAGE_DOWNLOAD_IDLE |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | cage_download_tool() ( |
| 75 | set -euo pipefail |
| 76 | local upstream="$1" checksum="$2" artifact="$3" hash="${4:-sha256sum}" mirror="${5:-}" |
| 77 | local attempts="${6:-4}" attempt status http url part host |
| 78 | if [ -f "$artifact" ]; then |
| 79 | if printf '%s %s\n' "$checksum" "$artifact" | "$hash" --check --status; then return 0; fi |
| 80 | rm -f "$artifact" |
| 81 | fi |
| 82 | for ((attempt=1; attempt<=attempts; attempt++)); do |
| 83 | url="$upstream" |
| 84 | part="$artifact.part" |
| 85 | if [ "$attempt" = 2 ] && [ -n "$mirror" ] && [ ! -s "$part" ]; then |
| 86 | url="$mirror" |
| 87 | part="$artifact.mirror.part" |
| 88 | fi |
| 89 | host="${url#https://}" |
| 90 | printf 'Downloading %s from %s (attempt %s/%s; resume from %s bytes).\n' "${artifact##*/}" "${host%%/*}" "$attempt" "$attempts" "$(if [ -f "$part" ]; then wc -c < "$part"; else echo 0; fi)" |
| 91 | if http="$(cage_curl_download "$part" --proto '=https' --proto-redir '=https' --tlsv1.2 --fail --location \ |
| 92 | --connect-timeout 15 \ |
| 93 | --continue-at - --write-out '%{http_code}' "$url" -o "$part" </dev/null)"; then |
| 94 | status=0 |
| 95 | else |
| 96 | status=$? |
| 97 | fi |
| 98 | |
| 99 | if [ "$status" = 0 ] || [ "$http" = 416 ]; then |
| 100 | if printf '%s %s\n' "$checksum" "$part" | "$hash" --check --status; then |
| 101 | mv -f "$part" "$artifact" || return 1 |
| 102 | rm -f "$artifact.mirror.part" |
| 103 | return 0 |
| 104 | fi |
| 105 | rm -f "$part" |
| 106 | if [ "$url" != "$upstream" ]; then |
| 107 | echo 'Mirror checksum mismatch; retrying from the official source.' >&2 |
| 108 | continue |
| 109 | fi |
| 110 | echo 'Tool checksum mismatch; installation refused. Retry downloads a fresh copy.' >&2 |
| 111 | return 1 |
| 112 | fi |
| 113 | if [ "$url" != "$upstream" ]; then |
| 114 | if [ "$http" = 404 ]; then |
| 115 | echo 'This version is missing on the mirror; trying the official source.' >&2 |
| 116 | else |
| 117 | echo 'Mirror download interrupted; trying the official source.' >&2 |
| 118 | fi |
| 119 | continue |
| 120 | fi |
| 121 | if [ "$attempt" = 1 ] && [ -n "$mirror" ] && [ ! -s "$part" ]; then |
| 122 | echo 'Official download unavailable; trying the fallback mirror.' >&2 |
| 123 | continue |
| 124 | fi |
| 125 | case "$status" in |
| 126 | 33|36) rm -f "$artifact.part" ;; |
| 127 | 5|6|7|18|28|35|52|55|56|92) ;; |
| 128 | 22) case "$http" in 408|429|500|502|503|504) ;; *) return "$status" ;; esac ;; |
| 129 | *) return "$status" ;; |
| 130 | esac |
| 131 | if [ "$attempt" != "$attempts" ]; then sleep "$attempt"; fi |
| 132 | done |
| 133 | echo 'Download interrupted; partial data is saved. Retry setup to continue.' >&2 |
| 134 | return "$status" |
| 135 | ) |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | cage_cache_claude() ( |
| 141 | set -euo pipefail |
| 142 | local arch="$1" wanted="$2" package metadata integrity checksum url cache |
| 143 | case "$arch" in arm64) package=claude-code-linux-arm64 ;; amd64) package=claude-code-linux-x64 ;; esac |
| 144 | metadata="$(npm view --registry=https://registry.npmjs.org "@anthropic-ai/$package@$wanted" dist --json </dev/null)" |
| 145 | integrity="$(printf '%s' "$metadata" | jq -er '.integrity | select(startswith("sha512-"))')" |
| 146 | checksum="$(printf '%s' "${integrity#sha512-}" | base64 -d | od -An -v -tx1 | tr -d ' \n')" |
| 147 | [[ "$checksum" =~ ^[0-9a-f]{128}$ ]] |
| 148 | url="$(printf '%s' "$metadata" | jq -er '.tarball | select(startswith("https://registry.npmjs.org/"))')" |
| 149 | cache="$HOME/.cache/cage/downloads/$checksum" |
| 150 | mkdir -p "$cache" |
| 151 | exec 9>"$cache/lock" |
| 152 | flock -w 60 9 |
| 153 | local mirror="https://cdn.npmmirror.com/packages/%40anthropic-ai/$package/$wanted/$package-$wanted.tgz" |
| 154 | cage_download_tool "$url" "$checksum" "$cache/$package.tgz" sha512sum "$mirror" |
| 155 | npm cache add --offline --ignore-scripts "$cache/$package.tgz" </dev/null |
| 156 | ) |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | cage_install_mise() ( |
| 162 | set -euo pipefail |
| 163 | local wanted="$1" arch="$2" release_url="$3" release_sum="$4" cache="$5" |
| 164 | local archive="$cache/mise.tar.gz" member=mise/bin/mise npm_arch=x64 |
| 165 | local npm_sum="$CAGE_MISE_NPM_AMD64_SHA256" npm_url npm_archive stage |
| 166 | if [ "$arch" = arm64 ]; then |
| 167 | npm_arch=arm64 |
| 168 | npm_sum="$CAGE_MISE_NPM_ARM64_SHA256" |
| 169 | fi |
| 170 | |
| 171 | |
| 172 | if ! cage_download_tool "$release_url" "$release_sum" "$archive" sha256sum '' 1; then |
| 173 | npm_url="https://cdn.npmmirror.com/packages/%40jdxcode/mise-linux-$npm_arch/$wanted/mise-linux-$npm_arch-$wanted.tgz" |
| 174 | npm_archive="$cache/$npm_sum/mise.tar.gz" |
| 175 | mkdir -p "${npm_archive%/*}" |
| 176 | if cage_download_tool "$npm_url" "$npm_sum" "$npm_archive" sha256sum '' 1; then |
| 177 | archive="$npm_archive" |
| 178 | member=package/bin/mise |
| 179 | else |
| 180 | echo 'mise mirror unavailable or failed verification; retrying the official release.' >&2 |
| 181 | fi |
| 182 | fi |
| 183 | if [ "$member" = mise/bin/mise ] && [ ! -f "$archive" ]; then |
| 184 | cage_download_tool "$release_url" "$release_sum" "$archive" || return $? |
| 185 | fi |
| 186 | if [ "${CAGE_SETUP_PROGRESS:-}" = 1 ]; then echo 'Installing mise...'; fi |
| 187 | mkdir -p "$HOME/.local/bin" |
| 188 | stage="$(mktemp "$HOME/.local/bin/.mise-new-XXXXXX")" |
| 189 | trap 'rm -f "$stage"' EXIT |
| 190 | tar -xOzf "$archive" "$member" >"$stage" || return $? |
| 191 | chmod 0755 "$stage" || return $? |
| 192 | mv -f "$stage" "$HOME/.local/bin/mise" |
| 193 | ) |
| 194 | |
| 195 | cage_install_tool() ( |
| 196 | set -euo pipefail |
| 197 | local tool="$1" arch current='' wanted url checksum artifact mirror='' |
| 198 | arch="$(dpkg --print-architecture)" |
| 199 | case "$arch" in arm64|amd64) ;; *) echo 'Cage tool packages support arm64 and amd64.' >&2; exit 1 ;; esac |
| 200 | case "$tool" in |
| 201 | code-server) |
| 202 | wanted="$CAGE_CODE_SERVER_VERSION" |
| 203 | if command -v code-server >/dev/null; then current="$(code-server --version | cage_tool_version)" || current=''; fi |
| 204 | url="https://github.com/coder/code-server/releases/download/v$wanted/code-server_${wanted}_${arch}.deb" |
| 205 | if [ "$arch" = arm64 ]; then checksum="$CAGE_CODE_SERVER_ARM64_SHA256"; else checksum="$CAGE_CODE_SERVER_AMD64_SHA256"; fi |
| 206 | mirror="https://mirrors.ustc.edu.cn/github-release/coder/code-server/v$wanted/code-server_${wanted}_${arch}.deb" |
| 207 | artifact=code-server.deb |
| 208 | ;; |
| 209 | mise) |
| 210 | wanted="$CAGE_MISE_VERSION" |
| 211 | if [ -x "$HOME/.local/bin/mise" ]; then current="$(MISE_DISABLE_UPDATE_WARNING=1 "$HOME/.local/bin/mise" --version | cage_tool_version)" || current=''; fi |
| 212 | if [ "$arch" = arm64 ]; then checksum="$CAGE_MISE_ARM64_SHA256"; else checksum="$CAGE_MISE_AMD64_SHA256"; fi |
| 213 | url="https://github.com/jdx/mise/releases/download/v$wanted/mise-v$wanted-linux-$(if [ "$arch" = arm64 ]; then echo arm64; else echo x64; fi).tar.gz" |
| 214 | artifact=mise.tar.gz |
| 215 | ;; |
| 216 | claude) |
| 217 | if [ "$arch" = amd64 ] && [ -r /proc/cpuinfo ] && ! grep -Eq '^flags[[:space:]]*:.*[[:space:]]sse4_2([[:space:]]|$)' /proc/cpuinfo; then |
| 218 | echo 'Claude Code 需要 SSE4.2 指令。当前 x86_64 客机未提供该指令,请检查宿主 CPU、虚拟化平台和虚拟 CPU 配置后重启环境。' >&2 |
| 219 | exit 1 |
| 220 | fi |
| 221 | wanted="$CAGE_CLAUDE_VERSION" |
| 222 | if command -v claude >/dev/null; then current="$(DISABLE_AUTOUPDATER=1 claude --version | cage_tool_version)" || current=''; fi |
| 223 | ;; |
| 224 | *) echo 'Unknown Cage tool.' >&2; exit 2 ;; |
| 225 | esac |
| 226 | if [ -n "$current" ] && dpkg --compare-versions "${current#v}" ge "$wanted"; then |
| 227 | printf '%s already meets required version %s.\n' "$tool" "$wanted" |
| 228 | exit 0 |
| 229 | fi |
| 230 | if [ "$tool" = claude ]; then |
| 231 | export npm_config_fetch_retries=3 npm_config_fetch_timeout=120000 |
| 232 | cage_cache_claude "$arch" "$wanted" |
| 233 | if [ "${CAGE_SETUP_PROGRESS:-}" = 1 ]; then echo "Installing Claude Code..."; fi |
| 234 | npm install --registry=https://registry.npmjs.org --prefix "$HOME/.local" --global "@anthropic-ai/claude-code@$wanted" </dev/null |
| 235 | exit 0 |
| 236 | fi |
| 237 | local cache="$HOME/.cache/cage/downloads/$checksum" |
| 238 | mkdir -p "$cache" |
| 239 | |
| 240 | exec 9>"$cache/lock" |
| 241 | flock -w 60 9 |
| 242 | if [ "$tool" = mise ]; then |
| 243 | cage_install_mise "$wanted" "$arch" "$url" "$checksum" "$cache" |
| 244 | exit $? |
| 245 | fi |
| 246 | cage_download_tool "$url" "$checksum" "$cache/$artifact" sha256sum "$mirror" |
| 247 | if [ "${CAGE_SETUP_PROGRESS:-}" = 1 ]; then printf 'Installing %s...\n' "$tool"; fi |
| 248 | sudo env DEBIAN_FRONTEND=noninteractive APT_CONFIG="${CAGE_APT_CONFIG:-/dev/null}" apt-get -o Acquire::Retries=3 install -y --no-install-recommends "$cache/$artifact" </dev/null |
| 249 | ) |