refactor: decouple vars stores from machine instances

Stores now get machine context from generator objects instead of storing
it internally. This enables future machine-independent generators and
reduces coupling.

- StoreBase.__init__ only takes flake parameter
- Store methods receive machine as explicit parameter
- Fixed all callers to pass machine context
This commit is contained in:
DavHau
2025-07-08 18:11:48 +07:00
parent 21b3a5f366
commit 0aa6288edb
16 changed files with 350 additions and 252 deletions

View File

@@ -58,8 +58,8 @@ def delete_machine(machine: Machine) -> None:
changed_paths.append(secret_path)
shutil.rmtree(secret_path)
changed_paths.extend(machine.public_vars_store.delete_store())
changed_paths.extend(machine.secret_vars_store.delete_store())
changed_paths.extend(machine.public_vars_store.delete_store(machine.name))
changed_paths.extend(machine.secret_vars_store.delete_store(machine.name))
# Remove the machine's key, and update secrets & vars that referenced it:
if secrets_has_machine(machine.flake.path, machine.name):
secrets_machine_remove(machine.flake.path, machine.name)

View File

@@ -66,13 +66,13 @@ def run_machine_install(opts: InstallOptions, target_host: Remote) -> None:
upload_dir.mkdir(parents=True)
machine.secret_facts_store.upload(upload_dir)
machine.secret_vars_store.populate_dir(
upload_dir, phases=["activation", "users", "services"]
machine.name, upload_dir, phases=["activation", "users", "services"]
)
partitioning_secrets = base_directory / "partitioning_secrets"
partitioning_secrets.mkdir(parents=True)
machine.secret_vars_store.populate_dir(
partitioning_secrets, phases=["partitioning"]
machine.name, partitioning_secrets, phases=["partitioning"]
)
if opts.password:

View File

@@ -104,13 +104,13 @@ class Machine:
def secret_vars_store(self) -> StoreBase:
secret_module = self.select("config.clan.core.vars.settings.secretModule")
module = importlib.import_module(secret_module)
return module.SecretStore(machine=self.name, flake=self.flake)
return module.SecretStore(flake=self.flake)
@cached_property
def public_vars_store(self) -> StoreBase:
public_module = self.select("config.clan.core.vars.settings.publicModule")
module = importlib.import_module(public_module)
return module.FactStore(machine=self.name, flake=self.flake)
return module.FactStore(flake=self.flake)
@property
def facts_data(self) -> dict[str, dict[str, Any]]:

View File

@@ -81,7 +81,9 @@ def morph_machine(
generate_vars([machine], generator_name=None, regenerate=False)
machine.secret_vars_store.populate_dir(
output_dir=Path("/run/secrets"), phases=["activation", "users", "services"]
machine.name,
output_dir=Path("/run/secrets"),
phases=["activation", "users", "services"],
)
# run(["nixos-facter", "-o", f"{flakedir}/machines/{name}/facter.json"]).stdout