From d689dd8f6c604e9e45d05d9745ccedd9b0997700 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 10 Nov 2024 16:03:53 +0100 Subject: [PATCH 1/2] pkgs/cli: Query `target-host` for `machines install` from configuration Query `target-host` for `clan machines install` from the nixos configuration (deployment.json), if possible. Remove `TARGET_HOST` option and introduce `--target-host` command line flag. This brings the installation subcommand in line with the update subcommand - improving consistency and usability. Closes: #2309 --- checks/installation/flake-module.nix | 2 +- pkgs/clan-cli/clan_cli/__init__.py | 6 ++++-- pkgs/clan-cli/clan_cli/machines/cli.py | 6 ++++-- pkgs/clan-cli/clan_cli/machines/install.py | 24 ++++++++++++---------- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/checks/installation/flake-module.nix b/checks/installation/flake-module.nix index f2bc20dee..89b5074d1 100644 --- a/checks/installation/flake-module.nix +++ b/checks/installation/flake-module.nix @@ -105,7 +105,7 @@ client.succeed("test -f test-flake/machines/test-install-machine/hardware-configuration.nix") client.succeed("clan machines update-hardware-config --backend nixos-facter --flake test-flake test-install-machine root@target>&2") client.succeed("test -f test-flake/machines/test-install-machine/facter.json") - client.succeed("clan machines install --debug --flake ${../..} --yes test-install-machine root@target >&2") + client.succeed("clan machines install --debug --flake ${../..} --yes test-install-machine --target-host root@target >&2") try: target.shutdown() except BrokenPipeError: diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 42b8e1b19..0f9d6260e 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -329,8 +329,10 @@ Examples: Will update the specified machine [MACHINE], if [MACHINE] is omitted, the command will attempt to update every configured machine. - $ clan machines install [MACHINES] [TARGET_HOST] - Will install the specified machine [MACHINE], to the specified [TARGET_HOST]. + $ clan machines install [MACHINE] --target-host [TARGET_HOST] + Will install the specified machine [MACHINE] to the specified [TARGET_HOST]. + If the `--target-host` flag is omitted will try to find host information by + checking the deployment configuration inside the specified machine. For more detailed information, visit: {help_hyperlink("deploy", "https://docs.clan.lol/deploy")} """ diff --git a/pkgs/clan-cli/clan_cli/machines/cli.py b/pkgs/clan-cli/clan_cli/machines/cli.py index d4d38cd09..812a27969 100644 --- a/pkgs/clan-cli/clan_cli/machines/cli.py +++ b/pkgs/clan-cli/clan_cli/machines/cli.py @@ -99,8 +99,10 @@ This subcommand provides an interface to install machines managed by clan. Examples: - $ clan machines install [MACHINE] [TARGET_HOST] - Will install the specified machine [MACHINE], to the specified [TARGET_HOST]. + $ clan machines install [MACHINE] --target-host [TARGET_HOST] + Will install the specified machine [MACHINE] to the specified [TARGET_HOST]. + If the `--target-host` flag is omitted will try to find host information by + checking the deployment configuration inside the specified machine. $ clan machines install [MACHINE] --json [JSON] Will install the specified machine [MACHINE] to the host exposed by diff --git a/pkgs/clan-cli/clan_cli/machines/install.py b/pkgs/clan-cli/clan_cli/machines/install.py index 0375420e8..f44dd6713 100644 --- a/pkgs/clan-cli/clan_cli/machines/install.py +++ b/pkgs/clan-cli/clan_cli/machines/install.py @@ -135,16 +135,20 @@ def install_command(args: argparse.Namespace) -> None: elif args.png: json_ssh_deploy = json.loads(qrcode_scan(args.png)) - if not json_ssh_deploy and not args.target_host: - msg = "No target host provided, please provide a target host." - raise ClanError(msg) - if json_ssh_deploy: target_host = f"root@{find_reachable_host_from_deploy_json(json_ssh_deploy)}" password = json_ssh_deploy["pass"] - else: + elif args.target_host: target_host = args.target_host password = None + else: + machine = Machine(name=args.machine, flake=args.flake, nix_options=args.option) + target_host = str(machine.target_host) + password = None + + if not target_host: + msg = "No target host provided, please provide a target host." + raise ClanError(msg) if not args.yes: ask = input(f"Install {args.machine} to {target_host}? [y/N] ") @@ -223,18 +227,16 @@ def register_install_parser(parser: argparse.ArgumentParser) -> None: ) add_dynamic_completer(machines_parser, complete_machines) - parser.add_argument( - "target_host", - type=str, - nargs="?", - help="ssh address to install to in the form of user@host:2222", - ) group = parser.add_mutually_exclusive_group(required=False) group.add_argument( "-j", "--json", help="specify the json file for ssh data (generated by starting the clan installer)", ) + group.add_argument( + "--target-host", + help="ssh address to install to in the form of user@host:2222", + ) group.add_argument( "-P", "--png", From 29c8cc212575f785fa7484e9525e984384f7c0b8 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 10 Nov 2024 18:18:20 +0100 Subject: [PATCH 2/2] docs: Update documentation for `clan machines install --target-host` --- docs/site/getting-started/deploy.md | 4 ++-- docs/site/getting-started/disk-encryption.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/site/getting-started/deploy.md b/docs/site/getting-started/deploy.md index 81f7495ca..8b2ebb99e 100644 --- a/docs/site/getting-started/deploy.md +++ b/docs/site/getting-started/deploy.md @@ -112,7 +112,7 @@ This process involves preparing a suitable hardware and disk partitioning config 1. **SSH with Password Authentication** Run the following command to install using SSH: ```bash - clan machines install [MACHINE] + clan machines install [MACHINE] --target-host ``` 2. **Scanning a QR Code for Installation Details** @@ -133,7 +133,7 @@ This process involves preparing a suitable hardware and disk partitioning config Replace `` with the **target computers' ip address**: ```bash - clan machines install [MACHINE] + clan machines install [MACHINE] --target-host ``` diff --git a/docs/site/getting-started/disk-encryption.md b/docs/site/getting-started/disk-encryption.md index 8fd81a630..d6367b4ac 100644 --- a/docs/site/getting-started/disk-encryption.md +++ b/docs/site/getting-started/disk-encryption.md @@ -96,7 +96,7 @@ blkdiscard /dev/disk/by-id/nvme-eui.002538b931b59865 4. Run the `clan` machine installation with the following command: ```bash -clan machines install gchq-local root@nixos-installer --yes --no-reboot +clan machines install gchq-local --target-host root@nixos-installer --yes --no-reboot ``` ### Step 2: ZFS Pool Import and System Installation