From a254ff46a19c5e50c53f0222ff9612efd114d1f2 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 11 Jun 2025 16:50:44 +0200 Subject: [PATCH] fix(tess/morph): skip creating existing machine --- pkgs/clan-cli/clan_cli/machines/create.py | 42 ++++++++++++----------- pkgs/clan-cli/clan_cli/machines/morph.py | 14 ++++---- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index 34b34875e..118565d3f 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -29,7 +29,8 @@ class CreateOptions: @API.register def create_machine( - opts: CreateOptions, commit: bool = True, _persist: bool = True + opts: CreateOptions, + commit: bool = True, ) -> None: """ Create a new machine in the clan directory. @@ -60,30 +61,31 @@ def create_machine( raise ClanError(msg, location="Create Machine") with machine_template( - flake=opts.clan_dir, template_ident=opts.template, dst_machine_name=machine_name + flake=opts.clan_dir, + template_ident=opts.template, + dst_machine_name=machine_name, ) as _machine_dir: # Write to the inventory if persist is true - if _persist: - target_host = opts.target_host - new_machine = opts.machine - new_machine["deploy"] = {"targetHost": target_host} # type: ignore + target_host = opts.target_host + 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) - - set_value_by_path( - inventory, - f"machines.{machine_name}", - new_machine, + 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" ) - inventory_store.write(inventory, message=f"machine '{machine_name}'") + raise ClanError(msg, description=description) + + set_value_by_path( + inventory, + f"machines.{machine_name}", + new_machine, + ) + inventory_store.write(inventory, message=f"machine '{machine_name}'") if commit: commit_file( diff --git a/pkgs/clan-cli/clan_cli/machines/morph.py b/pkgs/clan-cli/clan_cli/machines/morph.py index 2357b57d2..65e195827 100644 --- a/pkgs/clan-cli/clan_cli/machines/morph.py +++ b/pkgs/clan-cli/clan_cli/machines/morph.py @@ -11,6 +11,7 @@ from clan_lib.cmd import Log, RunOpts, run from clan_lib.dirs import get_clan_flake_toplevel_or_env from clan_lib.errors import ClanError from clan_lib.flake import Flake +from clan_lib.machines.actions import list_machines from clan_lib.machines.machines import Machine from clan_lib.nix import nix_build, nix_command from clan_lib.nix_models.clan import InventoryMachine @@ -69,12 +70,13 @@ def morph_machine( if name is None: name = random_hostname() - create_opts = CreateOptions( - template=template, - machine=InventoryMachine(name=name), - clan_dir=Flake(str(flakedir)), - ) - create_machine(create_opts, commit=False, _persist=False) + if name not in list_machines(flake): + create_opts = CreateOptions( + template=template, + machine=InventoryMachine(name=name), + clan_dir=Flake(str(flakedir)), + ) + create_machine(create_opts, commit=False) machine = Machine(name=name, flake=Flake(str(flakedir)))