1
0
forked from clan/clan-core

allow to override nix options in update/install/flash commands

This commit is contained in:
Jörg Thalheim 2024-05-29 09:08:23 +02:00
parent 5b926f57cc
commit 2ae50b7398
7 changed files with 40 additions and 22 deletions

View File

@ -75,12 +75,6 @@ def add_common_flags(parser: argparse.ArgumentParser) -> None:
default=[], default=[],
) )
def flake_path(arg: str) -> str | Path:
flake_dir = Path(arg).resolve()
if flake_dir.exists() and flake_dir.is_dir():
return flake_dir
return arg
parser.add_argument( parser.add_argument(
"--flake", "--flake",
help="path to the flake where the clan resides in, can be a remote flake or local, can be set through the [CLAN_DIR] environment variable", help="path to the flake where the clan resides in, can be a remote flake or local, can be set through the [CLAN_DIR] environment variable",

View File

@ -209,9 +209,9 @@ def generate_facts(
def generate_command(args: argparse.Namespace) -> None: def generate_command(args: argparse.Namespace) -> None:
if len(args.machines) == 0: if len(args.machines) == 0:
machines = get_all_machines(args.flake) machines = get_all_machines(args.flake, args.option)
else: else:
machines = get_selected_machines(args.flake, args.machines) machines = get_selected_machines(args.flake, args.option, args.machines)
generate_facts(machines, args.service, args.regenerate) generate_facts(machines, args.service, args.regenerate)

View File

@ -86,6 +86,7 @@ def flash_machine(
system_config: dict[str, Any], system_config: dict[str, Any],
dry_run: bool, dry_run: bool,
debug: bool, debug: bool,
extra_args: list[str] = [],
) -> None: ) -> None:
secret_facts_module = importlib.import_module(machine.secret_facts_module) secret_facts_module = importlib.import_module(machine.secret_facts_module)
secret_facts_store: SecretStoreBase = secret_facts_module.SecretStore( secret_facts_store: SecretStoreBase = secret_facts_module.SecretStore(
@ -128,6 +129,8 @@ def flash_machine(
json.dumps(system_config), json.dumps(system_config),
] ]
) )
disko_install.extend(["--option", "dry-run", "true"])
disko_install.extend(extra_args)
cmd = nix_shell( cmd = nix_shell(
["nixpkgs#disko"], ["nixpkgs#disko"],
@ -148,6 +151,8 @@ class FlashOptions:
mode: str mode: str
language: str language: str
keymap: str keymap: str
write_efi_boot_entries: bool
nix_options: list[str]
class AppendDiskAction(argparse.Action): class AppendDiskAction(argparse.Action):
@ -178,6 +183,7 @@ def flash_command(args: argparse.Namespace) -> None:
mode=args.mode, mode=args.mode,
language=args.lang, language=args.lang,
keymap=args.keymap, keymap=args.keymap,
nix_options=args.options,
) )
machine = Machine(opts.machine, flake=opts.flake) machine = Machine(opts.machine, flake=opts.flake)
@ -251,12 +257,14 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
help="device to flash to", help="device to flash to",
default={}, default={},
) )
mode_help = textwrap.dedent("""\ mode_help = textwrap.dedent(
"""\
Specify the mode of operation. Valid modes are: format, mount." Specify the mode of operation. Valid modes are: format, mount."
Format will format the disk before installing. Format will format the disk before installing.
Mount will mount the disk before installing. Mount will mount the disk before installing.
Mount is useful for updating an existing system without losing data. Mount is useful for updating an existing system without losing data.
""") """
)
parser.add_argument( parser.add_argument(
"--mode", "--mode",
type=str, type=str,

View File

@ -26,6 +26,7 @@ def install_nixos(
debug: bool = False, debug: bool = False,
password: str | None = None, password: str | None = None,
no_reboot: bool = False, no_reboot: bool = False,
extra_args: list[str] = [],
) -> None: ) -> None:
secret_facts_module = importlib.import_module(machine.secret_facts_module) secret_facts_module = importlib.import_module(machine.secret_facts_module)
log.info(f"installing {machine.name}") log.info(f"installing {machine.name}")
@ -56,6 +57,7 @@ def install_nixos(
f"{machine.flake}#{machine.name}", f"{machine.flake}#{machine.name}",
"--extra-files", "--extra-files",
str(tmpdir), str(tmpdir),
*extra_args,
] ]
if no_reboot: if no_reboot:
@ -95,6 +97,7 @@ class InstallOptions:
debug: bool debug: bool
no_reboot: bool no_reboot: bool
json_ssh_deploy: dict[str, str] | None json_ssh_deploy: dict[str, str] | None
nix_options: list[str]
def install_command(args: argparse.Namespace) -> None: def install_command(args: argparse.Namespace) -> None:
@ -127,6 +130,7 @@ def install_command(args: argparse.Namespace) -> None:
debug=args.debug, debug=args.debug,
no_reboot=args.no_reboot, no_reboot=args.no_reboot,
json_ssh_deploy=json_ssh_deploy, json_ssh_deploy=json_ssh_deploy,
nix_options=args.option,
) )
machine = Machine(opts.machine, flake=opts.flake) machine = Machine(opts.machine, flake=opts.flake)
machine.target_host_address = opts.target_host machine.target_host_address = opts.target_host
@ -142,6 +146,7 @@ def install_command(args: argparse.Namespace) -> None:
debug=opts.debug, debug=opts.debug,
password=password, password=password,
no_reboot=opts.no_reboot, no_reboot=opts.no_reboot,
extra_args=opts.nix_options,
) )

View File

@ -7,7 +7,7 @@ from .machines import Machine
# function to speedup eval if we want to evauluate all machines # function to speedup eval if we want to evauluate all machines
def get_all_machines(flake_dir: Path) -> list[Machine]: def get_all_machines(flake_dir: Path, nix_options: list[str]) -> list[Machine]:
config = nix_config() config = nix_config()
system = config["system"] system = config["system"]
json_path = run( json_path = run(
@ -19,13 +19,20 @@ def get_all_machines(flake_dir: Path) -> list[Machine]:
machines = [] machines = []
for name, machine_data in machines_json.items(): for name, machine_data in machines_json.items():
machines.append( machines.append(
Machine(name=name, flake=flake_dir, deployment_info=machine_data) Machine(
name=name,
flake=flake_dir,
deployment_info=machine_data,
nix_options=nix_options,
)
) )
return machines return machines
def get_selected_machines(flake_dir: Path, machine_names: list[str]) -> list[Machine]: def get_selected_machines(
flake_dir: Path, nix_options: list[str], machine_names: list[str]
) -> list[Machine]:
machines = [] machines = []
for name in machine_names: for name in machine_names:
machines.append(Machine(name=name, flake=flake_dir)) machines.append(Machine(name=name, flake=flake_dir, nix_options=nix_options))
return machines return machines

View File

@ -41,9 +41,10 @@ class QMPWrapper:
class Machine: class Machine:
flake: str | Path
name: str name: str
flake: str | Path
data: MachineData data: MachineData
nix_options: list[str]
eval_cache: dict[str, str] eval_cache: dict[str, str]
build_cache: dict[str, Path] build_cache: dict[str, Path]
_flake_path: Path | None _flake_path: Path | None
@ -55,6 +56,7 @@ class Machine:
name: str, name: str,
flake: Path | str, flake: Path | str,
deployment_info: dict | None = None, deployment_info: dict | None = None,
nix_options: list[str] = [],
machine: MachineData | None = None, machine: MachineData | None = None,
) -> None: ) -> None:
""" """
@ -76,6 +78,7 @@ class Machine:
self.build_cache: dict[str, Path] = {} self.build_cache: dict[str, Path] = {}
self._flake_path: Path | None = None self._flake_path: Path | None = None
self._deployment_info: None | dict = deployment_info self._deployment_info: None | dict = deployment_info
self.nix_options = nix_options
state_dir = vm_state_dir(flake_url=str(self.flake), vm_name=self.data.name) state_dir = vm_state_dir(flake_url=str(self.flake), vm_name=self.data.name)
@ -242,9 +245,9 @@ class Machine:
flake = f"path:{self.flake_dir}" flake = f"path:{self.flake_dir}"
args += [ args += [
f'{flake}#clanInternals.machines."{system}".{self.data.name}.{attr}', f'{flake}#clanInternals.machines."{system}".{self.data.name}.{attr}'
*nix_options,
] ]
args += nix_options + self.nix_options
if method == "eval": if method == "eval":
output = run_no_stdout(nix_eval(args)).stdout.strip() output = run_no_stdout(nix_eval(args)).stdout.strip()

View File

@ -110,11 +110,9 @@ def deploy_nixos(machines: MachineGroup) -> None:
ssh_arg += " -i " + host.key if host.key else "" ssh_arg += " -i " + host.key if host.key else ""
extra_args = host.meta.get("extra_args", [])
cmd = [ cmd = [
"nixos-rebuild", "nixos-rebuild",
"switch", "switch",
*extra_args,
"--fast", "--fast",
"--option", "--option",
"keep-going", "keep-going",
@ -124,6 +122,7 @@ def deploy_nixos(machines: MachineGroup) -> None:
"true", "true",
"--build-host", "--build-host",
"", "",
*machine.nix_options,
"--flake", "--flake",
f"{path}#{machine.name}", f"{path}#{machine.name}",
] ]
@ -143,7 +142,9 @@ def update(args: argparse.Namespace) -> None:
raise ClanError("Could not find clan flake toplevel directory") raise ClanError("Could not find clan flake toplevel directory")
machines = [] machines = []
if len(args.machines) == 1 and args.target_host is not None: if len(args.machines) == 1 and args.target_host is not None:
machine = Machine(name=args.machines[0], flake=args.flake) machine = Machine(
name=args.machines[0], flake=args.flake, nix_options=args.option
)
machine.target_host_address = args.target_host machine.target_host_address = args.target_host
machines.append(machine) machines.append(machine)
@ -153,7 +154,7 @@ def update(args: argparse.Namespace) -> None:
else: else:
if len(args.machines) == 0: if len(args.machines) == 0:
ignored_machines = [] ignored_machines = []
for machine in get_all_machines(args.flake): for machine in get_all_machines(args.flake, args.option):
if machine.deployment_info.get("requireExplicitUpdate", False): if machine.deployment_info.get("requireExplicitUpdate", False):
continue continue
try: try:
@ -173,7 +174,7 @@ def update(args: argparse.Namespace) -> None:
print(machine, file=sys.stderr) print(machine, file=sys.stderr)
else: else:
machines = get_selected_machines(args.flake, args.machines) machines = get_selected_machines(args.flake, args.option, args.machines)
deploy_nixos(MachineGroup(machines)) deploy_nixos(MachineGroup(machines))