-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathpodman.ts
More file actions
440 lines (406 loc) · 13.5 KB
/
podman.ts
File metadata and controls
440 lines (406 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/**
* Podman sandbox provider — creates Podman containers with bind-mounts.
*
* Usage:
* import { podman } from "sandcastle/sandboxes/podman";
* await run({ agent: claudeCode("claude-opus-4-7"), sandbox: podman() });
*/
import {
execFile,
execFileSync,
spawn,
type StdioOptions,
} from "node:child_process";
import { randomUUID } from "node:crypto";
import { createInterface } from "node:readline";
import {
createBindMountSandboxProvider,
type SandboxProvider,
type BindMountCreateOptions,
type BindMountSandboxHandle,
type ExecResult,
type InteractiveExecOptions,
} from "../SandboxProvider.js";
import type { MountConfig } from "../MountConfig.js";
import type { SelinuxLabel } from "../mountUtils.js";
import {
defaultImageName,
resolveUserMounts,
formatVolumeMount,
processFileMountParents,
} from "../mountUtils.js";
export interface PodmanOptions {
/** Podman image name (default: derived from repo directory name). */
readonly imageName?: string;
/**
* SELinux volume label suffix applied to bind mounts.
*
* - `"z"` — shared label (default). No-op on non-SELinux systems.
* - `"Z"` — private label; only this container can access the mount.
* - `false` — disable labeling entirely.
*/
readonly selinuxLabel?: SelinuxLabel;
/**
* User namespace mode for rootless Podman.
*
* - `"keep-id"` (default) — maps host UID to `containerUid` inside the
* container via `--userns=keep-id:uid=N,gid=N`, so both bind-mounted
* files and image-built files have correct ownership without chown.
* - `false` — disable; use for rootful Podman setups.
*/
readonly userns?: "keep-id" | false;
/**
* The UID of the `agent` user inside the container image (default: 1000).
*
* Must match the UID set in the Containerfile. Used with `--userns=keep-id`
* to map the host user to this UID inside the container.
*/
readonly containerUid?: number;
/**
* The GID of the `agent` user inside the container image (default: 1000).
*
* Must match the GID set in the Containerfile. Used with `--userns=keep-id`
* to map the host group to this GID inside the container.
*/
readonly containerGid?: number;
/**
* Additional host directories to bind-mount into the sandbox.
*
* Each entry specifies a `hostPath` (tilde-expanded) and `sandboxPath`.
* If `hostPath` does not exist, sandbox creation fails with a clear error.
*/
readonly mounts?: readonly MountConfig[];
/** Environment variables injected by this provider. Merged at launch time with env resolver and agent provider env. */
readonly env?: Record<string, string>;
/**
* Podman network(s) to attach the container to.
*
* - `"my-network"` → `--network my-network`
* - `["net1", "net2"]` → `--network net1 --network net2`
*
* When omitted, Podman's default network is used.
*/
readonly network?: string | readonly string[];
}
/**
* Create a Podman sandbox provider.
*
* The returned provider creates Podman containers with bind-mounts
* for the worktree and git directories. Calls the `podman` binary
* on PATH directly. On macOS/Windows, verifies that a Podman Machine
* is running before container creation.
*/
export const podman = (options?: PodmanOptions): SandboxProvider => {
const configuredImageName = options?.imageName;
const selinuxLabel = options?.selinuxLabel ?? "z";
const userns = options?.userns ?? "keep-id";
const containerUid = options?.containerUid ?? 1000;
const containerGid = options?.containerGid ?? 1000;
const sandboxHomedir = "/home/agent";
const userMounts = options?.mounts
? resolveUserMounts(options.mounts, sandboxHomedir)
: [];
// Validate file mounts and collect parent dirs to create at container start.
// Throws at construction time if any file mount parent is outside sandboxHomedir.
const parentDirsToCreate = processFileMountParents(
userMounts,
sandboxHomedir,
);
return createBindMountSandboxProvider({
name: "podman",
env: options?.env,
sandboxHomedir,
create: async (
createOptions: BindMountCreateOptions,
): Promise<BindMountSandboxHandle> => {
const containerName = `sandcastle-${randomUUID()}`;
const worktreePath =
createOptions.mounts.find(
(m) => m.hostPath === createOptions.worktreePath,
)?.sandboxPath ?? "/home/agent/workspace";
// Build volume mount strings with optional SELinux label (internal + user mounts)
const allMounts = [...createOptions.mounts, ...userMounts];
const volumeMounts = allMounts.map((m) =>
formatVolumeMount(m, selinuxLabel),
);
// Resolve image name
const imageName =
configuredImageName ?? defaultImageName(createOptions.hostRepoPath);
// Pre-flight: check Podman Machine on macOS/Windows
if (process.platform === "darwin" || process.platform === "win32") {
await checkPodmanMachine();
}
// Pre-flight: verify image exists locally
await checkImageExists(imageName);
const env = { ...createOptions.env, HOME: "/home/agent" };
const envArgs = Object.entries(env).flatMap(([key, value]) => [
"-e",
`${key}=${value}`,
]);
const volumeArgs = volumeMounts.flatMap((v) => ["-v", v]);
const usernsArgs = userns
? [`--userns=keep-id:uid=${containerUid},gid=${containerGid}`]
: [];
const userArgs = ["--user", `${containerUid}:${containerGid}`];
const networks = options?.network
? Array.isArray(options.network)
? options.network
: [options.network]
: [];
const networkArgs = networks.flatMap((n) => ["--network", n]);
// Start container via podman run
await new Promise<void>((resolve, reject) => {
execFile(
"podman",
[
"run",
"-d",
"--name",
containerName,
...userArgs,
...usernsArgs,
...networkArgs,
"-w",
worktreePath,
...envArgs,
...volumeArgs,
"--entrypoint",
"sleep",
imageName,
"infinity",
],
(error) => {
if (error) {
reject(new Error(`podman run failed: ${error.message}`));
} else {
resolve();
}
},
);
});
// Create parent directories for file mounts and chown to the container user
for (const dir of parentDirsToCreate) {
await new Promise<void>((resolve, reject) => {
execFile(
"podman",
[
"exec",
"--user",
"0:0",
containerName,
"sh",
"-c",
`mkdir -p "$1" && chown "$2" "$1"`,
"sh",
dir,
`${containerUid}:${containerGid}`,
],
(error) => {
if (error) {
reject(
new Error(
`Failed to create parent directory '${dir}' in container: ${error.message}`,
),
);
} else {
resolve();
}
},
);
});
}
// Set up signal handlers for cleanup
const onExit = () => {
try {
execFileSync("podman", ["rm", "-f", containerName], {
stdio: "ignore",
timeout: 5000,
});
} catch {
/* best-effort */
}
};
const onSignal = () => {
onExit();
process.exit(1);
};
process.on("exit", onExit);
process.on("SIGINT", onSignal);
process.on("SIGTERM", onSignal);
const handle: BindMountSandboxHandle = {
worktreePath,
exec: (
command: string,
opts?: {
onLine?: (line: string) => void;
cwd?: string;
sudo?: boolean;
stdin?: string;
},
): Promise<ExecResult> => {
const effectiveCommand = opts?.sudo ? `sudo ${command}` : command;
const args = ["exec"];
if (opts?.stdin !== undefined) args.push("-i");
if (opts?.cwd) args.push("-w", opts.cwd);
args.push(containerName, "sh", "-c", effectiveCommand);
return new Promise((resolve, reject) => {
const proc = spawn("podman", args, {
stdio: [
opts?.stdin !== undefined ? "pipe" : "ignore",
"pipe",
"pipe",
],
});
if (opts?.stdin !== undefined) {
proc.stdin!.write(opts.stdin);
proc.stdin!.end();
}
const stdoutChunks: string[] = [];
const stderrChunks: string[] = [];
if (opts?.onLine) {
const onLine = opts.onLine;
const rl = createInterface({ input: proc.stdout! });
rl.on("line", (line) => {
stdoutChunks.push(line);
onLine(line);
});
} else {
proc.stdout!.on("data", (chunk: Buffer) => {
stdoutChunks.push(chunk.toString());
});
}
proc.stderr!.on("data", (chunk: Buffer) => {
stderrChunks.push(chunk.toString());
});
proc.on("error", (error) => {
reject(new Error(`podman exec failed: ${error.message}`));
});
proc.on("close", (code) => {
resolve({
stdout: stdoutChunks.join(opts?.onLine ? "\n" : ""),
stderr: stderrChunks.join(""),
exitCode: code ?? 0,
});
});
});
},
interactiveExec: (
args: string[],
opts: InteractiveExecOptions,
): Promise<{ exitCode: number }> => {
return new Promise((resolve, reject) => {
const podmanArgs = ["exec"];
// Allocate a pseudo-terminal when stdin looks like a TTY
if (
"isTTY" in opts.stdin &&
(opts.stdin as { isTTY?: boolean }).isTTY
) {
podmanArgs.push("-it");
} else {
podmanArgs.push("-i");
}
if (opts.cwd) podmanArgs.push("-w", opts.cwd);
podmanArgs.push(containerName, ...args);
const proc = spawn("podman", podmanArgs, {
stdio: [opts.stdin, opts.stdout, opts.stderr] as StdioOptions,
});
proc.on("error", (error: Error) => {
reject(new Error(`podman exec failed: ${error.message}`));
});
proc.on("close", (code: number | null) => {
resolve({ exitCode: code ?? 0 });
});
});
},
copyFileIn: (hostPath: string, sandboxPath: string): Promise<void> =>
new Promise((resolve, reject) => {
execFile(
"podman",
["cp", hostPath, `${containerName}:${sandboxPath}`],
(error) => {
if (error) {
reject(new Error(`podman cp (in) failed: ${error.message}`));
} else {
resolve();
}
},
);
}),
copyFileOut: (sandboxPath: string, hostPath: string): Promise<void> =>
new Promise((resolve, reject) => {
execFile(
"podman",
["cp", `${containerName}:${sandboxPath}`, hostPath],
(error) => {
if (error) {
reject(new Error(`podman cp (out) failed: ${error.message}`));
} else {
resolve();
}
},
);
}),
close: async (): Promise<void> => {
process.removeListener("exit", onExit);
process.removeListener("SIGINT", onSignal);
process.removeListener("SIGTERM", onSignal);
await new Promise<void>((resolve, reject) => {
execFile("podman", ["rm", "-f", containerName], (error) => {
if (error) {
reject(new Error(`podman rm failed: ${error.message}`));
} else {
resolve();
}
});
});
},
};
return handle;
},
});
};
// Re-export for backwards compatibility
export { defaultImageName };
const checkImageExists = (imageName: string): Promise<void> =>
new Promise<void>((resolve, reject) => {
execFile("podman", ["image", "inspect", imageName], (error) => {
if (error) {
reject(
new Error(
`Image '${imageName}' not found locally. Build it first with 'podman build -t ${imageName} .'`,
),
);
} else {
resolve();
}
});
});
const podmanMachineError = () =>
new Error(
"Podman Machine is not running. Run 'podman machine init && podman machine start' first.",
);
const checkPodmanMachine = (): Promise<void> =>
new Promise<void>((resolve, reject) => {
execFile(
"podman",
["machine", "list", "--format", "json"],
(error, stdout) => {
if (error) {
reject(podmanMachineError());
return;
}
try {
const machines = JSON.parse(stdout.toString()) as Array<{
Running?: boolean;
}>;
if (machines.some((m) => m.Running)) {
resolve();
} else {
reject(podmanMachineError());
}
} catch {
reject(podmanMachineError());
}
},
);
});