From 8aed51c63e578f792ae1e7fbf0d81c501a467e26 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Mon, 26 May 2025 16:05:54 +0200 Subject: [PATCH] fix(morph/test): temporary workaround for morph test --- pkgs/clan-cli/clan_cli/machines/create.py | 52 ++++++++++++++--------- pkgs/clan-cli/clan_cli/machines/morph.py | 2 +- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index 4f1b7669a..e39d8c4f6 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -40,7 +40,19 @@ class CreateOptions: @API.register -def create_machine(opts: CreateOptions, commit: bool = True) -> None: +def create_machine( + opts: CreateOptions, commit: bool = True, _persist: bool = True +) -> None: + """ + Create a new machine in the clan directory. + + This function will create a new machine based on a template. + + :param opts: Options for creating the machine, including clan directory, machine details, and template name. + :param commit: Whether to commit the changes to the git repository. + :param _persist: Temporary workaround for 'morph'. Whether to persist the changes to the inventory store. + """ + if not opts.clan_dir.is_local: msg = f"Clan {opts.clan_dir} is not a local clan." description = "Import machine only works on local clans" @@ -91,9 +103,7 @@ def create_machine(opts: CreateOptions, commit: bool = True) -> None: if dst.exists(): msg = f"Machine {machine_name} already exists in {clan_dir}" - description = ( - "Please remove the existing machine folder" - ) + description = "Please remove the existing machine folder" raise ClanError(msg, description=description) # TODO(@Qubasa): move this into the template handler @@ -105,27 +115,27 @@ def create_machine(opts: CreateOptions, commit: bool = True) -> None: # TODO(@Qubasa): move this into the template handler copy_from_nixstore(src, dst) - target_host = opts.target_host + if _persist: + target_host = opts.target_host + new_machine = opts.machine + new_machine["deploy"] = {"targetHost": target_host} # type: ignore - new_machine = opts.machine - new_machine["deploy"] = {"targetHost": target_host} # type: ignore + inventory_store = InventoryStore(opts.clan_dir) + inventory = inventory_store.read() - inventory_store = InventoryStore(opts.clan_dir) - inventory = inventory_store.read() + if machine_name in inventory.get("machines", {}): + msg = f"Machine {machine_name} already exists in inventory" + description = ( + "Please delete the existing machine or import with a different name" + ) + raise ClanError(msg, description=description) - if machine_name in inventory.get("machines", {}): - msg = f"Machine {machine_name} already exists in inventory" - description = ( - "Please delete the existing machine or import with a different name" + apply_patch( + inventory, + f"machines.{machine_name}", + new_machine, ) - raise ClanError(msg, description=description) - - apply_patch( - inventory, - f"machines.{machine_name}", - new_machine, - ) - inventory_store.write(inventory, message=f"machine '{machine_name}'") + inventory_store.write(inventory, message=f"machine '{machine_name}'") # Commit at the end in that order to avoid committing halve-baked machines # TODO: automatic rollbacks if something goes wrong diff --git a/pkgs/clan-cli/clan_cli/machines/morph.py b/pkgs/clan-cli/clan_cli/machines/morph.py index f997db767..f9adb5739 100644 --- a/pkgs/clan-cli/clan_cli/machines/morph.py +++ b/pkgs/clan-cli/clan_cli/machines/morph.py @@ -74,7 +74,7 @@ def morph_machine( machine=InventoryMachine(name=name), clan_dir=Flake(str(flakedir)), ) - create_machine(create_opts, commit=False) + create_machine(create_opts, commit=False, _persist=False) machine = Machine(name=name, flake=Flake(str(flakedir)))