casperbox 0.2.1__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.
- casperbox/__init__.py +8 -0
- casperbox/assets/common/packages/software.list +70 -0
- casperbox/assets/common/rootfs/etc/profile.d/casper.sh +48 -0
- casperbox/assets/common/rootfs/etc/ssh/sshd_config.d/casper.conf +8 -0
- casperbox/assets/common/rootfs/usr/local/bin/entrypoint.sh +82 -0
- casperbox/assets/common/scripts/config_mlib_devel.sh +54 -0
- casperbox/assets/common/scripts/create_user.sh +39 -0
- casperbox/assets/common/scripts/install_base.sh +27 -0
- casperbox/assets/common/scripts/install_miniconda.sh +27 -0
- casperbox/assets/common/scripts/install_scilab.sh +45 -0
- casperbox/assets/common/scripts/setup_conda_env.sh +16 -0
- casperbox/assets/common/vnc/xstartup +9 -0
- casperbox/assets/versions/m2021a/Dockerfile +85 -0
- casperbox/assets/versions/m2021a/toolflow.toml +32 -0
- casperbox/cli.py +318 -0
- casperbox/config.py +362 -0
- casperbox/errors.py +2 -0
- casperbox/podman.py +262 -0
- casperbox/templates/casperbox.toml +46 -0
- casperbox/versions.py +85 -0
- casperbox-0.2.1.dist-info/METADATA +190 -0
- casperbox-0.2.1.dist-info/RECORD +24 -0
- casperbox-0.2.1.dist-info/WHEEL +4 -0
- casperbox-0.2.1.dist-info/entry_points.txt +2 -0
casperbox/cli.py
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
"""casperbox command-line interface."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import shlex
|
|
7
|
+
import shutil
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Annotated
|
|
12
|
+
|
|
13
|
+
import typer
|
|
14
|
+
|
|
15
|
+
from . import __version__, podman
|
|
16
|
+
from .config import DEFAULT_CONFIG_NAME, Config, load_config, render_template
|
|
17
|
+
from .errors import CasperError
|
|
18
|
+
from .versions import list_versions, load_toolflow
|
|
19
|
+
|
|
20
|
+
app = typer.Typer(
|
|
21
|
+
help="Build and run CASPER toolflow development containers with rootless podman.",
|
|
22
|
+
no_args_is_help=True,
|
|
23
|
+
add_completion=True,
|
|
24
|
+
context_settings={"help_option_names": ["-h", "--help"]},
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
ConfigOpt = Annotated[
|
|
28
|
+
Path | None,
|
|
29
|
+
typer.Option("-c", "--config", help=f"Config file (default: ./{DEFAULT_CONFIG_NAME})", show_default=False),
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ---------------------------------------------------------------------------
|
|
34
|
+
# Output helpers
|
|
35
|
+
# ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
def info(msg: str) -> None:
|
|
38
|
+
typer.secho("[INFO] ", fg=typer.colors.GREEN, nl=False, err=True)
|
|
39
|
+
typer.echo(msg, err=True)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def warn(msg: str) -> None:
|
|
43
|
+
typer.secho("[WARN] ", fg=typer.colors.YELLOW, nl=False, err=True)
|
|
44
|
+
typer.echo(msg, err=True)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _print_table(rows: list[tuple[str, ...]]) -> None:
|
|
48
|
+
"""Print rows as left-aligned columns; the first row is the header."""
|
|
49
|
+
widths = [max(len(r[i]) for r in rows) for i in range(len(rows[0]))]
|
|
50
|
+
for r in rows:
|
|
51
|
+
typer.echo(" ".join(f"{v:<{w}}" for v, w in zip(r, widths, strict=True)).rstrip())
|
|
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
|
+
# ---------------------------------------------------------------------------
|
|
60
|
+
# Global options
|
|
61
|
+
# ---------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
def _version_callback(value: bool) -> None:
|
|
64
|
+
if value:
|
|
65
|
+
typer.echo(f"casperbox {__version__}")
|
|
66
|
+
raise typer.Exit()
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@app.callback()
|
|
70
|
+
def main_callback(
|
|
71
|
+
ctx: typer.Context,
|
|
72
|
+
config: ConfigOpt = None,
|
|
73
|
+
version: Annotated[
|
|
74
|
+
bool, typer.Option("--version", "-v", callback=_version_callback, is_eager=True, help="Show the casperbox version")
|
|
75
|
+
] = False,
|
|
76
|
+
) -> None:
|
|
77
|
+
ctx.obj = {"config": config}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _load(ctx: typer.Context, config: Path | None) -> Config:
|
|
81
|
+
"""Load the config given by the command's -c, the global -c, or ./casperbox.toml."""
|
|
82
|
+
path = config or (ctx.obj or {}).get("config")
|
|
83
|
+
cfg = load_config(path)
|
|
84
|
+
for w in cfg.warnings:
|
|
85
|
+
warn(w)
|
|
86
|
+
return cfg
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _prepare(ctx: typer.Context, config: Path | None) -> Config:
|
|
90
|
+
podman.check_dependencies()
|
|
91
|
+
return _load(ctx, config)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# ---------------------------------------------------------------------------
|
|
95
|
+
# Commands
|
|
96
|
+
# ---------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
@app.command("list")
|
|
99
|
+
def list_cmd() -> None:
|
|
100
|
+
"""List supported toolflow versions."""
|
|
101
|
+
rows = [("TOOLFLOW", "VIVADO", "SCILAB", "PYTHON")]
|
|
102
|
+
for v in list_versions():
|
|
103
|
+
tf = load_toolflow(v)
|
|
104
|
+
rows.append((v, tf.vivado_version, tf.params["SCILAB_VERSION"], tf.params["PYTHON_VERSION"]))
|
|
105
|
+
_print_table(rows)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@app.command()
|
|
109
|
+
def init(
|
|
110
|
+
file: Annotated[Path, typer.Argument(help="Config file to create")] = Path(DEFAULT_CONFIG_NAME),
|
|
111
|
+
toolflow: Annotated[
|
|
112
|
+
str | None, typer.Option("--toolflow", "-t", help="Toolflow version (see `casperbox list`)")
|
|
113
|
+
] = None,
|
|
114
|
+
force: Annotated[bool, typer.Option("--force", "-f", help="Overwrite an existing file")] = False,
|
|
115
|
+
) -> None:
|
|
116
|
+
"""Create a config file from the template."""
|
|
117
|
+
versions = list_versions()
|
|
118
|
+
if toolflow is None:
|
|
119
|
+
if len(versions) != 1:
|
|
120
|
+
raise CasperError(f"Multiple toolflow versions available, choose one with --toolflow ({', '.join(versions)})")
|
|
121
|
+
toolflow = versions[0]
|
|
122
|
+
tf = load_toolflow(toolflow)
|
|
123
|
+
|
|
124
|
+
path = file.expanduser().absolute()
|
|
125
|
+
if path.exists() and not force:
|
|
126
|
+
raise CasperError(f"{path} already exists (use --force to overwrite)")
|
|
127
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
128
|
+
# The file holds passwords: readable by the current user only
|
|
129
|
+
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
130
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
131
|
+
f.write(render_template(tf))
|
|
132
|
+
os.chmod(path, 0o600)
|
|
133
|
+
info(f"Created {path} (toolflow {tf.version})")
|
|
134
|
+
|
|
135
|
+
if _in_git_repo(path.parent):
|
|
136
|
+
warn(f"{path.parent} is inside a git repository; add {path.name} to .gitignore (it contains passwords)")
|
|
137
|
+
|
|
138
|
+
cfg_arg = "" if path == Path.cwd() / DEFAULT_CONFIG_NAME else f" -c {shlex.quote(str(file))}"
|
|
139
|
+
typer.echo(f"\nNext: edit {file}, then run: casperbox{cfg_arg} up")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _in_git_repo(directory: Path) -> bool:
|
|
143
|
+
if not shutil.which("git"):
|
|
144
|
+
return False
|
|
145
|
+
r = subprocess.run(
|
|
146
|
+
["git", "-C", str(directory), "rev-parse", "--is-inside-work-tree"],
|
|
147
|
+
capture_output=True, text=True, check=False,
|
|
148
|
+
)
|
|
149
|
+
return r.stdout.strip() == "true"
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@app.command()
|
|
153
|
+
def build(
|
|
154
|
+
ctx: typer.Context,
|
|
155
|
+
config: ConfigOpt = None,
|
|
156
|
+
no_cache: Annotated[bool, typer.Option("--no-cache", help="Do not use the build cache")] = False,
|
|
157
|
+
) -> None:
|
|
158
|
+
"""Build the image for the config's toolflow version."""
|
|
159
|
+
cfg = _prepare(ctx, config)
|
|
160
|
+
_build(cfg, no_cache)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _build(cfg: Config, no_cache: bool = False) -> None:
|
|
164
|
+
info(f"Building image {cfg.toolflow.image} (the first build downloads a lot, please be patient)")
|
|
165
|
+
podman.run(podman.build_args(cfg, no_cache))
|
|
166
|
+
info(f"Image built: {cfg.toolflow.image}")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@app.command()
|
|
170
|
+
def up(ctx: typer.Context, config: ConfigOpt = None) -> None:
|
|
171
|
+
"""Start the container (builds the image if missing, recreates an existing container)."""
|
|
172
|
+
cfg = _prepare(ctx, config)
|
|
173
|
+
|
|
174
|
+
own = podman.get_container(cfg.name)
|
|
175
|
+
if own:
|
|
176
|
+
podman.check_owner(cfg, own)
|
|
177
|
+
podman.check_ports_free(cfg, own)
|
|
178
|
+
|
|
179
|
+
if not podman.image_exists(cfg.toolflow.image):
|
|
180
|
+
info(f"Image {cfg.toolflow.image} not found, building it")
|
|
181
|
+
_build(cfg)
|
|
182
|
+
podman.check_image_matches(cfg)
|
|
183
|
+
|
|
184
|
+
if not cfg.work_dir.exists():
|
|
185
|
+
info(f"Creating work directory {cfg.work_dir}")
|
|
186
|
+
cfg.work_dir.mkdir(parents=True)
|
|
187
|
+
|
|
188
|
+
if own:
|
|
189
|
+
info(f"Recreating container {cfg.name}")
|
|
190
|
+
podman.remove_container(cfg.name)
|
|
191
|
+
podman.run(podman.run_args(cfg), env=podman.container_env(cfg))
|
|
192
|
+
_print_access(cfg)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _print_access(cfg: Config) -> None:
|
|
196
|
+
host = "localhost" if cfg.bind_address in ("0.0.0.0", "::") else cfg.bind_address
|
|
197
|
+
cfg_arg = "" if cfg.path == Path.cwd() / DEFAULT_CONFIG_NAME else f" -c {shlex.quote(str(cfg.path))}"
|
|
198
|
+
typer.echo(f"\nContainer {cfg.name} is up (toolflow {cfg.version})")
|
|
199
|
+
typer.echo(f" SSH : ssh -p {cfg.ssh_port} {cfg.user}@{host}")
|
|
200
|
+
typer.echo(f" VNC : {host}:{cfg.vnc_port}")
|
|
201
|
+
typer.echo(f" Shell: casperbox{cfg_arg} shell")
|
|
202
|
+
for p in cfg.extra_ports:
|
|
203
|
+
typer.echo(f" Port : {p.host} -> {p.container}/{p.protocol}")
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@app.command()
|
|
207
|
+
def down(ctx: typer.Context, config: ConfigOpt = None) -> None:
|
|
208
|
+
"""Stop and remove the container."""
|
|
209
|
+
cfg = _prepare(ctx, config)
|
|
210
|
+
own = podman.get_container(cfg.name)
|
|
211
|
+
if not own:
|
|
212
|
+
info(f"Container {cfg.name} does not exist")
|
|
213
|
+
return
|
|
214
|
+
podman.check_owner(cfg, own)
|
|
215
|
+
podman.remove_container(cfg.name)
|
|
216
|
+
info(f"Container {cfg.name} removed")
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
@app.command()
|
|
220
|
+
def restart(ctx: typer.Context, config: ConfigOpt = None) -> None:
|
|
221
|
+
"""Recreate the container (re-reads the config)."""
|
|
222
|
+
down(ctx, config)
|
|
223
|
+
up(ctx, config)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@app.command()
|
|
227
|
+
def shell(ctx: typer.Context, config: ConfigOpt = None) -> None:
|
|
228
|
+
"""Open a login shell in the container as the container user."""
|
|
229
|
+
cfg = _prepare(ctx, config)
|
|
230
|
+
own = podman.get_container(cfg.name)
|
|
231
|
+
if not own:
|
|
232
|
+
raise CasperError(f"Container {cfg.name} does not exist, please run: casperbox up")
|
|
233
|
+
if own.state != "running":
|
|
234
|
+
raise CasperError(f"Container {cfg.name} is {own.state}, please run: casperbox up")
|
|
235
|
+
podman.exec_(["podman", "exec", "-it", "-u", cfg.user, "-w", cfg.home, cfg.name, "bash", "-l"])
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@app.command()
|
|
239
|
+
def status(ctx: typer.Context, config: ConfigOpt = None) -> None:
|
|
240
|
+
"""Show the container status."""
|
|
241
|
+
cfg = _prepare(ctx, config)
|
|
242
|
+
own = podman.get_container(cfg.name)
|
|
243
|
+
if not own:
|
|
244
|
+
typer.echo(f"{cfg.name}: not created")
|
|
245
|
+
return
|
|
246
|
+
typer.echo(f"{own.name}: {own.status}")
|
|
247
|
+
for p in own.ports:
|
|
248
|
+
typer.echo(f" {p.host} -> {p.container}/{p.protocol}")
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
@app.command()
|
|
252
|
+
def logs(
|
|
253
|
+
ctx: typer.Context,
|
|
254
|
+
config: ConfigOpt = None,
|
|
255
|
+
follow: Annotated[bool, typer.Option("--follow", "-f", help="Follow log output")] = False,
|
|
256
|
+
) -> None:
|
|
257
|
+
"""Show the container logs."""
|
|
258
|
+
cfg = _prepare(ctx, config)
|
|
259
|
+
cmd = ["podman", "logs"]
|
|
260
|
+
if follow:
|
|
261
|
+
cmd.append("-f")
|
|
262
|
+
podman.exec_([*cmd, cfg.name])
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@app.command("config")
|
|
266
|
+
def config_cmd(ctx: typer.Context, config: ConfigOpt = None) -> None:
|
|
267
|
+
"""Show the resolved config and the podman commands (passwords masked)."""
|
|
268
|
+
cfg = _load(ctx, config)
|
|
269
|
+
rows = [
|
|
270
|
+
("config", str(cfg.path)),
|
|
271
|
+
("toolflow", cfg.version),
|
|
272
|
+
("image", cfg.toolflow.image),
|
|
273
|
+
("container", cfg.name),
|
|
274
|
+
("user", f"{cfg.user} ({os.getuid()}:{os.getgid()})"),
|
|
275
|
+
("vivado", f"{cfg.vivado_dir} ({cfg.vivado_mount})"),
|
|
276
|
+
("license", str(cfg.license_dir)),
|
|
277
|
+
("work", str(cfg.work_dir)),
|
|
278
|
+
("network", podman.pasta_network(cfg)),
|
|
279
|
+
]
|
|
280
|
+
width = max(len(k) for k, _ in rows)
|
|
281
|
+
for k, v in rows:
|
|
282
|
+
typer.echo(f"{k:<{width}} {v}")
|
|
283
|
+
|
|
284
|
+
env = podman.container_env(cfg)
|
|
285
|
+
typer.echo("\nenvironment:")
|
|
286
|
+
for k, v in env.items():
|
|
287
|
+
typer.echo(f" {k}={'******' if k in podman.SECRET_ENV else v}")
|
|
288
|
+
typer.echo("\nbuild:")
|
|
289
|
+
typer.echo(" " + shlex.join(podman.build_args(cfg)))
|
|
290
|
+
typer.echo("\nrun:")
|
|
291
|
+
typer.echo(" " + shlex.join(podman.run_args(cfg)))
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
@app.command()
|
|
295
|
+
def ps() -> None:
|
|
296
|
+
"""List all containers created by casperbox."""
|
|
297
|
+
podman.check_dependencies()
|
|
298
|
+
containers = podman.list_containers()
|
|
299
|
+
if not containers:
|
|
300
|
+
typer.echo("No casperbox containers")
|
|
301
|
+
return
|
|
302
|
+
rows = [("NAME", "TOOLFLOW", "STATUS", "PORTS", "CONFIG")]
|
|
303
|
+
for c in containers:
|
|
304
|
+
ports = ",".join(f"{p.host}->{p.container}/{p.protocol}" for p in c.ports)
|
|
305
|
+
rows.append((c.name, c.labels.get(podman.LABEL_TOOLFLOW, ""), c.status, ports, c.config_path))
|
|
306
|
+
_print_table(rows)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def main() -> None:
|
|
310
|
+
try:
|
|
311
|
+
app()
|
|
312
|
+
except CasperError as e:
|
|
313
|
+
error(str(e))
|
|
314
|
+
sys.exit(1)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
if __name__ == "__main__":
|
|
318
|
+
main()
|
casperbox/config.py
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"""casperbox.toml: load, apply defaults, validate."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ipaddress
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import tomllib
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from pathlib import Path, PurePosixPath
|
|
11
|
+
from string import Template
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from .errors import CasperError
|
|
15
|
+
from .versions import Toolflow, load_toolflow
|
|
16
|
+
|
|
17
|
+
DEFAULT_CONFIG_NAME = "casperbox.toml"
|
|
18
|
+
|
|
19
|
+
# Container ports used by the image itself
|
|
20
|
+
SSH_CONTAINER_PORT = 22
|
|
21
|
+
VNC_CONTAINER_PORT = 5901
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class Mount:
|
|
26
|
+
host: Path
|
|
27
|
+
container: str
|
|
28
|
+
mode: str # ro | rw
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class PortMap:
|
|
33
|
+
host: int
|
|
34
|
+
container: int
|
|
35
|
+
protocol: str # tcp | udp
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class Config:
|
|
40
|
+
path: Path
|
|
41
|
+
toolflow: Toolflow
|
|
42
|
+
name: str
|
|
43
|
+
user: str
|
|
44
|
+
password: str
|
|
45
|
+
vnc_password: str
|
|
46
|
+
mac: str
|
|
47
|
+
shm_size: str
|
|
48
|
+
tz: str
|
|
49
|
+
vivado_dir: Path
|
|
50
|
+
vivado_mount: str
|
|
51
|
+
license_dir: Path
|
|
52
|
+
work_dir: Path
|
|
53
|
+
bind_address: str
|
|
54
|
+
ssh_port: int
|
|
55
|
+
vnc_port: int
|
|
56
|
+
vnc_geometry: str
|
|
57
|
+
mounts: list[Mount] = field(default_factory=list)
|
|
58
|
+
extra_ports: list[PortMap] = field(default_factory=list)
|
|
59
|
+
warnings: list[str] = field(default_factory=list)
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def version(self) -> str:
|
|
63
|
+
return self.toolflow.version
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def home(self) -> str:
|
|
67
|
+
return f"/home/{self.user}"
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def all_ports(self) -> list[PortMap]:
|
|
71
|
+
return [
|
|
72
|
+
PortMap(self.ssh_port, SSH_CONTAINER_PORT, "tcp"),
|
|
73
|
+
PortMap(self.vnc_port, VNC_CONTAINER_PORT, "tcp"),
|
|
74
|
+
*self.extra_ports,
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ---------------------------------------------------------------------------
|
|
79
|
+
# Template
|
|
80
|
+
# ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
def render_template(toolflow: Toolflow) -> str:
|
|
83
|
+
from importlib.resources import files
|
|
84
|
+
|
|
85
|
+
text = (files("casperbox") / "templates" / "casperbox.toml").read_text(encoding="utf-8")
|
|
86
|
+
return Template(text).substitute(version=toolflow.version, vivado_version=toolflow.vivado_version)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# ---------------------------------------------------------------------------
|
|
90
|
+
# Typed access to the TOML document
|
|
91
|
+
# ---------------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
class _Table:
|
|
94
|
+
"""Reads keys from one TOML table, checking types and rejecting unknown keys."""
|
|
95
|
+
|
|
96
|
+
def __init__(self, data: Any, where: str):
|
|
97
|
+
if not isinstance(data, dict):
|
|
98
|
+
raise CasperError(f"{where} must be a table")
|
|
99
|
+
self.data = data
|
|
100
|
+
self.where = where
|
|
101
|
+
self.used: set[str] = set()
|
|
102
|
+
|
|
103
|
+
def _key(self, key: str) -> str:
|
|
104
|
+
return f"{self.where}.{key}" if self.where else key
|
|
105
|
+
|
|
106
|
+
def get(self, key: str, typ: type, default: Any = ...) -> Any:
|
|
107
|
+
self.used.add(key)
|
|
108
|
+
if key not in self.data:
|
|
109
|
+
if default is ...:
|
|
110
|
+
raise CasperError(f"{self._key(key)} is required")
|
|
111
|
+
return default
|
|
112
|
+
value = self.data[key]
|
|
113
|
+
# bool is a subclass of int in Python; do not accept true/false as a port
|
|
114
|
+
if not isinstance(value, typ) or (typ is int and isinstance(value, bool)):
|
|
115
|
+
raise CasperError(f"{self._key(key)} must be a {typ.__name__}, got {value!r}")
|
|
116
|
+
return value
|
|
117
|
+
|
|
118
|
+
def table(self, key: str) -> _Table:
|
|
119
|
+
self.used.add(key)
|
|
120
|
+
return _Table(self.data.get(key, {}), self._key(key))
|
|
121
|
+
|
|
122
|
+
def array_of_tables(self, key: str) -> list[_Table]:
|
|
123
|
+
self.used.add(key)
|
|
124
|
+
items = self.data.get(key, [])
|
|
125
|
+
if not isinstance(items, list):
|
|
126
|
+
raise CasperError(f"{self._key(key)} must be an array of tables ([[{key}]])")
|
|
127
|
+
return [_Table(item, f"{self._key(key)}[{i}]") for i, item in enumerate(items)]
|
|
128
|
+
|
|
129
|
+
def finish(self) -> None:
|
|
130
|
+
unknown = sorted(set(self.data) - self.used)
|
|
131
|
+
if unknown:
|
|
132
|
+
raise CasperError(f"Unknown key(s) in {self.where or 'top level'}: {', '.join(unknown)}")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
# ---------------------------------------------------------------------------
|
|
136
|
+
# Validation helpers
|
|
137
|
+
# ---------------------------------------------------------------------------
|
|
138
|
+
|
|
139
|
+
_USER_RE = re.compile(r"^[a-z_][a-z0-9_-]{0,31}$")
|
|
140
|
+
_NAME_RE = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,62}$")
|
|
141
|
+
_MAC_RE = re.compile(r"^([0-9a-f]{2}:){5}[0-9a-f]{2}$")
|
|
142
|
+
_GEOMETRY_RE = re.compile(r"^[0-9]+x[0-9]+$")
|
|
143
|
+
_SHM_RE = re.compile(r"^[0-9]+[bkmgBKMG]?$")
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _host_path(value: str, base: Path) -> Path:
|
|
147
|
+
"""Expand ~ and resolve relative paths against the config file's directory."""
|
|
148
|
+
p = Path(value).expanduser()
|
|
149
|
+
if not p.is_absolute():
|
|
150
|
+
p = base / p
|
|
151
|
+
return p.resolve()
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _check_port(value: int, what: str) -> int:
|
|
155
|
+
if not 1 <= value <= 65535:
|
|
156
|
+
raise CasperError(f"{what} is out of range (1-65535): {value}")
|
|
157
|
+
return value
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _check_no_newline(value: str, what: str) -> str:
|
|
161
|
+
if "\n" in value or "\r" in value or "\0" in value:
|
|
162
|
+
raise CasperError(f"{what} must not contain newlines")
|
|
163
|
+
return value
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def normalize_mac(mac: str) -> str:
|
|
167
|
+
mac = mac.strip().lower().replace("-", ":")
|
|
168
|
+
if not _MAC_RE.match(mac):
|
|
169
|
+
raise CasperError(f"Invalid container.mac: {mac!r} (expected xx:xx:xx:xx:xx:xx)")
|
|
170
|
+
if int(mac[:2], 16) & 1:
|
|
171
|
+
raise CasperError(f"container.mac must not be a multicast address: {mac}")
|
|
172
|
+
if mac == "00:00:00:00:00:00":
|
|
173
|
+
raise CasperError("container.mac must not be all zeros")
|
|
174
|
+
return mac
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _protected_paths(user: str) -> list[str]:
|
|
178
|
+
home = f"/home/{user}"
|
|
179
|
+
return [
|
|
180
|
+
"/bin", "/boot", "/dev", "/etc", "/lib", "/lib64", "/proc", "/root", "/run",
|
|
181
|
+
"/sbin", "/sys", "/tmp", "/usr", "/var",
|
|
182
|
+
"/opt/conda", "/opt/scilab", "/opt/casper",
|
|
183
|
+
home, f"{home}/work", f"{home}/.Xilinx", f"{home}/toolflow", f"{home}/.local",
|
|
184
|
+
]
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
# ---------------------------------------------------------------------------
|
|
188
|
+
# Loading
|
|
189
|
+
# ---------------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
def resolve_config_path(path: Path | None) -> Path:
|
|
192
|
+
p = (path or Path(DEFAULT_CONFIG_NAME)).expanduser()
|
|
193
|
+
p = p if p.is_absolute() else Path.cwd() / p
|
|
194
|
+
if not p.is_file():
|
|
195
|
+
if path is None:
|
|
196
|
+
raise CasperError(
|
|
197
|
+
f"{DEFAULT_CONFIG_NAME} not found in {Path.cwd()}; "
|
|
198
|
+
"run `casperbox init` to create one, or pass -c FILE"
|
|
199
|
+
)
|
|
200
|
+
raise CasperError(f"Config file not found: {p}")
|
|
201
|
+
return p.resolve()
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def load_config(path: Path | None = None) -> Config:
|
|
205
|
+
cfg_path = resolve_config_path(path)
|
|
206
|
+
try:
|
|
207
|
+
with cfg_path.open("rb") as f:
|
|
208
|
+
doc = tomllib.load(f)
|
|
209
|
+
except tomllib.TOMLDecodeError as e:
|
|
210
|
+
raise CasperError(f"{cfg_path}: {e}") from e
|
|
211
|
+
return parse_config(doc, cfg_path)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def parse_config(doc: dict[str, Any], cfg_path: Path) -> Config:
|
|
215
|
+
base = cfg_path.parent
|
|
216
|
+
top = _Table(doc, "")
|
|
217
|
+
warnings: list[str] = []
|
|
218
|
+
|
|
219
|
+
toolflow = load_toolflow(top.get("version", str))
|
|
220
|
+
|
|
221
|
+
# ---- [container] ----
|
|
222
|
+
c = top.table("container")
|
|
223
|
+
name = c.get("name", str, f"casper-{toolflow.version}")
|
|
224
|
+
if not _NAME_RE.match(name):
|
|
225
|
+
raise CasperError(f"Invalid container.name: {name!r}")
|
|
226
|
+
user = c.get("user", str, "casper")
|
|
227
|
+
if not _USER_RE.match(user) or user == "root":
|
|
228
|
+
raise CasperError(f"Invalid container.user: {user!r}")
|
|
229
|
+
password = _check_no_newline(c.get("password", str, "casper"), "container.password")
|
|
230
|
+
if not password:
|
|
231
|
+
raise CasperError("container.password must not be empty")
|
|
232
|
+
vnc_password = _check_no_newline(c.get("vnc_password", str, password), "container.vnc_password")
|
|
233
|
+
if len(vnc_password) < 6:
|
|
234
|
+
raise CasperError("container.vnc_password must be at least 6 characters")
|
|
235
|
+
if len(vnc_password) > 8:
|
|
236
|
+
warnings.append("container.vnc_password is longer than 8 characters; only the first 8 are used")
|
|
237
|
+
mac = c.get("mac", str, "")
|
|
238
|
+
mac = normalize_mac(mac) if mac.strip() else ""
|
|
239
|
+
shm_size = c.get("shm_size", str, "2g")
|
|
240
|
+
if not _SHM_RE.match(shm_size):
|
|
241
|
+
raise CasperError(f"Invalid container.shm_size: {shm_size!r}")
|
|
242
|
+
tz = _check_no_newline(c.get("tz", str, "UTC"), "container.tz")
|
|
243
|
+
c.finish()
|
|
244
|
+
|
|
245
|
+
# ---- [vivado] ----
|
|
246
|
+
v = top.table("vivado")
|
|
247
|
+
vivado_dir = _host_path(v.get("dir", str), base)
|
|
248
|
+
vivado_mount = v.get("mount", str, "ro")
|
|
249
|
+
if vivado_mount not in ("ro", "rw"):
|
|
250
|
+
raise CasperError("vivado.mount must be 'ro' or 'rw'")
|
|
251
|
+
v.finish()
|
|
252
|
+
if not vivado_dir.is_dir():
|
|
253
|
+
raise CasperError(f"vivado.dir does not exist: {vivado_dir}")
|
|
254
|
+
vv = toolflow.vivado_version
|
|
255
|
+
if not (vivado_dir / "Vivado" / vv / "settings64.sh").is_file() and not (vivado_dir / "settings64.sh").is_file():
|
|
256
|
+
warnings.append(
|
|
257
|
+
f"Neither Vivado/{vv}/settings64.sh nor settings64.sh found under {vivado_dir}; "
|
|
258
|
+
f"toolflow {toolflow.version} requires Vivado {vv}"
|
|
259
|
+
)
|
|
260
|
+
# Vivado is mounted at the same path; it must not shadow system or toolflow directories
|
|
261
|
+
for protected in _protected_paths(user):
|
|
262
|
+
if protected == str(vivado_dir) or protected.startswith(f"{vivado_dir}/") or str(vivado_dir) == "/":
|
|
263
|
+
raise CasperError(f"vivado.dir={vivado_dir} would shadow {protected} in the container, please use another path")
|
|
264
|
+
|
|
265
|
+
# ---- [paths] ----
|
|
266
|
+
p = top.table("paths")
|
|
267
|
+
license_dir = _host_path(p.get("license_dir", str, "~/.Xilinx"), base)
|
|
268
|
+
work_dir = _host_path(p.get("work_dir", str, "./work"), base)
|
|
269
|
+
p.finish()
|
|
270
|
+
if not license_dir.is_dir():
|
|
271
|
+
raise CasperError(f"paths.license_dir does not exist: {license_dir}")
|
|
272
|
+
if work_dir.exists() and not work_dir.is_dir():
|
|
273
|
+
raise CasperError(f"paths.work_dir is not a directory: {work_dir}")
|
|
274
|
+
|
|
275
|
+
# ---- [ports] ----
|
|
276
|
+
pt = top.table("ports")
|
|
277
|
+
bind_address = pt.get("bind_address", str, "0.0.0.0")
|
|
278
|
+
try:
|
|
279
|
+
ipaddress.ip_address(bind_address)
|
|
280
|
+
except ValueError as e:
|
|
281
|
+
raise CasperError(f"Invalid ports.bind_address: {bind_address!r}") from e
|
|
282
|
+
ssh_port = _check_port(pt.get("ssh", int, 2222), "ports.ssh")
|
|
283
|
+
vnc_port = _check_port(pt.get("vnc", int, 5901), "ports.vnc")
|
|
284
|
+
pt.finish()
|
|
285
|
+
|
|
286
|
+
# ---- [vnc] ----
|
|
287
|
+
vn = top.table("vnc")
|
|
288
|
+
vnc_geometry = vn.get("geometry", str, "1920x1080")
|
|
289
|
+
if not _GEOMETRY_RE.match(vnc_geometry):
|
|
290
|
+
raise CasperError("vnc.geometry must be WIDTHxHEIGHT, e.g. 1920x1080")
|
|
291
|
+
vn.finish()
|
|
292
|
+
|
|
293
|
+
# ---- [[extra_ports]] ----
|
|
294
|
+
extra_ports: list[PortMap] = []
|
|
295
|
+
for t in top.array_of_tables("extra_ports"):
|
|
296
|
+
host = _check_port(t.get("host", int), f"{t.where}.host")
|
|
297
|
+
cport = _check_port(t.get("container", int), f"{t.where}.container")
|
|
298
|
+
proto = t.get("protocol", str, "tcp")
|
|
299
|
+
t.finish()
|
|
300
|
+
if proto not in ("tcp", "udp"):
|
|
301
|
+
raise CasperError(f"{t.where}.protocol must be 'tcp' or 'udp'")
|
|
302
|
+
if cport in (SSH_CONTAINER_PORT, VNC_CONTAINER_PORT):
|
|
303
|
+
raise CasperError(f"{t.where}: container ports 22/5901 are reserved")
|
|
304
|
+
extra_ports.append(PortMap(host, cport, proto))
|
|
305
|
+
|
|
306
|
+
seen: set[tuple[int, str]] = set()
|
|
307
|
+
for pm in [PortMap(ssh_port, 22, "tcp"), PortMap(vnc_port, 5901, "tcp"), *extra_ports]:
|
|
308
|
+
if (pm.host, pm.protocol) in seen:
|
|
309
|
+
raise CasperError(f"Duplicate host port {pm.host}/{pm.protocol}")
|
|
310
|
+
seen.add((pm.host, pm.protocol))
|
|
311
|
+
|
|
312
|
+
# ---- [[mounts]] ----
|
|
313
|
+
home = f"/home/{user}"
|
|
314
|
+
reserved = {str(vivado_dir), home, f"{home}/.Xilinx", f"{home}/work", f"{home}/toolflow"}
|
|
315
|
+
mounts: list[Mount] = []
|
|
316
|
+
for t in top.array_of_tables("mounts"):
|
|
317
|
+
hpath = _host_path(t.get("host", str), base)
|
|
318
|
+
cpath = t.get("container", str)
|
|
319
|
+
mode = t.get("mode", str, "rw")
|
|
320
|
+
t.finish()
|
|
321
|
+
if mode not in ("ro", "rw"):
|
|
322
|
+
raise CasperError(f"{t.where}.mode must be 'ro' or 'rw'")
|
|
323
|
+
if not hpath.exists():
|
|
324
|
+
raise CasperError(f"{t.where}.host does not exist: {hpath}")
|
|
325
|
+
if cpath == "~":
|
|
326
|
+
cpath = home
|
|
327
|
+
elif cpath.startswith("~/"):
|
|
328
|
+
cpath = f"{home}/{cpath[2:]}"
|
|
329
|
+
if not cpath.startswith("/"):
|
|
330
|
+
raise CasperError(f"{t.where}.container must be absolute or start with ~/: {cpath!r}")
|
|
331
|
+
cpath = os.path.normpath(cpath)
|
|
332
|
+
if ":" in str(hpath) or ":" in cpath:
|
|
333
|
+
raise CasperError(f"{t.where}: paths must not contain ':'")
|
|
334
|
+
if cpath in reserved:
|
|
335
|
+
raise CasperError(f"{t.where}.container conflicts with a reserved path: {cpath}")
|
|
336
|
+
reserved.add(cpath)
|
|
337
|
+
mounts.append(Mount(hpath, str(PurePosixPath(cpath)), mode))
|
|
338
|
+
|
|
339
|
+
top.finish()
|
|
340
|
+
|
|
341
|
+
return Config(
|
|
342
|
+
path=cfg_path,
|
|
343
|
+
toolflow=toolflow,
|
|
344
|
+
name=name,
|
|
345
|
+
user=user,
|
|
346
|
+
password=password,
|
|
347
|
+
vnc_password=vnc_password,
|
|
348
|
+
mac=mac,
|
|
349
|
+
shm_size=shm_size,
|
|
350
|
+
tz=tz,
|
|
351
|
+
vivado_dir=vivado_dir,
|
|
352
|
+
vivado_mount=vivado_mount,
|
|
353
|
+
license_dir=license_dir,
|
|
354
|
+
work_dir=work_dir,
|
|
355
|
+
bind_address=bind_address,
|
|
356
|
+
ssh_port=ssh_port,
|
|
357
|
+
vnc_port=vnc_port,
|
|
358
|
+
vnc_geometry=vnc_geometry,
|
|
359
|
+
mounts=mounts,
|
|
360
|
+
extra_ports=extra_ports,
|
|
361
|
+
warnings=warnings,
|
|
362
|
+
)
|
casperbox/errors.py
ADDED