@catamorphic/work-server 0.1.0-alpha.39 → 0.1.0-alpha.42
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/NOTICE.txt +7 -0
- package/README.md +9 -9
- package/dist/admission/routes.d.ts +1 -1
- package/dist/admission/routes.d.ts.map +1 -1
- package/dist/agent-capabilities.d.ts.map +1 -1
- package/dist/agents.d.ts +5 -6
- package/dist/agents.d.ts.map +1 -1
- package/dist/auth/fastify-auth.d.ts.map +1 -1
- package/dist/auth/work-auth-types.d.ts +2 -0
- package/dist/auth/work-auth-types.d.ts.map +1 -1
- package/dist/auth/work-auth.d.ts.map +1 -1
- package/dist/chunk-kcck20aw.js +676 -0
- package/dist/chunk-kcck20aw.js.map +11 -0
- package/dist/cluster.d.ts +8 -7
- package/dist/cluster.d.ts.map +1 -1
- package/dist/config.d.ts +5 -15
- package/dist/config.d.ts.map +1 -1
- package/dist/execution-config.d.ts +22 -0
- package/dist/execution-config.d.ts.map +1 -1
- package/dist/fake-agent.d.ts +1 -0
- package/dist/fake-agent.d.ts.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2730 -2851
- package/dist/index.js.map +28 -28
- package/dist/machines/contract-kit.d.ts +91 -0
- package/dist/machines/contract-kit.d.ts.map +1 -0
- package/dist/machines/contract.d.ts +383 -0
- package/dist/machines/contract.d.ts.map +1 -0
- package/dist/machines/sequencer.d.ts +267 -0
- package/dist/machines/sequencer.d.ts.map +1 -0
- package/dist/machines/simulation.d.ts +124 -0
- package/dist/machines/simulation.d.ts.map +1 -0
- package/dist/machines/work-machines.d.ts +61 -0
- package/dist/machines/work-machines.d.ts.map +1 -0
- package/dist/server.d.ts +15 -11
- package/dist/server.d.ts.map +1 -1
- package/dist/setup/machines.d.ts +9 -17
- package/dist/setup/machines.d.ts.map +1 -1
- package/dist/shares/share-routes.d.ts +1 -1
- package/dist/shares/share-routes.d.ts.map +1 -1
- package/dist/testing.d.ts +8 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +1013 -0
- package/dist/testing.js.map +11 -0
- package/dist/workers/disk-care.d.ts +41 -0
- package/dist/workers/disk-care.d.ts.map +1 -0
- package/dist/workers/harness-sign-ins.d.ts +146 -0
- package/dist/workers/harness-sign-ins.d.ts.map +1 -0
- package/dist/workers/machine-store.d.ts +196 -0
- package/dist/workers/machine-store.d.ts.map +1 -0
- package/dist/workers/member-machine-routes.d.ts +5 -3
- package/dist/workers/member-machine-routes.d.ts.map +1 -1
- package/dist/workers/member-machines.d.ts +47 -28
- package/dist/workers/member-machines.d.ts.map +1 -1
- package/dist/workers/placement.d.ts +23 -40
- package/dist/workers/placement.d.ts.map +1 -1
- package/dist/workers/sign-ins.d.ts +44 -17
- package/dist/workers/sign-ins.d.ts.map +1 -1
- package/dist/workers/worker-protocol.d.ts +5 -2
- package/dist/workers/worker-protocol.d.ts.map +1 -1
- package/dist/workers/worker-registry.d.ts +174 -223
- package/dist/workers/worker-registry.d.ts.map +1 -1
- package/dist/workers/worker-routes.d.ts.map +1 -1
- package/dist/workers/worker-runtime.d.ts.map +1 -1
- package/package.json +31 -21
- package/src/admission/routes.ts +5 -2
- package/src/agent-capabilities.ts +22 -8
- package/src/agents.ts +52 -23
- package/src/auth/fastify-auth.ts +149 -17
- package/src/auth/work-auth-types.ts +2 -0
- package/src/auth/work-auth.ts +27 -0
- package/src/cluster.ts +115 -42
- package/src/config.ts +15 -41
- package/src/execution-config.ts +40 -0
- package/src/fake-agent.ts +21 -4
- package/src/index.ts +26 -12
- package/src/machines/contract-kit.ts +648 -0
- package/src/machines/contract.ts +522 -0
- package/src/machines/sequencer.ts +1010 -0
- package/src/machines/simulation.ts +833 -0
- package/src/machines/work-machines.ts +231 -0
- package/src/server.ts +235 -150
- package/src/setup/machines.ts +128 -121
- package/src/shares/share-routes.ts +17 -1
- package/src/testing.ts +22 -0
- package/src/workers/disk-care.ts +121 -0
- package/src/workers/fake-claude-cli.ts +95 -0
- package/src/workers/fake-codex-cli.ts +11 -4
- package/src/workers/harness-sign-ins.ts +580 -0
- package/src/workers/machine-store.ts +1065 -0
- package/src/workers/member-machine-routes.ts +92 -20
- package/src/workers/member-machines.ts +163 -99
- package/src/workers/placement.ts +59 -70
- package/src/workers/sign-ins.ts +109 -52
- package/src/workers/worker-protocol.ts +5 -2
- package/src/workers/worker-registry.ts +894 -754
- package/src/workers/worker-routes.ts +4 -1
- package/src/workers/worker-runtime.ts +55 -23
- package/dist/test-support.d.ts +0 -133
- package/dist/test-support.d.ts.map +0 -1
- package/dist/workers/codex-sign-ins.d.ts +0 -107
- package/dist/workers/codex-sign-ins.d.ts.map +0 -1
- package/dist/workers/install-script.d.ts +0 -71
- package/dist/workers/install-script.d.ts.map +0 -1
- package/dist/workers/machine-classes.d.ts +0 -115
- package/dist/workers/machine-classes.d.ts.map +0 -1
- package/dist/workers/machine-rules.d.ts +0 -402
- package/dist/workers/machine-rules.d.ts.map +0 -1
- package/src/test-support.ts +0 -404
- package/src/workers/codex-sign-ins.ts +0 -360
- package/src/workers/install-script.ts +0 -394
- package/src/workers/machine-classes.ts +0 -152
- package/src/workers/machine-rules.ts +0 -1954
|
@@ -1,394 +0,0 @@
|
|
|
1
|
-
import type { FastifyInstance } from "fastify";
|
|
2
|
-
|
|
3
|
-
/** What every machine installs from this control plane (ADR 0205). */
|
|
4
|
-
export interface InstallTarget {
|
|
5
|
-
/** The control plane's public origin the worker dials. */
|
|
6
|
-
controlPlaneUrl: string;
|
|
7
|
-
/** The deployment's explicitly selected worker image. */
|
|
8
|
-
image: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The install target, or why machines cannot install from this server: a
|
|
13
|
-
* deployment-owned worker image is needed (`WORK_WORKER_IMAGE`), and a public origin other machines reach.
|
|
14
|
-
*/
|
|
15
|
-
export function installTarget(args: {
|
|
16
|
-
publicBase: string;
|
|
17
|
-
workerImage?: string;
|
|
18
|
-
}): { target: InstallTarget } | { unavailable: string } {
|
|
19
|
-
if (!args.workerImage)
|
|
20
|
-
return {
|
|
21
|
-
unavailable:
|
|
22
|
-
"This server does not know which worker image machines should run. Set WORK_WORKER_IMAGE to your deployment's pinned worker image (see @catamorphic/work-server deployment guidance).",
|
|
23
|
-
};
|
|
24
|
-
if (isLoopback(args.publicBase))
|
|
25
|
-
return {
|
|
26
|
-
unavailable:
|
|
27
|
-
"Machines cannot reach this server at a loopback address. Set WORK_PUBLIC_URL to the HTTPS origin workers dial.",
|
|
28
|
-
};
|
|
29
|
-
return {
|
|
30
|
-
target: { controlPlaneUrl: args.publicBase, image: args.workerImage },
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The sandbox backend a machine the script installs runs (`--sandbox`):
|
|
36
|
-
* `auto` picks microVMs where KVM is usable and gVisor elsewhere.
|
|
37
|
-
*/
|
|
38
|
-
export type WorkerSandbox = "auto" | "microsandbox" | "container";
|
|
39
|
-
|
|
40
|
-
/** The data directory the script uses unless told otherwise. */
|
|
41
|
-
export const DEFAULT_WORKER_DATA_DIR = "/var/lib/work";
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* The worker install script (ADR 0205): a POSIX sh script with the control
|
|
45
|
-
* plane and image baked in, holding no secret. It installs Docker where it
|
|
46
|
-
* is missing; with no usable `/dev/kvm`, or `--sandbox container`, it
|
|
47
|
-
* installs gVisor and registers the `runsc` runtime with Work's arguments
|
|
48
|
-
* (ADR 0204); then it runs the worker image with `WORK_SANDBOX` (`auto`
|
|
49
|
-
* unless `--sandbox` names a backend), the Docker socket, `/dev/kvm` when
|
|
50
|
-
* microVMs use it, and its data directory mounted at the same path.
|
|
51
|
-
*
|
|
52
|
-
* microsandbox runs each microVM through libkrun, which only opens
|
|
53
|
-
* `/dev/kvm` and keeps the guest's network in user space: the device and
|
|
54
|
-
* its group are all the worker container needs, never `--privileged` or
|
|
55
|
-
* added capabilities.
|
|
56
|
-
*
|
|
57
|
-
* Downloads land in files that are checked before use, never piped into a
|
|
58
|
-
* shell. Package installation waits, within bounds, for another one to
|
|
59
|
-
* finish, as on a cloud machine's first boot. The data directory, which is
|
|
60
|
-
* made private to the worker, is never a system directory.
|
|
61
|
-
*/
|
|
62
|
-
export function workerInstallScript(target: InstallTarget): string {
|
|
63
|
-
return `#!/bin/sh
|
|
64
|
-
# Installs a Work worker on this machine (ADR 0205), generated by the Work
|
|
65
|
-
# server at ${target.controlPlaneUrl}. It holds no secret; run it as root:
|
|
66
|
-
#
|
|
67
|
-
# curl -fsSL ${target.controlPlaneUrl}/api/workers/install.sh | sudo sh -s -- --code <enrollment code>
|
|
68
|
-
#
|
|
69
|
-
# Running it again is safe: it keeps what is installed and replaces the
|
|
70
|
-
# worker container, which keeps its enrollment in its data directory.
|
|
71
|
-
set -eu
|
|
72
|
-
# A failed download in a pipeline fails the script where the shell can say so.
|
|
73
|
-
# shellcheck disable=SC3040
|
|
74
|
-
if (set -o pipefail) 2>/dev/null; then set -o pipefail; fi
|
|
75
|
-
|
|
76
|
-
CONTROL_PLANE_URL=${shellQuote(target.controlPlaneUrl)}
|
|
77
|
-
IMAGE=${shellQuote(target.image)}
|
|
78
|
-
DATA_DIR=${DEFAULT_WORKER_DATA_DIR}
|
|
79
|
-
NAME=work-worker
|
|
80
|
-
SANDBOX=auto
|
|
81
|
-
CODE=
|
|
82
|
-
|
|
83
|
-
say() { printf 'work: %s\\n' "$*"; }
|
|
84
|
-
fail() {
|
|
85
|
-
printf 'work: %s\\n' "$*" >&2
|
|
86
|
-
exit 1
|
|
87
|
-
}
|
|
88
|
-
usage() {
|
|
89
|
-
cat <<'USAGE'
|
|
90
|
-
Usage: install.sh --code <code> [--image <image>] [--data-dir <dir>] [--name <name>] [--sandbox <backend>]
|
|
91
|
-
|
|
92
|
-
--code <code> the one-time enrollment code from the Work server (required)
|
|
93
|
-
--image <image> the worker image (default: the deployment's configured worker image)
|
|
94
|
-
--data-dir <dir> where the worker keeps its credential, sandboxes and
|
|
95
|
-
members' sign-ins (default: ${DEFAULT_WORKER_DATA_DIR}): a new
|
|
96
|
-
directory, or one under /var/lib, /srv, /opt, /data or
|
|
97
|
-
/home/<user> that is empty or a worker's
|
|
98
|
-
--name <name> the worker container's name (default: work-worker)
|
|
99
|
-
--sandbox <backend> auto (default: microVMs where KVM is usable, else
|
|
100
|
-
gVisor), microsandbox, or container (gVisor even where
|
|
101
|
-
KVM is usable)
|
|
102
|
-
USAGE
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
while [ "$#" -gt 0 ]; do
|
|
106
|
-
case "$1" in
|
|
107
|
-
--code | --image | --data-dir | --name | --sandbox)
|
|
108
|
-
[ "$#" -ge 2 ] || fail "$1 needs a value"
|
|
109
|
-
case "$1" in
|
|
110
|
-
--code) CODE=$2 ;;
|
|
111
|
-
--image) IMAGE=$2 ;;
|
|
112
|
-
--data-dir) DATA_DIR=$2 ;;
|
|
113
|
-
--name) NAME=$2 ;;
|
|
114
|
-
--sandbox) SANDBOX=$2 ;;
|
|
115
|
-
esac
|
|
116
|
-
shift 2
|
|
117
|
-
;;
|
|
118
|
-
--code=*) CODE=\${1#--code=}; shift ;;
|
|
119
|
-
--image=*) IMAGE=\${1#--image=}; shift ;;
|
|
120
|
-
--data-dir=*) DATA_DIR=\${1#--data-dir=}; shift ;;
|
|
121
|
-
--name=*) NAME=\${1#--name=}; shift ;;
|
|
122
|
-
--sandbox=*) SANDBOX=\${1#--sandbox=}; shift ;;
|
|
123
|
-
-h | --help) usage; exit 0 ;;
|
|
124
|
-
*) usage >&2; fail "Unknown option: $1" ;;
|
|
125
|
-
esac
|
|
126
|
-
done
|
|
127
|
-
|
|
128
|
-
# The data directory is handed to the worker's user and made private, so it
|
|
129
|
-
# is never a system directory: a new one, or an empty or worker's one under
|
|
130
|
-
# a place meant for data.
|
|
131
|
-
case "$DATA_DIR" in
|
|
132
|
-
/) ;;
|
|
133
|
-
*/) DATA_DIR=\${DATA_DIR%/} ;;
|
|
134
|
-
esac
|
|
135
|
-
data_dir_allowed() {
|
|
136
|
-
case "$1" in
|
|
137
|
-
/*) ;;
|
|
138
|
-
*) return 1 ;;
|
|
139
|
-
esac
|
|
140
|
-
case "$1" in
|
|
141
|
-
*//* | */./* | */../* | */. | */..) return 1 ;;
|
|
142
|
-
/ | /bin | /boot | /dev | /etc | /home | /lib | /lib32 | /lib64 | /libx32 | \\
|
|
143
|
-
/media | /mnt | /opt | /proc | /root | /run | /sbin | /snap | /srv | \\
|
|
144
|
-
/sys | /tmp | /usr | /var | /var/cache | /var/lib | /var/log | \\
|
|
145
|
-
/var/mail | /var/run | /var/spool | /var/tmp)
|
|
146
|
-
return 1 ;;
|
|
147
|
-
/bin/* | /boot/* | /dev/* | /etc/* | /lib/* | /lib32/* | /lib64/* | \\
|
|
148
|
-
/libx32/* | /proc/* | /root/* | /run/* | /sbin/* | /sys/* | /usr/*)
|
|
149
|
-
return 1 ;;
|
|
150
|
-
esac
|
|
151
|
-
[ -e "$1" ] || return 0
|
|
152
|
-
[ -d "$1" ] || return 1
|
|
153
|
-
[ -f "$1/worker-credential" ] && return 0
|
|
154
|
-
case "$1" in
|
|
155
|
-
/var/lib/?* | /srv/?* | /opt/?* | /data | /data/?* | /home/?*/?*)
|
|
156
|
-
[ -z "$(ls -A "$1")" ] ;;
|
|
157
|
-
*) return 1 ;;
|
|
158
|
-
esac
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
[ -n "$CODE" ] || fail "Pass the enrollment code: --code <code>"
|
|
162
|
-
[ "$(id -u)" -eq 0 ] || fail "Run this script as root, for example with sudo sh"
|
|
163
|
-
data_dir_allowed "$DATA_DIR" ||
|
|
164
|
-
fail "--data-dir $DATA_DIR is not a place for the worker's data: use a new directory, or an empty one under /var/lib, /srv, /opt, /data or /home/<user>"
|
|
165
|
-
case "$NAME" in
|
|
166
|
-
[a-zA-Z0-9]*) ;;
|
|
167
|
-
*) fail "--name must start with a letter or digit" ;;
|
|
168
|
-
esac
|
|
169
|
-
case "$SANDBOX" in
|
|
170
|
-
auto | microsandbox | container) ;;
|
|
171
|
-
*) fail "--sandbox is auto, microsandbox or container" ;;
|
|
172
|
-
esac
|
|
173
|
-
|
|
174
|
-
WORK_TMP=$(mktemp -d)
|
|
175
|
-
trap 'rm -rf "$WORK_TMP"' EXIT
|
|
176
|
-
|
|
177
|
-
# Download a file, and check it is there before it is used.
|
|
178
|
-
download() {
|
|
179
|
-
curl -fsSL --retry 3 --connect-timeout 20 "$1" -o "$2" ||
|
|
180
|
-
fail "Could not download $1"
|
|
181
|
-
[ -s "$2" ] || fail "$1 answered with nothing"
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
# On a first boot, cloud-init and unattended upgrades may be installing
|
|
185
|
-
# packages: wait for them, within bounds, before installing ours.
|
|
186
|
-
apt_busy() {
|
|
187
|
-
command -v fuser >/dev/null 2>&1 || return 1
|
|
188
|
-
fuser /var/lib/dpkg/lock-frontend /var/lib/dpkg/lock \\
|
|
189
|
-
/var/lib/apt/lists/lock >/dev/null 2>&1
|
|
190
|
-
}
|
|
191
|
-
wait_for_apt() {
|
|
192
|
-
waited=0
|
|
193
|
-
while apt_busy; do
|
|
194
|
-
if [ "$waited" -eq 0 ]; then
|
|
195
|
-
say "Waiting for another package installation to finish"
|
|
196
|
-
fi
|
|
197
|
-
waited=$((waited + 1))
|
|
198
|
-
[ "$waited" -le 60 ] ||
|
|
199
|
-
fail "Another package installation held apt's lock for ten minutes; run this script again later"
|
|
200
|
-
sleep 10
|
|
201
|
-
done
|
|
202
|
-
}
|
|
203
|
-
apt_get() {
|
|
204
|
-
tries=0
|
|
205
|
-
wait_for_apt
|
|
206
|
-
until DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=300 "$@"; do
|
|
207
|
-
tries=$((tries + 1))
|
|
208
|
-
[ "$tries" -lt 20 ] || fail "apt-get $1 kept failing"
|
|
209
|
-
say "apt-get $1 failed, perhaps behind another installation; trying again"
|
|
210
|
-
sleep 15
|
|
211
|
-
wait_for_apt
|
|
212
|
-
done
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
# Docker, with the official convenience script when it is missing.
|
|
216
|
-
if ! command -v docker >/dev/null 2>&1; then
|
|
217
|
-
command -v curl >/dev/null 2>&1 || fail "Install curl, then run this script again"
|
|
218
|
-
say "Installing Docker"
|
|
219
|
-
download https://get.docker.com "$WORK_TMP/get-docker.sh"
|
|
220
|
-
case "$(head -n 1 "$WORK_TMP/get-docker.sh")" in
|
|
221
|
-
'#!'*sh*) ;;
|
|
222
|
-
*) fail "https://get.docker.com did not answer with Docker's install script" ;;
|
|
223
|
-
esac
|
|
224
|
-
tries=0
|
|
225
|
-
until wait_for_apt && sh "$WORK_TMP/get-docker.sh"; do
|
|
226
|
-
tries=$((tries + 1))
|
|
227
|
-
[ "$tries" -lt 3 ] || fail "Docker's install script failed"
|
|
228
|
-
say "Docker's install script failed; trying again"
|
|
229
|
-
sleep 15
|
|
230
|
-
done
|
|
231
|
-
fi
|
|
232
|
-
if command -v systemctl >/dev/null 2>&1; then
|
|
233
|
-
systemctl enable --now docker >/dev/null 2>&1 || true
|
|
234
|
-
fi
|
|
235
|
-
docker info >/dev/null 2>&1 || fail "Docker is installed but its daemon does not answer"
|
|
236
|
-
|
|
237
|
-
restart_docker() {
|
|
238
|
-
if command -v systemctl >/dev/null 2>&1; then
|
|
239
|
-
systemctl restart docker
|
|
240
|
-
else
|
|
241
|
-
service docker restart
|
|
242
|
-
fi
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
# Isolation: microVMs where KVM is usable, gVisor everywhere else, unless
|
|
246
|
-
# --sandbox names one. gVisor runs what microVMs cannot: a sandbox whose
|
|
247
|
-
# setup has its own network (ADR 0257).
|
|
248
|
-
KVM=0
|
|
249
|
-
if [ "$SANDBOX" != container ] && [ -c /dev/kvm ] && [ -r /dev/kvm ] && [ -w /dev/kvm ]; then
|
|
250
|
-
KVM=1
|
|
251
|
-
say "KVM is available: agents run in microVMs"
|
|
252
|
-
elif [ "$SANDBOX" = microsandbox ]; then
|
|
253
|
-
fail "--sandbox microsandbox needs a /dev/kvm this script can open read-write"
|
|
254
|
-
else
|
|
255
|
-
if ! command -v runsc >/dev/null 2>&1; then
|
|
256
|
-
command -v apt-get >/dev/null 2>&1 ||
|
|
257
|
-
fail "This machine has no KVM and no apt-get. Install gVisor (https://gvisor.dev/docs/user_guide/install/), then run this script again"
|
|
258
|
-
say "No KVM: installing gVisor"
|
|
259
|
-
apt_get update -qq
|
|
260
|
-
apt_get install -y -qq ca-certificates curl gnupg >/dev/null
|
|
261
|
-
download https://gvisor.dev/archive.key "$WORK_TMP/gvisor.key"
|
|
262
|
-
grep -q 'BEGIN PGP PUBLIC KEY BLOCK' "$WORK_TMP/gvisor.key" ||
|
|
263
|
-
fail "https://gvisor.dev/archive.key is not a signing key"
|
|
264
|
-
gpg --dearmor --yes -o /usr/share/keyrings/gvisor-archive-keyring.gpg \\
|
|
265
|
-
"$WORK_TMP/gvisor.key"
|
|
266
|
-
ARCH=$(dpkg --print-architecture)
|
|
267
|
-
echo "deb [arch=$ARCH signed-by=/usr/share/keyrings/gvisor-archive-keyring.gpg] https://storage.googleapis.com/gvisor/releases release main" \\
|
|
268
|
-
>/etc/apt/sources.list.d/gvisor.list
|
|
269
|
-
apt_get update -qq
|
|
270
|
-
apt_get install -y -qq runsc >/dev/null
|
|
271
|
-
fi
|
|
272
|
-
# Sandboxes reach their egress proxy over a host socket and run nested
|
|
273
|
-
# Docker with raw sockets (ADR 0204). runsc merges its runtime into the
|
|
274
|
-
# daemon's configuration; Docker restarts only when that changed.
|
|
275
|
-
BEFORE=$(cksum /etc/docker/daemon.json 2>/dev/null || true)
|
|
276
|
-
runsc install -- --host-uds=open --net-raw
|
|
277
|
-
AFTER=$(cksum /etc/docker/daemon.json 2>/dev/null || true)
|
|
278
|
-
if [ "$BEFORE" != "$AFTER" ]; then
|
|
279
|
-
say "Restarting Docker with the gVisor runtime"
|
|
280
|
-
restart_docker
|
|
281
|
-
fi
|
|
282
|
-
RUNTIMES=$(docker info --format '{{json .Runtimes}}')
|
|
283
|
-
case "$RUNTIMES" in
|
|
284
|
-
*'"runsc"'*) ;;
|
|
285
|
-
*) fail "Docker did not load the runsc runtime; see /etc/docker/daemon.json" ;;
|
|
286
|
-
esac
|
|
287
|
-
say "gVisor is ready: agents run in gVisor sandboxes"
|
|
288
|
-
fi
|
|
289
|
-
|
|
290
|
-
# The worker's data directory belongs to the image's user (uid 1000).
|
|
291
|
-
mkdir -p "$DATA_DIR"
|
|
292
|
-
chown 1000:1000 "$DATA_DIR"
|
|
293
|
-
chmod 0700 "$DATA_DIR"
|
|
294
|
-
|
|
295
|
-
say "Pulling $IMAGE"
|
|
296
|
-
docker pull "$IMAGE" >/dev/null
|
|
297
|
-
if docker container inspect "$NAME" >/dev/null 2>&1; then
|
|
298
|
-
say "Replacing the existing $NAME container"
|
|
299
|
-
docker rm -f "$NAME" >/dev/null
|
|
300
|
-
fi
|
|
301
|
-
|
|
302
|
-
# The data directory is mounted at the same path, so the Docker daemon
|
|
303
|
-
# mounts what the worker writes there into its sandboxes. The image's health
|
|
304
|
-
# check asks the server's HTTP port, which a worker does not open.
|
|
305
|
-
set -- --detach --name "$NAME" --restart unless-stopped --no-healthcheck \\
|
|
306
|
-
--volume "$DATA_DIR:$DATA_DIR" --env "WORK_DATA_DIR=$DATA_DIR" \\
|
|
307
|
-
--volume /var/run/docker.sock:/var/run/docker.sock \\
|
|
308
|
-
--group-add "$(stat -c %g /var/run/docker.sock)" \\
|
|
309
|
-
--env "WORK_CONTROL_PLANE_URL=$CONTROL_PLANE_URL" \\
|
|
310
|
-
--env "WORK_WORKER_ENROLLMENT=$CODE" \\
|
|
311
|
-
--env "WORK_SANDBOX=$SANDBOX"
|
|
312
|
-
if [ "$KVM" = 1 ]; then
|
|
313
|
-
set -- "$@" --device /dev/kvm --group-add "$(stat -c %g /dev/kvm)"
|
|
314
|
-
fi
|
|
315
|
-
docker run "$@" "$IMAGE" bun apps/server/src/worker.ts >/dev/null
|
|
316
|
-
|
|
317
|
-
say "The worker is running as the container $NAME and connects to $CONTROL_PLANE_URL."
|
|
318
|
-
say "Follow it with: docker logs -f $NAME"
|
|
319
|
-
say "Its state: docker ps --filter name=$NAME"
|
|
320
|
-
`;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/** Where cloud-init writes the script on a cloud machine. */
|
|
324
|
-
export const CLOUD_INIT_SCRIPT_PATH = "/usr/local/sbin/work-install-worker";
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Cloud-init user data for a cloud machine (ADR 0205): the install script
|
|
328
|
-
* itself, written to disk and run once with the machine's enrollment code.
|
|
329
|
-
* Nothing is fetched over the network to start it.
|
|
330
|
-
*/
|
|
331
|
-
export function workerCloudInit(args: {
|
|
332
|
-
script: string;
|
|
333
|
-
code: string;
|
|
334
|
-
/** The machine's sandbox backend, when its class names one. */
|
|
335
|
-
sandbox?: WorkerSandbox;
|
|
336
|
-
}): string {
|
|
337
|
-
const command = [
|
|
338
|
-
"sh",
|
|
339
|
-
CLOUD_INIT_SCRIPT_PATH,
|
|
340
|
-
"--code",
|
|
341
|
-
args.code,
|
|
342
|
-
...(args.sandbox && args.sandbox !== "auto"
|
|
343
|
-
? ["--sandbox", args.sandbox]
|
|
344
|
-
: []),
|
|
345
|
-
]
|
|
346
|
-
.map((part) => JSON.stringify(part))
|
|
347
|
-
.join(", ");
|
|
348
|
-
return [
|
|
349
|
-
"#cloud-config",
|
|
350
|
-
"write_files:",
|
|
351
|
-
` - path: ${CLOUD_INIT_SCRIPT_PATH}`,
|
|
352
|
-
" owner: root:root",
|
|
353
|
-
` permissions: "0700"`,
|
|
354
|
-
" encoding: b64",
|
|
355
|
-
` content: ${Buffer.from(args.script).toString("base64")}`,
|
|
356
|
-
"runcmd:",
|
|
357
|
-
` - [${command}]`,
|
|
358
|
-
"",
|
|
359
|
-
].join("\n");
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* `GET /api/workers/install.sh`, public: what a machine runs to become a
|
|
364
|
-
* worker. Answers 503 naming what is missing when machines cannot install
|
|
365
|
-
* from this server.
|
|
366
|
-
*/
|
|
367
|
-
export function registerInstallScriptRoute(
|
|
368
|
-
app: FastifyInstance,
|
|
369
|
-
install: { target: InstallTarget } | { unavailable: string },
|
|
370
|
-
): void {
|
|
371
|
-
app.get("/api/workers/install.sh", async (_request, reply) => {
|
|
372
|
-
if ("unavailable" in install)
|
|
373
|
-
return reply
|
|
374
|
-
.status(503)
|
|
375
|
-
.type("text/plain; charset=utf-8")
|
|
376
|
-
.send(`${install.unavailable}\n`);
|
|
377
|
-
return reply
|
|
378
|
-
.type("text/x-shellscript; charset=utf-8")
|
|
379
|
-
.header("cache-control", "no-store")
|
|
380
|
-
.send(workerInstallScript(install.target));
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
/** Single-quoted for sh: safe whatever the value holds. */
|
|
385
|
-
function shellQuote(value: string): string {
|
|
386
|
-
return `'${value.replaceAll("'", `'\\''`)}'`;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
function isLoopback(base: string): boolean {
|
|
390
|
-
const { hostname } = new URL(base);
|
|
391
|
-
return (
|
|
392
|
-
hostname === "127.0.0.1" || hostname === "localhost" || hostname === "[::1]"
|
|
393
|
-
);
|
|
394
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Machine class and rule names: lowercase letters, digits, dashes inside.
|
|
6
|
-
* They become parts of host names and cloud labels.
|
|
7
|
-
*/
|
|
8
|
-
export const MachineName = z
|
|
9
|
-
.string()
|
|
10
|
-
.regex(
|
|
11
|
-
/^[a-z0-9]([a-z0-9-]{0,29}[a-z0-9])?$/,
|
|
12
|
-
"Use lowercase letters, digits, and dashes between them",
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
/** Labels Work sets on Hetzner resources itself. */
|
|
16
|
-
const RESERVED_HETZNER_LABELS = [
|
|
17
|
-
"work-machine",
|
|
18
|
-
"work-server-id",
|
|
19
|
-
"work-rule",
|
|
20
|
-
"work-class",
|
|
21
|
-
];
|
|
22
|
-
|
|
23
|
-
/** Hetzner Cloud label keys and values (an optional `prefix/` on keys). */
|
|
24
|
-
const HetznerLabels = z
|
|
25
|
-
.record(
|
|
26
|
-
z
|
|
27
|
-
.string()
|
|
28
|
-
.regex(
|
|
29
|
-
/^([a-z0-9]([-a-z0-9.]*[a-z0-9])?\/)?[A-Za-z0-9]([-A-Za-z0-9_.]{0,61}[A-Za-z0-9])?$/,
|
|
30
|
-
"Hetzner label names are letters, digits, dashes, underscores and dots",
|
|
31
|
-
),
|
|
32
|
-
z
|
|
33
|
-
.string()
|
|
34
|
-
.regex(
|
|
35
|
-
/^([A-Za-z0-9]([-A-Za-z0-9_.]{0,61}[A-Za-z0-9])?)?$/,
|
|
36
|
-
"Hetzner label values are at most 63 letters, digits, dashes, underscores and dots",
|
|
37
|
-
),
|
|
38
|
-
)
|
|
39
|
-
.refine(
|
|
40
|
-
(labels) => RESERVED_HETZNER_LABELS.every((key) => !(key in labels)),
|
|
41
|
-
`Work sets ${RESERVED_HETZNER_LABELS.join(", ")} itself`,
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The sandbox backend a class's machines run (`--sandbox` of the install
|
|
46
|
-
* script): `auto` picks microVMs where KVM is usable, gVisor elsewhere;
|
|
47
|
-
* `container` runs gVisor anyway, for Environments whose setup has its own
|
|
48
|
-
* network (ADR 0257), which microVMs cannot give.
|
|
49
|
-
*/
|
|
50
|
-
const MachineSandboxSchema = z.enum(["auto", "microsandbox", "container"]);
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* A Hetzner Cloud server per machine (ADR 0205). The API token is
|
|
54
|
-
* `WORK_HETZNER_TOKEN`, never part of the class.
|
|
55
|
-
*/
|
|
56
|
-
export const HetznerCloudClassSchema = z.strictObject({
|
|
57
|
-
platform: z.literal("hetzner-cloud"),
|
|
58
|
-
/** `cpx41`, `ccx33`, ... */
|
|
59
|
-
serverType: z.string().min(1).max(64),
|
|
60
|
-
/** `fsn1`, `nbg1`, `hel1`, `ash`, `hil`, `sin`. */
|
|
61
|
-
location: z.string().min(1).max(64),
|
|
62
|
-
/** An image name such as `ubuntu-24.04`, or an image id. */
|
|
63
|
-
image: z.string().min(1).max(128),
|
|
64
|
-
/** SSH key names or ids for root. */
|
|
65
|
-
sshKeys: z
|
|
66
|
-
.array(z.union([z.string().min(1).max(128), z.number().int().positive()]))
|
|
67
|
-
.max(50)
|
|
68
|
-
.optional(),
|
|
69
|
-
/** Firewall ids. */
|
|
70
|
-
firewalls: z.array(z.number().int().positive()).max(10).optional(),
|
|
71
|
-
/** Private network ids. */
|
|
72
|
-
networks: z.array(z.number().int().positive()).max(10).optional(),
|
|
73
|
-
/** Extra labels on the server, for the project's own bookkeeping. */
|
|
74
|
-
labels: HetznerLabels.optional(),
|
|
75
|
-
/** Keep a snapshot of the disk when the machine is destroyed. */
|
|
76
|
-
snapshot: z.boolean().default(false),
|
|
77
|
-
sandbox: MachineSandboxSchema.optional(),
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
/** Machines the operator enrolled into a pool with this class label. */
|
|
81
|
-
export const PoolClassSchema = z.strictObject({ platform: z.literal("pool") });
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Machines the `machineProvisioner` hook creates and destroys. `options`
|
|
85
|
-
* is the hook's own (an instance type, an image, a subnet): Work passes it
|
|
86
|
-
* through to every `create` unread.
|
|
87
|
-
*/
|
|
88
|
-
export const CustomClassSchema = z.strictObject({
|
|
89
|
-
platform: z.literal("custom"),
|
|
90
|
-
options: z.record(z.string(), z.json()).optional(),
|
|
91
|
-
/** The backend `enrollment.cloudInit` installs. */
|
|
92
|
-
sandbox: MachineSandboxSchema.optional(),
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
export const MachineClassSchema = z.discriminatedUnion("platform", [
|
|
96
|
-
HetznerCloudClassSchema,
|
|
97
|
-
PoolClassSchema,
|
|
98
|
-
CustomClassSchema,
|
|
99
|
-
]);
|
|
100
|
-
export type MachineClass = z.output<typeof MachineClassSchema>;
|
|
101
|
-
export type MachinePlatform = MachineClass["platform"];
|
|
102
|
-
|
|
103
|
-
/** `WORK_MACHINES_CONFIG`: the machine classes rules may name. */
|
|
104
|
-
export const MachinesConfigSchema = z.strictObject({
|
|
105
|
-
classes: z.record(MachineName, MachineClassSchema),
|
|
106
|
-
});
|
|
107
|
-
export type MachinesConfig = z.output<typeof MachinesConfigSchema>;
|
|
108
|
-
|
|
109
|
-
/** Read and validate the machines file; errors name the file. */
|
|
110
|
-
export function machinesConfigFromFile(file: string): MachinesConfig {
|
|
111
|
-
let raw: unknown;
|
|
112
|
-
try {
|
|
113
|
-
raw = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
114
|
-
} catch (error) {
|
|
115
|
-
throw new Error(
|
|
116
|
-
`WORK_MACHINES_CONFIG (${file}) is not readable JSON: ${
|
|
117
|
-
error instanceof Error ? error.message : String(error)
|
|
118
|
-
}`,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
const parsed = MachinesConfigSchema.safeParse(raw);
|
|
122
|
-
if (!parsed.success) {
|
|
123
|
-
const issue = parsed.error.issues[0];
|
|
124
|
-
throw new Error(
|
|
125
|
-
`WORK_MACHINES_CONFIG (${file}): ${
|
|
126
|
-
issue
|
|
127
|
-
? `${issue.path.join(".") || "file"}: ${issue.message}`
|
|
128
|
-
: "invalid machine classes"
|
|
129
|
-
}`,
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
return parsed.data;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Check machine classes against what the server has: a Hetzner token for
|
|
137
|
-
* `hetzner-cloud` classes and a provisioner for `custom` ones. Returns the
|
|
138
|
-
* reason a server cannot start with them, if any.
|
|
139
|
-
*/
|
|
140
|
-
export function machineClassesProblem(args: {
|
|
141
|
-
classes: Readonly<Record<string, MachineClass>>;
|
|
142
|
-
hetznerToken: boolean;
|
|
143
|
-
provisioner: boolean;
|
|
144
|
-
}): string | undefined {
|
|
145
|
-
for (const [name, machineClass] of Object.entries(args.classes)) {
|
|
146
|
-
if (machineClass.platform === "hetzner-cloud" && !args.hetznerToken)
|
|
147
|
-
return `Machine class '${name}' is on Hetzner Cloud: set WORK_HETZNER_TOKEN to a Hetzner Cloud API token with read and write access`;
|
|
148
|
-
if (machineClass.platform === "custom" && !args.provisioner)
|
|
149
|
-
return `Machine class '${name}' is custom: extend the Work server with the machineProvisioner hook, or use hetzner-cloud or pool`;
|
|
150
|
-
}
|
|
151
|
-
return undefined;
|
|
152
|
-
}
|