fix(morph/test): temporary workaround for morph test

This commit is contained in:
Johannes Kirschbauer
2025-05-26 16:05:54 +02:00
parent 73d72298cc
commit 8aed51c63e
2 changed files with 32 additions and 22 deletions

View File

@@ -40,7 +40,19 @@ class CreateOptions:
@API.register @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: if not opts.clan_dir.is_local:
msg = f"Clan {opts.clan_dir} is not a local clan." msg = f"Clan {opts.clan_dir} is not a local clan."
description = "Import machine only works on local clans" description = "Import machine only works on local clans"
@@ -91,9 +103,7 @@ def create_machine(opts: CreateOptions, commit: bool = True) -> None:
if dst.exists(): if dst.exists():
msg = f"Machine {machine_name} already exists in {clan_dir}" msg = f"Machine {machine_name} already exists in {clan_dir}"
description = ( description = "Please remove the existing machine folder"
"Please remove the existing machine folder"
)
raise ClanError(msg, description=description) raise ClanError(msg, description=description)
# TODO(@Qubasa): move this into the template handler # 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 # TODO(@Qubasa): move this into the template handler
copy_from_nixstore(src, dst) 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 inventory_store = InventoryStore(opts.clan_dir)
new_machine["deploy"] = {"targetHost": target_host} # type: ignore inventory = inventory_store.read()
inventory_store = InventoryStore(opts.clan_dir) if machine_name in inventory.get("machines", {}):
inventory = inventory_store.read() 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", {}): apply_patch(
msg = f"Machine {machine_name} already exists in inventory" inventory,
description = ( f"machines.{machine_name}",
"Please delete the existing machine or import with a different name" new_machine,
) )
raise ClanError(msg, description=description) inventory_store.write(inventory, message=f"machine '{machine_name}'")
apply_patch(
inventory,
f"machines.{machine_name}",
new_machine,
)
inventory_store.write(inventory, message=f"machine '{machine_name}'")
# Commit at the end in that order to avoid committing halve-baked machines # Commit at the end in that order to avoid committing halve-baked machines
# TODO: automatic rollbacks if something goes wrong # TODO: automatic rollbacks if something goes wrong

View File

@@ -74,7 +74,7 @@ def morph_machine(
machine=InventoryMachine(name=name), machine=InventoryMachine(name=name),
clan_dir=Flake(str(flakedir)), 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))) machine = Machine(name=name, flake=Flake(str(flakedir)))