hw-configure: use hostname specified in the nixos configuration

This commit is contained in:
Jörg Thalheim
2024-07-22 08:21:43 +02:00
parent fabfba77cf
commit 3bbfe7fad2
2 changed files with 25 additions and 12 deletions

View File

@@ -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: Generate the `hardware-configuration.nix` file for your machine by executing the following command:
```bash ```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. `<IP>` 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. `<IP>`
!!! Example !!! Example
```bash ```bash
clan machines hw-generate jon <IP> clan machines hw-generate jon
``` ```
This command connects to `<IP>` 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 ### Step 3: Custom Disk Formatting

View File

@@ -5,10 +5,12 @@ import logging
from pathlib import Path from pathlib import Path
from clan_cli.api import API from clan_cli.api import API
from clan_cli.clan_uri import FlakeId
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
from ..cmd import run, run_no_stdout from ..cmd import run, run_no_stdout
from ..completions import add_dynamic_completer, complete_machines from ..completions import add_dynamic_completer, complete_machines
from ..machines.machines import Machine
from ..nix import nix_config, nix_eval, nix_shell from ..nix import nix_config, nix_eval, nix_shell
from .types import machine_name_type from .types import machine_name_type
@@ -88,9 +90,9 @@ def show_machine_hardware_platform(
@API.register @API.register
def generate_machine_hardware_info( def generate_machine_hardware_info(
clan_dir: str | Path, clan_dir: FlakeId,
machine_name: str, machine_name: str,
hostname: str, hostname: str | None = None,
password: str | None = None, password: str | None = None,
force: bool | None = False, force: bool | None = False,
) -> HardwareInfo: ) -> HardwareInfo:
@@ -98,6 +100,13 @@ def generate_machine_hardware_info(
Generate hardware information for a machine Generate hardware information for a machine
and place the resulting *.nix file in the machine's directory. 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( cmd = nix_shell(
[ [
"nixpkgs#openssh", "nixpkgs#openssh",
@@ -109,10 +118,14 @@ def generate_machine_hardware_info(
*(["sshpass", "-p", f"{password}"] if password else []), *(["sshpass", "-p", f"{password}"] if password else []),
"ssh", "ssh",
# Disable strict host key checking # Disable strict host key checking
"-o StrictHostKeyChecking=no", "-o",
"StrictHostKeyChecking=no",
# Disable known hosts file # Disable known hosts file
"-o UserKnownHostsFile=/dev/null", "-o",
f"root@{hostname}", "UserKnownHostsFile=/dev/null",
"-p",
str(machine.target_host.port),
target_host,
"nixos-generate-config", "nixos-generate-config",
# Filesystems are managed by disko # Filesystems are managed by disko
"--no-filesystems", "--no-filesystems",
@@ -147,9 +160,8 @@ def generate_machine_hardware_info(
def hw_generate_command(args: argparse.Namespace) -> None: def hw_generate_command(args: argparse.Namespace) -> None:
flake_path = args.flake.path
hw_info = generate_machine_hardware_info( 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("----")
print("Successfully generated hardware information.") print("Successfully generated hardware information.")
@@ -166,9 +178,10 @@ def register_hw_generate(parser: argparse.ArgumentParser) -> None:
type=machine_name_type, type=machine_name_type,
) )
machine_parser = parser.add_argument( machine_parser = parser.add_argument(
"hostname", "target_host",
help="hostname of the machine",
type=str, type=str,
nargs="?",
help="ssh address to install to in the form of user@host:2222",
) )
machine_parser = parser.add_argument( machine_parser = parser.add_argument(
"--password", "--password",