fix(tess/morph): skip creating existing machine

This commit is contained in:
Johannes Kirschbauer
2025-06-11 16:50:44 +02:00
parent b5ed9fdfed
commit a254ff46a1
2 changed files with 30 additions and 26 deletions

View File

@@ -29,7 +29,8 @@ class CreateOptions:
@API.register @API.register
def create_machine( def create_machine(
opts: CreateOptions, commit: bool = True, _persist: bool = True opts: CreateOptions,
commit: bool = True,
) -> None: ) -> None:
""" """
Create a new machine in the clan directory. Create a new machine in the clan directory.
@@ -60,30 +61,31 @@ def create_machine(
raise ClanError(msg, location="Create Machine") raise ClanError(msg, location="Create Machine")
with machine_template( 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: ) as _machine_dir:
# Write to the inventory if persist is true # Write to the inventory if persist is true
if _persist: target_host = opts.target_host
target_host = opts.target_host new_machine = opts.machine
new_machine = opts.machine new_machine["deploy"] = {"targetHost": target_host} # type: ignore
new_machine["deploy"] = {"targetHost": target_host} # type: ignore
inventory_store = InventoryStore(opts.clan_dir) inventory_store = InventoryStore(opts.clan_dir)
inventory = inventory_store.read() inventory = inventory_store.read()
if machine_name in inventory.get("machines", {}): if machine_name in inventory.get("machines", {}):
msg = f"Machine {machine_name} already exists in inventory" msg = f"Machine {machine_name} already exists in inventory"
description = ( description = (
"Please delete the existing machine or import with a different name" "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,
) )
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: if commit:
commit_file( commit_file(

View File

@@ -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.dirs import get_clan_flake_toplevel_or_env
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
from clan_lib.flake import Flake from clan_lib.flake import Flake
from clan_lib.machines.actions import list_machines
from clan_lib.machines.machines import Machine from clan_lib.machines.machines import Machine
from clan_lib.nix import nix_build, nix_command from clan_lib.nix import nix_build, nix_command
from clan_lib.nix_models.clan import InventoryMachine from clan_lib.nix_models.clan import InventoryMachine
@@ -69,12 +70,13 @@ def morph_machine(
if name is None: if name is None:
name = random_hostname() name = random_hostname()
create_opts = CreateOptions( if name not in list_machines(flake):
template=template, create_opts = CreateOptions(
machine=InventoryMachine(name=name), template=template,
clan_dir=Flake(str(flakedir)), machine=InventoryMachine(name=name),
) clan_dir=Flake(str(flakedir)),
create_machine(create_opts, commit=False, _persist=False) )
create_machine(create_opts, commit=False)
machine = Machine(name=name, flake=Flake(str(flakedir))) machine = Machine(name=name, flake=Flake(str(flakedir)))