@parcha/agentrun 0.4.11 → 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/install.sh ADDED
@@ -0,0 +1,789 @@
1
+ #!/bin/sh
2
+ # AgentRun's installer, for macOS and Linux (x64, arm64):
3
+ #
4
+ # curl -fsSL https://agentrun.ai/install | sh
5
+ # curl -fsSL https://agentrun.ai/install | sh -s -- --version 0.4.11 --no-start
6
+ #
7
+ # It brings its own Node.js: the newest release on the major line of the launcher's engines floor (`>=X.Y.Z`, the
8
+ # security release it needs) that is at least that floor, as nodejs.org/dist/index.json lists them, so later security
9
+ # releases come with the next install. It is downloaded from nodejs.org and checked against that release's
10
+ # SHASUMS256.txt, into ~/.agentrun/node/vX.Y.Z. It installs @parcha/agentrun, and the server it depends on, with that Node
11
+ # into ~/.agentrun/lib/<version>, and writes ~/.agentrun/bin/agentrun, which always runs that Node. The node, nvm,
12
+ # Homebrew or global npm on this machine are never used or changed. ~/.agentrun is also the server's data directory: the
13
+ # installer keeps to bin, lib, node, logs and agentrun.pid, names the server never uses. No sudo. Running it again
14
+ # installs the version asked for (the latest by default) and removes the others, except one a running server still uses.
15
+ #
16
+ # Then it starts AgentRun as a service of this user (`agentrun service install`: launchd on macOS, systemd --user on
17
+ # Linux), so it keeps running after the terminal closes and starts again at login. Where this user has no service
18
+ # manager (a container, say), it starts AgentRun in the background until the machine stops (`agentrun stop` stops it).
19
+ # Either way it returns once AgentRun answers, and prints its address. With --tailscale it is also served over HTTPS on
20
+ # the person's own tailnet (lib/tailscale.mjs), on port 7340. Options: --help.
21
+ #
22
+ # With --vps it sets up a Linux server someone rents (Ubuntu or Debian, as root) in place of this user's install: Docker
23
+ # Engine and its compose plugin from Docker's apt repository when Docker is missing (an existing Docker is never
24
+ # changed), then /opt/agentrun with a compose file and an .env (mode 600) holding the API token and the seal key, made
25
+ # once and kept. Docker Compose runs the release's published server image with this machine's Docker socket, so a run's
26
+ # computer can be a Docker sandbox. It is private: on the person's tailnet when Tailscale is logged in (the same
27
+ # lib/tailscale.mjs, run in that image with this machine's tailscale and its socket), else on loopback with the steps to
28
+ # add Tailscale; on every address only with --public. Running it again upgrades to the release it names.
29
+ #
30
+ # The whole script is one function, called on its last line, so a download cut short runs nothing.
31
+
32
+ set -eu
33
+
34
+ main() {
35
+ registry=https://registry.npmjs.org
36
+ package=@parcha/agentrun
37
+ version=latest
38
+ tarball=
39
+ node_version=
40
+ start=default
41
+ ts_flag=
42
+ vps=
43
+ vps_public=
44
+ image=
45
+ this_computer=
46
+
47
+ while [ $# -gt 0 ]; do
48
+ case $1 in
49
+ --version) [ $# -ge 2 ] || fail "--version takes a version or npm tag"; version=$2; shift ;;
50
+ --version=*) version=${1#--version=} ;;
51
+ --package) [ $# -ge 2 ] || fail "--package takes a tarball"; tarball=$2; this_computer=--package; shift ;;
52
+ --package=*) tarball=${1#--package=}; this_computer=--package ;;
53
+ --node-version) [ $# -ge 2 ] || fail "--node-version takes a Node.js version"; node_version=${2#v}; this_computer=--node-version; shift ;;
54
+ --node-version=*) node_version=${1#--node-version=}; node_version=${node_version#v}; this_computer=--node-version ;;
55
+ --no-start) start=none; this_computer=$1 ;;
56
+ --service) start=service; this_computer=$1 ;;
57
+ --tailscale | --no-tailscale) ts_flag=$1; this_computer=$1 ;;
58
+ --vps) vps=1 ;;
59
+ --public) vps_public=1 ;;
60
+ --image) [ $# -ge 2 ] || fail "--image takes an image"; image=$2; shift ;;
61
+ --image=*) image=${1#--image=} ;;
62
+ -h | --help) usage; return 0 ;;
63
+ *) fail "unknown option $1 (see --help)" ;;
64
+ esac
65
+ shift
66
+ done
67
+ if [ -n "$vps" ]; then
68
+ [ -z "$this_computer" ] || fail "$this_computer is for an install on this computer; --vps runs AgentRun in Docker (see --help)"
69
+ vps_install
70
+ return 0
71
+ fi
72
+ [ -z "$vps_public$image" ] || fail "--public and --image go with --vps (see --help)"
73
+ if [ -n "$node_version" ] && ! printf '%s\n' "$node_version" | grep -qx '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*'; then
74
+ fail "--node-version $node_version is not a Node.js version (X.Y.Z)"
75
+ fi
76
+
77
+ [ -n "${HOME:-}" ] || fail "HOME is not set"
78
+ case $HOME in *"'"*) fail "HOME ($HOME) has a single quote in it, and the command this writes quotes paths with one" ;; esac
79
+ [ "$(id -u)" != 0 ] || say "Note: installing as root, into $HOME/.agentrun. AgentRun runs as the user who installs it."
80
+
81
+ detect_platform
82
+ need tar
83
+ need gzip
84
+ root=$HOME/.agentrun
85
+
86
+ # The launcher's version and its Node floor: from the tarball given, or from the registry's document for that version.
87
+ if [ -n "$tarball" ]; then
88
+ [ -f "$tarball" ] || fail "--package $tarball: no such file"
89
+ case $tarball in /*) ;; *) tarball=$(pwd)/$tarball ;; esac
90
+ manifest=$(tar -xzOf "$tarball" package/package.json | tr -d '\n\r\t ') || fail "--package $tarball is not an npm package tarball"
91
+ spec=$tarball
92
+ else
93
+ case $version in *[!A-Za-z0-9.+-]*) fail "--version $version is not a version or npm tag" ;; esac
94
+ manifest=$(download "$registry/@parcha%2fagentrun/$version") || fail "$package has no version or tag $version on $registry"
95
+ fi
96
+ # The manifest, one JSON member a line: enough to read its own name, version and engines.node without a JSON parser.
97
+ members=$(printf '%s' "$manifest" | tr ',{}' '\n\n\n')
98
+ printf '%s\n' "$members" | grep -qx "\"name\":\"$package\"" || fail "the package to install is not $package"
99
+ version=$(printf '%s\n' "$members" | sed -n 's/^"version":"\([^"]*\)"$/\1/p' | sort -u)
100
+ case $version in "" | *[!A-Za-z0-9.+-]*) fail "$package names no single version" ;; esac
101
+ floor=$(printf '%s\n' "$members" | awk 'engines && /^"node":/ { print; exit } { engines = ($0 == "\"engines\":") }' |
102
+ sed -n 's/^"node":">=\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)"$/\1/p')
103
+ [ -n "$floor" ] || fail "$package $version names no Node.js floor (engines.node \">=X.Y.Z\")"
104
+ [ -n "$tarball" ] || spec=$package@$version
105
+ if [ -n "$node_version" ] && ! at_least "$node_version" "$floor"; then
106
+ fail "--node-version $node_version is below the Node.js $package $version needs, $floor"
107
+ fi
108
+
109
+ mkdir -p "$root"
110
+ chmod 700 "$root"
111
+ # Every download and build lands in a folder beside its final place, so each one moves in whole, in one rename.
112
+ work=$(mktemp -d "$root/.install.XXXXXX")
113
+ trap 'rm -rf "$work"' EXIT
114
+ trap 'exit 130' INT
115
+ trap 'exit 143' TERM
116
+
117
+ if [ -n "$node_version" ]; then
118
+ say "Node.js $node_version, as --node-version asks"
119
+ else
120
+ resolve_node
121
+ say "Node.js $node_version, the newest ${floor%%.*}.x at or above $floor"
122
+ fi
123
+ node_dir=$root/node/v$node_version
124
+ if [ -x "$node_dir/bin/node" ] && [ "$("$node_dir/bin/node" --version 2>/dev/null)" = "v$node_version" ]; then
125
+ say "Node.js $node_version is already in $node_dir"
126
+ else
127
+ install_node
128
+ fi
129
+
130
+ lib_dir=$root/lib/$version
131
+ say "Installing $package $version with Node.js $node_version ..."
132
+ mkdir -p "$work/lib"
133
+ printf '{ "private": true }\n' > "$work/lib/package.json"
134
+ # npm from this Node, with this Node first on PATH for the packages' own scripts. The person's npm config (a registry
135
+ # mirror, a proxy) still applies, as it does to npx.
136
+ if ! PATH="$node_dir/bin:$PATH" "$node_dir/bin/npm" install --prefix "$work/lib" --no-audit --no-fund \
137
+ --update-notifier=false --loglevel=error "$spec" > "$work/npm.log" 2>&1; then
138
+ tail -n 20 "$work/npm.log" >&2
139
+ fail "npm could not install $spec (its last lines are above)"
140
+ fi
141
+ entry=$lib_dir/node_modules/@parcha/agentrun/bin/agentrun.mjs
142
+ [ -f "$work/lib/node_modules/@parcha/agentrun/bin/agentrun.mjs" ] || fail "npm installed $spec without its bin/agentrun.mjs"
143
+ printf '%s\n' "$node_dir" > "$work/lib/.node"
144
+ mkdir -p "$root/lib"
145
+ replace "$work/lib" "$lib_dir"
146
+
147
+ # The command: this version on this Node, whatever node is on PATH. Its Node goes first on PATH too, as the server's own
148
+ # launcher and the harness installs run `node` and `npm` by name.
149
+ shim=$root/bin/agentrun
150
+ mkdir -p "$root/bin" "$root/logs"
151
+ cat > "$work/agentrun" <<EOF
152
+ #!/bin/sh
153
+ # AgentRun $version on its own Node.js $node_version, written by the AgentRun installer (https://agentrun.ai/install).
154
+ PATH='$node_dir/bin':"\$PATH"
155
+ export PATH
156
+ exec '$node_dir/bin/node' '$entry' "\$@"
157
+ EOF
158
+ chmod 755 "$work/agentrun"
159
+ mv -f "$work/agentrun" "$shim"
160
+ "$shim" help > /dev/null 2>&1 || fail "$shim does not run: $("$shim" help 2>&1 | tail -n 3)"
161
+ say "Installed AgentRun $version (Node.js $node_version) in $root"
162
+ tailnet_js=$lib_dir/node_modules/@parcha/agentrun/lib/tailscale.mjs
163
+ [ -z "$ts_flag" ] || [ -f "$tailnet_js" ] || fail "AgentRun $version has no $ts_flag; install a newer version"
164
+
165
+ add_to_path
166
+
167
+ case $start in
168
+ none)
169
+ say ""
170
+ say "Start it: $(shown_shim) service install ${ts_flag:+$ts_flag } (keeps running, starts again at login)"
171
+ say " or: $(shown_shim) (in this terminal; Ctrl-C stops it)"
172
+ ;;
173
+ service)
174
+ stop_background
175
+ "$shim" service install ${ts_flag:+"$ts_flag"}
176
+ ;;
177
+ default)
178
+ # A service already installed restarts on this version, or installs again to go on or off the tailnet; status
179
+ # exits 3 when there is none.
180
+ code=0
181
+ "$shim" service status > /dev/null 2>&1 || code=$?
182
+ if { [ "$code" = 0 ] || [ "$code" = 1 ]; } && [ -z "$ts_flag" ]; then
183
+ "$shim" service restart
184
+ elif [ "$code" = 0 ] || [ "$code" = 1 ] || { [ "$code" = 3 ] && service_manager; }; then
185
+ stop_background
186
+ "$shim" service install ${ts_flag:+"$ts_flag"}
187
+ else
188
+ [ "$code" = 3 ] || why="AgentRun $version has no service command"
189
+ say "AgentRun can't run as a service here ($why), so it runs in the background."
190
+ start_background
191
+ fi
192
+ ;;
193
+ esac
194
+
195
+ # After the start, so the version a server ran until now goes once it has stopped.
196
+ prune
197
+ }
198
+
199
+ usage() {
200
+ cat <<'EOF'
201
+ AgentRun's installer, for macOS and Linux (x64, arm64):
202
+
203
+ curl -fsSL https://agentrun.ai/install | sh
204
+ curl -fsSL https://agentrun.ai/install | sh -s -- [options]
205
+
206
+ It installs AgentRun, with a Node.js of its own, in ~/.agentrun, and starts it as a service of this user (launchd on
207
+ macOS, systemd --user on Linux), which keeps running after the terminal closes. Where there is no service manager, it
208
+ starts AgentRun in the background until the machine stops; `agentrun stop` stops it. It returns once AgentRun answers.
209
+
210
+ Options:
211
+ --version V the @parcha/agentrun version or npm tag to install (default: latest)
212
+ --package FILE install the launcher from FILE, a tarball `npm pack` made, in place of the registry's
213
+ --node-version V the Node.js version to install, at least the launcher's floor (default: the newest release on the
214
+ floor's major line, from nodejs.org/dist/index.json)
215
+ --no-start install only
216
+ --service install the service, and fail if this machine can't run one
217
+ --tailscale also serve it over HTTPS on your own tailnet only, at this machine's name there
218
+ (tailscale serve, never Funnel; Tailscale must be installed and logged in, and it says how when
219
+ not); --no-tailscale takes it off
220
+ -h, --help this help
221
+
222
+ On a Linux server you rent (Ubuntu or Debian, as root):
223
+
224
+ curl -fsSL https://agentrun.ai/install | sh -s -- --vps
225
+
226
+ --vps AgentRun in Docker Compose in /opt/agentrun, its agents' shell in Docker sandboxes. It installs
227
+ Docker Engine from Docker's apt repository when Docker is missing, and serves AgentRun on your
228
+ tailnet when Tailscale is logged in; without Tailscale, AgentRun stays on this server's loopback and
229
+ the installer says how to add it. Running it again upgrades AgentRun and keeps its token and seal key.
230
+ The server gets this machine's Docker socket, which is root on it: keep the server for AgentRun alone
231
+ --public with --vps: listen on every address of the server, port 7340, over plain HTTP, in place of the
232
+ tailnet or loopback. AGENTRUN_PUBLIC_URL names the address when the server's own is not public
233
+ --image IMAGE with --vps: run IMAGE in place of the release's ghcr.io/parcha-ai/agentrun-server image
234
+ --version V with --vps: the release of that @parcha/agentrun version or tag (default: latest)
235
+ EOF
236
+ }
237
+
238
+ say() { printf '%s\n' "$*"; }
239
+ fail() {
240
+ printf 'agentrun install: %s\n' "$*" >&2
241
+ exit 1
242
+ }
243
+ need() { command -v "$1" > /dev/null 2>&1 || fail "it needs $1, which this machine does not have"; }
244
+
245
+ download() {
246
+ if command -v curl > /dev/null 2>&1; then
247
+ curl -fsSL --retry 3 --proto '=https' --tlsv1.2 "$1"
248
+ elif command -v wget > /dev/null 2>&1; then
249
+ wget -qO- "$1"
250
+ else
251
+ fail "it needs curl or wget, which this machine does not have"
252
+ fi
253
+ }
254
+
255
+ detect_platform() {
256
+ case $(uname -s) in
257
+ Darwin) os=darwin ;;
258
+ Linux) os=linux ;;
259
+ *) fail "AgentRun runs on macOS and Linux; this is $(uname -s). On Windows, use WSL." ;;
260
+ esac
261
+ case $(uname -m) in
262
+ x86_64 | amd64) arch=x64 ;;
263
+ arm64 | aarch64) arch=arm64 ;;
264
+ *) fail "AgentRun's Node.js comes for x64 and arm64; this is $(uname -m)" ;;
265
+ esac
266
+ # A shell under Rosetta on Apple silicon says x86_64: the machine is arm64.
267
+ if [ "$os" = darwin ] && [ "$arch" = x64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = 1 ]; then arch=arm64; fi
268
+ # On a musl libc (Alpine), Node's musl build: nodejs.org has one for x64 only.
269
+ suffix=
270
+ if [ "$os" = linux ] && { ls /lib/ld-musl-* > /dev/null 2>&1 || ldd --version 2>&1 | grep -qi musl; }; then
271
+ [ "$arch" = x64 ] || fail "nodejs.org has no Node.js for musl on $arch; use a glibc Linux"
272
+ suffix=-musl
273
+ fi
274
+ }
275
+
276
+ sha256() {
277
+ if command -v sha256sum > /dev/null 2>&1; then
278
+ sha256sum "$1" | cut -d ' ' -f 1
279
+ elif command -v shasum > /dev/null 2>&1; then
280
+ shasum -a 256 "$1" | cut -d ' ' -f 1
281
+ else
282
+ fail "it needs sha256sum or shasum to check Node.js, and this machine has neither"
283
+ fi
284
+ }
285
+
286
+ # Whether version $1 is at least version $2 (X.Y.Z each).
287
+ at_least() {
288
+ printf '%s %s\n' "$1" "$2" | awk '{ split($1, a, "."); split($2, b, ".")
289
+ for (i = 1; i <= 3; i++) if (a[i] + 0 != b[i] + 0) exit !(a[i] + 0 > b[i] + 0)
290
+ exit 0 }'
291
+ }
292
+
293
+ # Of the versions on stdin (X.Y.Z, one a line), the newest on $1's major line that is at least $1.
294
+ newest_from() {
295
+ awk -F. -v floor="$1" 'BEGIN { split(floor, f, ".") }
296
+ $1 + 0 == f[1] + 0 && ($2 + 0 > f[2] + 0 || ($2 + 0 == f[2] + 0 && $3 + 0 >= f[3] + 0)) &&
297
+ (best == "" || $2 + 0 > b2 || ($2 + 0 == b2 && $3 + 0 > b3)) { best = $0; b2 = $2 + 0; b3 = $3 + 0 }
298
+ END { if (best != "") print best }'
299
+ }
300
+
301
+ # $node_version: the newest release nodejs.org builds for this platform on the floor's major line, at least the floor.
302
+ # index.json holds one release a line, its version first and its builds in "files" ("linux-x64", "osx-arm64-tar").
303
+ resolve_node() {
304
+ case $os in darwin) build=osx-$arch-tar ;; *) build=$os-$arch$suffix ;; esac
305
+ download https://nodejs.org/dist/index.json > "$work/index.json" ||
306
+ fail "could not read https://nodejs.org/dist/index.json to choose a Node.js; --node-version V names one"
307
+ node_version=$(grep -F "\"$build\"" "$work/index.json" |
308
+ sed -n 's/^{"version":"v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' | newest_from "$floor")
309
+ [ -n "$node_version" ] || fail "nodejs.org lists no Node.js ${floor%%.*}.x at or above $floor built for $build"
310
+ }
311
+
312
+ install_node() {
313
+ base=node-v$node_version-$os-$arch$suffix
314
+ say "Downloading Node.js $node_version ($os-$arch$suffix) from nodejs.org ..."
315
+ download "https://nodejs.org/dist/v$node_version/$base.tar.gz" > "$work/$base.tar.gz" || fail "could not download $base.tar.gz"
316
+ download "https://nodejs.org/dist/v$node_version/SHASUMS256.txt" > "$work/SHASUMS256.txt" || fail "could not download Node.js $node_version's SHASUMS256.txt"
317
+ expected=$(awk -v file="$base.tar.gz" '$2 == file { print $1 }' "$work/SHASUMS256.txt")
318
+ [ -n "$expected" ] || fail "SHASUMS256.txt of Node.js $node_version lists no $base.tar.gz"
319
+ [ "$(sha256 "$work/$base.tar.gz")" = "$expected" ] || fail "$base.tar.gz does not match its SHA-256 in SHASUMS256.txt; nothing was installed"
320
+ say "Checked $base.tar.gz against SHASUMS256.txt"
321
+ tar -xzf "$work/$base.tar.gz" -C "$work"
322
+ [ "$("$work/$base/bin/node" --version)" = "v$node_version" ] || fail "the Node.js downloaded does not run here"
323
+ mkdir -p "$root/node"
324
+ replace "$work/$base" "$node_dir"
325
+ }
326
+
327
+ # Moves the folder $1 to $2, in place of what $2 held.
328
+ replace() {
329
+ if [ -e "$2" ]; then
330
+ mv "$2" "$work/old"
331
+ mv "$1" "$2"
332
+ rm -rf "$work/old"
333
+ else
334
+ mv "$1" "$2"
335
+ fi
336
+ }
337
+
338
+ # Removes every other version in lib/ and node/ unless a running process names it on its command line: a server started
339
+ # before this install keeps its files until it stops, and its Node (lib/<version>/.node). Without ps, removes nothing.
340
+ prune() {
341
+ running=$(ps -A -o args= 2> /dev/null) || return 0
342
+ keep_node=$node_dir
343
+ for dir in "$root/lib"/*; do
344
+ [ -d "$dir" ] || continue
345
+ if [ "$dir" = "$lib_dir" ] || in_use "$dir"; then
346
+ [ -f "$dir/.node" ] && keep_node="$keep_node
347
+ $(cat "$dir/.node")"
348
+ else
349
+ rm -rf "$dir"
350
+ fi
351
+ done
352
+ for dir in "$root/node"/*; do
353
+ [ -d "$dir" ] || continue
354
+ printf '%s\n' "$keep_node" | grep -qxF "$dir" || in_use "$dir" || rm -rf "$dir"
355
+ done
356
+ }
357
+
358
+ in_use() {
359
+ case $running in *"$1/"*) return 0 ;; esac
360
+ return 1
361
+ }
362
+
363
+ # Whether this user has a service manager AgentRun can be a service of; $why says why not.
364
+ service_manager() {
365
+ why=
366
+ if [ "$os" = darwin ]; then
367
+ launchctl print "gui/$(id -u)" > /dev/null 2>&1 && return 0
368
+ why="no launchd session for this user: a login over ssh has none"
369
+ else
370
+ command -v systemctl > /dev/null 2>&1 || { why="no systemd"; return 1; }
371
+ systemctl --user show-environment > /dev/null 2>&1 && return 0
372
+ why="no systemd user session"
373
+ fi
374
+ return 1
375
+ }
376
+
377
+ # Whether process $1 runs, from /proc where there is one (a container may have no ps), else from ps. A process that ended
378
+ # is a zombie until its parent reaps it, and in a container with no init nothing does, so a zombie has ended. $args is
379
+ # its command line.
380
+ alive() {
381
+ case $1 in '' | *[!0-9]* | 0 | 1) return 1 ;; esac
382
+ if [ -r "/proc/$1/stat" ]; then
383
+ state=$(sed 's/.*) \(.\).*/\1/' "/proc/$1/stat" 2> /dev/null)
384
+ args=$(tr '\0' ' ' < "/proc/$1/cmdline" 2> /dev/null)
385
+ elif [ -e /proc/self/stat ]; then
386
+ return 1
387
+ else
388
+ state=$(ps -o stat= -p "$1" 2> /dev/null | tr -d ' ' | cut -c 1)
389
+ args=$(ps -o args= -p "$1" 2> /dev/null)
390
+ fi
391
+ [ -n "$state" ] && [ "$state" != Z ]
392
+ }
393
+
394
+ # Whether process $1 runs this install's files: a pid file outlives a crash, and its pid can be another process's by now.
395
+ ours() {
396
+ alive "$1" || return 1
397
+ case $args in *"$root/"*) return 0 ;; esac
398
+ return 1
399
+ }
400
+
401
+ # Stops the AgentRun an earlier install started in the background, so only one server uses ~/.agentrun. The installer does
402
+ # it itself, as `agentrun stop` does, since the version installed may have no `agentrun stop`: a SIGTERM to the launcher,
403
+ # which passes it on to its server (the pid in .server.json) and waits for it. Both get 45 s, past the server's 30 s
404
+ # drain, then a SIGKILL; one that outlives that fails the install, so a second server never starts beside it.
405
+ stop_background() {
406
+ bg=$(cat "$root/agentrun.pid" 2> /dev/null) || return 0
407
+ if ours "$bg"; then
408
+ server=$(sed -n 's/.*"pid": *\([0-9][0-9]*\).*/\1/p' "$root/.server.json" 2> /dev/null | head -n 1)
409
+ say "Stopping the AgentRun started in the background before (pid $bg) ..."
410
+ kill -s TERM "$bg" 2> /dev/null || true
411
+ waited=0
412
+ while { ours "$bg" || ours "$server"; } && [ "$waited" -lt 45 ]; do
413
+ sleep 1
414
+ waited=$((waited + 1))
415
+ done
416
+ if ours "$bg" || ours "$server"; then
417
+ # Started with setsid, it leads a process group, the server's too.
418
+ kill -s KILL -- "-$bg" 2> /dev/null || true
419
+ for each in $bg $server; do
420
+ if ours "$each"; then kill -s KILL "$each" 2> /dev/null || true; fi
421
+ done
422
+ sleep 1
423
+ if ours "$bg" || ours "$server"; then
424
+ fail "the AgentRun started before (pid $bg) still runs after a SIGKILL; stop it, then run the installer again"
425
+ fi
426
+ fi
427
+ fi
428
+ rm -f "$root/agentrun.pid"
429
+ }
430
+
431
+ # AgentRun in the background, where there is no service manager: in a session of its own where setsid is there (Linux),
432
+ # so neither the terminal closing nor a Ctrl-C to it stops AgentRun, with nothing open on this script's output, so a
433
+ # `curl | sh` returns. Its output goes to the service's log, its pid to agentrun.pid for `agentrun stop`. Waits for the
434
+ # launcher's "AgentRun is running at" line, or for it to end (it stops a server that never answers, after 60 s).
435
+ start_background() {
436
+ # On the tailnet (--tailscale, or an earlier install's entry kept): the server takes its https address and a fixed port.
437
+ # Checked before the AgentRun running now stops, so a refusal (Funnel on there) leaves it running.
438
+ [ -n "$ts_flag" ] || [ ! -f "$root/tailscale.json" ] || [ ! -f "$tailnet_js" ] || ts_flag=--tailscale
439
+ set -- --no-open
440
+ public=
441
+ if [ "$ts_flag" = --no-tailscale ]; then
442
+ tailnet off
443
+ elif [ "$ts_flag" = --tailscale ]; then
444
+ set -- --no-open --port 7340
445
+ public=$(tailnet address 7340) || exit 1
446
+ fi
447
+ stop_background
448
+ log=$root/logs/agentrun.log
449
+ touch "$log"
450
+ from=$(($(wc -c < "$log") + 1))
451
+ launch=
452
+ command -v setsid > /dev/null 2>&1 && launch=setsid
453
+ if [ -n "$public" ]; then
454
+ AGENTRUN_PUBLIC_URL=$public AGENTRUN_TRUSTED_PROXIES=loopback nohup $launch "$shim" "$@" < /dev/null >> "$log" 2>&1 &
455
+ else
456
+ nohup $launch "$shim" "$@" < /dev/null >> "$log" 2>&1 &
457
+ fi
458
+ pid=$!
459
+ printf '%s\n' "$pid" > "$root/agentrun.pid"
460
+ url=
461
+ waited=0
462
+ while [ "$waited" -lt 120 ]; do
463
+ url=$(tail -c "+$from" "$log" | sed -n 's/^AgentRun is running at //p' | head -n 1)
464
+ [ -z "$url" ] || break
465
+ alive "$pid" || break
466
+ sleep 1
467
+ waited=$((waited + 1))
468
+ done
469
+ if [ -z "$url" ]; then
470
+ tail -c "+$from" "$log" | tail -n 20 >&2
471
+ kill "$pid" 2> /dev/null || true
472
+ fail "AgentRun did not start (its last lines are above, and all of them are in $log)"
473
+ fi
474
+ # The launcher names the https address it was given; on this machine it is the loopback one, in the server's record,
475
+ # until serve is on.
476
+ here=
477
+ [ -z "$public" ] || here=$(sed -n 's/.*"base_url": *"\(http[^"]*\)\/v2".*/\1\//p' "$root/.server.json" 2> /dev/null | head -n 1)
478
+ [ -z "$here" ] || url=$here
479
+ say "AgentRun is running at $url"
480
+ say "It runs in the background until this machine or container stops. $(shown_shim) stop stops it; its log is $log."
481
+ [ -z "$public" ] || tailnet serve 7340 "$public" || exit 1
482
+ }
483
+
484
+ # tailscale.mjs of the version installed, on its Node: `address PORT`, `serve PORT URL` or `off`.
485
+ tailnet() {
486
+ verb=$1
487
+ shift
488
+ "$node_dir/bin/node" "$tailnet_js" "$verb" "${1:-}" "curl -fsSL https://agentrun.ai/install | sh -s -- $ts_flag" "${2:-}"
489
+ }
490
+
491
+ shown_shim() {
492
+ case ":$PATH:" in *":$root/bin:"*) printf agentrun ;; *) printf '%s' "$shim" ;; esac
493
+ }
494
+
495
+ # One line in the shell's startup file puts ~/.agentrun/bin on PATH, when no line there does yet.
496
+ add_to_path() {
497
+ case ":$PATH:" in *":$root/bin:"*) return 0 ;; esac
498
+ line='export PATH="$HOME/.agentrun/bin:$PATH"'
499
+ case $(basename "${SHELL:-sh}") in
500
+ zsh) rc=${ZDOTDIR:-$HOME}/.zshrc ;;
501
+ bash) if [ "$os" = darwin ]; then rc=$HOME/.bash_profile; else rc=$HOME/.bashrc; fi ;;
502
+ fish)
503
+ rc=${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/agentrun.fish
504
+ line='fish_add_path --global "$HOME/.agentrun/bin"'
505
+ mkdir -p "$(dirname "$rc")"
506
+ ;;
507
+ *) rc=$HOME/.profile ;;
508
+ esac
509
+ if grep -qs '\.agentrun/bin' "$rc"; then
510
+ say "$rc already puts ~/.agentrun/bin on PATH; a new terminal will have agentrun."
511
+ return 0
512
+ fi
513
+ printf '\n%s\n' "$line" >> "$rc"
514
+ say "Added ~/.agentrun/bin to PATH in $rc. A new terminal will have agentrun; in this one, run:"
515
+ say " $line"
516
+ }
517
+
518
+ # --vps: AgentRun in Docker Compose in /opt/agentrun on a server someone rents, as root. As Coolify's and Dokploy's
519
+ # one-line installers do: Docker when it is missing, secrets made once with openssl into an .env that later runs keep.
520
+ vps_install() {
521
+ vps_dir=/opt/agentrun
522
+ port=7340
523
+ loopback=http://127.0.0.1:7340 # key-gate: loopback-rule the server it starts, on the loopback of this server
524
+ again="curl -fsSL https://agentrun.ai/install | sh -s -- --vps${vps_public:+ --public}"
525
+ [ "$(uname -s)" = Linux ] || fail "--vps sets up a Linux server; this is $(uname -s)"
526
+ [ "$(id -u)" = 0 ] || fail "--vps installs Docker and writes $vps_dir, so it runs as root: curl -fsSL https://agentrun.ai/install | sudo sh -s -- --vps${vps_public:+ --public}"
527
+ need curl
528
+ mkdir -p "$vps_dir/tailscale"
529
+ chmod 700 "$vps_dir" "$vps_dir/tailscale"
530
+ work=$(mktemp -d "$vps_dir/.install.XXXXXX")
531
+ trap 'rm -rf "$work"' EXIT
532
+ trap 'exit 130' INT
533
+ trap 'exit 143' TERM
534
+ vps_image
535
+ vps_docker
536
+ command -v openssl > /dev/null 2>&1 || vps_apt install openssl
537
+
538
+ if ! docker image inspect "$image" > /dev/null 2>&1; then
539
+ say "Pulling $image ..."
540
+ docker pull -q "$image" > /dev/null || fail "could not pull $image"
541
+ fi
542
+
543
+ # Where it is reached: every address with --public; else the tailnet's https address when Tailscale is logged in, or
544
+ # this server's loopback, with the steps to add Tailscale said.
545
+ bind=127.0.0.1 # key-gate: loopback-rule the port is published on the loopback of this server unless --public
546
+ trusted=
547
+ if [ -n "$vps_public" ]; then
548
+ bind=0.0.0.0
549
+ url=${AGENTRUN_PUBLIC_URL:-}
550
+ if [ -z "$url" ]; then
551
+ address=$(ip -4 route get 1.1.1.1 2> /dev/null | sed -n 's/.* src \([0-9.]*\).*/\1/p' | head -n 1)
552
+ [ -n "$address" ] || fail "--public: this server's IPv4 address is unknown (ip route get); name it, then run again: curl -fsSL https://agentrun.ai/install | AGENTRUN_PUBLIC_URL=http://<address>:$port/ sh -s -- --vps --public"
553
+ url=http://$address:$port/
554
+ fi
555
+ say "--public: AgentRun listens on every address of this server, port $port, over plain HTTP, at $url"
556
+ say " Anyone who reaches that address reaches its console and API, and the setup code, your password and the API"
557
+ say " token cross the network unencrypted. Run again without --public to use Tailscale, or put a TLS proxy in front."
558
+ else
559
+ url=$(vps_tailnet address) || exit 1
560
+ if [ -n "$url" ]; then
561
+ # tailscale serve reaches the server through Docker's bridge, a private range, and names each visitor.
562
+ trusted=uniquelocal
563
+ else
564
+ url=$loopback/
565
+ fi
566
+ fi
567
+
568
+ vps_files
569
+ say "Starting AgentRun $release in Docker Compose ($vps_dir) ..."
570
+ if ! vps_compose up -d --wait --wait-timeout 300 > "$work/up.log" 2>&1; then
571
+ tail -n 20 "$work/up.log" >&2
572
+ vps_compose logs --no-color --tail 40 agentrun >&2 || true
573
+ fail "AgentRun did not come up healthy within 5 minutes (the lines above say why; cd $vps_dir && docker compose logs has them all)"
574
+ fi
575
+
576
+ # Its first visit must ask for the setup code: a server that would take a password from anyone is stopped.
577
+ host=${url#*://}
578
+ host=${host%%/*}
579
+ session=$(curl -fsS --max-time 10 -H "Host: $host" "$loopback/v2/auth/session") || session=
580
+ owned=
581
+ case $session in
582
+ *'"password_set":true'* | *'"password_set": true'*) owned=1 ;;
583
+ *'"setup_code":true'* | *'"setup_code": true'*) ;;
584
+ *)
585
+ vps_compose stop > /dev/null 2>&1 || true
586
+ fail "AgentRun answered with no setup code asked (${session:-no answer}), so whoever reached it first could take it; it was stopped"
587
+ ;;
588
+ esac
589
+
590
+ if [ "$trusted" = uniquelocal ]; then
591
+ vps_tailnet serve "$url" "\`docker compose logs\` in $vps_dir" || exit 1
592
+ fi
593
+
594
+ say ""
595
+ say "AgentRun $release is running at $url"
596
+ if [ "$bind" != 0.0.0.0 ] && [ -z "$trusted" ]; then
597
+ say "It is on this server only. From your computer: ssh -N -L $port:127.0.0.1:$port root@<this server>, then open $url" # key-gate: loopback-rule the ssh tunnel line to the loopback port
598
+ fi
599
+ if [ -n "$owned" ]; then
600
+ say "This server has its owner: sign in at $url"
601
+ else
602
+ # A good code is kept and a new one printed only when the last is expired or spent (POST /v2/setup/code).
603
+ curl -fsS --max-time 10 -X POST -H "Host: $host" "$loopback/v2/setup/code" > /dev/null 2>&1 || true
604
+ line=
605
+ waited=0
606
+ while [ -z "$line" ] && [ "$waited" -lt 10 ]; do
607
+ line=$(vps_compose logs --no-color --no-log-prefix agentrun 2> /dev/null | sed -n 's/.*\(Set up this server: .*\)$/\1/p' | tail -n 1)
608
+ [ -n "$line" ] || sleep 1
609
+ waited=$((waited + 1))
610
+ done
611
+ say "${line:-Set up this server: its link is in the log (below)}"
612
+ say "The code works once, for 30 minutes. Read the newest one later: cd $vps_dir && docker compose logs agentrun | grep 'Set up this server' | tail -n 1"
613
+ fi
614
+ say "The API token is AGENTRUN_TOKEN in $vps_dir/.env. Agents get a shell in Docker sandboxes on this server: AgentRun has"
615
+ say "its Docker socket, which is root here, so keep this server for AgentRun alone."
616
+ }
617
+
618
+ # The image of the release asked for: the server the launcher of that version (or tag) depends on, whose release tag is
619
+ # v<that version>. --image names one in its place.
620
+ vps_image() {
621
+ if [ -n "$image" ]; then
622
+ release=$image
623
+ return 0
624
+ fi
625
+ case $version in *[!A-Za-z0-9.+-]*) fail "--version $version is not a version or npm tag" ;; esac
626
+ manifest=$(download "$registry/@parcha%2fagentrun/$version") || fail "$package has no version or tag $version on $registry"
627
+ server=$(printf '%s' "$manifest" | tr ',{}' '\n\n\n' | sed -n 's/^"@parcha\/agentrun-serve":"\([^"]*\)"$/\1/p' | sort -u)
628
+ case $server in "" | *[!A-Za-z0-9.+-]*) fail "$package $version names no single server version (@parcha/agentrun-serve)" ;; esac
629
+ release=v$server
630
+ image=ghcr.io/parcha-ai/agentrun-server:$release
631
+ }
632
+
633
+ # Docker Engine 26 or later with its compose plugin: one already here is used as it is, never changed; with none, Docker
634
+ # Engine from Docker's own apt repository, as https://docs.docker.com/engine/install/ubuntu/ (and debian/) set it up.
635
+ vps_docker() {
636
+ if command -v docker > /dev/null 2>&1; then
637
+ engine=$(docker version --format '{{.Server.Version}}' 2> /dev/null) ||
638
+ fail "Docker is installed here, and its daemon does not answer (docker version). Start it (systemctl start docker), then run again: $again"
639
+ say "Docker Engine $engine is already here; the installer leaves it as it is."
640
+ else
641
+ vps_install_docker
642
+ fi
643
+ major=${engine%%.*}
644
+ case $major in "" | *[!0-9]*) fail "Docker says its version is $engine, which is not Docker Engine's" ;; esac
645
+ [ "$major" -ge 26 ] || fail "Docker sandboxes need Docker Engine 26 or later (a named volume's subpath), and this one is $engine. Upgrade it, then run again: $again"
646
+ compose=$(docker compose version --short 2> /dev/null) ||
647
+ fail "This Docker has no compose plugin (docker compose). Install it (https://docs.docker.com/compose/install/linux/), then run again: $again"
648
+ say "Docker Compose $compose"
649
+ }
650
+
651
+ vps_install_docker() {
652
+ [ -r /etc/os-release ] || fail "Docker is not installed, and this server has no /etc/os-release to say which Linux it is. Install Docker Engine 26 or later with its compose plugin (https://docs.docker.com/engine/install/), then run again: $again"
653
+ distro=$(. /etc/os-release && printf '%s' "${ID:-}")
654
+ codename=$(. /etc/os-release && printf '%s' "${UBUNTU_CODENAME:-${VERSION_CODENAME:-}}")
655
+ case $distro in
656
+ ubuntu | debian) ;;
657
+ *) fail "Docker is not installed, and the installer installs it on Ubuntu or Debian only (this is ${distro:-unknown}). Install Docker Engine 26 or later with its compose plugin (https://docs.docker.com/engine/install/), then run again: $again" ;;
658
+ esac
659
+ [ -n "$codename" ] || fail "/etc/os-release names no release codename for Docker's apt repository"
660
+ repo=https://download.docker.com/linux/$distro
661
+ say "Installing Docker Engine and its compose plugin from Docker's apt repository ($repo) ..."
662
+ vps_apt update
663
+ vps_apt install ca-certificates
664
+ if grep -qs download.docker.com /etc/apt/sources.list /etc/apt/sources.list.d/*; then
665
+ say "Docker's apt repository is already in this server's apt sources; installing from it."
666
+ else
667
+ install -m 0755 -d /etc/apt/keyrings
668
+ download "$repo/gpg" > "$work/docker.asc" || fail "could not download $repo/gpg"
669
+ install -m 0644 "$work/docker.asc" /etc/apt/keyrings/docker.asc
670
+ printf 'Types: deb\nURIs: %s\nSuites: %s\nComponents: stable\nArchitectures: %s\nSigned-By: /etc/apt/keyrings/docker.asc\n' \
671
+ "$repo" "$codename" "$(dpkg --print-architecture)" > "$work/docker.sources"
672
+ install -m 0644 "$work/docker.sources" /etc/apt/sources.list.d/docker.sources
673
+ vps_apt update
674
+ fi
675
+ vps_apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
676
+ waited=0
677
+ until engine=$(docker version --format '{{.Server.Version}}' 2> /dev/null); do
678
+ [ "$waited" -lt 30 ] || fail "Docker Engine is installed, and its daemon does not answer (systemctl status docker says why)"
679
+ sleep 1
680
+ waited=$((waited + 1))
681
+ done
682
+ say "Installed Docker Engine $engine"
683
+ }
684
+
685
+ vps_apt() {
686
+ if ! DEBIAN_FRONTEND=noninteractive apt-get -y -qq -o Dpkg::Use-Pty=0 "$@" > "$work/apt.log" 2>&1; then
687
+ tail -n 20 "$work/apt.log" >&2
688
+ fail "apt-get $* failed (its last lines are above)"
689
+ fi
690
+ }
691
+
692
+ # Compose reads the shell's variables before .env: those it reads here come from .env alone, and its files from here.
693
+ vps_compose() { (cd "$vps_dir" && unset AGENTRUN_IMAGE AGENTRUN_BIND DOCKER_GID AGENTRUN_SEAL_KEY AGENTRUN_TOKEN AGENTRUN_PUBLIC_URL AGENTRUN_TRUSTED_PROXIES COMPOSE_FILE COMPOSE_PROJECT_NAME COMPOSE_PROFILES COMPOSE_ENV_FILES && docker compose "$@"); }
694
+
695
+ # lib/tailscale.mjs of the image, run in it as root with this server's tailscale and its socket: `address`, or `serve URL
696
+ # LOG`. Its record of the serve entry is kept in $vps_dir/tailscale.
697
+ vps_tailnet() {
698
+ verb=$1
699
+ shift
700
+ ts=$(command -v tailscale 2> /dev/null) || ts=
701
+ sock=
702
+ [ -z "$ts" ] || [ ! -d /var/run/tailscale ] || sock=/var/run/tailscale
703
+ docker run --rm --network none --user 0:0 -e HOME=/root -v "$vps_dir/tailscale:/root/.agentrun" \
704
+ ${ts:+-v} ${ts:+"$ts:/usr/local/bin/tailscale:ro"} ${sock:+-v} ${sock:+"$sock:$sock"} \
705
+ --entrypoint node "$image" /opt/agentrun/packages/agentrun/lib/tailscale.mjs "$verb" "$port" "$again" "$@"
706
+ }
707
+
708
+ # .env: the token and the seal key made once and kept, then the settings this run chose; any other line stays as it is.
709
+ # compose.yaml: written again on every run (changes of one's own go in compose.override.yaml beside it).
710
+ vps_files() {
711
+ env_file=$vps_dir/.env
712
+ token=$(sed -n 's/^AGENTRUN_TOKEN=//p' "$env_file" 2> /dev/null | tail -n 1)
713
+ seal=$(sed -n 's/^AGENTRUN_SEAL_KEY=//p' "$env_file" 2> /dev/null | tail -n 1)
714
+ made=
715
+ if [ -z "$token" ]; then
716
+ token=$(openssl rand -hex 32)
717
+ made="the API token"
718
+ fi
719
+ if [ -z "$seal" ]; then
720
+ seal=$(openssl rand -base64 32)
721
+ made="${made:+$made and }the seal key"
722
+ fi
723
+ [ -n "$token" ] && [ -n "$seal" ] || fail "openssl made no API token or seal key"
724
+ gid=$(stat -c %g /var/run/docker.sock) || fail "this Docker has no /var/run/docker.sock to give AgentRun"
725
+ {
726
+ if [ -f "$env_file" ]; then
727
+ grep -v -E '^(AGENTRUN_TOKEN|AGENTRUN_SEAL_KEY|AGENTRUN_IMAGE|AGENTRUN_BIND|AGENTRUN_PUBLIC_URL|AGENTRUN_TRUSTED_PROXIES|DOCKER_GID)=' "$env_file" || true
728
+ else
729
+ echo "# AgentRun's settings for Docker Compose. The installer (--vps) keeps AGENTRUN_TOKEN and AGENTRUN_SEAL_KEY, and"
730
+ echo "# writes the lines after them on every run. Lines of your own (a model key, AGENTRUN_BOX_ALLOW) stay; keep this"
731
+ echo "# file's seal key: every saved secret is sealed under it."
732
+ fi
733
+ printf 'AGENTRUN_TOKEN=%s\n' "$token"
734
+ printf 'AGENTRUN_SEAL_KEY=%s\n' "$seal"
735
+ printf 'AGENTRUN_IMAGE=%s\n' "$image"
736
+ printf 'AGENTRUN_BIND=%s\n' "$bind"
737
+ printf 'AGENTRUN_PUBLIC_URL=%s\n' "$url"
738
+ printf 'AGENTRUN_TRUSTED_PROXIES=%s\n' "$trusted"
739
+ printf 'DOCKER_GID=%s\n' "$gid"
740
+ } > "$work/env"
741
+ chmod 600 "$work/env"
742
+ mv -f "$work/env" "$env_file"
743
+ if [ -n "$made" ]; then
744
+ say "Made $made in $env_file (mode 600), kept on every later run"
745
+ else
746
+ say "Kept the API token and the seal key in $env_file"
747
+ fi
748
+
749
+ cat > "$work/compose.yaml" << 'EOF'
750
+ # AgentRun, written by its installer (curl -fsSL https://agentrun.ai/install | sh -s -- --vps) on every run: put changes
751
+ # of your own in compose.override.yaml beside this file, which Docker Compose reads too. .env holds the settings.
752
+ #
753
+ # The server gets this server's Docker socket, so a run's computer can be a Docker sandbox: a container beside the
754
+ # server's, with no socket, every capability dropped, no network but its egress proxy, and of the data volume only its
755
+ # run's folder. Whatever reaches that socket is root on this server: keep the server for AgentRun alone.
756
+ name: agentrun
757
+ services:
758
+ agentrun:
759
+ image: ${AGENTRUN_IMAGE:?run the installer again}
760
+ ports:
761
+ - "${AGENTRUN_BIND:?run the installer again}:7340:7340"
762
+ volumes:
763
+ - agentrun-data:/data
764
+ - /var/run/docker.sock:/var/run/docker.sock
765
+ group_add:
766
+ - "${DOCKER_GID:?run the installer again}"
767
+ healthcheck:
768
+ test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:7340/v2/health').then(({ ok, status }) => { if (!ok) throw new Error(String(status)) })"] # key-gate: loopback-rule the servers own loopback, its health check
769
+ interval: 30s
770
+ timeout: 5s
771
+ retries: 3
772
+ start_period: 60s
773
+ start_interval: 2s
774
+ # Every line of .env reaches the server: the installer's, and a model key, a price or AGENTRUN_BOX_ALLOW you add.
775
+ env_file: .env
776
+ environment:
777
+ AGENTRUN_BOXES: docker
778
+ AGENTRUN_SEAL_KEY: ${AGENTRUN_SEAL_KEY:?run the installer again}
779
+ AGENTRUN_TOKEN: ${AGENTRUN_TOKEN:?run the installer again}
780
+ AGENTRUN_PUBLIC_URL: ${AGENTRUN_PUBLIC_URL:?run the installer again}
781
+ restart: unless-stopped
782
+ stop_grace_period: 15s
783
+ volumes:
784
+ agentrun-data:
785
+ EOF
786
+ mv -f "$work/compose.yaml" "$vps_dir/compose.yaml"
787
+ }
788
+
789
+ main "$@"