From 3bbfe7fad2986ae9866e8ea76f5184b54083f3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 22 Jul 2024 08:21:43 +0200 Subject: [PATCH] hw-configure: use hostname specified in the nixos configuration --- docs/site/getting-started/configure.md | 6 ++-- pkgs/clan-cli/clan_cli/machines/hardware.py | 31 +++++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/docs/site/getting-started/configure.md b/docs/site/getting-started/configure.md index 8c91e987f..1341d8dde 100644 --- a/docs/site/getting-started/configure.md +++ b/docs/site/getting-started/configure.md @@ -144,17 +144,17 @@ These steps will allow you to update your machine later. Generate the `hardware-configuration.nix` file for your machine by executing the following command: ```bash -clan machines hw-generate [MACHINE_NAME] [HOSTNAME] +clan machines hw-generate [MACHINE_NAME] ``` replace `[MACHINE_NAME]` with the name of the machine i.e. `jon` and `[HOSTNAME]` with the `ip_adress` or `hostname` of the machine within the network. i.e. `` !!! Example ```bash - clan machines hw-generate jon + clan machines hw-generate jon ``` - This command connects to `` as `root`, runs `nixos-generate-config` to detect hardware configurations (excluding filesystems), and writes them to `machines/jon/hardware-configuration.nix`. + This command connects to the ip configured in the previous step, runs `nixos-generate-config` to detect hardware configurations (excluding filesystems), and writes them to `machines/jon/hardware-configuration.nix`. ### Step 3: Custom Disk Formatting diff --git a/pkgs/clan-cli/clan_cli/machines/hardware.py b/pkgs/clan-cli/clan_cli/machines/hardware.py index 23dec3016..4d9fe3393 100644 --- a/pkgs/clan-cli/clan_cli/machines/hardware.py +++ b/pkgs/clan-cli/clan_cli/machines/hardware.py @@ -5,10 +5,12 @@ import logging from pathlib import Path from clan_cli.api import API +from clan_cli.clan_uri import FlakeId from clan_cli.errors import ClanError from ..cmd import run, run_no_stdout from ..completions import add_dynamic_completer, complete_machines +from ..machines.machines import Machine from ..nix import nix_config, nix_eval, nix_shell from .types import machine_name_type @@ -88,9 +90,9 @@ def show_machine_hardware_platform( @API.register def generate_machine_hardware_info( - clan_dir: str | Path, + clan_dir: FlakeId, machine_name: str, - hostname: str, + hostname: str | None = None, password: str | None = None, force: bool | None = False, ) -> HardwareInfo: @@ -98,6 +100,13 @@ def generate_machine_hardware_info( Generate hardware information for a machine and place the resulting *.nix file in the machine's directory. """ + + machine = Machine(machine_name, flake=clan_dir) + if hostname is not None: + machine.target_host_address = hostname + + host = machine.target_host + target_host = f"{host.user or 'root'}@{host.host}" cmd = nix_shell( [ "nixpkgs#openssh", @@ -109,10 +118,14 @@ def generate_machine_hardware_info( *(["sshpass", "-p", f"{password}"] if password else []), "ssh", # Disable strict host key checking - "-o StrictHostKeyChecking=no", + "-o", + "StrictHostKeyChecking=no", # Disable known hosts file - "-o UserKnownHostsFile=/dev/null", - f"root@{hostname}", + "-o", + "UserKnownHostsFile=/dev/null", + "-p", + str(machine.target_host.port), + target_host, "nixos-generate-config", # Filesystems are managed by disko "--no-filesystems", @@ -147,9 +160,8 @@ def generate_machine_hardware_info( def hw_generate_command(args: argparse.Namespace) -> None: - flake_path = args.flake.path hw_info = generate_machine_hardware_info( - flake_path, args.machine, args.hostname, args.password, args.force + args.flake, args.machine, args.hostname, args.password, args.force ) print("----") print("Successfully generated hardware information.") @@ -166,9 +178,10 @@ def register_hw_generate(parser: argparse.ArgumentParser) -> None: type=machine_name_type, ) machine_parser = parser.add_argument( - "hostname", - help="hostname of the machine", + "target_host", type=str, + nargs="?", + help="ssh address to install to in the form of user@host:2222", ) machine_parser = parser.add_argument( "--password",