Refactor StoreBase to take machine name string instead of Machine object

- Updated StoreBase.__init__ to accept machine: str and flake: Flake
- Modified all StoreBase subclasses (in_repo, vm, fs, sops, password_store) to match new signature
- Added select_machine method to Flake class for machine-specific attribute selection
- Updated Machine.select to use the new Flake.select_machine method
- Fixed all test cases to pass machine name and flake to store constructors
- Maintained backward compatibility by keeping the same external API

This reduces coupling between the store system and the Machine class,
making the architecture more modular and flexible.
This commit is contained in:
DavHau
2025-07-07 17:11:55 +07:00
parent 0cc1f072f7
commit 61edc1e06f
10 changed files with 137 additions and 132 deletions

View File

@@ -906,3 +906,20 @@ nix repl --expr 'rec {{
self.get_from_nix([selector])
value = self._cache.select(selector)
return value
def select_machine(self, machine_name: str, selector: str) -> Any:
"""
Select a nix attribute for a specific machine.
Args:
machine_name: The name of the machine
selector: The attribute selector string relative to the machine config
apply: Optional function to apply to the result
"""
from clan_lib.nix import nix_config
config = nix_config()
system = config["system"]
full_selector = f'clanInternals.machines."{system}"."{machine_name}".{selector}'
return self.select(full_selector)

View File

@@ -13,7 +13,6 @@ from clan_cli.vars._types import StoreBase
from clan_lib.api import API
from clan_lib.errors import ClanCmdError, ClanError
from clan_lib.flake import Flake
from clan_lib.nix import nix_config
from clan_lib.nix_models.clan import InventoryMachine
from clan_lib.ssh.remote import Remote
@@ -105,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)
return module.SecretStore(machine=self.name, 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)
return module.FactStore(machine=self.name, flake=self.flake)
@property
def facts_data(self) -> dict[str, dict[str, Any]]:
@@ -160,13 +159,7 @@ class Machine:
Select a nix attribute of the machine
@attr: the attribute to get
"""
config = nix_config()
system = config["system"]
return self.flake.select(
f'clanInternals.machines."{system}"."{self.name}".{attr}'
)
return self.flake.select_machine(self.name, attr)
@dataclass(frozen=True)