@leitwerk-dev/worker-runners 0.1.9 → 0.2.1
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/dist/{chunk-6WACBFXN.js → chunk-2P3CWUP3.js} +16 -5
- package/dist/chunk-2P3CWUP3.js.map +1 -0
- package/dist/chunk-4F7EMXMU.js +119 -0
- package/dist/chunk-4F7EMXMU.js.map +1 -0
- package/dist/chunk-6UZ2REWV.js +90 -0
- package/dist/chunk-6UZ2REWV.js.map +1 -0
- package/dist/{chunk-DL3Y3R6E.js → chunk-A3XSYWA5.js} +74 -8
- package/dist/chunk-A3XSYWA5.js.map +1 -0
- package/dist/{chunk-2TSWMVLZ.js → chunk-BRIIMAZX.js} +158 -39
- package/dist/chunk-BRIIMAZX.js.map +1 -0
- package/dist/{chunk-IRFVGUXJ.js → chunk-DFPYUJIK.js} +10 -3
- package/dist/chunk-DFPYUJIK.js.map +1 -0
- package/dist/{chunk-HLTGLGUS.js → chunk-H4JEE3ZT.js} +70 -93
- package/dist/chunk-H4JEE3ZT.js.map +1 -0
- package/dist/chunk-ISH4647R.js +506 -0
- package/dist/chunk-ISH4647R.js.map +1 -0
- package/dist/chunk-UGKQOL5E.js +39 -0
- package/dist/chunk-UGKQOL5E.js.map +1 -0
- package/dist/chunk-V7TTFLGE.js +899 -0
- package/dist/chunk-V7TTFLGE.js.map +1 -0
- package/dist/chunk-YDYI3BD2.js +315 -0
- package/dist/chunk-YDYI3BD2.js.map +1 -0
- package/dist/chunk-ZGFD76I6.js +16 -0
- package/dist/chunk-ZGFD76I6.js.map +1 -0
- package/dist/docker-client-public.d.ts +2 -0
- package/dist/docker-client-public.d.ts.map +1 -0
- package/dist/docker-client-public.js +7 -0
- package/dist/docker-client-public.js.map +1 -0
- package/dist/{docker-engine-client-Cpmy2yma.d.ts → docker-engine-client-Btw6ZTXk.d.ts} +48 -34
- package/dist/docker-engine-client.d.ts +47 -33
- package/dist/docker-engine-client.d.ts.map +1 -1
- package/dist/docker-engine-http-client.d.ts +18 -9
- package/dist/docker-engine-http-client.d.ts.map +1 -1
- package/dist/docker-engine-http-client.js +12 -201
- package/dist/docker-engine-http-client.js.map +1 -1
- package/dist/docker-public.d.ts +2 -0
- package/dist/docker-public.d.ts.map +1 -0
- package/dist/docker-public.js +13 -0
- package/dist/docker-public.js.map +1 -0
- package/dist/docker-worker-runner.d.ts +25 -8
- package/dist/docker-worker-runner.d.ts.map +1 -1
- package/dist/docker-worker-runner.js +12 -235
- package/dist/docker-worker-runner.js.map +1 -1
- package/dist/fake-docker-engine-client.d.ts.map +1 -1
- package/dist/filesystem-process-state-exporter.d.ts +10 -0
- package/dist/filesystem-process-state-exporter.d.ts.map +1 -0
- package/dist/helper-process-state-exporter.d.ts +28 -0
- package/dist/helper-process-state-exporter.d.ts.map +1 -0
- package/dist/index.d.ts +2 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +46 -9
- package/dist/index.js.map +1 -1
- package/dist/kubernetes-api-client-CEe7pHC4.d.ts +453 -0
- package/dist/kubernetes-api-client.d.ts +113 -6
- package/dist/kubernetes-api-client.d.ts.map +1 -1
- package/dist/kubernetes-client-public.d.ts +2 -0
- package/dist/kubernetes-client-public.d.ts.map +1 -0
- package/dist/kubernetes-client-public.js +9 -0
- package/dist/kubernetes-client-public.js.map +1 -0
- package/dist/kubernetes-http-client.d.ts +11 -0
- package/dist/kubernetes-http-client.d.ts.map +1 -1
- package/dist/kubernetes-http-client.js +10 -295
- package/dist/kubernetes-http-client.js.map +1 -1
- package/dist/kubernetes-manifests.d.ts +189 -83
- package/dist/kubernetes-manifests.d.ts.map +1 -1
- package/dist/kubernetes-public.d.ts +2 -0
- package/dist/kubernetes-public.d.ts.map +1 -0
- package/dist/kubernetes-public.js +13 -0
- package/dist/kubernetes-public.js.map +1 -0
- package/dist/kubernetes-startup-sampler.d.ts +14 -0
- package/dist/kubernetes-startup-sampler.d.ts.map +1 -0
- package/dist/kubernetes-volume-pool-api.d.ts +114 -0
- package/dist/kubernetes-volume-pool-api.d.ts.map +1 -0
- package/dist/kubernetes-volume-pool.d.ts +41 -0
- package/dist/kubernetes-volume-pool.d.ts.map +1 -0
- package/dist/kubernetes-worker-runner.d.ts +46 -6
- package/dist/kubernetes-worker-runner.d.ts.map +1 -1
- package/dist/kubernetes-worker-runner.js +12 -161
- package/dist/kubernetes-worker-runner.js.map +1 -1
- package/dist/local-public.d.ts +2 -0
- package/dist/local-public.d.ts.map +1 -0
- package/dist/local-public.js +11 -0
- package/dist/local-public.js.map +1 -0
- package/dist/local-worker-runner.d.ts +22 -5
- package/dist/local-worker-runner.d.ts.map +1 -1
- package/dist/local-worker-runner.js +14 -108
- package/dist/local-worker-runner.js.map +1 -1
- package/dist/process-volume-retention.d.ts +15 -0
- package/dist/process-volume-retention.d.ts.map +1 -1
- package/dist/runner-utils.d.ts +8 -1
- package/dist/runner-utils.d.ts.map +1 -1
- package/dist/session-transfer-helper-cli.d.ts +2 -0
- package/dist/session-transfer-helper-cli.d.ts.map +1 -0
- package/dist/session-transfer-helper-cli.js +9 -0
- package/dist/session-transfer-helper-cli.js.map +1 -0
- package/dist/session-transfer-helper.d.ts +7 -0
- package/dist/session-transfer-helper.d.ts.map +1 -0
- package/dist/session-transfer-helper.js +11 -0
- package/dist/session-transfer-helper.js.map +1 -0
- package/dist/types-public.d.ts +3 -0
- package/dist/types-public.d.ts.map +1 -0
- package/dist/types-public.js +7 -0
- package/dist/types-public.js.map +1 -0
- package/dist/types.d.ts +140 -39
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -11
- package/dist/types.js.map +1 -1
- package/dist/worker-labels.d.ts +36 -2
- package/dist/worker-labels.d.ts.map +1 -1
- package/package.json +26 -23
- package/src/docker-client-public.ts +1 -0
- package/src/docker-engine-client.ts +62 -36
- package/src/docker-engine-http-client.ts +90 -96
- package/src/docker-public.ts +1 -0
- package/src/docker-worker-runner.ts +194 -58
- package/src/fake-docker-engine-client.ts +13 -41
- package/src/filesystem-process-state-exporter.ts +42 -0
- package/src/helper-process-state-exporter.ts +149 -0
- package/src/index.ts +5 -94
- package/src/kubernetes-api-client.ts +179 -13
- package/src/kubernetes-client-public.ts +1 -0
- package/src/kubernetes-http-client.ts +239 -92
- package/src/kubernetes-manifests.ts +469 -345
- package/src/kubernetes-public.ts +1 -0
- package/src/kubernetes-startup-sampler.ts +191 -0
- package/src/kubernetes-volume-pool-api.ts +241 -0
- package/src/kubernetes-volume-pool.ts +375 -0
- package/src/kubernetes-worker-runner.ts +361 -44
- package/src/local-public.ts +1 -0
- package/src/local-worker-runner.ts +93 -10
- package/src/process-volume-retention.ts +15 -0
- package/src/runner-utils.ts +17 -2
- package/src/session-transfer-helper-cli.ts +5 -0
- package/src/session-transfer-helper.ts +108 -0
- package/src/types-public.ts +12 -0
- package/src/types.ts +169 -36
- package/src/worker-labels.ts +47 -2
- package/dist/chunk-2TSWMVLZ.js.map +0 -1
- package/dist/chunk-6WACBFXN.js.map +0 -1
- package/dist/chunk-DL3Y3R6E.js.map +0 -1
- package/dist/chunk-DUFA24LP.js +0 -401
- package/dist/chunk-DUFA24LP.js.map +0 -1
- package/dist/chunk-HLTGLGUS.js.map +0 -1
- package/dist/chunk-IRFVGUXJ.js.map +0 -1
- package/dist/chunk-P4DIFRBH.js +0 -187
- package/dist/chunk-P4DIFRBH.js.map +0 -1
- package/dist/docker-engine-client.js +0 -10
- package/dist/docker-engine-client.js.map +0 -1
- package/dist/fake-docker-engine-client.js +0 -131
- package/dist/fake-docker-engine-client.js.map +0 -1
- package/dist/kubernetes-api-client-BcwF2uon.d.ts +0 -286
- package/dist/kubernetes-api-client.js +0 -142
- package/dist/kubernetes-api-client.js.map +0 -1
- package/dist/kubernetes-manifests.js +0 -379
- package/dist/kubernetes-manifests.js.map +0 -1
- package/dist/process-volume-retention.js +0 -46
- package/dist/process-volume-retention.js.map +0 -1
- package/dist/runner-utils.js +0 -36
- package/dist/runner-utils.js.map +0 -1
- package/dist/worker-labels.js +0 -83
- package/dist/worker-labels.js.map +0 -1
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
import { mkdirSync } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
DockerContainerExit,
|
|
5
|
+
DockerEngineClient,
|
|
6
|
+
DockerMountSpec,
|
|
7
|
+
} from "./docker-engine-client.js";
|
|
4
8
|
import { isDockerEngineNotFoundError } from "./docker-engine-http-client.js";
|
|
9
|
+
import { createFilesystemProcessStateExporter } from "./filesystem-process-state-exporter.js";
|
|
10
|
+
import {
|
|
11
|
+
createHelperProcessStateExporter,
|
|
12
|
+
exportHelperEnvironment,
|
|
13
|
+
} from "./helper-process-state-exporter.js";
|
|
5
14
|
import { UnitExitNotifier } from "./runner-utils.js";
|
|
15
|
+
import {
|
|
16
|
+
SESSION_TRANSFER_HELPER_ENTRY_PATH,
|
|
17
|
+
SESSION_TRANSFER_HELPER_MOUNT_PATH,
|
|
18
|
+
} from "./session-transfer-helper.js";
|
|
6
19
|
import type {
|
|
7
20
|
IsolatedStartWorkerInput,
|
|
21
|
+
ProcessStateExporter,
|
|
22
|
+
ProcessStateExportHelperRelayProvider,
|
|
8
23
|
ProcessVolume,
|
|
9
24
|
StopWorkerOptions,
|
|
10
25
|
WorkerExitInfo,
|
|
@@ -14,42 +29,52 @@ import type {
|
|
|
14
29
|
WorkerUnitRef,
|
|
15
30
|
} from "./types.js";
|
|
16
31
|
import {
|
|
32
|
+
buildExportHelperLabels,
|
|
17
33
|
buildWorkerUnitLabels,
|
|
34
|
+
managedExportHelperLabelSelector,
|
|
18
35
|
managedWorkerLabelSelector,
|
|
19
36
|
parseWorkerUnitIdentity,
|
|
20
37
|
} from "./worker-labels.js";
|
|
21
38
|
|
|
39
|
+
/** @internal */
|
|
22
40
|
export type DockerProcessVolumeMode = "bind" | "named_volume";
|
|
23
41
|
|
|
42
|
+
/** @internal */
|
|
24
43
|
export interface DockerProcessVolumeOptions {
|
|
44
|
+
/** @internal */
|
|
25
45
|
mode: DockerProcessVolumeMode;
|
|
26
|
-
/** Host root for per-process bind mounts (bind mode). */
|
|
46
|
+
/** Host root for per-process bind mounts (bind mode). @internal */
|
|
27
47
|
hostRoot: string;
|
|
28
|
-
/** Mount path injected into worker containers. */
|
|
48
|
+
/** Mount path injected into worker containers. @internal */
|
|
29
49
|
mountPath: string;
|
|
30
|
-
/** Prefix for named volumes (named_volume mode). */
|
|
50
|
+
/** Prefix for named volumes (named_volume mode). @internal */
|
|
31
51
|
namedVolumePrefix?: string;
|
|
32
52
|
}
|
|
33
53
|
|
|
54
|
+
/** @internal */
|
|
34
55
|
export interface DockerWorkerRunnerOptions {
|
|
56
|
+
/** @internal */
|
|
35
57
|
engine: DockerEngineClient;
|
|
58
|
+
/** @internal */
|
|
36
59
|
volume: DockerProcessVolumeOptions;
|
|
37
|
-
/** Shared private network attached to every worker container. */
|
|
60
|
+
/** Shared private network attached to every worker container. @internal */
|
|
38
61
|
defaultNetwork: string;
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
/** Host CA bundle mounted read-only into workers and
|
|
62
|
+
/** Exact isolation used for Docker-requiring processes. Omit to make them unavailable. @internal */
|
|
63
|
+
privateDaemonIsolation?: "privileged" | "sysbox-runc";
|
|
64
|
+
/** Host CA bundle mounted read-only into workers and export helpers. @internal */
|
|
42
65
|
serverCaFile?: string;
|
|
66
|
+
/** Stable internal URL used by named-volume export helpers. @internal */
|
|
67
|
+
serverUrl?: string;
|
|
68
|
+
/** Trusted image containing the bundled session-transfer helper entrypoint. @internal */
|
|
69
|
+
exporterImage?: string;
|
|
70
|
+
/** Server-owned relay registry used by named-volume export helpers. @internal */
|
|
71
|
+
helperRelays?: ProcessStateExportHelperRelayProvider;
|
|
43
72
|
}
|
|
44
73
|
|
|
45
74
|
const DEFAULT_NAMED_VOLUME_PREFIX = "leitwerk-process-";
|
|
46
75
|
const NANO_CPUS_PER_CPU = 1_000_000_000;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* Docker-managed volume so inner image/layer state never lands in the
|
|
50
|
-
* server-opaque process volume mounted at the worker's `mountPath`.
|
|
51
|
-
*/
|
|
52
|
-
const DIND_DAEMON_STORAGE_PATH = "/var/lib/docker";
|
|
76
|
+
const PRIVATE_DOCKER_ENV = "LEITWERK_PRIVATE_DOCKER";
|
|
77
|
+
const PROCESS_VOLUME_MOUNT_PATH_ENV = "LEITWERK_PROCESS_VOLUME_MOUNT_PATH";
|
|
53
78
|
const WORKER_SERVER_CA_MOUNT_PATH = "/leitwerk/server-ca.pem";
|
|
54
79
|
|
|
55
80
|
/** Replaces characters Docker rejects in container names. */
|
|
@@ -109,12 +134,7 @@ function parseMemoryToBytes(memory: string | undefined): number | undefined {
|
|
|
109
134
|
return Number.isFinite(bytes) && bytes > 0 ? bytes : undefined;
|
|
110
135
|
}
|
|
111
136
|
|
|
112
|
-
function mapExit(info: {
|
|
113
|
-
statusCode: number;
|
|
114
|
-
oomKilled?: boolean;
|
|
115
|
-
signal?: string;
|
|
116
|
-
error?: string;
|
|
117
|
-
}): WorkerExitInfo {
|
|
137
|
+
function mapExit(info: DockerContainerExit): WorkerExitInfo {
|
|
118
138
|
return {
|
|
119
139
|
exitCode: Number.isFinite(info.statusCode) ? info.statusCode : null,
|
|
120
140
|
signal: info.signal ?? null,
|
|
@@ -131,10 +151,21 @@ function mapExit(info: {
|
|
|
131
151
|
* container exits are observed via the engine's wait call and mapped into the
|
|
132
152
|
* supervisor's existing failure paths through `onExit`.
|
|
133
153
|
*/
|
|
154
|
+
/** @internal */
|
|
134
155
|
export function createDockerWorkerRunner(options: DockerWorkerRunnerOptions): {
|
|
156
|
+
/** @internal */
|
|
135
157
|
runner: WorkerRunner<IsolatedStartWorkerInput>;
|
|
158
|
+
/** @internal */
|
|
136
159
|
volume: ProcessVolume;
|
|
160
|
+
/** @internal */
|
|
161
|
+
exporter: ProcessStateExporter;
|
|
137
162
|
} {
|
|
163
|
+
if (
|
|
164
|
+
options.volume.mode === "named_volume" &&
|
|
165
|
+
(!options.serverUrl || !options.exporterImage || !options.helperRelays)
|
|
166
|
+
) {
|
|
167
|
+
throw new Error("Docker named-volume transfer exporter is not configured");
|
|
168
|
+
}
|
|
138
169
|
const { engine } = options;
|
|
139
170
|
const mountPath = options.volume.mountPath;
|
|
140
171
|
const namedVolumePrefix = options.volume.namedVolumePrefix ?? DEFAULT_NAMED_VOLUME_PREFIX;
|
|
@@ -169,6 +200,40 @@ export function createDockerWorkerRunner(options: DockerWorkerRunnerOptions): {
|
|
|
169
200
|
|
|
170
201
|
const exitNotifier = new UnitExitNotifier();
|
|
171
202
|
const watched = new Set<string>();
|
|
203
|
+
const stopPromises = new Map<string, Promise<void>>();
|
|
204
|
+
|
|
205
|
+
function stopAndRemove(ref: WorkerUnitRef, opts: StopWorkerOptions): Promise<void> {
|
|
206
|
+
const existing = stopPromises.get(ref.unitId);
|
|
207
|
+
if (existing) return existing;
|
|
208
|
+
const operation = Promise.resolve()
|
|
209
|
+
.then(async () => {
|
|
210
|
+
const timeoutSeconds = Math.max(0, Math.ceil(opts.graceMs / 1000));
|
|
211
|
+
try {
|
|
212
|
+
await engine.stopContainer(ref.unitId, { timeoutSeconds });
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (!isDockerEngineNotFoundError(error)) throw error;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
await engine.removeContainer(ref.unitId, { force: true });
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (!isDockerEngineNotFoundError(error)) throw error;
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
.finally(() => stopPromises.delete(ref.unitId));
|
|
223
|
+
stopPromises.set(ref.unitId, operation);
|
|
224
|
+
return operation;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function applyServerCa(mounts: DockerMountSpec[], env: Record<string, string>): void {
|
|
228
|
+
if (options.serverCaFile) {
|
|
229
|
+
mounts.push({
|
|
230
|
+
source: options.serverCaFile,
|
|
231
|
+
target: WORKER_SERVER_CA_MOUNT_PATH,
|
|
232
|
+
readOnly: true,
|
|
233
|
+
});
|
|
234
|
+
env.NODE_EXTRA_CA_CERTS = WORKER_SERVER_CA_MOUNT_PATH;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
172
237
|
|
|
173
238
|
function watchForExit(unitId: string): void {
|
|
174
239
|
if (watched.has(unitId)) return;
|
|
@@ -183,7 +248,8 @@ export function createDockerWorkerRunner(options: DockerWorkerRunnerOptions): {
|
|
|
183
248
|
}
|
|
184
249
|
|
|
185
250
|
const runner: WorkerRunner<IsolatedStartWorkerInput> = {
|
|
186
|
-
async start(input: IsolatedStartWorkerInput) {
|
|
251
|
+
async start(input: IsolatedStartWorkerInput, observer) {
|
|
252
|
+
observer?.report("preparing_runtime");
|
|
187
253
|
const volume = input.volume;
|
|
188
254
|
const labels = buildWorkerUnitLabels({
|
|
189
255
|
instanceId: input.instanceId,
|
|
@@ -191,28 +257,21 @@ export function createDockerWorkerRunner(options: DockerWorkerRunnerOptions): {
|
|
|
191
257
|
serverEpoch: input.serverEpoch,
|
|
192
258
|
});
|
|
193
259
|
const mounts: DockerMountSpec[] = [{ source: volume.id, target: volume.mountPath }];
|
|
194
|
-
const env = {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
env.NODE_EXTRA_CA_CERTS = WORKER_SERVER_CA_MOUNT_PATH;
|
|
202
|
-
}
|
|
203
|
-
const privileged = input.isolation.dind === "privileged";
|
|
204
|
-
const dindMode = input.isolation.dind;
|
|
205
|
-
const sysbox = dindMode === "sysbox";
|
|
206
|
-
if (sysbox && !options.sysboxRuntime) {
|
|
260
|
+
const env: Record<string, string> = {
|
|
261
|
+
...input.env,
|
|
262
|
+
[PROCESS_VOLUME_MOUNT_PATH_ENV]: volume.mountPath,
|
|
263
|
+
};
|
|
264
|
+
applyServerCa(mounts, env);
|
|
265
|
+
const isolation = input.docker ? options.privateDaemonIsolation : undefined;
|
|
266
|
+
if (input.docker && !isolation) {
|
|
207
267
|
throw new Error(
|
|
208
|
-
`Cannot start
|
|
268
|
+
`Cannot start Docker-requiring process ${input.instanceId}: docker.private_daemon is not configured`,
|
|
209
269
|
);
|
|
210
270
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const anonymousVolumes = dindMode === false ? undefined : [DIND_DAEMON_STORAGE_PATH];
|
|
271
|
+
if (input.docker) env[PRIVATE_DOCKER_ENV] = "1";
|
|
272
|
+
const privileged = isolation === "privileged";
|
|
273
|
+
const sysbox = isolation === "sysbox-runc";
|
|
274
|
+
observer?.report("allocating_runtime");
|
|
216
275
|
const { id } = await engine.createContainer({
|
|
217
276
|
name: containerName(input.instanceId, input.workerId),
|
|
218
277
|
image: input.image.reference,
|
|
@@ -221,11 +280,11 @@ export function createDockerWorkerRunner(options: DockerWorkerRunnerOptions): {
|
|
|
221
280
|
mounts,
|
|
222
281
|
networkMode: options.defaultNetwork,
|
|
223
282
|
privileged,
|
|
224
|
-
...(sysbox ? { runtime:
|
|
225
|
-
...(anonymousVolumes ? { anonymousVolumes } : {}),
|
|
283
|
+
...(sysbox ? { runtime: "sysbox-runc" } : {}),
|
|
226
284
|
nanoCpus: parseCpuToNanoCpus(input.resources?.cpu),
|
|
227
285
|
memoryBytes: parseMemoryToBytes(input.resources?.memory),
|
|
228
286
|
});
|
|
287
|
+
observer?.report("starting_runtime");
|
|
229
288
|
await engine.startContainer(id);
|
|
230
289
|
const ref: WorkerUnitRef = {
|
|
231
290
|
instanceId: input.instanceId,
|
|
@@ -233,22 +292,14 @@ export function createDockerWorkerRunner(options: DockerWorkerRunnerOptions): {
|
|
|
233
292
|
unitId: id,
|
|
234
293
|
};
|
|
235
294
|
watchForExit(id);
|
|
236
|
-
return exitNotifier.wrapUnit(ref, ref.unitId
|
|
295
|
+
return exitNotifier.wrapUnit(ref, ref.unitId, {
|
|
296
|
+
replacementHandoff: "stop-before-replacement",
|
|
297
|
+
});
|
|
237
298
|
},
|
|
238
299
|
async stop(ref: WorkerUnitRef, opts: StopWorkerOptions) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
} catch (error) {
|
|
243
|
-
if (!isDockerEngineNotFoundError(error)) throw error;
|
|
244
|
-
}
|
|
245
|
-
try {
|
|
246
|
-
await engine.removeContainer(ref.unitId, { force: true });
|
|
247
|
-
} catch (error) {
|
|
248
|
-
if (!isDockerEngineNotFoundError(error)) throw error;
|
|
249
|
-
}
|
|
250
|
-
// The process volume is intentionally kept for resume; removing the ephemeral
|
|
251
|
-
// container reclaims Docker-managed anonymous volumes (including DinD storage).
|
|
300
|
+
await stopAndRemove(ref, opts);
|
|
301
|
+
// Container removal is the replacement handoff boundary. The process volume,
|
|
302
|
+
// including private daemon state, remains available to the replacement.
|
|
252
303
|
},
|
|
253
304
|
async list(): Promise<WorkerUnitDescriptor[]> {
|
|
254
305
|
const summaries = await engine.listContainers({
|
|
@@ -288,9 +339,94 @@ export function createDockerWorkerRunner(options: DockerWorkerRunnerOptions): {
|
|
|
288
339
|
unitId: descriptor.unitId,
|
|
289
340
|
};
|
|
290
341
|
watchForExit(descriptor.unitId);
|
|
291
|
-
return exitNotifier.wrapUnit(ref, ref.unitId
|
|
342
|
+
return exitNotifier.wrapUnit(ref, ref.unitId, {
|
|
343
|
+
replacementHandoff: "stop-before-replacement",
|
|
344
|
+
});
|
|
292
345
|
},
|
|
293
346
|
};
|
|
294
347
|
|
|
295
|
-
|
|
348
|
+
const exporterServerUrl = options.serverUrl ?? "";
|
|
349
|
+
const exporterImage = options.exporterImage ?? "";
|
|
350
|
+
const helperRelays = options.helperRelays as ProcessStateExportHelperRelayProvider;
|
|
351
|
+
const exporter =
|
|
352
|
+
options.volume.mode === "bind"
|
|
353
|
+
? createFilesystemProcessStateExporter({
|
|
354
|
+
allowedRoots: [options.volume.hostRoot],
|
|
355
|
+
resolveSource: async (instanceId) => {
|
|
356
|
+
const { id } = await volume.ensure(instanceId);
|
|
357
|
+
return {
|
|
358
|
+
workspaceRoot: path.join(id, "workspace"),
|
|
359
|
+
sessionFile: path.join(id, "tree", "primary.jsonl"),
|
|
360
|
+
};
|
|
361
|
+
},
|
|
362
|
+
})
|
|
363
|
+
: createHelperProcessStateExporter({
|
|
364
|
+
volume,
|
|
365
|
+
helperRelays,
|
|
366
|
+
async launch(input) {
|
|
367
|
+
const mounts: DockerMountSpec[] = ["workspace", "tree"].map((volumeSubpath) => ({
|
|
368
|
+
source: input.volume.id,
|
|
369
|
+
target: path.posix.join(SESSION_TRANSFER_HELPER_MOUNT_PATH, volumeSubpath),
|
|
370
|
+
readOnly: true,
|
|
371
|
+
volumeSubpath,
|
|
372
|
+
}));
|
|
373
|
+
const env = exportHelperEnvironment({
|
|
374
|
+
serverUrl: exporterServerUrl,
|
|
375
|
+
exportId: input.exportId,
|
|
376
|
+
credential: input.credential,
|
|
377
|
+
});
|
|
378
|
+
applyServerCa(mounts, env);
|
|
379
|
+
const created = await engine.createContainer({
|
|
380
|
+
name: sanitizeNameSegment(input.name),
|
|
381
|
+
image: exporterImage,
|
|
382
|
+
command: ["node", SESSION_TRANSFER_HELPER_ENTRY_PATH],
|
|
383
|
+
env: toEnvList(env),
|
|
384
|
+
labels: buildExportHelperLabels({
|
|
385
|
+
instanceId: input.instanceId,
|
|
386
|
+
exportId: input.exportId,
|
|
387
|
+
}),
|
|
388
|
+
mounts,
|
|
389
|
+
networkMode: options.defaultNetwork,
|
|
390
|
+
privileged: false,
|
|
391
|
+
});
|
|
392
|
+
try {
|
|
393
|
+
await engine.startContainer(created.id);
|
|
394
|
+
} catch (error) {
|
|
395
|
+
try {
|
|
396
|
+
await engine.removeContainer(created.id, { force: true });
|
|
397
|
+
} catch (cleanupError) {
|
|
398
|
+
if (!isDockerEngineNotFoundError(cleanupError)) throw cleanupError;
|
|
399
|
+
}
|
|
400
|
+
throw error;
|
|
401
|
+
}
|
|
402
|
+
return {
|
|
403
|
+
wait: async () => {
|
|
404
|
+
const exit = await engine.waitContainer(created.id);
|
|
405
|
+
return { exitCode: exit.statusCode, reason: exit.error };
|
|
406
|
+
},
|
|
407
|
+
remove: async () => {
|
|
408
|
+
try {
|
|
409
|
+
await engine.removeContainer(created.id, { force: true });
|
|
410
|
+
} catch (error) {
|
|
411
|
+
if (!isDockerEngineNotFoundError(error)) throw error;
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
};
|
|
415
|
+
},
|
|
416
|
+
async reconcileHelpers() {
|
|
417
|
+
const helpers = await engine.listContainers({
|
|
418
|
+
labels: managedExportHelperLabelSelector(),
|
|
419
|
+
});
|
|
420
|
+
await Promise.all(
|
|
421
|
+
helpers.map(async (helper) => {
|
|
422
|
+
try {
|
|
423
|
+
await engine.removeContainer(helper.id, { force: true });
|
|
424
|
+
} catch (error) {
|
|
425
|
+
if (!isDockerEngineNotFoundError(error)) throw error;
|
|
426
|
+
}
|
|
427
|
+
}),
|
|
428
|
+
);
|
|
429
|
+
},
|
|
430
|
+
});
|
|
431
|
+
return { runner, volume, exporter };
|
|
296
432
|
}
|
|
@@ -48,17 +48,13 @@ export function createFakeDockerEngineClient(): FakeDockerEngineClient {
|
|
|
48
48
|
const volumesRemoved: string[] = [];
|
|
49
49
|
const stopCalls: Array<{ id: string; timeoutSeconds: number }> = [];
|
|
50
50
|
const removeCalls: Array<{ id: string; force?: boolean }> = [];
|
|
51
|
-
function
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
resolve = res;
|
|
58
|
-
});
|
|
59
|
-
return { promise, resolve };
|
|
51
|
+
function requireContainer(id: string): FakeContainerRecord {
|
|
52
|
+
const record = containers.get(id);
|
|
53
|
+
if (!record) {
|
|
54
|
+
throw new Error(`No such container: ${id}`);
|
|
55
|
+
}
|
|
56
|
+
return record;
|
|
60
57
|
}
|
|
61
|
-
|
|
62
58
|
return {
|
|
63
59
|
containers,
|
|
64
60
|
volumesEnsured,
|
|
@@ -68,7 +64,7 @@ export function createFakeDockerEngineClient(): FakeDockerEngineClient {
|
|
|
68
64
|
async createContainer(spec: DockerContainerSpec) {
|
|
69
65
|
fakeContainerCounter += 1;
|
|
70
66
|
const id = `fake_container_${fakeContainerCounter}`;
|
|
71
|
-
const { promise, resolve } =
|
|
67
|
+
const { promise, resolve } = Promise.withResolvers<DockerContainerExit>();
|
|
72
68
|
containers.set(id, {
|
|
73
69
|
id,
|
|
74
70
|
spec,
|
|
@@ -79,10 +75,7 @@ export function createFakeDockerEngineClient(): FakeDockerEngineClient {
|
|
|
79
75
|
return { id };
|
|
80
76
|
},
|
|
81
77
|
async startContainer(id: string) {
|
|
82
|
-
const record =
|
|
83
|
-
if (!record) {
|
|
84
|
-
throw new Error(`No such container: ${id}`);
|
|
85
|
-
}
|
|
78
|
+
const record = requireContainer(id);
|
|
86
79
|
record.running = true;
|
|
87
80
|
},
|
|
88
81
|
async stopContainer(id: string, opts: { timeoutSeconds: number }) {
|
|
@@ -121,10 +114,7 @@ export function createFakeDockerEngineClient(): FakeDockerEngineClient {
|
|
|
121
114
|
const matchesFilter = (labels: Record<string, string>) =>
|
|
122
115
|
Object.entries(filter.labels).every(([key, value]) => labels[key] === value);
|
|
123
116
|
for (const record of containers.values()) {
|
|
124
|
-
if (!record.running) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
if (!matchesFilter(record.spec.labels)) {
|
|
117
|
+
if (!record.running || !matchesFilter(record.spec.labels)) {
|
|
128
118
|
continue;
|
|
129
119
|
}
|
|
130
120
|
summaries.push({
|
|
@@ -133,13 +123,7 @@ export function createFakeDockerEngineClient(): FakeDockerEngineClient {
|
|
|
133
123
|
});
|
|
134
124
|
}
|
|
135
125
|
for (const [id, seeded] of seededState.entries()) {
|
|
136
|
-
if (containers.has(id)) {
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
if (seeded.state !== "running") {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
if (!matchesFilter(seeded.labels)) {
|
|
126
|
+
if (containers.has(id) || seeded.state !== "running" || !matchesFilter(seeded.labels)) {
|
|
143
127
|
continue;
|
|
144
128
|
}
|
|
145
129
|
summaries.push({ id, labels: seeded.labels });
|
|
@@ -147,11 +131,7 @@ export function createFakeDockerEngineClient(): FakeDockerEngineClient {
|
|
|
147
131
|
return summaries;
|
|
148
132
|
},
|
|
149
133
|
async waitContainer(id: string): Promise<DockerContainerExit> {
|
|
150
|
-
|
|
151
|
-
if (!record) {
|
|
152
|
-
throw new Error(`No such container: ${id}`);
|
|
153
|
-
}
|
|
154
|
-
return record.exitPromise;
|
|
134
|
+
return requireContainer(id).exitPromise;
|
|
155
135
|
},
|
|
156
136
|
async ensureVolume(name: string) {
|
|
157
137
|
volumesEnsured.push(name);
|
|
@@ -160,19 +140,11 @@ export function createFakeDockerEngineClient(): FakeDockerEngineClient {
|
|
|
160
140
|
volumesRemoved.push(name);
|
|
161
141
|
},
|
|
162
142
|
simulateExit(id: string, exit: DockerContainerExit) {
|
|
163
|
-
const record =
|
|
164
|
-
if (!record) {
|
|
165
|
-
throw new Error(`No such container: ${id}`);
|
|
166
|
-
}
|
|
143
|
+
const record = requireContainer(id);
|
|
167
144
|
record.running = false;
|
|
168
145
|
record.resolveExit?.(exit);
|
|
169
146
|
},
|
|
170
|
-
seedContainer(record
|
|
171
|
-
id: string;
|
|
172
|
-
labels: Record<string, string>;
|
|
173
|
-
running?: boolean;
|
|
174
|
-
state?: string;
|
|
175
|
-
}) {
|
|
147
|
+
seedContainer(record) {
|
|
176
148
|
seededState.set(record.id, {
|
|
177
149
|
labels: record.labels,
|
|
178
150
|
state: record.state ?? (record.running ? "running" : "exited"),
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { lstat, realpath } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isPathInside, prepareTransferArchive } from "@leitwerk-dev/session-transfer";
|
|
4
|
+
import type { ProcessStateExporter } from "./types.js";
|
|
5
|
+
|
|
6
|
+
export interface FilesystemExportSource {
|
|
7
|
+
workspaceRoot: string;
|
|
8
|
+
sessionFile: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createFilesystemProcessStateExporter(input: {
|
|
12
|
+
resolveSource(instanceId: string): FilesystemExportSource | Promise<FilesystemExportSource>;
|
|
13
|
+
allowedRoots: readonly string[];
|
|
14
|
+
}): ProcessStateExporter {
|
|
15
|
+
return {
|
|
16
|
+
async prepare(request) {
|
|
17
|
+
const source = await input.resolveSource(request.instanceId);
|
|
18
|
+
const allowedRoots = await Promise.all(
|
|
19
|
+
input.allowedRoots.map((root) => realpath(path.resolve(root))),
|
|
20
|
+
);
|
|
21
|
+
const workspaceRoot = await realpath(path.resolve(source.workspaceRoot));
|
|
22
|
+
const sessionFile = await realpath(path.resolve(source.sessionFile));
|
|
23
|
+
for (const candidate of [workspaceRoot, sessionFile]) {
|
|
24
|
+
if (!allowedRoots.some((root) => isPathInside(root, candidate))) {
|
|
25
|
+
throw new Error("Transfer source escapes configured process storage roots");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (!(await lstat(workspaceRoot)).isDirectory())
|
|
29
|
+
throw new Error("Process workspace is unavailable");
|
|
30
|
+
if (!(await lstat(sessionFile)).isFile())
|
|
31
|
+
throw new Error("Primary Pi session is unavailable");
|
|
32
|
+
return prepareTransferArchive({
|
|
33
|
+
workspaceRoot,
|
|
34
|
+
sessionFile,
|
|
35
|
+
manifest: request.manifest,
|
|
36
|
+
limits: request.limits,
|
|
37
|
+
signal: request.signal,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
async reconcile() {},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { Readable } from "node:stream";
|
|
2
|
+
import type {
|
|
3
|
+
ProcessStateExporter,
|
|
4
|
+
ProcessStateExportHelperRelayProvider,
|
|
5
|
+
ProcessVolume,
|
|
6
|
+
VolumeRef,
|
|
7
|
+
} from "./types.js";
|
|
8
|
+
|
|
9
|
+
export interface ExportHelperLaunchInput {
|
|
10
|
+
name: string;
|
|
11
|
+
instanceId: string;
|
|
12
|
+
exportId: string;
|
|
13
|
+
credential: string;
|
|
14
|
+
volume: VolumeRef;
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ExportHelperUnit {
|
|
19
|
+
wait(): Promise<{ exitCode: number | null; reason?: string }>;
|
|
20
|
+
remove(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function exportHelperEnvironment(input: {
|
|
24
|
+
serverUrl: string;
|
|
25
|
+
exportId: string;
|
|
26
|
+
credential: string;
|
|
27
|
+
}): Record<string, string> {
|
|
28
|
+
return {
|
|
29
|
+
LEITWERK_EXPORT_SERVER_URL: input.serverUrl,
|
|
30
|
+
LEITWERK_EXPORT_ID: input.exportId,
|
|
31
|
+
LEITWERK_EXPORT_CREDENTIAL: input.credential,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createHelperProcessStateExporter(input: {
|
|
36
|
+
volume: Pick<ProcessVolume, "ensure">;
|
|
37
|
+
helperRelays: ProcessStateExportHelperRelayProvider;
|
|
38
|
+
launch(request: ExportHelperLaunchInput): Promise<ExportHelperUnit>;
|
|
39
|
+
reconcileHelpers(): Promise<void>;
|
|
40
|
+
}): ProcessStateExporter {
|
|
41
|
+
return {
|
|
42
|
+
async prepare(request) {
|
|
43
|
+
request.signal?.throwIfAborted();
|
|
44
|
+
const volume = await input.volume.ensure(request.instanceId);
|
|
45
|
+
request.signal?.throwIfAborted();
|
|
46
|
+
const relay = input.helperRelays.create({
|
|
47
|
+
instanceId: request.instanceId,
|
|
48
|
+
manifest: request.manifest,
|
|
49
|
+
});
|
|
50
|
+
let removed = false;
|
|
51
|
+
let removal: Promise<void> | null = null;
|
|
52
|
+
let retryTimer: NodeJS.Timeout | null = null;
|
|
53
|
+
let retryDelayMs = 250;
|
|
54
|
+
let streamCompleted = false;
|
|
55
|
+
let helper: ExportHelperUnit;
|
|
56
|
+
try {
|
|
57
|
+
helper = await input.launch({
|
|
58
|
+
name: `leitwerk-export-${request.instanceId}-${relay.exportId}`,
|
|
59
|
+
instanceId: request.instanceId,
|
|
60
|
+
exportId: relay.exportId,
|
|
61
|
+
credential: relay.credential,
|
|
62
|
+
volume,
|
|
63
|
+
signal: request.signal,
|
|
64
|
+
});
|
|
65
|
+
} catch (error) {
|
|
66
|
+
relay.fail(error instanceof Error ? error : new Error("export_helper_launch_failed"));
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
const remove = async (): Promise<void> => {
|
|
70
|
+
if (removed || retryTimer) return;
|
|
71
|
+
if (removal) return removal;
|
|
72
|
+
removal = Promise.resolve()
|
|
73
|
+
.then(() => helper.remove())
|
|
74
|
+
.then(
|
|
75
|
+
() => {
|
|
76
|
+
removed = true;
|
|
77
|
+
},
|
|
78
|
+
() => {
|
|
79
|
+
// Keep cleanup owned until the runner confirms removal. A transient
|
|
80
|
+
// API failure must not escape an abort or stream event handler.
|
|
81
|
+
retryTimer = setTimeout(() => {
|
|
82
|
+
retryTimer = null;
|
|
83
|
+
void remove();
|
|
84
|
+
}, retryDelayMs);
|
|
85
|
+
retryTimer.unref();
|
|
86
|
+
retryDelayMs = Math.min(retryDelayMs * 2, 30_000);
|
|
87
|
+
},
|
|
88
|
+
)
|
|
89
|
+
.finally(() => {
|
|
90
|
+
removal = null;
|
|
91
|
+
});
|
|
92
|
+
return removal;
|
|
93
|
+
};
|
|
94
|
+
const onAbort = (): void => {
|
|
95
|
+
relay.fail(new Error("session_transfer_cancelled"));
|
|
96
|
+
void remove();
|
|
97
|
+
};
|
|
98
|
+
request.signal?.addEventListener("abort", onAbort, { once: true });
|
|
99
|
+
const exited = helper.wait().then(
|
|
100
|
+
(exit) => {
|
|
101
|
+
if (!removed && (!streamCompleted || exit.exitCode !== 0)) {
|
|
102
|
+
relay.fail(new Error(exit.reason ?? "export_helper_failed"));
|
|
103
|
+
}
|
|
104
|
+
return exit;
|
|
105
|
+
},
|
|
106
|
+
(error: unknown) => {
|
|
107
|
+
relay.fail(error instanceof Error ? error : new Error("export_helper_wait_failed"));
|
|
108
|
+
return { exitCode: null, reason: "export_helper_wait_failed" };
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
try {
|
|
112
|
+
request.signal?.throwIfAborted();
|
|
113
|
+
const report = await relay.waitForPreflight(request.signal);
|
|
114
|
+
request.signal?.throwIfAborted();
|
|
115
|
+
return {
|
|
116
|
+
...report,
|
|
117
|
+
stream(): Readable {
|
|
118
|
+
const stream = relay.activateStream();
|
|
119
|
+
let cleanupRequested = false;
|
|
120
|
+
const cleanup = (error?: Error): void => {
|
|
121
|
+
if (cleanupRequested) return;
|
|
122
|
+
cleanupRequested = true;
|
|
123
|
+
if (!streamCompleted && error) relay.fail(error);
|
|
124
|
+
request.signal?.removeEventListener("abort", onAbort);
|
|
125
|
+
if (streamCompleted) void exited.then(remove);
|
|
126
|
+
else void remove();
|
|
127
|
+
};
|
|
128
|
+
stream.once("end", () => {
|
|
129
|
+
streamCompleted = true;
|
|
130
|
+
cleanup();
|
|
131
|
+
});
|
|
132
|
+
stream.once("error", (error: Error) => cleanup(error));
|
|
133
|
+
stream.once("close", () => {
|
|
134
|
+
if (!streamCompleted) relay.fail(new Error("session_transfer_stream_closed"));
|
|
135
|
+
cleanup();
|
|
136
|
+
});
|
|
137
|
+
return stream;
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
} catch (error) {
|
|
141
|
+
request.signal?.removeEventListener("abort", onAbort);
|
|
142
|
+
relay.fail(error instanceof Error ? error : new Error("session_transfer_failed"));
|
|
143
|
+
await remove();
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
reconcile: input.reconcileHelpers,
|
|
148
|
+
};
|
|
149
|
+
}
|