Files
computer-playbook/cli/administration/deploy/development/mirrors.py
T
kevinveenbirkenbachandClaude Opus 5 f7637801be fix(deploy-cli): read INFINITO_SRC_DIR when it is used, not when the module loads
mirrors.py resolved the container repo root at import time, so every module of
cli.administration.deploy.development raised KeyError on a host that does not
export INFINITO_SRC_DIR. autodoc skips what it cannot import, which dropped 107
docstrings from the documentation corpus and made the docs catalog appear to
drift by that many entries between runs. Whether they were present depended on
whether the shell had inherited the deploy environment.

_require_env already exists for this and raises a named error rather than
substituting a value, so the failure stays loud and only moves to the call.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-23 17:44:25 +02:00

89 lines
2.6 KiB
Python

from __future__ import annotations
import os
import shlex
from pathlib import Path
def _require_env(name: str) -> str:
val = os.environ.get(name)
if val is None or not str(val).strip():
raise RuntimeError(f"Required environment variable is missing or empty: {name}")
return str(val).strip()
def container_repo_root() -> Path:
"""Return the repository root inside the deploy container.
Returns:
The path ``INFINITO_SRC_DIR`` names.
"""
return Path(_require_env("INFINITO_SRC_DIR"))
def should_use_mirrors() -> bool:
"""True iff image references should be rewritten to the GHCR mirror."""
from .profile import Profile
return Profile().image_mirror_enabled()
def _exec_env() -> dict[str, str]:
"""
All variables are mandatory. No fallbacks.
"""
return {
"INFINITO_RUNNING_ON_GITHUB": _require_env("INFINITO_RUNNING_ON_GITHUB"),
"GITHUB_REPOSITORY_OWNER": _require_env("GITHUB_REPOSITORY_OWNER"),
"GITHUB_REPOSITORY": _require_env("GITHUB_REPOSITORY"),
"INFINITO_GHCR_MIRROR_PREFIX": _require_env("INFINITO_GHCR_MIRROR_PREFIX"),
}
def generate_ci_mirrors_file(compose, *, inventory_dir: str) -> str:
"""
Generate mirrors.yml inside the container using cli.contributing.mirror.resolver.
Writes:
<inventory_dir>/mirrors.yml
"""
inv_root = str(inventory_dir).rstrip("/")
if not inv_root:
raise RuntimeError("inventory_dir must not be empty")
env = _exec_env()
ghcr_namespace = env["GITHUB_REPOSITORY_OWNER"].lower()
ghcr_repository = env["GITHUB_REPOSITORY"].split("/", 1)[-1].lower()
ghcr_prefix = env["INFINITO_GHCR_MIRROR_PREFIX"].strip().strip("/")
if not ghcr_prefix:
raise RuntimeError("INFINITO_GHCR_MIRROR_PREFIX must not be empty")
mirrors_path = f"{inv_root}/mirrors.yml"
repo_root = str(container_repo_root())
cmd = [
"bash",
"-lc",
(
"set -euo pipefail; "
f"mkdir -p {shlex.quote(inv_root)}; "
'"${PYTHON:-python3}" -m cli.contributing.mirror.resolver '
f"--repo-root {shlex.quote(repo_root)} "
f"--ghcr-namespace {shlex.quote(ghcr_namespace)} "
f"--ghcr-repository {shlex.quote(ghcr_repository)} "
f"--ghcr-prefix {shlex.quote(ghcr_prefix)} "
f"> {shlex.quote(mirrors_path)}; "
f"echo '[init] mirrors generated:' {shlex.quote(mirrors_path)}; "
f"wc -l {shlex.quote(mirrors_path)}"
),
]
compose.exec(
cmd,
check=True,
workdir=repo_root,
extra_env=env,
)
return mirrors_path