feat(scripts/update-vars): expose all configurables as arguments {repo_root, test_dir, check_attr}

This commit is contained in:
Johannes Kirschbauer
2025-04-15 22:36:43 +02:00
parent f1856fbaa3
commit 23270ff0ce

View File

@@ -13,29 +13,19 @@ from clan_cli.machines.machines import Machine
from clan_cli.nix import nix_build, nix_config, nix_eval from clan_cli.nix import nix_build, nix_config, nix_eval
from clan_cli.vars.generate import generate_vars from clan_cli.vars.generate import generate_vars
if _project_root := os.environ.get("PRJ_ROOT"):
clan_core_dir = Path(_project_root)
else:
msg = "PRJ_ROOT not set. Enter the dev environment first"
raise Exception(msg) # noqa TRY002
sops_priv_key = ( sops_priv_key = (
"AGE-SECRET-KEY-1PL0M9CWRCG3PZ9DXRTTLMCVD57U6JDFE8K7DNVQ35F4JENZ6G3MQ0RQLRV" "AGE-SECRET-KEY-1PL0M9CWRCG3PZ9DXRTTLMCVD57U6JDFE8K7DNVQ35F4JENZ6G3MQ0RQLRV"
) )
sops_pub_key = "age1qm0p4vf9jvcnn43s6l4prk8zn6cx0ep9gzvevxecv729xz540v8qa742eg" sops_pub_key = "age1qm0p4vf9jvcnn43s6l4prk8zn6cx0ep9gzvevxecv729xz540v8qa742eg"
def _test_dir(test_name: str) -> Path: def machine_names(repo_root: Path, check_attr: str) -> list[str]:
return Path(clan_core_dir / "checks" / test_name)
def machine_names(test_name: str) -> list[str]:
""" """
Get the machine names from the test flake Get the machine names from the test flake
""" """
cmd = nix_eval( cmd = nix_eval(
[ [
f"{clan_core_dir}#checks.{nix_config()['system']}.{test_name}.nodes", f"{repo_root}#checks.{nix_config()['system']}.{check_attr}.nodes",
"--apply", "--apply",
"builtins.attrNames", "builtins.attrNames",
] ]
@@ -51,9 +41,9 @@ class TestMachine(Machine):
clan-core#checks.<system>.<test_name>.nodes.<machine_name>.<attr> clan-core#checks.<system>.<test_name>.nodes.<machine_name>.<attr>
""" """
def __init__(self, name: str, flake: Flake, test_name: str) -> None: def __init__(self, name: str, flake: Flake, check_attr: str) -> None:
super().__init__(name, flake) super().__init__(name, flake)
self.test_name = test_name self.check_attr = check_attr
@property @property
def deployment(self) -> dict: def deployment(self) -> dict:
@@ -61,7 +51,7 @@ class TestMachine(Machine):
return self._deployment return self._deployment
cmd = nix_build( cmd = nix_build(
[ [
f"{clan_core_dir}#checks.{nix_config()['system']}.{self.test_name}.nodes.{self.name}.system.clan.deployment.file" f"{self.flake.path}#checks.{nix_config()['system']}.{self.check_attr}.nodes.{self.name}.system.clan.deployment.file"
] ]
) )
out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE) out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
@@ -86,7 +76,7 @@ class TestMachine(Machine):
# return self.nix("eval", attr, nix_options) # return self.nix("eval", attr, nix_options)
cmd = nix_eval( cmd = nix_eval(
[ [
f"{clan_core_dir}#checks.{nix_config()['system']}.{self.test_name}.nodes.{self.name}.{attr}" f"{self.flake.path}#checks.{nix_config()['system']}.{self.check_attr}.nodes.{self.name}.{attr}"
] ]
) )
out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE) out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
@@ -108,7 +98,7 @@ class TestMachine(Machine):
cmd = nix_build( cmd = nix_build(
[ [
f"{clan_core_dir}#checks.{nix_config()['system']}.{self.test_name}.nodes.{self.name}.{attr}" f"{self.flake.path}#checks.{nix_config()['system']}.{self.check_attr}.nodes.{self.name}.{attr}"
] ]
) )
out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE) out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
@@ -125,12 +115,34 @@ def parse_args() -> argparse.Namespace:
import argparse import argparse
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Update the vars of an inventory test", description="""
Update the vars of a 'makeTestClan' integration test.
See 'clanLib.test.makeTestClan' for more information on how to create such a test.
""",
) )
parser.add_argument( parser.add_argument(
"test_name", "--repo_root",
type=Path,
help="""
Should be an absolute path to the repo root.
This path is used as root to evaluate and build attributes using the nix commands.
i.e. 'nix eval <repo_root>#checks ...'
""",
required=False,
default=os.environ.get("PRJ_ROOT"),
)
parser.add_argument(
"test_dir",
type=Path,
help="""
The folder of the test. Usually passed as 'directory' to clan in the test.
Must be relative to the repo_root.
""",
)
parser.add_argument(
"check_attr",
type=str, type=str,
help="The name of the test to update", help="The attribute name of the flake#checks to update",
) )
return parser.parse_args() return parser.parse_args()
@@ -138,14 +150,17 @@ def parse_args() -> argparse.Namespace:
if __name__ == "__main__": if __name__ == "__main__":
os.environ["CLAN_NO_COMMIT"] = "1" os.environ["CLAN_NO_COMMIT"] = "1"
args = parse_args() args = parse_args()
test_dir = _test_dir(args.test_name) test_dir = args.repo_root / args.test_dir
subprocess.run(["rm", "-rf", f"{test_dir}/vars", f"{test_dir}/sops"]) subprocess.run(["rm", "-rf", f"{test_dir}/vars", f"{test_dir}/sops"])
flake = Flake(str(test_dir)) flake = Flake(str(test_dir))
flake._path = test_dir # noqa SLF001 flake._path = test_dir # noqa SLF001
flake._is_local = True # noqa SLF001 flake._is_local = True # noqa SLF001
machines = [ machines = [
TestMachine(name, flake, args.test_name) TestMachine(name, flake, args.check_attr)
for name in machine_names(args.test_name) for name in machine_names(
args.repo_root,
args.check_attr,
)
] ]
user = "admin" user = "admin"
admin_key_path = Path(flake.path / "sops" / "users" / user / "key.json") admin_key_path = Path(flake.path / "sops" / "users" / user / "key.json")