Files
computer-playbook/cli/administration/deploy/development/inventory/builder.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

143 lines
4.5 KiB
Python

"""Inventory folder materialisation.
`build_dev_inventory` is the SPOT that turns a `DevInventorySpec` into a
fully baked inventory folder via the `infinito administration inventory
provision` CLI. `build_dev_inventory_matrix` is the orchestrator: it
runs the planner and builds every round's folder.
"""
from __future__ import annotations
import json
import os
from typing import TYPE_CHECKING, Any
from cli.administration.deploy.development.common import DEV_INVENTORY_VARS_FILE
from cli.administration.deploy.development.mirrors import (
generate_ci_mirrors_file,
should_use_mirrors_on_ci,
)
from .payload import _bake_overrides, _resolve_variant_payloads
from .planner import plan_dev_inventory_matrix
from .spec import DevInventorySpec, PlanEntry
if TYPE_CHECKING:
from collections.abc import Mapping, Sequence
from cli.administration.deploy.development.compose import Compose
def build_dev_inventory(compose: Compose, spec: DevInventorySpec) -> None:
"""Build the inventory folder described by `spec` and ensure its
vault password file exists. Two side-effects, no return value: after
this call, `spec.inventory_dir` contains a complete, variant-resolved
inventory.
"""
inv_root = spec.inventory_root()
variant_payloads = _resolve_variant_payloads(
roles_dir=str(compose.repo_root / "roles"),
include=spec.include,
active_variants=spec.variant_selectors(),
)
vars_payload = _bake_overrides(
base_overrides=spec.overrides(),
variant_payloads=variant_payloads,
)
cmd = [
"infinito",
"administration",
"inventory",
"provision",
inv_root,
"--host",
"localhost",
"--vars-file",
DEV_INVENTORY_VARS_FILE,
"--vars",
json.dumps(vars_payload, sort_keys=True),
"--include",
",".join(spec.include),
]
variant_selectors = spec.variant_selectors()
if variant_selectors:
cmd += ["--app-variants", json.dumps(variant_selectors, sort_keys=True)]
if should_use_mirrors_on_ci():
mirrors_file = generate_ci_mirrors_file(compose, inventory_dir=inv_root)
cmd += ["--mirror", mirrors_file]
extra_env: dict[str, str] = {}
if spec.services_disabled:
extra_env["disable"] = spec.services_disabled
compose.exec(
cmd,
check=True,
workdir=os.environ["INFINITO_SRC_DIR"],
extra_env=extra_env or None,
)
_ensure_vault_password_file(compose, inventory_dir=inv_root)
def build_dev_inventory_matrix(
compose: Compose,
*,
base_inventory_dir: str,
primary_apps: Sequence[str],
storage_constrained: bool,
runtime: str,
extra_vars: Mapping[str, Any] | None = None,
services_disabled: str = "",
include_filter: Sequence[str] | None = None,
) -> list[PlanEntry]:
"""Build every folder in the matrix plan and return the plan.
`include_filter`, when provided, is the set of role names a caller
has already filtered (e.g. by `disable` removal of provider
roles). Each round's include is intersected with this set before
being baked, so the inventory and `--include` flag stay aligned
with whatever the deploy step will actually deploy.
"""
plan = plan_dev_inventory_matrix(
roles_dir=str(compose.repo_root / "roles"),
primary_apps=primary_apps,
base_inventory_dir=base_inventory_dir,
)
allow: set[str] | None = set(include_filter) if include_filter is not None else None
for _round_index, inv_dir, round_variants, raw_include, _purge_set in plan:
round_include = (
tuple(role for role in raw_include if role in allow)
if allow is not None
else raw_include
)
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)
return plan
def _ensure_vault_password_file(compose: Compose, *, inventory_dir: str) -> None:
inv_root = str(inventory_dir).rstrip("/")
pw_file = f"{inv_root}/.password"
compose.exec(
[
"sh",
"-lc",
f"mkdir -p {inv_root} && "
f"[ -f {pw_file} ] || "
f"printf '%s\n' 'ci-vault-password' > {pw_file}",
],
check=True,
)