fix(tess/morph): skip creating existing machine
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user