Merge pull request 'hardware-update-split' (#5261) from Qubasa/clan-core:hardware-update-split into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/5261
This commit is contained in:
@@ -555,8 +555,8 @@ def main() -> None:
|
||||
else:
|
||||
log.error(e) # noqa: TRY400
|
||||
sys.exit(1)
|
||||
except KeyboardInterrupt as ex:
|
||||
log.warning("Interrupted by user", exc_info=ex)
|
||||
except KeyboardInterrupt:
|
||||
log.warning("Interrupted by user")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import argparse
|
||||
from .create import register_create_parser
|
||||
from .delete import register_delete_parser
|
||||
from .generations import register_generations_parser
|
||||
from .hardware import register_update_hardware_config
|
||||
from .hardware import register_init_hardware_config, register_update_hardware_config
|
||||
from .install import register_install_parser
|
||||
from .list import register_list_parser
|
||||
from .morph import register_morph_parser
|
||||
@@ -89,8 +89,8 @@ Examples:
|
||||
)
|
||||
register_list_parser(list_parser)
|
||||
|
||||
update_hardware_config_parser = subparser.add_parser(
|
||||
"update-hardware-config",
|
||||
init_hardware_config_parser = subparser.add_parser(
|
||||
"init-hardware-config",
|
||||
help="Generate hardware specifics for a machine",
|
||||
description="""
|
||||
|
||||
@@ -106,7 +106,7 @@ The target must be a Linux based system reachable via SSH.
|
||||
"""
|
||||
Examples:
|
||||
|
||||
$ clan machines update-hardware-config [MACHINE] --target-host root@<ip>
|
||||
$ clan machines init-hardware-config [MACHINE] --target-host root@<ip>
|
||||
Will generate the facter.json hardware report for `[TARGET_HOST]` and place the result in facter.json for the given machine `[MACHINE]`.
|
||||
|
||||
For more detailed information, visit: https://docs.clan.lol/guides/getting-started/configure/#machine-configuration
|
||||
@@ -114,6 +114,27 @@ For more detailed information, visit: https://docs.clan.lol/guides/getting-start
|
||||
"""
|
||||
),
|
||||
)
|
||||
register_init_hardware_config(init_hardware_config_parser)
|
||||
|
||||
update_hardware_config_parser = subparser.add_parser(
|
||||
"update-hardware-config",
|
||||
help="Generate hardware specifics for a machine",
|
||||
description="""
|
||||
Generates hardware specifics for a machine. Such as the host platform, available kernel modules, etc.
|
||||
The target must be a Linux based system reachable via SSH
|
||||
""",
|
||||
epilog=(
|
||||
"""
|
||||
Examples:
|
||||
|
||||
$ clan machines update-hardware-config [MACHINE] --target-host root@<ip>
|
||||
Will generate the facter.json hardware report for `[TARGET_HOST]` and place the result in facter.json for the given machine `[MACHINE]`.
|
||||
|
||||
For more detailed information, visit: https://docs.clan.lol/guides/getting-started/configure/#machine-configuration
|
||||
|
||||
"""
|
||||
),
|
||||
)
|
||||
register_update_hardware_config(update_hardware_config_parser)
|
||||
|
||||
install_parser = subparser.add_parser(
|
||||
|
||||
@@ -7,7 +7,8 @@ from clan_lib.flake import require_flake
|
||||
from clan_lib.machines.hardware import (
|
||||
HardwareConfig,
|
||||
HardwareGenerateOptions,
|
||||
run_machine_hardware_info,
|
||||
run_machine_hardware_info_init,
|
||||
run_machine_hardware_info_update,
|
||||
)
|
||||
from clan_lib.machines.machines import Machine
|
||||
from clan_lib.machines.suggestions import validate_machine_names
|
||||
@@ -56,11 +57,51 @@ def update_hardware_config_command(args: argparse.Namespace) -> None:
|
||||
log.info("Aborted.")
|
||||
return
|
||||
|
||||
run_machine_hardware_info(opts, target_host)
|
||||
run_machine_hardware_info_update(opts, target_host)
|
||||
|
||||
|
||||
def register_update_hardware_config(parser: argparse.ArgumentParser) -> None:
|
||||
parser.set_defaults(func=update_hardware_config_command)
|
||||
def init_hardware_config_command(args: argparse.Namespace) -> None:
|
||||
flake = require_flake(args.flake)
|
||||
validate_machine_names([args.machine], flake)
|
||||
machine = Machine(flake=flake, name=args.machine)
|
||||
opts = HardwareGenerateOptions(
|
||||
machine=machine,
|
||||
password=args.password,
|
||||
backend=HardwareConfig(args.backend),
|
||||
)
|
||||
|
||||
if args.target_host:
|
||||
target_host = Remote.from_ssh_uri(
|
||||
machine_name=machine.name,
|
||||
address=args.target_host,
|
||||
)
|
||||
else:
|
||||
target_host = machine.target_host()
|
||||
|
||||
target_host = target_host.override(
|
||||
host_key_check=args.host_key_check,
|
||||
private_key=args.identity_file,
|
||||
)
|
||||
|
||||
if not args.yes:
|
||||
confirm = (
|
||||
input(
|
||||
"WARNING: This will reboot the target machine into a temporary NixOS system "
|
||||
"to gather hardware information. This may disrupt any services running on the machine. "
|
||||
f"Update hardware configuration for machine '{machine.name}' at '{target_host.target}'? [y/N]:"
|
||||
)
|
||||
.strip()
|
||||
.lower()
|
||||
)
|
||||
if confirm not in ("y", "yes"):
|
||||
log.info("Aborted.")
|
||||
return
|
||||
|
||||
run_machine_hardware_info_init(opts, target_host)
|
||||
|
||||
|
||||
def register_init_hardware_config(parser: argparse.ArgumentParser) -> None:
|
||||
parser.set_defaults(func=init_hardware_config_command)
|
||||
machine_parser = parser.add_argument(
|
||||
"machine",
|
||||
help="the name of the machine",
|
||||
@@ -76,7 +117,52 @@ def register_update_hardware_config(parser: argparse.ArgumentParser) -> None:
|
||||
"-y",
|
||||
"--yes",
|
||||
action="store_true",
|
||||
help="Automatic yes to prompts; assume 'yes' as answer to all prompts and run non-interactively.",
|
||||
help="do not ask for confirmation.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--host-key-check",
|
||||
choices=list(get_args(HostKeyCheck)),
|
||||
default="ask",
|
||||
help="Host key (.ssh/known_hosts) check mode.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--password",
|
||||
help="Pre-provided password the cli will prompt otherwise if needed.",
|
||||
type=str,
|
||||
required=False,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--backend",
|
||||
help="The type of hardware report to generate.",
|
||||
choices=["nixos-generate-config", "nixos-facter"],
|
||||
default="nixos-facter",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-i",
|
||||
dest="identity_file",
|
||||
type=Path,
|
||||
help="specify which SSH private key file to use",
|
||||
)
|
||||
|
||||
|
||||
def register_update_hardware_config(parser: argparse.ArgumentParser) -> None:
|
||||
parser.set_defaults(func=update_hardware_config_command)
|
||||
machine_parser = parser.add_argument(
|
||||
"machine",
|
||||
help="the name of the machine",
|
||||
type=machine_name_type,
|
||||
)
|
||||
add_dynamic_completer(machine_parser, complete_machines)
|
||||
parser.add_argument(
|
||||
"--target-host",
|
||||
type=str,
|
||||
help="ssh address to install to in the form of user@host:2222",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-y",
|
||||
"--yes",
|
||||
action="store_true",
|
||||
help="do not ask for confirmation.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--host-key-check",
|
||||
|
||||
Reference in New Issue
Block a user