expose nixos-facter in cli

This commit is contained in:
Jörg Thalheim
2024-09-25 18:17:25 +02:00
parent 83c87fdf6f
commit 7b93ef6704
2 changed files with 17 additions and 12 deletions

View File

@@ -21,7 +21,7 @@ log = logging.getLogger(__name__)
@dataclass @dataclass
class HardwareReport: class HardwareReport:
file: Literal["nixos-generate-config", "nixos-facter"] backend: Literal["nixos-generate-config", "nixos-facter"]
hw_nix_file = "hardware-configuration.nix" hw_nix_file = "hardware-configuration.nix"
@@ -104,9 +104,7 @@ def generate_machine_hardware_info(
password: str | None = None, password: str | None = None,
keyfile: str | None = None, keyfile: str | None = None,
force: bool | None = False, force: bool | None = False,
report_type: Literal[ backend: Literal["nixos-generate-config", "nixos-facter"] = "nixos-generate-config",
"nixos-generate-config", "nixos-facter"
] = "nixos-generate-config",
) -> HardwareReport: ) -> HardwareReport:
""" """
Generate hardware information for a machine Generate hardware information for a machine
@@ -119,7 +117,7 @@ def generate_machine_hardware_info(
config_command = ( config_command = (
["nixos-facter"] ["nixos-facter"]
if report_type == "nixos-facter" if backend == "nixos-facter"
else [ else [
"nixos-generate-config", "nixos-generate-config",
# Filesystems are managed by disko # Filesystems are managed by disko
@@ -162,7 +160,7 @@ def generate_machine_hardware_info(
raise ClanError(msg) raise ClanError(msg)
hw_file = Path( hw_file = Path(
f"{clan_dir}/machines/{machine_name}/{hw_nix_file if report_type == 'nixos-generate-config' else facter_file}" f"{clan_dir}/machines/{machine_name}/{hw_nix_file if backend == 'nixos-generate-config' else facter_file}"
) )
hw_file.parent.mkdir(parents=True, exist_ok=True) hw_file.parent.mkdir(parents=True, exist_ok=True)
@@ -213,7 +211,7 @@ def generate_machine_hardware_info(
location=f"{__name__} {hw_file}", location=f"{__name__} {hw_file}",
) from e ) from e
return HardwareReport(report_type) return HardwareReport(backend)
@dataclass @dataclass
@@ -223,6 +221,7 @@ class HardwareGenerateOptions:
target_host: str | None target_host: str | None
password: str | None password: str | None
force: bool | None force: bool | None
backend: Literal["nixos-generate-config", "nixos-facter"]
def update_hardware_config_command(args: argparse.Namespace) -> None: def update_hardware_config_command(args: argparse.Namespace) -> None:
@@ -232,13 +231,13 @@ def update_hardware_config_command(args: argparse.Namespace) -> None:
target_host=args.target_host, target_host=args.target_host,
password=args.password, password=args.password,
force=args.force, force=args.force,
backend=args.backend,
) )
hw_info = generate_machine_hardware_info( generate_machine_hardware_info(
opts.flake, opts.machine, opts.target_host, opts.password opts.flake, opts.machine, opts.target_host, opts.password, opts.backend
) )
print("Successfully generated hardware information.") print("Successfully generated hardware information.")
print(f"Target: {opts.machine} ({opts.target_host})") print(f"Target: {opts.machine} ({opts.target_host})")
print(f"Type: {hw_info.file}")
def register_update_hardware_config(parser: argparse.ArgumentParser) -> None: def register_update_hardware_config(parser: argparse.ArgumentParser) -> None:
@@ -260,6 +259,12 @@ def register_update_hardware_config(parser: argparse.ArgumentParser) -> None:
type=str, type=str,
required=False, required=False,
) )
machine_parser = parser.add_argument(
"--backend",
help="The type of hardware report to generate.",
choices=["nixos-generate-config", "nixos-facter"],
default="nixos-generate-config",
)
machine_parser = parser.add_argument( machine_parser = parser.add_argument(
"--force", "--force",
help="Will overwrite the hardware-configuration.nix file.", help="Will overwrite the hardware-configuration.nix file.",

View File

@@ -81,7 +81,7 @@ const InstallMachine = (props: InstallMachineProps) => {
machine_name: props.name, machine_name: props.name,
}); });
if (result.status === "error") throw new Error("Failed to fetch data"); if (result.status === "error") throw new Error("Failed to fetch data");
return result.data?.file === "nixos-facter" || null; return result.data?.backend === "nixos-facter" || null;
} }
return null; return null;
}, },
@@ -157,7 +157,7 @@ const InstallMachine = (props: InstallMachineProps) => {
machine_name: props.name, machine_name: props.name,
keyfile: props.sshKey?.name, keyfile: props.sshKey?.name,
hostname: props.targetHost, hostname: props.targetHost,
report_type: "nixos-facter", backend: "nixos-facter",
}); });
toast.dismiss(loading_toast); toast.dismiss(loading_toast);
hwInfoQuery.refetch(); hwInfoQuery.refetch();