Files
computer-playbook/cli/administration/deploy/development/exec.py
T
kevinveenbirkenbachandClaude Opus 4.7 eb4bcd23a8 refactor(env/spot): forbid .get() defaults + cache namespace rename
Three new lint tests close gaps in the INFINITO_* SPOT contract:
* tests/lint/filesystem/env/test_default_env_keys_handled.py
  Every default.env key must be wired through a handler.
* tests/lint/filesystem/env/test_used_keys_declared.py
  Every INFINITO_* in .sh/.py must live in env/{default,ci}.env or
  a handler; per-line `# nocheck:` suppresses runtime-only refs.
* tests/lint/filesystem/python/test_no_infinito_get_defaults.py
  Forbid ANY default in `.get("INFINITO_*", ...)` -- including the
  empty-string fallback (which silently masks an unset key). The
  nocheck check walks every physical line the call spans so an
  autoformat-split call still honours the marker.

Cache namespace flattens to a single prefix:
  INFINITO_PACKAGE_CACHE_*  -> INFINITO_CACHE_PACKAGE_*
  INFINITO_REGISTRY_CACHE_* -> INFINITO_CACHE_REGISTRY_*
The five previously driver-injected paths (APT_LIST, NPMRC,
PIP_CONF, REGISTRY_PROXY_CONF, FRONTEND_CA_FILE) move into
env/default.env and passthrough.STATIC_KEYS; the
`cache_env_overrides()` helper and its two call sites are gone
along with the test-fixture override for FRONTEND_CA_DIR.

SPOT-strict rewrites:
  - 19 sites: `something.get("INFINITO_X", "")` -> subscript
    `something["INFINITO_X"]` (loud KeyError when env loader did
    not run; all keys live in env/default.env).
  - 2 handler-internal bootstrap reads suppressed with
    `# nocheck: handler-bootstrap-read`.
  - 30 runtime-only / driver-injected / test-fixture references
    annotated with `# nocheck: <reason>` per-line.
  - Unit tests that previously removed SPOT-guaranteed keys via
    `{k: v ... if k \!= KEY}` or `clear=True` switch to setting
    the key to "" explicitly, matching the new contract.

Side fixes:
  - INFINITO_DNS_DOMAIN + INFINITO_DNS_HOSTS_FILE move from
    shell-side defaults in common.sh to env/default.env and
    passthrough.STATIC_KEYS.
  - dns_systemd_is_operational() drops the dead
    INFINITO_DNS_FORCE_HOSTS_FALLBACK override branch (the
    variable was never set anywhere).
  - INFINITO_WORKER_FETCH switches from `os.environ.get(KEY, "1")`
    to `os.environ[KEY]`.
  - scripts/tests/code/wrapper.sh exec args move into a bash
    array so `# nocheck:` on INFINITO_TEST_TYPE can live on its
    own element line (SC2215 fix).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 22:26:16 +02:00

64 lines
2.0 KiB
Python

from __future__ import annotations
import argparse
import os
from .common import make_compose
def add_parser(sub: argparse._SubParsersAction) -> None:
p = sub.add_parser("exec", help="Execute a command inside the infinito container.")
p.add_argument(
"--env",
action="append",
default=[],
metavar="KEY=VALUE",
help=(
"Inject KEY=VALUE into the container environment for this "
"exec call. Repeatable. Lets bash callers pass per-run "
"context (INFINITO_INVENTORY_FILE, INFINITO_APPS, ...) into in-container "
"helper scripts without inlining heredocs."
),
)
p.add_argument(
"cmd",
nargs=argparse.REMAINDER,
help="Command to execute (use `--` to separate).",
)
p.set_defaults(_handler=handler)
def _parse_env_pairs(pairs: list[str]) -> dict[str, str]:
parsed: dict[str, str] = {}
for pair in pairs:
if "=" not in pair:
raise SystemExit(f"--env expects KEY=VALUE; got {pair!r} without '='")
key, value = pair.split("=", 1)
key = key.strip()
if not key:
raise SystemExit(f"--env KEY is empty in {pair!r}")
parsed[key] = value
return parsed
def handler(args: argparse.Namespace) -> int:
compose = make_compose()
cmd = list(args.cmd or [])
if cmd and cmd[0] == "--":
cmd = cmd[1:]
if not cmd:
raise SystemExit("exec requires a command (e.g. exec -- sh -lc 'whoami')")
extra_env: dict[str, str] = {}
services_disabled = os.environ["INFINITO_SERVICES_DISABLED"]
if services_disabled:
extra_env["INFINITO_SERVICES_DISABLED"] = services_disabled
# Caller-supplied --env entries win over implicit ones (current convention
# in the dev CLI: explicit user input overrides implicit defaults).
extra_env.update(_parse_env_pairs(args.env or []))
r = compose.exec(cmd, check=False, extra_env=extra_env or None)
return int(r.returncode)