Files
computer-playbook/cli/administration/deploy/development/init.py
T
kevinveenbirkenbachandClaude Opus 4.7 8e293bd921 fix(compose-deploy): stop persisting runtime routing knobs into .env
Root cause of step-08 deploy failures across all 4 test-development
distros in run 26572559126: `make compose-deploy mode=reinstall
apps=web-app-dashboard INFINITO_SERVICES_DISABLED=matomo` (step 07)
made make export the cmdline var into the recipe's shell env, which
reached `runner.py` → `make setup` → `make dotenv`. The dotenv
generator's `setdefault` then baked the runtime-only value into the
on-disk `.env`. Step 08 sourced that .env, filtered `web-app-matomo`
out of its primary apps, and exited with "All primary apps disabled".

Fix:

- Drop the 7 routing knobs (apps, mode, purge, bundles, disable,
  full_cycle, variant) from `passthrough.STATIC_KEYS` and from
  `default.env`. They are per-invocation; .env is the persistent SPOT
  and must not capture them.
- Switch every consumer from `os.environ["KEY"]` strict access to
  `os.environ.get("KEY", "")` so the missing-key path stops at "no
  filter" instead of a KeyError.
- Rename the env-var surface to match the make-alias surface 1:1
  (lowercase): INFINITO_APPS → apps, INFINITO_SERVICES_DISABLED →
  disable, INFINITO_BUNDLES → bundles, INFINITO_FULL_CYCLE →
  full_cycle, INFINITO_VARIANT → variant, INFINITO_DEPLOY_MODE → mode,
  INFINITO_PURGE_ENTITIES → purge. The make alias was the user-facing
  name in every doc; routing-knob env vars now use the same name so
  the cmdline form, the recipe env-prefix, and the consumer reads
  all line up.
- Collapse the Makefile `compose-deploy` recipe to just the two
  knobs whose alias differs from the env name (`type` →
  INFINITO_DEPLOY_TYPE, `debug` → INFINITO_DEBUG). GNU Make
  auto-exports cmdline variables into the recipe's shell env, so the
  identical-name `$(if $(apps),apps="$(apps)")` lines were pure
  noise.
- Drop `disable: ${disable}` from compose.yml. The dev CLI builds
  `extra_env` per `docker exec` call and forwards `disable` via
  `-e`, so the container env never needs the value frozen at
  compose-up time.
- Update the 3 actual leak call sites
  (07/08/09_deploy_*.sh) to the make-alias form
  (`disable=matomo`, `full_cycle=true`, `variant=1`).
- Add `SIM112` to ruff's ignore set: the lowercase env names are
  the deliberate make/env parity, not a typo.

Repro before fix: `INFINITO_SERVICES_DISABLED=matomo make dotenv`
wrote `INFINITO_SERVICES_DISABLED=matomo` to `.env`. After fix:
`disable=matomo make dotenv` leaves `.env` untouched.

Tests: `make test` and `make autoformat` green (2711 tests, 8 lint
sub-targets).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-28 17:47:03 +02:00

207 lines
7.1 KiB
Python

from __future__ import annotations
import json
import os
from typing import TYPE_CHECKING, Any
from cli.administration.inventory.provision.services_disabler import (
find_provider_roles,
parse_services_disabled,
)
from cli.meta.runtime import detect_runtime
from .common import make_compose
from .inventory import (
DevInventorySpec,
build_dev_inventory,
filter_plan_to_variant,
plan_dev_inventory_matrix,
)
from .storage import detect_storage_constrained
if TYPE_CHECKING:
import argparse
def add_parser(sub: argparse._SubParsersAction) -> None:
p = sub.add_parser(
"init",
help="Create development inventory inside the infinito container.",
)
p.add_argument(
"--inventory-dir",
default=os.environ.get("INFINITO_INVENTORY_DIR"),
required=os.environ.get("INFINITO_INVENTORY_DIR") is None,
help=(
"Inventory directory base (default: $INFINITO_INVENTORY_DIR). "
"When the included apps declare more than one matrix-deploy "
"variant, sibling folders `<dir>-0`, `<dir>-1`, ... are "
"created; otherwise the directory is used as-is."
),
)
g = p.add_mutually_exclusive_group(required=True)
g.add_argument(
"--apps",
help="One or more application ids (will include run_after deps automatically).",
)
g.add_argument(
"--include",
help="Comma-separated list of application ids to include (no deps resolution).",
)
p.add_argument(
"--threshold-gib",
type=int,
default=100,
help="Free-space threshold (GiB) below which STORAGE_CONSTRAINED is enabled (default: 100).",
)
p.add_argument(
"--force-storage-constrained",
choices=["true", "false"],
default=None,
help="Override storage detection explicitly.",
)
p.add_argument(
"--vars",
default=None,
help="JSON object merged into inventory variables (overrides win).",
)
p.add_argument(
"--variant",
type=int,
default=_env_variant(),
help=(
"Pin the matrix init to a single round (zero-based index). "
"Useful when only one variant of a multi-variant app needs to "
"be (re-)materialised. Defaults to the variant "
"environment variable when set, otherwise full-matrix mode."
),
)
p.set_defaults(_handler=handler)
def _env_variant() -> int | None:
raw = os.environ.get("variant", "").strip()
if not raw:
return None
try:
return int(raw)
except ValueError:
raise SystemExit(
f"variant environment variable must be an integer, got {raw!r}"
) from None
def handler(args: argparse.Namespace) -> int:
compose = make_compose()
# `primary_apps` is the user-facing app list before transitive resolution.
# The variant-aware planner expands each round's full include set
# itself (services edges depend on variant-merged services maps), so
# init no longer pre-resolves run_after / service deps here.
if args.apps:
primary_apps = [
x.strip() for x in args.apps.replace(",", " ").split() if x.strip()
]
else:
primary_apps = [x.strip() for x in (args.include or "").split(",") if x.strip()]
if not primary_apps:
raise SystemExit("Primary app list is empty")
# `disable` removes provider roles from the inventory at
# `infinito administration inventory provision` time anyway, so do the same filter on
# the primary list here. Otherwise the variant-aware resolver would
# still pull a disabled provider in via service edges, only to have
# the inventory step strip it back out — leaving the round's
# include list inconsistent with what the inventory actually
# contains.
raw_disabled = os.environ.get("disable", "").strip()
disabled_app_ids: set[str] = set()
if raw_disabled:
services = parse_services_disabled(raw_disabled)
roles_dir = compose.repo_root / "roles"
provider_map = find_provider_roles(services, roles_dir)
disabled_app_ids = set(provider_map.values())
primary_apps = [a for a in primary_apps if a not in disabled_app_ids]
if not primary_apps:
raise SystemExit(
"All primary apps disabled by `disable` — nothing to initialise"
)
extra_vars: dict[str, Any] | None = None
if args.vars is not None:
try:
parsed = json.loads(args.vars)
except Exception as exc:
raise SystemExit(f"--vars must be valid JSON: {exc}") from exc
if not isinstance(parsed, dict):
raise SystemExit("--vars must be a JSON object")
extra_vars = parsed
if args.force_storage_constrained is not None:
storage_constrained = args.force_storage_constrained == "true"
else:
storage_constrained = detect_storage_constrained(
compose, threshold_gib=int(args.threshold_gib)
)
plan = plan_dev_inventory_matrix(
roles_dir=str(compose.repo_root / "roles"),
primary_apps=primary_apps,
base_inventory_dir=str(args.inventory_dir),
)
try:
plan = filter_plan_to_variant(plan, args.variant)
except ValueError as exc:
raise SystemExit(f"--variant: {exc}") from exc
runtime = os.environ.get("RUNTIME") or detect_runtime()
services_disabled = os.environ.get("disable", "")
for _round_index, inv_dir, round_variants, include_R, _purge_set in plan:
round_include = tuple(
role for role in include_R if role not in disabled_app_ids
)
if not round_include:
print(
f">>> Skipping inventory at {inv_dir}: include set is empty "
"after `disable` filter"
)
continue
spec = DevInventorySpec(
inventory_dir=inv_dir,
include=round_include,
storage_constrained=storage_constrained,
runtime=runtime,
extra_vars=extra_vars,
services_disabled=services_disabled,
active_variants=round_variants,
)
build_dev_inventory(compose, spec)
if len(plan) == 1:
_, inv_dir, round_variants, include_R, _purge_set = plan[0]
non_zero = {a: i for a, i in round_variants.items() if i}
suffix = f" variants={non_zero}" if non_zero else ""
print(
f">>> Inventory initialized at {inv_dir} "
f"(include={','.join(include_R)} "
f"storage_constrained={storage_constrained}){suffix}"
)
else:
print(
f">>> Matrix inventory initialized in {len(plan)} folders "
f"(primary_apps={','.join(primary_apps)} "
f"storage_constrained={storage_constrained}):"
)
for round_index, inv_dir, round_variants, include_R, _purge_set in plan:
non_zero = {a: i for a, i in round_variants.items() if i}
print(
f" [round {round_index}] {inv_dir} "
f"include={','.join(include_R)}"
+ (f" variants={non_zero}" if non_zero else "")
)
return 0