跳到主要内容

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

浏览文件

install.sh

212 行 · 6.8 KB历史下载
1#!/bin/sh
2#
3# agent-vm installer, served at https://www.agent-vm.org/install.sh
4#
5# curl -fsSL https://www.agent-vm.org/install.sh | sh
6# curl -fsSL https://www.agent-vm.org/install.sh | sh -s -- --git
7#
8# Options:
9# --version X.Y.Z install that release instead of the latest one
10# --git install a git clone of main instead of a release
11# --dir DIR where agent-vm goes (default: ~/.local/share/agent-vm,
12# or $XDG_DATA_HOME/agent-vm)
13#
14# A release is the tarball of this machine's OS and architecture published on
15# GitHub, checked against its SHA256SUMS. Running this again replaces it with
16# the latest release. A clone is updated with `git pull`, or by running this
17# again with --git; agent-vm.sh builds it (Go needed, and on macOS the Xcode
18# command line tools).
19#
20# Then `agent-vm.sh install` links agent-vm into ~/.local/bin (AGENT_VM_BIN_DIR
21# overrides). Nothing needs root.
22#
23# Everything is in functions and only the last line runs them, so a download
24# cut short runs nothing.
25
26set -eu
27
28REPO="sylvinus/agent-vm"
29
30say() { printf 'agent-vm: %s\n' "$*"; }
31die() { printf 'agent-vm: error: %s\n' "$*" >&2; exit 1; }
32
33need() {
34 command -v "$1" >/dev/null 2>&1 || die "$1 is required"
35}
36
37usage() {
38 echo "usage: install.sh [--version X.Y.Z] [--git] [--dir DIR]" >&2
39 exit 2
40}
41
42# HTTPS only, redirects included: the release assets redirect to another host.
43fetch() {
44 curl --proto '=https' --proto-redir '=https' --tlsv1.2 -fsSL --retry 3 -o "$2" "$1"
45}
46
47sha256_of() {
48 if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1
49 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1
50 else die "neither sha256sum nor shasum is installed"
51 fi
52}
53
54# The release's name for this machine: darwin-arm64, linux-amd64...
55platform() {
56 os="$(uname -s)"; arch="$(uname -m)"
57 case "$os" in
58 Darwin) os=darwin ;;
59 Linux) os=linux ;;
60 MINGW*|MSYS*|CYGWIN*) os=windows ;;
61 esac
62 case "$arch" in
63 x86_64|amd64) arch=amd64 ;;
64 aarch64|arm64) arch=arm64 ;;
65 esac
66 echo "$os-$arch"
67}
68
69cleanup() {
70 [ -z "$TMP" ] || rm -rf "$TMP"
71 [ -z "$LEFTOVER" ] || rm -rf "$LEFTOVER"
72 # Stopped between the two renames: the previous version goes back.
73 if [ -n "$ROLLBACK" ] && [ -e "$ROLLBACK" ] && [ ! -e "$DIR" ]; then
74 mv "$ROLLBACK" "$DIR" && say "interrupted: the previous version is back in $DIR"
75 fi
76}
77
78install_release() {
79 need curl
80 need tar
81 if [ -e "$DIR/.git" ]; then
82 die "$DIR is a git clone: update it with 'git -C \"$DIR\" pull', or run this with --git"
83 fi
84 if [ -e "$DIR" ] && [ ! -f "$DIR/agent-vm.sh" ]; then
85 die "$DIR exists and is not an agent-vm install: move it, or pick another --dir"
86 fi
87
88 if [ -n "$WANT_VERSION" ]; then
89 base="https://github.com/$REPO/releases/download/v$WANT_VERSION"
90 else
91 base="https://github.com/$REPO/releases/latest/download"
92 fi
93 fetch "$base/SHA256SUMS" "$TMP/SHA256SUMS" || die "could not download $base/SHA256SUMS"
94
95 # One line per asset: "<sha256> agent-vm-X.Y.Z-<os>-<arch>.tar.gz", this
96 # machine's taken. Releases before 0.3 have one, agent-vm-X.Y.Z.tar.gz.
97 platform="$(platform)"
98 tarball="$(awk -v p="$platform" '$2 ~ "^agent-vm-[0-9]+\\.[0-9]+\\.[0-9]+-" p "\\.tar\\.gz$" { print $2; exit }' "$TMP/SHA256SUMS")"
99 if [ -z "$tarball" ]; then
100 tarball="$(awk '$2 ~ /^agent-vm-[0-9]+\.[0-9]+\.[0-9]+\.tar\.gz$/ { print $2; exit }' "$TMP/SHA256SUMS")"
101 fi
102 [ -n "$tarball" ] || die "this release has no build for $platform"
103 expected="$(awk -v f="$tarball" '$2 == f { print $1; exit }' "$TMP/SHA256SUMS")"
104 version="${tarball#agent-vm-}"
105 version="${version%.tar.gz}"
106 version="${version%-"$platform"}"
107 if [ -n "$WANT_VERSION" ] && [ "$version" != "$WANT_VERSION" ]; then
108 die "asked for $WANT_VERSION, the release has $tarball"
109 fi
110
111 # From the tag, not from latest/: a release published in between would not
112 # match the checksum read above.
113 say "downloading agent-vm $version"
114 fetch "https://github.com/$REPO/releases/download/v$version/$tarball" "$TMP/$tarball" \
115 || die "could not download $tarball"
116 actual="$(sha256_of "$TMP/$tarball")"
117 [ "$actual" = "$expected" ] || die "checksum mismatch for $tarball (expected $expected, got $actual)"
118
119 mkdir "$TMP/x"
120 tar -xzf "$TMP/$tarball" -C "$TMP/x"
121 src="$TMP/x/agent-vm-$version"
122 [ -f "$src/agent-vm.sh" ] || die "$tarball has no agent-vm-$version/agent-vm.sh"
123
124 # Staged next to $DIR so the swap is two renames on one filesystem. The
125 # previous version is left in LEFTOVER, which cleanup removes, or put back
126 # from ROLLBACK when the swap did not finish.
127 mkdir -p "$(dirname "$DIR")"
128 LEFTOVER="$DIR.new.$$"
129 mv "$src" "$LEFTOVER"
130 if [ -e "$DIR" ]; then
131 ROLLBACK="$DIR.old.$$"
132 mv "$DIR" "$ROLLBACK"
133 mv "$LEFTOVER" "$DIR"
134 LEFTOVER="$ROLLBACK"
135 ROLLBACK=""
136 else
137 mv "$LEFTOVER" "$DIR"
138 LEFTOVER=""
139 fi
140 say "agent-vm $version is in $DIR"
141}
142
143install_git() {
144 need git
145 if [ -e "$DIR/.git" ]; then
146 say "updating the clone in $DIR"
147 git -C "$DIR" pull --ff-only
148 elif [ -e "$DIR" ]; then
149 die "$DIR exists and is not a git clone: move it, or pick another --dir"
150 else
151 mkdir -p "$(dirname "$DIR")"
152 git clone "https://github.com/$REPO.git" "$DIR"
153 fi
154}
155
156main() {
157 WANT_VERSION=""; USE_GIT=""; DIR=""
158 while [ $# -gt 0 ]; do
159 case "$1" in
160 --version)
161 [ $# -ge 2 ] || usage
162 WANT_VERSION="$2"; shift ;;
163 --version=*) WANT_VERSION="${1#*=}" ;;
164 --git) USE_GIT=1 ;;
165 --dir)
166 [ $# -ge 2 ] || usage
167 DIR="$2"; shift ;;
168 --dir=*) DIR="${1#*=}" ;;
169 -h|--help) usage ;;
170 *) usage ;;
171 esac
172 shift
173 done
174 WANT_VERSION="${WANT_VERSION#v}"
175 if [ -n "$WANT_VERSION" ]; then
176 printf '%s\n' "$WANT_VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' \
177 || die "not a version: '$WANT_VERSION' (expected X.Y.Z)"
178 [ -z "$USE_GIT" ] || die "--version installs a release: it does not go with --git"
179 fi
180
181 case "$(uname -s)" in
182 Darwin|Linux|MINGW*|MSYS*|CYGWIN*) ;;
183 *) die "agent-vm runs on macOS, Linux and Windows (Git Bash) only" ;;
184 esac
185 need bash
186
187 [ -n "${HOME:-}" ] || die "HOME is not set"
188 [ -n "$DIR" ] || DIR="${XDG_DATA_HOME:-$HOME/.local/share}/agent-vm"
189 # C:/... and C:\... are absolute too, in Git Bash.
190 case "$DIR" in
191 /*|[A-Za-z]:/*|[A-Za-z]:\\*) ;;
192 *) DIR="$(pwd)/$DIR" ;;
193 esac
194 DIR="${DIR%/}"
195
196 TMP=""; LEFTOVER=""; ROLLBACK=""
197 trap cleanup EXIT
198 trap 'exit 1' HUP INT TERM
199 TMP="$(mktemp -d 2>/dev/null || mktemp -d -t agent-vm)"
200
201 if [ -n "$USE_GIT" ]; then install_git; else install_release; fi
202
203 # `install` can go on to run `agent-vm setup`, whose brew and limactl calls
204 # may read stdin: give them the terminal, not the rest of this pipe.
205 if (exec </dev/tty) 2>/dev/null; then
206 bash "$DIR/agent-vm.sh" install </dev/tty
207 else
208 bash "$DIR/agent-vm.sh" install </dev/null
209 fi
210}
211
212main "$@"