Files
computer-playbook/utils/tests/swarm/write_extras.py
T
kevinveenbirkenbachandClaude Fable 5 63328b488f fix(swarm-tests): carry DOMAIN_PRIMARY into the inventory extras
extend_inventory adds the nfs-server host without host_vars, so the
extras file is its only var source; without the cluster domain it fell
back to the group_vars default and rendered unresolvable
*.infinito.localhost endpoints.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-04 17:05:57 +02:00

95 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Runtime-only extras YAML for the swarm-NFS test pipeline.
Companion to the static ``inventories/development/swarm.yml``; both
are loaded by the deploy step. Generates an ed25519 keypair at
``KEY_PATH`` if missing so the public half can land in
``users.administrator.authorized_keys``.
Inputs (env): ``NFS_IP``, ``MGR_IP``, ``MGR``, ``OUT_PATH`` (default
``/tmp/swarm-nfs-extras.yml``), ``KEY_PATH`` (default
``/tmp/swarm-nfs-admin.key``).
"""
from __future__ import annotations
import copy
import os
import subprocess
import sys
from pathlib import Path
from cli.meta.runtime import detect_runtime
from utils import PROJECT_ROOT
from utils.cache.yaml import dump_yaml, load_yaml
from utils.env.parser import parse_static_env
_DEFAULT_INVENTORY = PROJECT_ROOT / "inventories" / "development" / "default.yml"
def _ensure_keypair(key_path: Path) -> str:
if not key_path.is_file():
subprocess.run(
[
"ssh-keygen",
"-t",
"ed25519",
"-N",
"",
"-f",
str(key_path),
"-C",
"swarm-test",
"-q",
],
check=True,
)
pub = Path(f"{key_path}.pub").read_text(encoding="utf-8") # nocheck: cache-read
return pub.strip()
def main() -> int:
nfs_ip = os.environ["NFS_IP"]
mgr_ip = os.environ["MGR_IP"]
mgr = os.environ["MGR"]
out_path = Path(os.environ.get("OUT_PATH", "/tmp/swarm-nfs-extras.yml")) # noqa: S108 - ephemeral swarm-test path, overridable via OUT_PATH
key_path = Path(os.environ.get("KEY_PATH", "/tmp/swarm-nfs-admin.key")) # noqa: S108 - ephemeral swarm-test path, overridable via KEY_PATH
admin_pubkey = _ensure_keypair(key_path)
default_users = copy.deepcopy(load_yaml(str(_DEFAULT_INVENTORY)).get("users", {}))
admin = dict(default_users.get("administrator", {}))
admin["authorized_keys"] = [admin_pubkey]
default_users["administrator"] = admin
static_env = parse_static_env(PROJECT_ROOT / "default.env")
extras = {
"RUNTIME": detect_runtime(),
"DOMAIN_PRIMARY": os.environ.get("INFINITO_DOMAIN")
or static_env["INFINITO_DOMAIN"],
"storage": {
"backend": "nfs",
"nfs": {
"server": nfs_ip,
"export_base": static_env["INFINITO_SWARM_NFS_EXPORT_BASE"],
"version": 4,
},
},
"swarm": {
"manager": {"advertise_addr": mgr_ip},
"registry": {"host": mgr, "port": 5000},
"network": {"encryption": True},
},
"nfs_server_ip": nfs_ip,
"users": default_users,
}
dump_yaml(str(out_path), extras)
print(out_path.read_text()) # nocheck: cache-read — re-reads the file just written
return 0
if __name__ == "__main__":
sys.exit(main())