Merge pull request 'fix-hw-generate' (#1964) from fix-hw-generate into main

This commit is contained in:
clan-bot
2024-08-24 10:11:34 +00:00
4 changed files with 40 additions and 25 deletions

View File

@@ -10,10 +10,10 @@
let let
dependencies = [ dependencies = [
pkgs.disko pkgs.disko
self.clanInternals.machines.${pkgs.hostPlatform.system}.test_install_machine.config.system.build.toplevel self.clanInternals.machines.${pkgs.hostPlatform.system}.test-install-machine.config.system.build.toplevel
self.clanInternals.machines.${pkgs.hostPlatform.system}.test_install_machine.config.system.build.diskoScript self.clanInternals.machines.${pkgs.hostPlatform.system}.test-install-machine.config.system.build.diskoScript
self.clanInternals.machines.${pkgs.hostPlatform.system}.test_install_machine.config.system.build.diskoScript.drvPath self.clanInternals.machines.${pkgs.hostPlatform.system}.test-install-machine.config.system.build.diskoScript.drvPath
self.clanInternals.machines.${pkgs.hostPlatform.system}.test_install_machine.config.system.clan.deployment.file self.clanInternals.machines.${pkgs.hostPlatform.system}.test-install-machine.config.system.clan.deployment.file
] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs); ] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs);
closureInfo = pkgs.closureInfo { rootPaths = dependencies; }; closureInfo = pkgs.closureInfo { rootPaths = dependencies; };
in in
@@ -42,7 +42,7 @@
testScript = '' testScript = ''
start_all() start_all()
machine.succeed("clan flash --debug --flake ${../..} --yes --disk main /dev/vdb test_install_machine") machine.succeed("clan flash --debug --flake ${../..} --yes --disk main /dev/vdb test-install-machine")
''; '';
} { inherit pkgs self; }; } { inherit pkgs self; };
}; };

View File

@@ -1,14 +1,14 @@
{ self, lib, ... }: { self, lib, ... }:
{ {
clan.machines.test_install_machine = { clan.machines.test-install-machine = {
clan.core.networking.targetHost = "test_install_machine"; clan.core.networking.targetHost = "test-install-machine";
fileSystems."/".device = lib.mkDefault "/dev/vdb"; fileSystems."/".device = lib.mkDefault "/dev/vdb";
boot.loader.grub.device = lib.mkDefault "/dev/vdb"; boot.loader.grub.device = lib.mkDefault "/dev/vdb";
imports = [ self.nixosModules.test_install_machine ]; imports = [ self.nixosModules.test-install-machine ];
}; };
flake.nixosModules = { flake.nixosModules = {
test_install_machine = test-install-machine =
{ lib, modulesPath, ... }: { lib, modulesPath, ... }:
{ {
imports = [ imports = [
@@ -34,9 +34,9 @@
let let
dependencies = [ dependencies = [
self self
self.nixosConfigurations.test_install_machine.config.system.build.toplevel self.nixosConfigurations.test-install-machine.config.system.build.toplevel
self.nixosConfigurations.test_install_machine.config.system.build.diskoScript self.nixosConfigurations.test-install-machine.config.system.build.diskoScript
self.nixosConfigurations.test_install_machine.config.system.clan.deployment.file self.nixosConfigurations.test-install-machine.config.system.clan.deployment.file
pkgs.stdenv.drvPath pkgs.stdenv.drvPath
pkgs.nixos-anywhere pkgs.nixos-anywhere
] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs); ] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs);
@@ -96,9 +96,12 @@
start_all() start_all()
client.succeed("${pkgs.coreutils}/bin/install -Dm 600 ${../lib/ssh/privkey} /root/.ssh/id_ed25519") client.succeed("${pkgs.coreutils}/bin/install -Dm 600 ${../lib/ssh/privkey} /root/.ssh/id_ed25519")
client.wait_until_succeeds("ssh -o StrictHostKeyChecking=accept-new -v root@target hostname") client.wait_until_succeeds("timeout 2 ssh -o StrictHostKeyChecking=accept-new -v root@target hostname")
client.succeed("cp -r ${../..} test-flake && chmod -R +w test-flake")
client.succeed("clan machines install --debug --flake ${../..} --yes test_install_machine root@target >&2") client.fail("test -f test-flake/machines/test-install-machine/hardware-configuration.nix")
client.succeed("clan machines hw-generate --flake test-flake test-install-machine root@target>&2")
client.succeed("test -f test-flake/machines/test-install-machine/hardware-configuration.nix")
client.succeed("clan machines install --debug --flake ${../..} --yes test-install-machine root@target >&2")
try: try:
target.shutdown() target.shutdown()
except BrokenPipeError: except BrokenPipeError:

View File

@@ -1,7 +1,7 @@
import argparse import argparse
import dataclasses
import json import json
import logging import logging
from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Literal from typing import Literal
@@ -19,7 +19,7 @@ from .types import machine_name_type
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@dataclasses.dataclass @dataclass
class HardwareReport: class HardwareReport:
file: Literal["nixos-generate-config", "nixos-facter"] file: Literal["nixos-generate-config", "nixos-facter"]
@@ -120,8 +120,6 @@ def generate_machine_hardware_info(
[ [
"nixpkgs#openssh", "nixpkgs#openssh",
"nixpkgs#sshpass", "nixpkgs#sshpass",
# Provides nixos-generate-config on non-NixOS systems
"nixpkgs#nixos-install-tools",
], ],
[ [
*(["sshpass", "-p", f"{password}"] if password else []), *(["sshpass", "-p", f"{password}"] if password else []),
@@ -202,15 +200,29 @@ def generate_machine_hardware_info(
return HardwareReport("nixos-generate-config") return HardwareReport("nixos-generate-config")
@dataclass
class HardwareGenerateOptions:
flake: FlakeId
machine: str
target_host: str | None
password: str | None
force: bool | None
def hw_generate_command(args: argparse.Namespace) -> None: def hw_generate_command(args: argparse.Namespace) -> None:
hw_info = generate_machine_hardware_info( opts = HardwareGenerateOptions(
args.flake, args.machine, args.hostname, args.password, args.force flake=args.flake,
machine=args.machine,
target_host=args.target_host,
password=args.password,
force=args.force,
)
hw_info = generate_machine_hardware_info(
opts.flake, opts.machine, opts.target_host, opts.password, opts.force
) )
print("----")
print("Successfully generated hardware information.") print("Successfully generated hardware information.")
print(f"Target: {args.machine} ({args.hostname})") print(f"Target: {opts.machine} ({opts.target_host})")
print(f"Type: {hw_info.file}") print(f"Type: {hw_info.file}")
print("----")
def register_hw_generate(parser: argparse.ArgumentParser) -> None: def register_hw_generate(parser: argparse.ArgumentParser) -> None:

View File

@@ -17,6 +17,6 @@ def machine_name_type(arg_value: str) -> str:
) )
if not VALID_HOSTNAME.match(arg_value): if not VALID_HOSTNAME.match(arg_value):
raise argparse.ArgumentTypeError( raise argparse.ArgumentTypeError(
"Invalid character in machine name. Allowed characters are a-z, 0-9, ., -, and _. Must not start with a number" "Invalid character in machine name. Allowed characters are a-z, 0-9, ., and -. Must not start with a number"
) )
return arg_value return arg_value