Merge pull request 'clan-cli: Fix bug where --target_host is getting ignored' (#2205) from Qubasa/clan-core:Qubasa-main into main

This commit is contained in:
clan-bot
2024-10-05 16:59:17 +00:00
7 changed files with 35 additions and 15 deletions

View File

@@ -10,6 +10,6 @@ class ClanJSONEncoder(json.JSONEncoder):
return o.to_json()
# Check if the object is a dataclass
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
return dataclasses.asdict(o) # type: ignore[call-overload]
# Otherwise, use the default serialization
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)
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
if opts.backend == "nixos-generate-config":

View File

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

View File

@@ -12,6 +12,12 @@ class MachineGroup:
def __init__(self, machines: list[Machine]) -> None:
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(
self, func: Callable[[Machine], T], check: bool = True
) -> list[HostResult[T]]:

View File

@@ -26,6 +26,7 @@ class Machine:
flake: FlakeId
nix_options: list[str] = field(default_factory=list)
cached_deployment: None | dict[str, Any] = None
override_target_host: None | str = None
_eval_cache: dict[str, str] = field(default_factory=dict)
_build_cache: dict[str, Path] = field(default_factory=dict)
@@ -57,18 +58,16 @@ class Machine:
@property
def target_host_address(self) -> str:
# deploymentAddress is deprecated.
val = self.deployment.get("targetHost") or self.deployment.get(
"deploymentAddress"
val = (
self.override_target_host
or self.deployment.get("targetHost")
or self.deployment.get("deploymentAddress")
)
if val is None:
msg = f"the 'clan.core.networking.targetHost' nixos option is not set for machine '{self.name}'"
raise ClanError(msg)
return val
@target_host_address.setter
def target_host_address(self, value: str) -> None:
self.deployment["targetHost"] = value
@property
def secret_facts_module(
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}'"
raise ClanError(msg)
# Copy targetHost to machine
m.target_host_address = machine.deploy.targetHost
m.override_target_host = machine.deploy.targetHost
group_machines.append(m)
deploy_machine(MachineGroup(group_machines))
@@ -118,11 +118,13 @@ def deploy_machine(machines: MachineGroup) -> None:
generate_facts([machine], None, False)
generate_vars([machine], None, False)
upload_secrets(machine)
upload_secrets(machine)
path = upload_sources(
str(machine.flake.path) if machine.flake.is_local() else machine.flake.url,
target,
flake_url=str(machine.flake.path)
if machine.flake.is_local()
else machine.flake.url,
remote_url=target,
)
if host.host_key_check != HostKeyCheck.STRICT:
ssh_arg += " -o StrictHostKeyChecking=no"
@@ -168,7 +170,7 @@ def update(args: argparse.Namespace) -> None:
machine = Machine(
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)
elif args.target_host is not None:
@@ -199,7 +201,8 @@ def update(args: argparse.Namespace) -> None:
else:
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:

View File

@@ -176,6 +176,12 @@ class Host:
self.verbose_ssh = verbose_ssh
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(
self,
displayed_cmd: str,
@@ -547,6 +553,12 @@ class HostGroup:
def __init__(self, hosts: list[Host]) -> None:
self.hosts = hosts
def __repr__(self) -> str:
return str(self)
def __str__(self) -> str:
return f"HostGroup({self.hosts})"
def _run_local(
self,
cmd: str | list[str],