mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-09 12:17:13 +00:00
Introduces per-role `meta/variants.yml` for declaring multi-variant
deploys (e.g. WordPress Single-Site vs. Multisite) and reshapes the
development deploy CLI to materialise one inventory folder per variant
and iterate them with co-located sync+async passes.
Why:
- The previous single-inventory + Pass-1-then-Pass-2 split deployed each
variant twice and ran the async pass against a host left in a
different variant's state, masking real bugs.
- Inventory and runtime concerns were tangled: variant selection lived
as a runtime extra-var and required cross-stage coordination.
How:
- `roles/<role>/meta/variants.yml` (file format docs at
docs/contributing/artefact/files/role/variants.md): YAML list of
deep-merge overrides on top of `config/main.yml`. Empty entry `{}` is
variant 0 (= legacy payload). 6 roles get their dev overrides moved
here from inventories/dev.yml; WordPress declares 2 variants.
- `utils/runtime_data.py` gains `_build_variants` / `get_variants` and
drops the retired `_active_variants` runtime selector. Loader always
returns variant 0 as the default; non-zero variants reach
`get_merged_applications` as inventory-level `applications.<app>`
overrides baked at init time.
- New `cli/deploy/development/inventory.py`: `DevInventorySpec`,
`plan_dev_inventory_matrix` (pure planner shared between init and
deploy), `build_dev_inventory_matrix` (materialises one folder per
round at `<dir>-<R>`, or `<dir>` when single-variant). Single-folder
paths preserve full backward compat.
- `cli/deploy/development/deploy.py`: matrix loop with per-round
cleanup (only apps whose variant changed AND that are about to
re-deploy), `--variant <idx>` (or `VARIANT=` env) to pin a single
round, and `--full-cycle` (or `FULL_CYCLE=true`) which interleaves
sync + async per round so the async re-deploy targets exactly the
host state the matching sync deploy just produced.
- `cli/deploy/development/exec.py` gains repeatable `--env KEY=VAL` so
bash callers can inject per-run context into in-container helpers
without inlining heredocs.
- `scripts/meta/env/inventory.sh` exports `INVENTORY_VARS_FILE`
(Python mirror in `cli.deploy.development.common.DEV_INVENTORY_VARS_FILE`;
test_inventory_vars_file_spot.py locks them against drift).
- `make deploy-fresh-purged-apps` and `scripts/tests/deploy/ci/dedicated.sh`
switch to a single init + single deploy `--full-cycle`. The
`make deploy-reuse-*` bash scripts honour `VARIANT=` by suffixing
`INVENTORY_DIR`/`INVENTORY_FILE`.
- The 5 `scripts/tests/deploy/local/deploy/*.sh` scripts now delegate
their in-container heredocs to dedicated helpers in
`scripts/tests/deploy/local/utils/`, removing the bash-in-bash
quoting hell.
Tests:
- `tests/unit/utils/test_variants.py` (renamed from
`test_application_variants.py`): loader edge cases, variant-zero
default, retired-`_active_variants` guard.
- `tests/unit/cli/deploy/development/test_inventory.py` (new): planner,
variant baking, builder, `filter_plan_to_variant`, `DevInventorySpec`.
- `tests/unit/cli/deploy/development/test_matrix_deploy.py` (new):
per-round deploy filter, cleanup decisions, `--variant` pinning,
`--full-cycle` per-round interleave.
- `tests/unit/cli/deploy/development/test_inventory_vars_file_spot.py`
(new): drift guard for the bash <-> Python literal pair.
- `tests/unit/cli/deploy/development/test_exec.py` extended for `--env`.
Docs:
- `docs/contributing/artefact/files/role/variants.md` (new): file format.
- `docs/contributing/design/matrix-deploy.md` (new): runtime mechanism
(folder layout, --variant, --full-cycle, cleanup logic).
- `docs/contributing/actions/deploy.md` (new): contributor dev-deploy
flow (make targets, --variant pinning).
- `docs/administration/deploy.md` slimmed to admin-only (was mixed
admin+dev), cross-links to the new contributor doc.
- `docs/agents/action/iteration/role.md` keeps only agent-iteration
recipes; mechanism explanation moved out and linked.
- `docs/contributing/documentation.md` adds the "no self-SPOT
declaration" rule; `applications.md` renamed its SoT header for
consistency.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
343 lines
12 KiB
Python
343 lines
12 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import subprocess
|
|
from typing import Any, Mapping
|
|
|
|
from cli.create.inventory.services_disabler import (
|
|
find_provider_roles,
|
|
parse_services_disabled,
|
|
)
|
|
|
|
from .common import make_compose, repo_root_from_here, resolve_deploy_ids_for_apps
|
|
from .inventory import filter_plan_to_variant, plan_dev_inventory_matrix
|
|
|
|
|
|
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}"
|
|
)
|
|
|
|
|
|
def _env_full_cycle() -> bool:
|
|
return os.environ.get("FULL_CYCLE", "").strip().lower() == "true"
|
|
|
|
|
|
def _run_deploy(
|
|
compose,
|
|
*,
|
|
deploy_ids: list[str],
|
|
debug: bool,
|
|
passthrough: list[str],
|
|
inventory_dir: str,
|
|
extra_ansible_vars: Mapping[str, Any] | None = None,
|
|
) -> int:
|
|
inv_root = str(inventory_dir).rstrip("/")
|
|
inv_file = f"{inv_root}/devices.yml"
|
|
pw_file = f"{inv_root}/.password"
|
|
|
|
cmd = [
|
|
"python3",
|
|
"-m",
|
|
"cli.deploy.dedicated",
|
|
inv_file,
|
|
"-p",
|
|
pw_file,
|
|
"-vv",
|
|
"--assert",
|
|
"true",
|
|
"--diff",
|
|
"--id",
|
|
*deploy_ids,
|
|
]
|
|
if debug:
|
|
cmd.insert(cmd.index("--diff") + 1, "--debug")
|
|
|
|
# Ansible extra-vars (`-e key=value`) for caller-supplied runtime toggles
|
|
# (typically the async-pass override). JSON-encoding the value preserves
|
|
# bool/int/list semantics across the shell hop into Ansible.
|
|
if extra_ansible_vars:
|
|
for key, value in extra_ansible_vars.items():
|
|
cmd.extend(["-e", f"{key}={json.dumps(value)}"])
|
|
|
|
if passthrough:
|
|
cmd.extend(passthrough)
|
|
|
|
extra_env: dict[str, str] = {
|
|
# Force ANSI colors even when no TTY is allocated (CI default).
|
|
"ANSIBLE_FORCE_COLOR": "1",
|
|
"PY_COLORS": "1",
|
|
"TERM": "xterm-256color",
|
|
}
|
|
services_disabled = os.environ.get("SERVICES_DISABLED", "")
|
|
if services_disabled:
|
|
extra_env["SERVICES_DISABLED"] = services_disabled
|
|
|
|
# Propagate the markers that feed MODE_CI (requirement 006) into the
|
|
# container so `ansible_facts['env']` sees them. Without this, the
|
|
# Playwright stage never runs because MODE_CI collapses to false.
|
|
# See docs/requirements/006-playwright-service-gated-tests.md.
|
|
for marker in (
|
|
"INFINITO_MAKE_DEPLOY",
|
|
"GITHUB_ACTIONS",
|
|
"ACT",
|
|
"INFINITO_SKIP_E2E",
|
|
):
|
|
value = os.environ.get(marker, "")
|
|
if value:
|
|
extra_env[marker] = value
|
|
|
|
# Live stream output for immediate visibility.
|
|
r = compose.exec(
|
|
cmd,
|
|
check=False,
|
|
live=True,
|
|
extra_env=extra_env,
|
|
)
|
|
|
|
return int(r.returncode)
|
|
|
|
|
|
def _purge_app_entities(*, container: str, app_ids: list[str]) -> None:
|
|
"""Run the per-app cleanup script for every app whose variant just
|
|
changed between matrix-deploy rounds.
|
|
|
|
`scripts/tests/deploy/local/purge/entity.sh` removes the
|
|
application's containers, networks, and Ansible-managed state on
|
|
the host so the next round boots from a clean slate. Failures are
|
|
surfaced (the matrix MUST NOT silently mix variant state across
|
|
rounds).
|
|
"""
|
|
if not app_ids:
|
|
return
|
|
repo_root = repo_root_from_here()
|
|
purge_script = (
|
|
repo_root / "scripts" / "tests" / "deploy" / "local" / "purge" / "entity.sh"
|
|
)
|
|
env = os.environ.copy()
|
|
env["APPS"] = ",".join(app_ids)
|
|
env["INFINITO_CONTAINER"] = container
|
|
print(
|
|
"=== matrix-deploy: purging entities between rounds for "
|
|
f"{', '.join(app_ids)} ==="
|
|
)
|
|
subprocess.run(
|
|
["bash", str(purge_script)],
|
|
cwd=str(repo_root),
|
|
env=env,
|
|
check=True,
|
|
)
|
|
|
|
|
|
def add_parser(sub: argparse._SubParsersAction) -> None:
|
|
p = sub.add_parser(
|
|
"deploy", help="Run deploy inside the infinito container (requires inventory)."
|
|
)
|
|
p.add_argument(
|
|
"--distro",
|
|
default=os.environ.get("INFINITO_DISTRO", "arch"),
|
|
choices=["arch", "debian", "ubuntu", "fedora", "centos"],
|
|
help="Target distro (compose env INFINITO_DISTRO).",
|
|
)
|
|
|
|
p.add_argument(
|
|
"--inventory-dir",
|
|
default=os.environ.get("INVENTORY_DIR"),
|
|
required=os.environ.get("INVENTORY_DIR") is None,
|
|
help=(
|
|
"Inventory directory base (default: $INVENTORY_DIR). When the "
|
|
"primary apps declare more than one matrix-deploy variant, the "
|
|
"wrapper iterates the sibling folders `<dir>-0`, `<dir>-1`, ... "
|
|
"produced by the matching `init` step; 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(
|
|
"--id",
|
|
nargs="+",
|
|
default=None,
|
|
help="Explicit application ids (space-separated).",
|
|
)
|
|
p.add_argument(
|
|
"--debug",
|
|
action=argparse.BooleanOptionalAction,
|
|
default=False,
|
|
help="Enable/disable Ansible debug mode (default: disabled).",
|
|
)
|
|
p.add_argument(
|
|
"--variant",
|
|
type=int,
|
|
default=_env_variant(),
|
|
help=(
|
|
"Pin the matrix deploy to a single round (zero-based index), "
|
|
"skipping inter-round cleanup. Useful for redeploying one "
|
|
"specific variant without iterating the whole matrix. Defaults "
|
|
"to the VARIANT environment variable when set, otherwise full-"
|
|
"matrix mode."
|
|
),
|
|
)
|
|
p.add_argument(
|
|
"--full-cycle",
|
|
action=argparse.BooleanOptionalAction,
|
|
default=_env_full_cycle(),
|
|
help=(
|
|
"After each round's regular deploy, immediately re-run the "
|
|
"deploy with `-e ASYNC_ENABLED=true` (the async update pass). "
|
|
"Pass 1 + Pass 2 stay co-located on the SAME variant so the "
|
|
"async re-deploy always runs against the host state the "
|
|
"matching sync deploy just produced. Defaults to the "
|
|
"FULL_CYCLE environment variable (true|false) when set."
|
|
),
|
|
)
|
|
p.add_argument(
|
|
"ansible_args",
|
|
nargs=argparse.REMAINDER,
|
|
help="Passthrough args appended to `cli.deploy.dedicated` (use `--` to separate).",
|
|
)
|
|
p.set_defaults(_handler=handler)
|
|
|
|
|
|
def handler(args: argparse.Namespace) -> int:
|
|
compose = make_compose(distro=args.distro)
|
|
|
|
if args.apps:
|
|
deploy_ids = resolve_deploy_ids_for_apps(compose, args.apps)
|
|
primary_app_ids = [
|
|
a.strip() for a in args.apps.replace(",", " ").split() if a.strip()
|
|
]
|
|
else:
|
|
deploy_ids = list(args.id or [])
|
|
primary_app_ids = list(deploy_ids)
|
|
|
|
# Remove any app IDs that were disabled via SERVICES_DISABLED so the deploy
|
|
# list stays consistent with the inventory created by the init step.
|
|
raw_disabled = os.environ.get("SERVICES_DISABLED", "").strip()
|
|
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())
|
|
deploy_ids = [app_id for app_id in deploy_ids if app_id not in disabled_app_ids]
|
|
primary_app_ids = [
|
|
app_id for app_id in primary_app_ids if app_id not in disabled_app_ids
|
|
]
|
|
|
|
# argparse.REMAINDER includes the leading '--' if present; drop it
|
|
passthrough = list(args.ansible_args or [])
|
|
if passthrough and passthrough[0] == "--":
|
|
passthrough = passthrough[1:]
|
|
|
|
# Matrix-deploy: the init step produced one inventory folder per round
|
|
# (`<dir>-0`, `<dir>-1`, ...; or just `<dir>` when there is a single
|
|
# round) with the per-app variant data already baked into each folder's
|
|
# `host_vars`. Here we just iterate the same plan and deploy against
|
|
# each folder. Between rounds we purge exactly the apps whose target
|
|
# variant changed; apps that stayed on variant 0 are NOT purged. The
|
|
# final round is followed by no purge so the last state remains
|
|
# available for inspection / follow-up specs.
|
|
plan = plan_dev_inventory_matrix(
|
|
roles_dir=str(compose.repo_root / "roles"),
|
|
include=primary_app_ids,
|
|
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}")
|
|
|
|
container_name = os.environ.get(
|
|
"INFINITO_CONTAINER", f"infinito_nexus_{args.distro}"
|
|
)
|
|
|
|
rc = 0
|
|
previous_round_variants: dict[str, int] = {}
|
|
for round_index, inv_dir, round_variants in plan:
|
|
# Round 0 always deploys the full include set (the baseline state
|
|
# of every primary app + its dependencies). Rounds R>0 only
|
|
# re-deploy apps that ACTUALLY have a variant N for that round
|
|
# (i.e. `round_variants[app] == round_index`); apps that fall back
|
|
# to variant 0 are left at whatever state the previous round
|
|
# produced. This avoids wasted re-deploys of apps that have no
|
|
# real variant for the current round.
|
|
if round_index == 0:
|
|
round_deploy_ids = deploy_ids
|
|
else:
|
|
round_deploy_ids = sorted(
|
|
app_id for app_id, idx in round_variants.items() if idx == round_index
|
|
)
|
|
if not round_deploy_ids:
|
|
print(
|
|
f"=== matrix-deploy: round {round_index + 1}/{len(plan)} "
|
|
f"skipped (no apps with a real variant {round_index}) ==="
|
|
)
|
|
previous_round_variants = round_variants
|
|
continue
|
|
|
|
# Cleanup applies only to apps we are about to re-deploy this
|
|
# round AND whose variant changed since the previous round.
|
|
# `previous_round_variants` only becomes truthy after the first
|
|
# iteration completes, so single-folder modes (N=1 or `--variant`
|
|
# pinned) skip the purge entirely. Apps that are not being
|
|
# re-deployed retain their state from earlier rounds, so purging
|
|
# them would leave the host in an inconsistent half-applied state.
|
|
if previous_round_variants:
|
|
changed = sorted(
|
|
app_id
|
|
for app_id in round_deploy_ids
|
|
if previous_round_variants.get(app_id, 0)
|
|
!= round_variants.get(app_id, 0)
|
|
)
|
|
if changed:
|
|
_purge_app_entities(container=container_name, app_ids=changed)
|
|
|
|
pass_label = (
|
|
f"matrix-deploy: round {round_index + 1}/{len(plan)} "
|
|
f"inv={inv_dir} variants={round_variants} apps={round_deploy_ids}"
|
|
)
|
|
print(f"=== {pass_label} PASS 1 (sync) ===")
|
|
rc = _run_deploy(
|
|
compose,
|
|
deploy_ids=round_deploy_ids,
|
|
debug=bool(args.debug),
|
|
passthrough=passthrough,
|
|
inventory_dir=inv_dir,
|
|
)
|
|
if rc != 0:
|
|
return rc
|
|
|
|
# When `--full-cycle` is set, the async update pass runs IMMEDIATELY
|
|
# against the same variant's host state, before we move to the next
|
|
# round. This co-locates PASS 1 and PASS 2 per variant so the async
|
|
# re-deploy never accidentally targets a host that was left in a
|
|
# different variant by a prior round.
|
|
if bool(args.full_cycle):
|
|
print(f"=== {pass_label} PASS 2 (async) ===")
|
|
rc = _run_deploy(
|
|
compose,
|
|
deploy_ids=round_deploy_ids,
|
|
debug=bool(args.debug),
|
|
passthrough=passthrough,
|
|
inventory_dir=inv_dir,
|
|
extra_ansible_vars={"ASYNC_ENABLED": True},
|
|
)
|
|
if rc != 0:
|
|
return rc
|
|
|
|
previous_round_variants = round_variants
|
|
|
|
return rc
|