agentcabin 0.2.0__py3-none-any.whl

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.
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env bash
2
+ # Run the install scripts of [[tools]] from agentbox.toml:
3
+ #
4
+ # install_tools.sh root|user DIR
5
+ #
6
+ # DIR holds NN-<name>.sh (the script) and NN-<name>.version for each tool, written by agentbox in
7
+ # config order. Tools run one after the other and a failing script stops the build.
8
+ #
9
+ # Environment of a script: VERSION (the tool's `version`), DEB_ARCH (amd64 | arm64), CONTAINER_USER,
10
+ # HOME and PATH (with ~/.local/bin first). `root` tools run as root, `user` tools as the container user.
11
+ set -euo pipefail
12
+
13
+ run_as="${1:?usage: install_tools.sh root|user DIR}"
14
+ dir="${2:?usage: install_tools.sh root|user DIR}"
15
+ : "${CONTAINER_USER:?CONTAINER_USER is not set}"
16
+
17
+ shopt -s nullglob
18
+ scripts=("${dir}"/*.sh)
19
+ if (( ${#scripts[@]} == 0 )); then
20
+ echo "No ${run_as} tools"
21
+ exit 0
22
+ fi
23
+
24
+ deb_arch="$(dpkg --print-architecture)"
25
+ for script in "${scripts[@]}"; do
26
+ name="$(basename "${script}" .sh)"
27
+ version="$(cat "${dir}/${name}.version")"
28
+ echo "==> tool ${name#*-} (${run_as}${version:+, version ${version}})"
29
+ if [[ "${run_as}" == root ]]; then
30
+ env HOME=/root VERSION="${version}" DEB_ARCH="${deb_arch}" bash "${script}"
31
+ else
32
+ home="/home/${CONTAINER_USER}"
33
+ runuser -u "${CONTAINER_USER}" -- env \
34
+ HOME="${home}" USER="${CONTAINER_USER}" LOGNAME="${CONTAINER_USER}" \
35
+ PATH="${home}/.local/bin:${PATH}" \
36
+ CONTAINER_USER="${CONTAINER_USER}" VERSION="${version}" DEB_ARCH="${deb_arch}" \
37
+ bash "${script}"
38
+ fi
39
+ done
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env bash
2
+ # Keep a copy of the Claude Code and Codex config the tools created in the container user's home.
3
+ # A bind mount over ~/.claude or ~/.codex hides what the image put there; the entrypoint copies these
4
+ # defaults into the mount at every start, skipping files that already exist.
5
+ set -euo pipefail
6
+
7
+ : "${CONTAINER_USER:?CONTAINER_USER is not set}"
8
+ defaults=/usr/local/share/agentbox/defaults
9
+ install -d "${defaults}"
10
+ for name in .claude .codex; do
11
+ if [[ -d "/home/${CONTAINER_USER}/${name}" ]]; then
12
+ cp -a "/home/${CONTAINER_USER}/${name}" "${defaults}/${name}"
13
+ fi
14
+ done
15
+ ls -A "${defaults}"
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env bash
2
+ # Point the Ubuntu apt sources at the mirror in APT_MIRROR ([build] apt_mirror in the config).
3
+ # Replaces archive.ubuntu.com (also country mirrors such as us.archive.ubuntu.com) and
4
+ # security.ubuntu.com in both formats:
5
+ # /etc/apt/sources.list Ubuntu 20.04 / 22.04
6
+ # /etc/apt/sources.list.d/ubuntu.sources Ubuntu 24.04+ (deb822)
7
+ # Empty APT_MIRROR: keep the base image's sources. The mirror stays configured in the image.
8
+ set -euo pipefail
9
+
10
+ mirror="${APT_MIRROR:-}"
11
+ if [[ -z "${mirror}" ]]; then
12
+ echo "apt mirror: default (from the base image)"
13
+ exit 0
14
+ fi
15
+ mirror="${mirror%/}"
16
+
17
+ files=()
18
+ for f in /etc/apt/sources.list /etc/apt/sources.list.d/ubuntu.sources; do
19
+ [[ -f "${f}" ]] && files+=("${f}")
20
+ done
21
+ pattern='https?://([a-z]{2}\.)?(archive|security)\.ubuntu\.com/ubuntu/?'
22
+ if (( ${#files[@]} == 0 )) || ! grep -qE "${pattern}" "${files[@]}"; then
23
+ echo "Error: apt_mirror is set, but no Ubuntu apt sources (archive/security.ubuntu.com) were found" \
24
+ "to replace; apt_mirror only works with Ubuntu base images" >&2
25
+ exit 1
26
+ fi
27
+
28
+ sed -i -E "s#${pattern}#${mirror}/#g" "${files[@]}"
29
+ echo "apt mirror: ${mirror}"
30
+ grep -hE '^[[:space:]]*(deb|URIs:)' "${files[@]}" | sed 's/^/ /'
@@ -0,0 +1,16 @@
1
+ #!/bin/sh
2
+ # TigerVNC session startup: XFCE desktop.
3
+ #
4
+ # vncserver is started from a login shell. The desktop runs on the system tools and its own
5
+ # D-Bus session: a session bus inherited from the starting shell cannot find the XFCE services,
6
+ # which leaves a black desktop. Terminals opened on the desktop run ~/.bashrc as usual.
7
+ unset SESSION_MANAGER
8
+ unset DBUS_SESSION_BUS_ADDRESS
9
+ export XDG_SESSION_TYPE=x11
10
+ export XDG_CURRENT_DESKTOP=XFCE
11
+
12
+ export PATH="${HOME}/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
13
+
14
+ [ -r "$HOME/.Xresources" ] && xrdb "$HOME/.Xresources"
15
+
16
+ exec /usr/bin/dbus-launch --exit-with-session /usr/bin/startxfce4
agentbox/cli.py ADDED
@@ -0,0 +1,543 @@
1
+ """Command-line interface: `agentcabin`."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import shlex
7
+ import shutil
8
+ import subprocess
9
+ import sys
10
+ from datetime import datetime
11
+ from pathlib import Path
12
+ from typing import Annotated
13
+
14
+ import typer
15
+
16
+ from . import __version__, podman
17
+ from .config import (
18
+ AUTO_PORT,
19
+ AUTO_PORT_START,
20
+ CONFIG_NAME,
21
+ DEFAULT_OS,
22
+ TESTED_OS,
23
+ TOOL_NAME,
24
+ Config,
25
+ load_config,
26
+ normalize_image,
27
+ render_template,
28
+ save_ports,
29
+ )
30
+ from .errors import AgentboxError
31
+
32
+ tool = TOOL_NAME
33
+
34
+
35
+ # ---------------------------------------------------------------------------
36
+ # Output helpers
37
+ # ---------------------------------------------------------------------------
38
+
39
+ def info(msg: str) -> None:
40
+ typer.secho("[INFO] ", fg=typer.colors.GREEN, nl=False, err=True)
41
+ typer.echo(msg, err=True)
42
+
43
+
44
+ def warn(msg: str) -> None:
45
+ typer.secho("[WARN] ", fg=typer.colors.YELLOW, nl=False, err=True)
46
+ typer.echo(msg, err=True)
47
+
48
+
49
+ def stdin_is_tty() -> bool:
50
+ """Whether questions can be asked (separate function so that tests can replace it)."""
51
+ return sys.stdin.isatty()
52
+
53
+
54
+ def error(msg: str) -> None:
55
+ typer.secho("[ERROR] ", fg=typer.colors.RED, nl=False, err=True)
56
+ typer.echo(msg, err=True)
57
+
58
+
59
+ def _print_table(rows: list[tuple[str, ...]]) -> None:
60
+ """Print rows as left-aligned columns; the first row is the header."""
61
+ widths = [max(len(r[i]) for r in rows) for i in range(len(rows[0]))]
62
+ for r in rows:
63
+ typer.echo(" ".join(f"{v:<{w}}" for v, w in zip(r, widths, strict=True)).rstrip())
64
+
65
+
66
+ def _in_git_repo(directory: Path) -> bool:
67
+ if not shutil.which("git"):
68
+ return False
69
+ r = subprocess.run(
70
+ ["git", "-C", str(directory), "rev-parse", "--is-inside-work-tree"],
71
+ capture_output=True, text=True, check=False,
72
+ )
73
+ return r.stdout.strip() == "true"
74
+
75
+
76
+ def _write_config(file: Path, content: str, force: bool) -> Path:
77
+ path = file.expanduser().absolute()
78
+ if path.exists() and not force:
79
+ raise AgentboxError(f"{path} already exists (use --force to overwrite)")
80
+ path.parent.mkdir(parents=True, exist_ok=True)
81
+ # The file holds passwords: readable by the current user only
82
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
83
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
84
+ f.write(content)
85
+ os.chmod(path, 0o600)
86
+ return path
87
+
88
+
89
+ # ---------------------------------------------------------------------------
90
+ # App
91
+ # ---------------------------------------------------------------------------
92
+
93
+ app = typer.Typer(
94
+ help="Build and run containers for AI coding agents (SSH, VNC desktop, Firefox, Claude Code, Codex) "
95
+ "with rootless podman.",
96
+ no_args_is_help=True,
97
+ add_completion=True,
98
+ context_settings={"help_option_names": ["-h", "--help"]},
99
+ )
100
+
101
+ config_opt = typer.Option(None, "-c", "--config", help=f"Config file (default: ./{CONFIG_NAME})", show_default=False)
102
+
103
+
104
+ def version_callback(value: bool) -> None:
105
+ if value:
106
+ typer.echo(f"{tool} {__version__}")
107
+ raise typer.Exit()
108
+
109
+
110
+ @app.callback()
111
+ def main_callback(
112
+ ctx: typer.Context,
113
+ config: Path | None = config_opt,
114
+ version: bool = typer.Option(
115
+ False, "--version", "-v", callback=version_callback, is_eager=True, help=f"Show the {tool} version"
116
+ ),
117
+ ) -> None:
118
+ ctx.obj = {"config": config}
119
+
120
+
121
+ def load(ctx: typer.Context, config: Path | None) -> Config:
122
+ """Load the config given by the command's -c, the global -c, or the default file."""
123
+ cfg = load_config(config or (ctx.obj or {}).get("config"))
124
+ for w in cfg.warnings:
125
+ warn(w)
126
+ return cfg
127
+
128
+
129
+ def prepare(ctx: typer.Context, config: Path | None) -> Config:
130
+ podman.check_dependencies()
131
+ return load(ctx, config)
132
+
133
+
134
+ def do_build(cfg: Config, no_cache: bool = False) -> None:
135
+ info(f"Checking base image {cfg.base_image}")
136
+ podman.check_base_image(cfg)
137
+ info(f"Building image {cfg.image} (the first build downloads a lot, please be patient)")
138
+ with podman.build_context(cfg) as context:
139
+ podman.run(podman.build_args(cfg, context, no_cache))
140
+ info(f"Image built: {cfg.image}")
141
+
142
+
143
+ # ---- list, init ----
144
+
145
+ @app.command("list")
146
+ def list_cmd() -> None:
147
+ """List tested base OS images (any apt-based image can be used)."""
148
+ rows = [("OS", "IMAGE", "")]
149
+ for o in TESTED_OS:
150
+ rows.append((o, normalize_image(o), "default" if o == DEFAULT_OS else ""))
151
+ _print_table(rows)
152
+
153
+
154
+ @app.command()
155
+ def init(
156
+ file: Annotated[Path, typer.Argument(help="Config file to create")] = Path(CONFIG_NAME),
157
+ os_image: str = typer.Option(
158
+ DEFAULT_OS, "--os", help=f"Base OS image, name:tag (tested: see `{tool} list`)"
159
+ ),
160
+ force: Annotated[bool, typer.Option("--force", "-f", help="Overwrite an existing file")] = False,
161
+ ) -> None:
162
+ """Create a config file from the template."""
163
+ path = _write_config(file, render_template(os_image), force)
164
+ info(f"Created {path} (os {os_image})")
165
+ if os_image not in TESTED_OS:
166
+ warn(f"os {os_image!r} has not been tested (tested: {', '.join(TESTED_OS)})")
167
+ if _in_git_repo(path.parent):
168
+ warn(f"{path.parent} is inside a git repository; add {path.name} to .gitignore (it contains passwords)")
169
+ cfg_arg = "" if path == Path.cwd() / CONFIG_NAME else f" -c {shlex.quote(str(file))}"
170
+ typer.echo(f"\nNext: edit {file}, then run: {tool}{cfg_arg} up")
171
+
172
+
173
+ # ---- build, up, down, ... ----
174
+
175
+ @app.command()
176
+ def build(
177
+ ctx: typer.Context,
178
+ config: Path | None = config_opt,
179
+ no_cache: Annotated[bool, typer.Option("--no-cache", help="Do not use the build cache")] = False,
180
+ ) -> None:
181
+ """Build the image."""
182
+ do_build(prepare(ctx, config), no_cache)
183
+
184
+
185
+ def confirm_warnings(cfg: Config, yes: bool) -> None:
186
+ """Ask before creating a container when the config produced warnings."""
187
+ if not cfg.warnings or yes:
188
+ return
189
+ if not stdin_is_tty():
190
+ raise AgentboxError(
191
+ f"{len(cfg.warnings)} warning(s) above; not continuing without confirmation "
192
+ "(stdin is not a terminal). Fix them, or pass --yes to continue anyway."
193
+ )
194
+ if not typer.confirm(f"{len(cfg.warnings)} warning(s) above. Continue creating the container?", default=False):
195
+ info("Aborted, nothing was changed")
196
+ raise typer.Exit(1)
197
+
198
+
199
+ def kept_paths(cfg: Config) -> list[str]:
200
+ """Container paths of the mounted directories (their files survive a recreate)."""
201
+ return [f"~{m.container[len(cfg.home):]}" if m.container.startswith(f"{cfg.home}/") else m.container
202
+ for m in cfg.mounts]
203
+
204
+
205
+ def confirm_recreate(cfg: Config, own: podman.ContainerInfo, yes: bool) -> None:
206
+ """Ask before removing an existing container: what is not in a mount is lost."""
207
+ if yes:
208
+ return
209
+ kept = kept_paths(cfg)
210
+ warn(
211
+ f"Container {cfg.name} already exists ({own.state}). It will be removed and created again "
212
+ f"from {cfg.path.name}:\n"
213
+ " - changes made in the container outside the mounted directories are lost\n"
214
+ " (e.g. packages installed with apt, edits to ~/.bashrc or ~/.config)\n"
215
+ " - programs running in it and SSH/VNC sessions are stopped\n"
216
+ + (f" Kept: files in the mounted directories ({', '.join(kept)})\n" if kept
217
+ else " No directories are mounted, so nothing in the container is kept\n")
218
+ + f" To only restart it and keep its data, run `{tool} restart` instead."
219
+ )
220
+ if not stdin_is_tty():
221
+ raise AgentboxError(
222
+ f"Container {cfg.name} already exists; not recreating it without confirmation "
223
+ "(stdin is not a terminal). Pass --yes to recreate it."
224
+ )
225
+ if not typer.confirm(f"Remove and recreate container {cfg.name}?", default=False):
226
+ info("Aborted, nothing was changed")
227
+ raise typer.Exit(1)
228
+
229
+
230
+ def print_access(cfg: Config, what: str) -> None:
231
+ host = "localhost" if cfg.bind_address in ("0.0.0.0", "::") else cfg.bind_address
232
+ cfg_arg = "" if cfg.path == Path.cwd() / CONFIG_NAME else f" -c {shlex.quote(str(cfg.path))}"
233
+ typer.echo(f"\nContainer {cfg.name} {what} ({cfg.describe})")
234
+ typer.echo(f" SSH : ssh -p {cfg.ssh_port} {cfg.user}@{host}")
235
+ typer.echo(f" VNC : {host}:{cfg.vnc_port}")
236
+ typer.echo(f" Shell: {tool}{cfg_arg} shell")
237
+ if cfg.gpus:
238
+ typer.echo(f" GPUs : {', '.join(cfg.gpus)}")
239
+ if cfg.rdma:
240
+ typer.echo(f" RDMA : {', '.join(cfg.rdma)}")
241
+ if cfg.host_network:
242
+ typer.echo(" Network: host (all NICs of this machine are available in the container)")
243
+ else:
244
+ for p in cfg.extra_ports:
245
+ typer.echo(f" Port : {p.host} -> {p.container}/{p.protocol}")
246
+
247
+
248
+ def do_up(cfg: Config, yes: bool) -> None:
249
+ own = podman.get_container(cfg.name)
250
+ if own:
251
+ podman.check_owner(cfg, own)
252
+ podman.resolve_host_settings(cfg) # interface name -> IP, RDMA device nodes
253
+ if cfg.bind_name:
254
+ info(f"ports.bind_address {cfg.bind_name} = {cfg.bind_address}")
255
+ podman.check_host_display(cfg, own)
256
+ auto_ports = podman.pick_auto_ports(cfg, own) # ports.ssh / ports.vnc = 0
257
+ for key, port in auto_ports.items():
258
+ setattr(cfg, f"{key}_port", port)
259
+ podman.check_ports_free(cfg, own)
260
+ podman.check_limits(cfg)
261
+ podman.check_gpus(cfg)
262
+ if own:
263
+ confirm_recreate(cfg, own, yes) # before a possibly long image build
264
+ if auto_ports:
265
+ save_ports(cfg.path, auto_ports)
266
+ info("Picked free host port(s) " + ", ".join(f"{k} {p}" for k, p in auto_ports.items())
267
+ + f"; saved in {cfg.path.name}")
268
+
269
+ state = podman.image_state(cfg)
270
+ if state == "missing":
271
+ info(f"Image {cfg.image} not found, building it")
272
+ do_build(cfg)
273
+ elif state == "outdated":
274
+ info(f"Image {cfg.image} is out of date (build files or settings changed), rebuilding it")
275
+ do_build(cfg)
276
+ podman.check_image_matches(cfg)
277
+
278
+ for m in cfg.mounts:
279
+ if m.create and not m.host.exists():
280
+ info(f"Creating directory {m.host}")
281
+ m.host.mkdir(parents=True, mode=0o700)
282
+
283
+ if own:
284
+ info(f"Recreating container {cfg.name}")
285
+ podman.remove_container(cfg.name)
286
+ podman.run(podman.run_args(cfg), env=podman.container_env(cfg))
287
+ print_access(cfg, "is up")
288
+
289
+
290
+ @app.command()
291
+ def up(
292
+ ctx: typer.Context,
293
+ config: Path | None = config_opt,
294
+ yes: Annotated[bool, typer.Option(
295
+ "--yes", "-y", help="Do not ask for confirmation (warnings, recreating an existing container)")] = False,
296
+ ) -> None:
297
+ """Start the container (builds the image if missing or outdated; asks before recreating an existing one)."""
298
+ cfg = prepare(ctx, config)
299
+ confirm_warnings(cfg, yes)
300
+ do_up(cfg, yes)
301
+
302
+
303
+ @app.command()
304
+ def down(ctx: typer.Context, config: Path | None = config_opt) -> None:
305
+ """Stop and remove the container."""
306
+ cfg = prepare(ctx, config)
307
+ own = podman.get_container(cfg.name)
308
+ if not own:
309
+ info(f"Container {cfg.name} does not exist")
310
+ return
311
+ podman.check_owner(cfg, own)
312
+ podman.remove_container(cfg.name)
313
+ info(f"Container {cfg.name} removed")
314
+
315
+
316
+ @app.command()
317
+ def restart(ctx: typer.Context, config: Path | None = config_opt) -> None:
318
+ """Stop and start the container again, keeping its data (config changes need `up`)."""
319
+ cfg = prepare(ctx, config)
320
+ own = podman.get_container(cfg.name)
321
+ if not own:
322
+ raise AgentboxError(f"Container {cfg.name} does not exist, please run: {tool} up")
323
+ podman.check_owner(cfg, own)
324
+ info(f"Restarting container {cfg.name} ({own.state}); its data is kept")
325
+ podman.run(["podman", "restart", cfg.name])
326
+ info(f"The container keeps the settings it was created with; run `{tool} up` to apply config changes")
327
+ print_access(cfg, "restarted")
328
+
329
+
330
+ def _running_container(cfg: Config) -> podman.ContainerInfo:
331
+ own = podman.get_container(cfg.name)
332
+ if not own:
333
+ raise AgentboxError(f"Container {cfg.name} does not exist, please run: {tool} up")
334
+ if own.state != "running":
335
+ raise AgentboxError(f"Container {cfg.name} is {own.state}, please run: {tool} up")
336
+ return own
337
+
338
+
339
+ @app.command()
340
+ def shell(ctx: typer.Context, config: Path | None = config_opt) -> None:
341
+ """Open a login shell in the container as the container user."""
342
+ cfg = prepare(ctx, config)
343
+ _running_container(cfg)
344
+ podman.exec_(["podman", "exec", "-it", *podman.terminal_env(), "-u", cfg.user, "-w", cfg.home,
345
+ cfg.name, "bash", "-l"])
346
+
347
+
348
+ @app.command()
349
+ def cp(
350
+ ctx: typer.Context,
351
+ src: Annotated[str, typer.Argument(help="Source; a path starting with ':' is in the container")],
352
+ dest: Annotated[str, typer.Argument(help="Destination; a path starting with ':' is in the container")],
353
+ config: Path | None = config_opt,
354
+ ) -> None:
355
+ """Copy files or directories between this machine and the container (:PATH is in the container).
356
+
357
+ Container paths that are relative, or start with ~, are relative to the container user's home.
358
+ Files copied into the home directory are given to the container user.
359
+
360
+ \b
361
+ agentcabin cp notes.txt :~/Projects/ # into the container
362
+ agentcabin cp :~/Projects/out.log . # out of the container
363
+ """
364
+ if src.startswith(":") == dest.startswith(":"):
365
+ raise AgentboxError("Exactly one of SRC and DEST must be a container path (start it with ':')")
366
+ cfg = prepare(ctx, config)
367
+ _running_container(cfg)
368
+ if src.startswith(":"):
369
+ local = Path(dest).expanduser()
370
+ podman.run(["podman", "cp", f"{cfg.name}:{podman.container_path(cfg, src[1:])}", str(local)])
371
+ return
372
+ local = Path(src).expanduser()
373
+ if not local.exists():
374
+ raise AgentboxError(f"{local} does not exist")
375
+ target = podman.container_path(cfg, dest[1:])
376
+ is_dir = subprocess.run(["podman", "exec", cfg.name, "test", "-d", target], check=False).returncode == 0
377
+ podman.run(["podman", "cp", str(local), f"{cfg.name}:{target}"])
378
+ copied = f"{target.rstrip('/')}/{local.name}" if is_dir else target
379
+ if copied.startswith(f"{cfg.home}/"): # podman cp creates the files as root
380
+ podman.run(["podman", "exec", "-u", "root", cfg.name, "chown", "-R", f"{cfg.user}:", copied])
381
+
382
+
383
+ @app.command()
384
+ def status(ctx: typer.Context, config: Path | None = config_opt) -> None:
385
+ """Show the image and container status."""
386
+ cfg = prepare(ctx, config)
387
+ image_note = {
388
+ "missing": "not built",
389
+ "outdated": f"out of date, `{tool} up` or `{tool} build` will rebuild it",
390
+ "current": "up to date",
391
+ }[podman.image_state(cfg)]
392
+ typer.echo(f"{cfg.image}: {image_note}")
393
+ own = podman.get_container(cfg.name)
394
+ if not own:
395
+ typer.echo(f"{cfg.name}: not created")
396
+ return
397
+ typer.echo(f"{own.name}: {own.status}")
398
+ limits = podman.container_limits(own.name)
399
+ typer.echo(" Limits: " + (", ".join(f"{k} {v}" for k, v in limits.items()) or "none"))
400
+ gpus = [d.removeprefix("nvidia.com/gpu=") for d in podman.container_gpus(own.name)]
401
+ typer.echo(" GPUs : " + (", ".join(gpus) or "none"))
402
+ rdma = [d.removeprefix("/dev/infiniband/") for d in podman.container_rdma(own.name)]
403
+ typer.echo(" RDMA : " + (", ".join(rdma) or "none"))
404
+ typer.echo(f" Network: {podman.container_network(own.name) or 'unknown'}")
405
+ if own.ports:
406
+ typer.echo(" Ports:")
407
+ for p in own.ports:
408
+ typer.echo(f" {p.host} -> {p.container}/{p.protocol}")
409
+ mounts = podman.container_mounts(own.name)
410
+ if mounts:
411
+ typer.echo(" Mounts:")
412
+ width = max(len(m.source) for m in mounts)
413
+ for m in mounts:
414
+ typer.echo(f" {m.source:<{width}} -> {m.destination} ({m.mode})")
415
+
416
+
417
+ @app.command()
418
+ def logs(
419
+ ctx: typer.Context,
420
+ config: Path | None = config_opt,
421
+ follow: Annotated[bool, typer.Option("--follow", "-f", help="Follow log output")] = False,
422
+ ) -> None:
423
+ """Show the container logs."""
424
+ cfg = prepare(ctx, config)
425
+ cmd = ["podman", "logs"]
426
+ if follow:
427
+ cmd.append("-f")
428
+ podman.exec_([*cmd, cfg.name])
429
+
430
+
431
+ @app.command()
432
+ def commit(
433
+ ctx: typer.Context,
434
+ image: Annotated[str | None, typer.Argument(
435
+ help="Image name[:tag]; default localhost/<container>:snapshot-<date>-<time>")] = None,
436
+ config: Path | None = config_opt,
437
+ output: Annotated[Path | None, typer.Option(
438
+ "--output", "-o", help="Also save the image to this tar file (load it with `podman load -i FILE`)")] = None,
439
+ force: Annotated[bool, typer.Option("--force", help="Overwrite the --output file if it exists")] = False,
440
+ ) -> None:
441
+ """Save the container, with the changes made in it, as an image (optionally to a tar file)."""
442
+ cfg = prepare(ctx, config)
443
+ own = podman.get_container(cfg.name)
444
+ if not own:
445
+ raise AgentboxError(f"Container {cfg.name} does not exist, please run: {tool} up")
446
+ podman.check_owner(cfg, own)
447
+ if output:
448
+ output = output.expanduser().resolve()
449
+ if output.exists() and not force:
450
+ raise AgentboxError(f"{output} already exists; use --force to overwrite it")
451
+ if not output.parent.is_dir():
452
+ raise AgentboxError(f"Directory does not exist: {output.parent}")
453
+ image = image or f"localhost/{cfg.name.lower()}:snapshot-{datetime.now():%Y%m%d-%H%M%S}"
454
+
455
+ kept = kept_paths(cfg)
456
+ if kept:
457
+ warn(f"Mounted directories are not part of the image: {', '.join(kept)}")
458
+ warn("The image contains the container's passwords (user and VNC) and SSH host keys; "
459
+ "only share it with people who may know them")
460
+ info(f"Committing container {cfg.name} ({own.state}) to image {image}")
461
+ podman.run(podman.commit_args(cfg.name, image))
462
+ info(f"Image created: {image}")
463
+ if output:
464
+ if output.exists():
465
+ output.unlink()
466
+ info(f"Saving {image} to {output} (this can take a while)")
467
+ podman.run(["podman", "save", "-o", str(output), image])
468
+ info(f"Saved: {output}; load it on another machine with: podman load -i {output.name}")
469
+
470
+
471
+ @app.command("config")
472
+ def config_cmd(ctx: typer.Context, config: Path | None = config_opt) -> None:
473
+ """Show the resolved config and the podman commands (passwords masked)."""
474
+ cfg = load(ctx, config)
475
+ rows = [("config", str(cfg.path)), ("os", cfg.base_image), ("packages", f"{len(cfg.packages)}")]
476
+ rows += [("apt repo", f"{r.name}: {r.url} {r.suite or '<codename>'} {' '.join(r.components)}".rstrip()
477
+ + (" (key fingerprint checked)" if r.key_fingerprint else ""))
478
+ for r in cfg.apt_repos] or [("apt repos", "-")]
479
+ rows += [("tool", f"{t.name}{' ' + t.version if t.version else ''} ({t.run_as})") for t in cfg.tools] \
480
+ or [("tools", "-")]
481
+ rows += [
482
+ ("apt mirror", cfg.apt_mirror or "default"),
483
+ ("image", cfg.image),
484
+ ("container", cfg.name),
485
+ ("user", f"{cfg.user} ({os.getuid()}:{os.getgid()})"),
486
+ ]
487
+ rows.append(("ports", ", ".join(
488
+ f"{key} " + (f"auto (first free from {AUTO_PORT_START[key]}, picked by up)" if port == AUTO_PORT else str(port))
489
+ for key, port in (("ssh", cfg.ssh_port), ("vnc", cfg.vnc_port)))))
490
+ rows.append(("limits", " ".join(podman.limit_args(cfg)) or "none"))
491
+ rows.append(("gpus", ", ".join(cfg.gpus) or "none"))
492
+ rows.append(("rdma", ", ".join(cfg.rdma) or "none"))
493
+ rows.append(("network", podman.pasta_network(cfg)))
494
+ if cfg.bind_name:
495
+ try:
496
+ podman.resolve_bind_address(cfg)
497
+ rows.append(("bind", f"{cfg.bind_name} = {cfg.bind_address}"))
498
+ except AgentboxError as e:
499
+ warn(str(e))
500
+ if cfg.rdma:
501
+ try:
502
+ cfg.rdma_paths = podman.rdma_devices(cfg.rdma)
503
+ rows.append(("rdma nodes", " ".join(cfg.rdma_paths)))
504
+ except AgentboxError as e:
505
+ warn(str(e))
506
+ width = max(len(k) for k, _ in rows)
507
+ for k, v in rows:
508
+ typer.echo(f"{k:<{width}} {v}")
509
+
510
+ typer.echo("\nenvironment:")
511
+ for k, v in podman.container_env(cfg).items():
512
+ typer.echo(f" {k}={'******' if k in podman.SECRET_ENV else v}")
513
+ typer.echo("\nbuild (CONTEXT: temporary copy of the package's assets plus tools/):")
514
+ typer.echo(" " + shlex.join(podman.build_args(cfg, Path("CONTEXT"))))
515
+ typer.echo("\nrun:")
516
+ typer.echo(" " + shlex.join(podman.run_args(cfg)))
517
+
518
+
519
+ @app.command()
520
+ def ps() -> None:
521
+ """List all containers created by agentbox."""
522
+ podman.check_dependencies()
523
+ containers = podman.list_containers()
524
+ if not containers:
525
+ typer.echo(f"No {tool} containers")
526
+ return
527
+ rows = [("NAME", "OS", "STATUS", "PORTS", "CONFIG")]
528
+ for c in containers:
529
+ ports = ",".join(f"{p.host}->{p.container}/{p.protocol}" for p in c.ports)
530
+ rows.append((c.name, c.labels.get(podman.LABEL_OS, ""), c.status, ports, c.config_path))
531
+ _print_table(rows)
532
+
533
+
534
+ def main() -> None:
535
+ try:
536
+ app()
537
+ except AgentboxError as e:
538
+ error(str(e))
539
+ sys.exit(1)
540
+
541
+
542
+ if __name__ == "__main__":
543
+ main()