跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

installer.sh

260 行 · 11.1 KB历史下载
1#!/usr/bin/env bash
2#
3# Checks of www/public/install.sh, the curl installer, and of agent-vm.sh,
4# which it runs. Stub curl and git serve releases laid out as release.sh
5# makes them (a tarball per platform: agent-vm and agent-vm.sh), one laid out
6# as 0.2's, and a clone whose make builds a stub binary: nothing is
7# downloaded or built. Then what 0.2 left behind: its link to agent-vm.sh,
8# and a shell rc sourcing it. Run by TestInstaller (setup_script_test.go), or
9# alone:
10#
11# tests/installer.sh
12
13set -uo pipefail
14
15SELF_DIR="$(CDPATH= cd -- "$(dirname "${BASH_SOURCE[0]:-$0}")/.." >/dev/null && pwd)"
16INSTALLER="$SELF_DIR/www/public/install.sh"
17
18FAIL=0
19PASSED=0
20pass() { PASSED=$((PASSED + 1)); printf ' ok %s\n' "$1"; }
21fail() { FAIL=$((FAIL + 1)); printf ' FAIL %s\n' "$1"; }
22check() {
23 if [ "$2" = "$3" ]; then pass "$1"
24 else fail "$1"; printf ' expected: %s\n actual: %s\n' "$3" "$2"; fi
25}
26
27# The installer's name for this machine, and another platform's.
28case "$(uname -s)-$(uname -m)" in
29 Darwin-arm64) PLATFORM=darwin-arm64 ;;
30 Darwin-x86_64) PLATFORM=darwin-amd64 ;;
31 Linux-aarch64 | Linux-arm64) PLATFORM=linux-arm64 ;;
32 Linux-x86_64) PLATFORM=linux-amd64 ;;
33 *) echo " skip: no release build for $(uname -s) $(uname -m)"; exit 0 ;;
34esac
35case "$PLATFORM" in *-arm64) OTHER="${PLATFORM%-*}-amd64" ;; *) OTHER="${PLATFORM%-*}-arm64" ;; esac
36
37sha256() {
38 if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1
39 else shasum -a 256 "$1" | cut -d' ' -f1
40 fi
41}
42
43SB="$(mktemp -d)"
44SB="$(CDPATH= cd -P -- "$SB" >/dev/null && pwd)"
45trap 'rm -rf "$SB"' EXIT
46
47WF="$SB/wfix"; WBIN="$SB/wbin"; WH="$SB/whome"
48mkdir -p "$WBIN" "$WH" "$WF"
49cat > "$WBIN/curl" <<'STUB'
50#!/bin/sh
51out=""; url=""
52while [ $# -gt 0 ]; do
53 case "$1" in -o) out="$2"; shift ;; https://*) url="$1" ;; esac
54 shift
55done
56echo "$url" >> "$WF/log"
57case "$url" in
58 */releases/latest/download/*) f="$WF/$(cat "$WF/latest")/${url##*/}" ;;
59 */releases/download/*) t="${url%/*}"; f="$WF/${t##*/}/${url##*/}" ;;
60 *) exit 22 ;;
61esac
62[ -f "$f" ] || exit 22
63cp "$f" "$out"
64STUB
65cat > "$WBIN/git" <<'STUB'
66#!/bin/sh
67case "$1" in
68 clone) echo "git $*" >> "$WF/log"; mkdir -p "$3/.git" && cp -R "$WF/src/." "$3/" ;;
69 -C)
70 [ -d "$2/.git" ] || exit 128
71 case "$3" in
72 rev-parse) cat "$WF/head" ;;
73 *) echo "git $*" >> "$WF/log" ;;
74 esac ;;
75esac
76STUB
77chmod +x "$WBIN/curl" "$WBIN/git"
78
79# The agent-vm binary, as far as these tests see it: it logs its calls, and
80# `install` links it onto the PATH and says what to run next.
81stub_binary() { # <file> <mark>
82 cat > "$1" <<EOF
83#!/bin/sh
84echo "$2 \$*" >> "\$WF/ran"
85[ "\$1" = install ] || exit 0
86mkdir -p "\$AGENT_VM_BIN_DIR"
87ln -sfn "\$0" "\$AGENT_VM_BIN_DIR/agent-vm"
88echo "agent-vm $2 installed. Next: cd your-project && agent-vm claude"
89EOF
90 chmod +x "$1"
91}
92
93# A release as release.sh makes it: a tarball for this platform and one for
94# another, their sums in SHA256SUMS. corrupt: this platform's sum is wrong.
95make_release() { # <version> [corrupt]
96 local d="$WF/v$1" p
97 mkdir -p "$d"
98 : > "$d/SHA256SUMS"
99 for p in "$OTHER" "$PLATFORM"; do
100 local s="$WF/stage-$1-$p/agent-vm-$1"
101 mkdir -p "$s"
102 cp "$SELF_DIR/agent-vm.sh" "$s/"
103 stub_binary "$s/agent-vm" "$1-$p"
104 tar -czf "$d/agent-vm-$1-$p.tar.gz" -C "$WF/stage-$1-$p" "agent-vm-$1"
105 local sum; sum="$(sha256 "$d/agent-vm-$1-$p.tar.gz")"
106 [ -z "${2:-}" ] || [ "$p" != "$PLATFORM" ] || sum="0000$sum"
107 printf '%s agent-vm-%s-%s.tar.gz\n' "$sum" "$1" "$p" >> "$d/SHA256SUMS"
108 done
109 echo "v$1" > "$WF/latest"
110}
111
112# A release as 0.2's: one tarball, agent-vm.sh the command itself.
113make_old_release() { # <version>
114 local d="$WF/v$1" s="$WF/stage-$1/agent-vm-$1"
115 mkdir -p "$d" "$s"
116 printf '#!/usr/bin/env bash\necho "0.2 $*" >> "$WF/ran"\n' > "$s/agent-vm.sh"
117 tar -czf "$d/agent-vm-$1.tar.gz" -C "$WF/stage-$1" "agent-vm-$1"
118 printf '%s agent-vm-%s.tar.gz\n' "$(sha256 "$d/agent-vm-$1.tar.gz")" "$1" > "$d/SHA256SUMS"
119 echo "v$1" > "$WF/latest"
120}
121
122# The clone: agent-vm.sh, and a make that builds a stub binary, counted.
123mkdir -p "$WF/src"
124cp "$SELF_DIR/agent-vm.sh" "$WF/src/"
125cat > "$WF/src/Makefile" <<'EOF'
126agent-vm:
127 echo built >> "$$WF/builds"
128 mkdir -p _output/bin
129 cp stub _output/bin/agent-vm
130EOF
131stub_binary "$WF/src/stub" clone
132echo head1 > "$WF/head"
133
134winst() {
135 ( export HOME="$WH" WF XDG_DATA_HOME= AGENT_VM_BIN_DIR="$WH/bin" PATH="$WBIN:$PATH"
136 sh "${WINSTALLER:-$INSTALLER}" "$@" ) 2>&1
137}
138WDIR="$WH/.local/share/agent-vm"
139
140echo "www/public/install.sh (curl | sh)"
141
142# 0.2, installed by this installer: its link leads to agent-vm.sh.
143make_old_release 0.2.1
144out="$(winst)"; rc=$?
145check "0.2 release: installs, and runs its agent-vm.sh" "$rc:$(cat "$WF/ran")" "0:0.2 install"
146mkdir -p "$WH/bin" && ln -sfn "$WDIR/agent-vm.sh" "$WH/bin/agent-vm"
147
148make_release 1.0.0
149: > "$WF/log"; : > "$WF/ran"
150out="$(winst)"; rc=$?
151check "release over 0.2: installs" "$rc" "0"
152case "$out" in *"agent-vm claude"*) pass "release: the binary's install ran" ;; *) fail "release: $out" ;; esac
153check "release: this machine's tarball, through agent-vm.sh" "$(cat "$WF/ran")" "1.0.0-$PLATFORM install"
154check "release: the binary linked onto the PATH, over 0.2's link" "$(readlink "$WH/bin/agent-vm")" "$WDIR/agent-vm"
155check "release: sums from latest/, only this machine's tarball, from its tag" "$(tr '\n' ' ' < "$WF/log")" \
156 "https://github.com/sylvinus/agent-vm/releases/latest/download/SHA256SUMS https://github.com/sylvinus/agent-vm/releases/download/v1.0.0/agent-vm-1.0.0-$PLATFORM.tar.gz "
157
158# What 0.2 left that install did not replace: a link to agent-vm.sh, and a
159# shell rc sourcing it. Both still run agent-vm.
160ln -sfn "$WDIR/agent-vm.sh" "$SB/old-link"
161: > "$WF/ran"
162( export WF; "$SB/old-link" version )
163check "0.2's link to agent-vm.sh runs the binary" "$(cat "$WF/ran")" "1.0.0-$PLATFORM version"
164: > "$WF/ran"
165out="$(export WF; bash -c 'set +e; source "$1"; agent-vm list; echo "shell still here: $?"' _ "$WDIR/agent-vm.sh" 2>&1)"
166check "bash rc sourcing agent-vm.sh: an agent-vm function, the shell kept" "$out:$(cat "$WF/ran")" "shell still here: 0:1.0.0-$PLATFORM list"
167if command -v zsh >/dev/null 2>&1; then
168 : > "$WF/ran"
169 out="$(export WF; zsh -fc 'source "$1"; agent-vm list; echo "shell still here: $?"' _ "$WDIR/agent-vm.sh" 2>&1)"
170 check "zsh rc sourcing agent-vm.sh: the same" "$out:$(cat "$WF/ran")" "shell still here: 0:1.0.0-$PLATFORM list"
171else
172 printf ' skip zsh rc (no zsh)\n'
173fi
174
175make_release 1.1.0
176: > "$WF/ran"
177out="$(winst)"; rc=$?
178check "rerun: replaced by the new release" "$rc:$(cat "$WF/ran")" "0:1.1.0-$PLATFORM install"
179check "rerun: no staging or old copy left" "$(ls -A "$WH/.local/share")" "agent-vm"
180check "rerun: the link still leads to the binary" "$(readlink "$WH/bin/agent-vm")" "$WDIR/agent-vm"
181
182# The swap stopped between its two renames (a failed mv, a Ctrl-C): the
183# previous version goes back where it was, nothing else left.
184make_release 1.1.5
185mkdir -p "$SB/wmvfail"
186printf '#!/bin/sh\ncase "$1" in *.new.*) exit 1 ;; esac\nexec %s "$@"\n' "$(command -v mv)" > "$SB/wmvfail/mv"
187chmod +x "$SB/wmvfail/mv"
188out="$(PATH="$SB/wmvfail:$PATH" winst)"; rc=$?
189: > "$WF/ran"; ( export WF; "$WDIR/agent-vm.sh" version )
190check "swap cut short: the previous version is back" "$rc:$(cat "$WF/ran")" "1:1.1.0-$PLATFORM version"
191check "swap cut short: no staging or old copy left" "$(ls -A "$WH/.local/share")" "agent-vm"
192
193make_release 1.2.0 corrupt
194out="$(winst)"; rc=$?
195: > "$WF/ran"; ( export WF; "$WDIR/agent-vm.sh" version )
196check "bad checksum: refused, the install is untouched" "$rc:$(cat "$WF/ran")" "1:1.1.0-$PLATFORM version"
197case "$out" in *"checksum mismatch"*) pass "bad checksum: said" ;; *) fail "bad checksum: $out" ;; esac
198
199# Windows, in Git Bash: its tarball, agent-vm.exe run by agent-vm.sh.
200mkdir -p "$SB/winbin" "$WF/v1.1.8" "$WF/stage-win/agent-vm-1.1.8"
201printf '#!/bin/sh\ncase "$1" in -s) echo MINGW64_NT-10.0 ;; -m) echo x86_64 ;; esac\n' > "$SB/winbin/uname"
202chmod +x "$SB/winbin/uname"
203cp "$SELF_DIR/agent-vm.sh" "$WF/stage-win/agent-vm-1.1.8/"
204stub_binary "$WF/stage-win/agent-vm-1.1.8/agent-vm.exe" 1.1.8-windows-amd64
205tar -czf "$WF/v1.1.8/agent-vm-1.1.8-windows-amd64.tar.gz" -C "$WF/stage-win" agent-vm-1.1.8
206printf '%s agent-vm-1.1.8-windows-amd64.tar.gz\n' "$(sha256 "$WF/v1.1.8/agent-vm-1.1.8-windows-amd64.tar.gz")" > "$WF/v1.1.8/SHA256SUMS"
207: > "$WF/ran"
208out="$(PATH="$SB/winbin:$PATH" winst --version 1.1.8 --dir "$SB/win")"; rc=$?
209check "Windows (Git Bash): its tarball, agent-vm.exe run" "$rc:$(cat "$WF/ran")" "0:1.1.8-windows-amd64 install"
210
211# A release with no build for this machine: said, nothing changed.
212mkdir -p "$WF/v1.2.5"
213printf '0000 agent-vm-1.2.5-%s.tar.gz\n' "$OTHER" > "$WF/v1.2.5/SHA256SUMS"
214echo v1.2.5 > "$WF/latest"
215out="$(winst)"; rc=$?
216case "$rc:$out" in 1:*"no build for $PLATFORM"*) pass "no build for this machine: said" ;; *) fail "no build: $out" ;; esac
217
218: > "$WF/log"
219out="$(winst --version v1.0.0)"; rc=$?
220: > "$WF/ran"; ( export WF; "$WDIR/agent-vm.sh" version )
221check "--version: that release" "$rc:$(cat "$WF/ran")" "0:1.0.0-$PLATFORM version"
222check "--version: its sums, from its tag" "$(head -n 1 "$WF/log")" \
223 "https://github.com/sylvinus/agent-vm/releases/download/v1.0.0/SHA256SUMS"
224check "--version: not a version (exit 1)" "$(winst --version 1.0 >/dev/null; echo $?)" "1"
225check "--version with --git (exit 1)" "$(winst --version 1.0.0 --git >/dev/null; echo $?)" "1"
226check "unknown option (exit 2)" "$(winst --nope >/dev/null; echo $?)" "2"
227
228make_release 1.3.0
229mkdir -p "$SB/wother"; echo mine > "$SB/wother/notes"
230out="$(winst --dir "$SB/wother")"; rc=$?
231check "a directory of the user's: refused, left as it was" "$rc:$(ls "$SB/wother")" "1:notes"
232
233# A clone: agent-vm.sh builds it, then runs its install.
234rm -rf "${WDIR:?}" "${WH:?}/bin"
235: > "$WF/log"; : > "$WF/builds"
236out="$(winst --git)"; rc=$?
237check "--git: clones" "$rc:$(head -n 1 "$WF/log")" "0:git clone https://github.com/sylvinus/agent-vm.git $WDIR"
238check "--git: built once" "$(wc -l < "$WF/builds" | tr -d ' ')" "1"
239check "--git: the built binary linked onto the PATH" "$(readlink "$WH/bin/agent-vm")" "$WDIR/_output/bin/agent-vm"
240out="$(winst --git)"; rc=$?
241check "--git again: pulls" "$rc:$(tail -n 1 "$WF/log")" "0:git -C $WDIR pull --ff-only"
242check "--git again, nothing pulled: not rebuilt" "$(wc -l < "$WF/builds" | tr -d ' ')" "1"
243# 0.2's link into a clone, after a git pull to 0.3: rebuilt, then run.
244echo head2 > "$WF/head"
245ln -sfn "$WDIR/agent-vm.sh" "$SB/clone-link"
246: > "$WF/ran"
247( export WF PATH="$WBIN:$PATH"; "$SB/clone-link" version ) 2>/dev/null
248check "a 0.2 link into a clone, after a pull: rebuilt, then run" "$(wc -l < "$WF/builds" | tr -d ' '):$(cat "$WF/ran")" "2:clone version"
249out="$(winst)"; rc=$?
250check "release over a clone: refused" "$rc" "1"
251case "$out" in *"is a git clone"*"git -C"*) pass "release over a clone: says how to update it" ;; *) fail "release over a clone: $out" ;; esac
252
253# A download cut short must run nothing: only the last line calls main.
254rm -rf "$WDIR"
255sed '$d' "$INSTALLER" > "$SB/install-cut.sh"
256out="$(WINSTALLER="$SB/install-cut.sh" winst)"; rc=$?
257check "cut short: runs nothing" "$rc:$([ -e "$WDIR" ] && echo there)" "0:"
258
259printf '\n%s passed, %s failed\n' "$PASSED" "$FAIL"
260[ "$FAIL" -eq 0 ] || exit 1