Merge pull request 'clan-lib: machines.py: Remove host_key attribute' (#4034) from Qubasa/clan-core:minimize_machine_obj into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4034
This commit is contained in:
Luis Hebendanz
2025-06-19 16:14:49 +00:00
9 changed files with 22 additions and 54 deletions

View File

@@ -159,11 +159,7 @@ def generate_machine_hardware_info(
def update_hardware_config_command(args: argparse.Namespace) -> None: def update_hardware_config_command(args: argparse.Namespace) -> None:
host_key_check = HostKeyCheck.from_str(args.host_key_check) host_key_check = HostKeyCheck.from_str(args.host_key_check)
machine = Machine( machine = Machine(flake=args.flake, name=args.machine)
flake=args.flake,
name=args.machine,
host_key_check=host_key_check,
)
opts = HardwareGenerateOptions( opts = HardwareGenerateOptions(
machine=machine, machine=machine,
password=args.password, password=args.password,
@@ -172,12 +168,10 @@ def update_hardware_config_command(args: argparse.Namespace) -> None:
if args.target_host: if args.target_host:
target_host = Remote.from_deployment_address( target_host = Remote.from_deployment_address(
machine_name=machine.name, machine_name=machine.name, address=args.target_host
address=args.target_host, ).override(host_key_check=host_key_check)
host_key_check=host_key_check,
)
else: else:
target_host = machine.target_host() target_host = machine.target_host().override(host_key_check=host_key_check)
generate_machine_hardware_info(opts, target_host) generate_machine_hardware_info(opts, target_host)

View File

@@ -189,10 +189,8 @@ def install_command(args: argparse.Namespace) -> None:
) )
if target_host_str is not None: if target_host_str is not None:
target_host = Remote.from_deployment_address( target_host = Remote.from_deployment_address(
machine_name=machine.name, machine_name=machine.name, address=target_host_str
address=target_host_str, ).override(host_key_check=host_key_check)
host_key_check=host_key_check,
)
else: else:
target_host = machine.target_host().override(host_key_check=host_key_check) target_host = machine.target_host().override(host_key_check=host_key_check)

View File

@@ -220,10 +220,7 @@ def update_command(args: argparse.Namespace) -> None:
for machine_name in selected_machines: for machine_name in selected_machines:
machine = Machine( machine = Machine(
name=machine_name, name=machine_name, flake=args.flake, nix_options=args.option
flake=args.flake,
nix_options=args.option,
host_key_check=HostKeyCheck.from_str(args.host_key_check),
) )
machines.append(machine) machines.append(machine)
@@ -281,8 +278,7 @@ def update_command(args: argparse.Namespace) -> None:
target_host = Remote.from_deployment_address( target_host = Remote.from_deployment_address(
machine_name=machine.name, machine_name=machine.name,
address=args.target_host, address=args.target_host,
host_key_check=host_key_check, ).override(host_key_check=host_key_check)
)
else: else:
target_host = machine.target_host() target_host = machine.target_host()
runtime.async_run( runtime.async_run(

View File

@@ -47,10 +47,8 @@ class DeployInfo:
msg = "Hostname cannot be empty." msg = "Hostname cannot be empty."
raise ClanError(msg) raise ClanError(msg)
remote = Remote.from_deployment_address( remote = Remote.from_deployment_address(
machine_name="clan-installer", machine_name="clan-installer", address=host
address=host, ).override(host_key_check=host_key_check)
host_key_check=host_key_check,
)
remotes.append(remote) remotes.append(remote)
return DeployInfo(addrs=remotes) return DeployInfo(addrs=remotes)
@@ -64,9 +62,8 @@ class DeployInfo:
remote = Remote.from_deployment_address( remote = Remote.from_deployment_address(
machine_name="clan-installer", machine_name="clan-installer",
address=addr, address=addr,
host_key_check=host_key_check,
password=password, password=password,
) ).override(host_key_check=host_key_check)
addrs.append(remote) addrs.append(remote)
else: else:
msg = f"Invalid address format: {addr}" msg = f"Invalid address format: {addr}"
@@ -75,10 +72,9 @@ class DeployInfo:
remote = Remote.from_deployment_address( remote = Remote.from_deployment_address(
machine_name="clan-installer", machine_name="clan-installer",
address=tor_addr, address=tor_addr,
host_key_check=host_key_check,
password=password, password=password,
tor_socks=True, tor_socks=True,
) ).override(host_key_check=host_key_check)
addrs.append(remote) addrs.append(remote)
return DeployInfo(addrs=addrs) return DeployInfo(addrs=addrs)

View File

@@ -9,7 +9,6 @@ from typing import TYPE_CHECKING, Any, Literal
from clan_cli.facts import public_modules as facts_public_modules from clan_cli.facts import public_modules as facts_public_modules
from clan_cli.facts import secret_modules as facts_secret_modules from clan_cli.facts import secret_modules as facts_secret_modules
from clan_cli.ssh.host_key import HostKeyCheck
from clan_cli.vars._types import StoreBase from clan_cli.vars._types import StoreBase
from clan_lib.api import API from clan_lib.api import API
@@ -33,8 +32,6 @@ class Machine:
nix_options: list[str] = field(default_factory=list) nix_options: list[str] = field(default_factory=list)
host_key_check: HostKeyCheck = HostKeyCheck.STRICT
def get_inv_machine(self) -> "InventoryMachine": def get_inv_machine(self) -> "InventoryMachine":
return get_machine(self.flake, self.name) return get_machine(self.flake, self.name)
@@ -149,9 +146,7 @@ class Machine:
description="See https://docs.clan.lol/guides/getting-started/deploy/#setting-the-target-host for more information.", description="See https://docs.clan.lol/guides/getting-started/deploy/#setting-the-target-host for more information.",
) )
data = remote.data data = remote.data
return data.override( return data
host_key_check=self.host_key_check,
)
def build_host(self) -> Remote | None: def build_host(self) -> Remote | None:
""" """
@@ -162,9 +157,7 @@ class Machine:
if remote: if remote:
data = remote.data data = remote.data
return data.override( return data
host_key_check=self.host_key_check,
)
return None return None
@@ -266,9 +259,7 @@ def get_host(
return RemoteSource( return RemoteSource(
data=Remote.from_deployment_address( data=Remote.from_deployment_address(
machine_name=machine.name, machine_name=machine.name, address=host_str
address=host_str,
host_key_check=machine.host_key_check,
), ),
source=source, source=source,
) )

View File

@@ -3,8 +3,6 @@ import urllib.parse
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from clan_cli.ssh.host_key import HostKeyCheck
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -15,7 +13,6 @@ def parse_deployment_address(
*, *,
machine_name: str, machine_name: str,
address: str, address: str,
host_key_check: HostKeyCheck,
forward_agent: bool = True, forward_agent: bool = True,
meta: dict[str, Any] | None = None, meta: dict[str, Any] | None = None,
private_key: Path | None = None, private_key: Path | None = None,
@@ -73,7 +70,6 @@ def parse_deployment_address(
port=port, port=port,
private_key=private_key, private_key=private_key,
password=password, password=password,
host_key_check=host_key_check,
command_prefix=machine_name, command_prefix=machine_name,
forward_agent=forward_agent, forward_agent=forward_agent,
ssh_options=options, ssh_options=options,

View File

@@ -91,7 +91,6 @@ class Remote:
*, *,
machine_name: str, machine_name: str,
address: str, address: str,
host_key_check: HostKeyCheck,
forward_agent: bool = True, forward_agent: bool = True,
private_key: Path | None = None, private_key: Path | None = None,
password: str | None = None, password: str | None = None,
@@ -104,7 +103,6 @@ class Remote:
return parse_deployment_address( return parse_deployment_address(
machine_name=machine_name, machine_name=machine_name,
address=address, address=address,
host_key_check=host_key_check,
forward_agent=forward_agent, forward_agent=forward_agent,
private_key=private_key, private_key=private_key,
password=password, password=password,

View File

@@ -114,8 +114,7 @@ def test_parse_deployment_address(
result = Remote.from_deployment_address( result = Remote.from_deployment_address(
machine_name=machine_name, machine_name=machine_name,
address=test_addr, address=test_addr,
host_key_check=HostKeyCheck.STRICT, ).override(host_key_check=HostKeyCheck.STRICT)
)
if expected_exception: if expected_exception:
return return
@@ -132,8 +131,8 @@ def test_parse_deployment_address(
def test_parse_ssh_options() -> None: def test_parse_ssh_options() -> None:
addr = "root@example.com:2222?IdentityFile=/path/to/private/key&StrictRemoteKeyChecking=yes" addr = "root@example.com:2222?IdentityFile=/path/to/private/key&StrictRemoteKeyChecking=yes"
host = Remote.from_deployment_address( host = Remote.from_deployment_address(machine_name="foo", address=addr).override(
machine_name="foo", address=addr, host_key_check=HostKeyCheck.STRICT host_key_check=HostKeyCheck.STRICT
) )
assert host.address == "example.com" assert host.address == "example.com"
assert host.port == 2222 assert host.port == 2222

View File

@@ -191,16 +191,16 @@ def test_clan_create_api(
clan_dir_flake, inv_machine, target_host=f"{host.target}:{ssh_port_var}" clan_dir_flake, inv_machine, target_host=f"{host.target}:{ssh_port_var}"
) )
) )
machine = Machine( machine = Machine(name=vm_name, flake=clan_dir_flake)
name=vm_name, flake=clan_dir_flake, host_key_check=HostKeyCheck.NONE
)
machines.append(machine) machines.append(machine)
assert len(machines) == 1 assert len(machines) == 1
# Invalidate cache because of new machine creation # Invalidate cache because of new machine creation
clan_dir_flake.invalidate_cache() clan_dir_flake.invalidate_cache()
target_host = machine.target_host().override(private_key=private_key) target_host = machine.target_host().override(
private_key=private_key, host_key_check=HostKeyCheck.NONE
)
result = check_machine_online(target_host) result = check_machine_online(target_host)
assert result == "Online", f"Machine {machine.name} is not online" assert result == "Online", f"Machine {machine.name} is not online"