From b21c98db7f1bba61b04ecdc99d4c1f82ec8c987f Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 6 May 2025 17:09:37 +0200 Subject: [PATCH] Refactor(machine/install): don't mutate the machine class during installation --- pkgs/clan-cli/clan_cli/machines/install.py | 47 ++++++++++++---------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/install.py b/pkgs/clan-cli/clan_cli/machines/install.py index e05bfa767..2c7b28ac1 100644 --- a/pkgs/clan-cli/clan_cli/machines/install.py +++ b/pkgs/clan-cli/clan_cli/machines/install.py @@ -162,7 +162,32 @@ def install_machine(opts: InstallOptions) -> None: def install_command(args: argparse.Namespace) -> None: host_key_check = HostKeyCheck.from_str(args.host_key_check) try: - machine = Machine(name=args.machine, flake=args.flake, nix_options=args.option) + # Only if the caller did not specify a target_host via args.target_host + # Find a suitable target_host that is reachable + target_host = args.target_host + deploy_info: DeployInfo | None = ssh_command_parse(args) + + if deploy_info and not args.target_host: + host = find_reachable_host(deploy_info, host_key_check) + if host is None: + use_tor = True + target_host = f"root@{deploy_info.tor}" + else: + target_host = host.target + + if args.password: + password = args.password + elif deploy_info and deploy_info.pwd: + password = deploy_info.pwd + else: + password = None + + machine = Machine( + name=args.machine, + flake=args.flake, + nix_options=args.option, + override_target_host=target_host, + ) use_tor = False if machine._class_ == "darwin": @@ -173,26 +198,6 @@ def install_command(args: argparse.Namespace) -> None: msg = "Could not find clan flake toplevel directory" raise ClanError(msg) - deploy_info: DeployInfo | None = ssh_command_parse(args) - - if args.target_host: - machine.override_target_host = args.target_host - elif deploy_info: - host = find_reachable_host(deploy_info, host_key_check) - if host is None: - use_tor = True - machine.override_target_host = f"root@{deploy_info.tor}" - else: - machine.override_target_host = host.target - password = deploy_info.pwd - - if args.password: - password = args.password - elif deploy_info and deploy_info.pwd: - password = deploy_info.pwd - else: - password = None - if not args.yes: ask = input( f"Install {args.machine} to {machine.target_host_address}? [y/N] "