clan-cli: Fix bug where --target_host is getting ignored

This commit is contained in:
Qubasa
2024-10-05 18:50:40 +02:00
parent 9530d6aee7
commit 5967bb347c
7 changed files with 35 additions and 15 deletions

View File

@@ -10,6 +10,6 @@ class ClanJSONEncoder(json.JSONEncoder):
return o.to_json() return o.to_json()
# Check if the object is a dataclass # Check if the object is a dataclass
if dataclasses.is_dataclass(o): if dataclasses.is_dataclass(o):
return dataclasses.asdict(o) return dataclasses.asdict(o) # type: ignore[call-overload]
# Otherwise, use the default serialization # Otherwise, use the default serialization
return super().default(o) return super().default(o)

View File

@@ -111,7 +111,7 @@ def generate_machine_hardware_info(opts: HardwareGenerateOptions) -> HardwareRep
machine = Machine(opts.machine, flake=opts.flake) machine = Machine(opts.machine, flake=opts.flake)
if opts.target_host is not None: if opts.target_host is not None:
machine.target_host_address = opts.target_host machine.override_target_host = opts.target_host
hw_file = opts.flake.path / "machines" / opts.machine hw_file = opts.flake.path / "machines" / opts.machine
if opts.backend == "nixos-generate-config": if opts.backend == "nixos-generate-config":

View File

@@ -110,7 +110,7 @@ class InstallOptions:
@API.register @API.register
def install_machine(opts: InstallOptions, password: str | None) -> None: def install_machine(opts: InstallOptions, password: str | None) -> None:
machine = Machine(opts.machine, flake=opts.flake) machine = Machine(opts.machine, flake=opts.flake)
machine.target_host_address = opts.target_host machine.override_target_host = opts.target_host
install_nixos( install_nixos(
machine, machine,

View File

@@ -12,6 +12,12 @@ class MachineGroup:
def __init__(self, machines: list[Machine]) -> None: def __init__(self, machines: list[Machine]) -> None:
self.group = HostGroup([m.target_host for m in machines]) self.group = HostGroup([m.target_host for m in machines])
def __repr__(self) -> str:
return str(self)
def __str__(self) -> str:
return f"MachineGroup({self.group})"
def run_function( def run_function(
self, func: Callable[[Machine], T], check: bool = True self, func: Callable[[Machine], T], check: bool = True
) -> list[HostResult[T]]: ) -> list[HostResult[T]]:

View File

@@ -26,6 +26,7 @@ class Machine:
flake: FlakeId flake: FlakeId
nix_options: list[str] = field(default_factory=list) nix_options: list[str] = field(default_factory=list)
cached_deployment: None | dict[str, Any] = None cached_deployment: None | dict[str, Any] = None
override_target_host: None | str = None
_eval_cache: dict[str, str] = field(default_factory=dict) _eval_cache: dict[str, str] = field(default_factory=dict)
_build_cache: dict[str, Path] = field(default_factory=dict) _build_cache: dict[str, Path] = field(default_factory=dict)
@@ -57,18 +58,16 @@ class Machine:
@property @property
def target_host_address(self) -> str: def target_host_address(self) -> str:
# deploymentAddress is deprecated. # deploymentAddress is deprecated.
val = self.deployment.get("targetHost") or self.deployment.get( val = (
"deploymentAddress" self.override_target_host
or self.deployment.get("targetHost")
or self.deployment.get("deploymentAddress")
) )
if val is None: if val is None:
msg = f"the 'clan.core.networking.targetHost' nixos option is not set for machine '{self.name}'" msg = f"the 'clan.core.networking.targetHost' nixos option is not set for machine '{self.name}'"
raise ClanError(msg) raise ClanError(msg)
return val return val
@target_host_address.setter
def target_host_address(self, value: str) -> None:
self.deployment["targetHost"] = value
@property @property
def secret_facts_module( def secret_facts_module(
self, self,

View File

@@ -98,7 +98,7 @@ def update_machines(base_path: str, machines: list[InventoryMachine]) -> None:
msg = f"'TargetHost' is not set for machine '{machine.name}'" msg = f"'TargetHost' is not set for machine '{machine.name}'"
raise ClanError(msg) raise ClanError(msg)
# Copy targetHost to machine # Copy targetHost to machine
m.target_host_address = machine.deploy.targetHost m.override_target_host = machine.deploy.targetHost
group_machines.append(m) group_machines.append(m)
deploy_machine(MachineGroup(group_machines)) deploy_machine(MachineGroup(group_machines))
@@ -118,11 +118,13 @@ def deploy_machine(machines: MachineGroup) -> None:
generate_facts([machine], None, False) generate_facts([machine], None, False)
generate_vars([machine], None, False) generate_vars([machine], None, False)
upload_secrets(machine)
upload_secrets(machine)
path = upload_sources( path = upload_sources(
str(machine.flake.path) if machine.flake.is_local() else machine.flake.url, flake_url=str(machine.flake.path)
target, if machine.flake.is_local()
else machine.flake.url,
remote_url=target,
) )
if host.host_key_check != HostKeyCheck.STRICT: if host.host_key_check != HostKeyCheck.STRICT:
ssh_arg += " -o StrictHostKeyChecking=no" ssh_arg += " -o StrictHostKeyChecking=no"
@@ -168,7 +170,7 @@ def update(args: argparse.Namespace) -> None:
machine = Machine( machine = Machine(
name=args.machines[0], flake=args.flake, nix_options=args.option name=args.machines[0], flake=args.flake, nix_options=args.option
) )
machine.target_host_address = args.target_host machine.override_target_host = args.target_host
machines.append(machine) machines.append(machine)
elif args.target_host is not None: elif args.target_host is not None:
@@ -199,7 +201,8 @@ def update(args: argparse.Namespace) -> None:
else: else:
machines = get_selected_machines(args.flake, args.option, args.machines) machines = get_selected_machines(args.flake, args.option, args.machines)
deploy_machine(MachineGroup(machines)) group = MachineGroup(machines)
deploy_machine(group)
def register_update_parser(parser: argparse.ArgumentParser) -> None: def register_update_parser(parser: argparse.ArgumentParser) -> None:

View File

@@ -176,6 +176,12 @@ class Host:
self.verbose_ssh = verbose_ssh self.verbose_ssh = verbose_ssh
self.ssh_options = ssh_options self.ssh_options = ssh_options
def __repr__(self) -> str:
return str(self)
def __str__(self) -> str:
return f"{self.user}@{self.host}" + str(self.port if self.port else "")
def _prefix_output( def _prefix_output(
self, self,
displayed_cmd: str, displayed_cmd: str,
@@ -547,6 +553,12 @@ class HostGroup:
def __init__(self, hosts: list[Host]) -> None: def __init__(self, hosts: list[Host]) -> None:
self.hosts = hosts self.hosts = hosts
def __repr__(self) -> str:
return str(self)
def __str__(self) -> str:
return f"HostGroup({self.hosts})"
def _run_local( def _run_local(
self, self,
cmd: str | list[str], cmd: str | list[str],