API: fix create machine should set machine description and other data

This commit is contained in:
Johannes Kirschbauer
2024-12-10 15:10:09 +01:00
parent 1536c77c19
commit a3423130ed

View File

@@ -17,7 +17,6 @@ from clan_cli.inventory import Machine as InventoryMachine
from clan_cli.inventory import ( from clan_cli.inventory import (
MachineDeploy, MachineDeploy,
load_inventory_json, load_inventory_json,
merge_template_inventory,
set_inventory, set_inventory,
) )
from clan_cli.machines.list import list_nixos_machines from clan_cli.machines.list import list_nixos_machines
@@ -110,12 +109,9 @@ def create_machine(opts: CreateOptions) -> None:
src = tmpdirp / "machines" / opts.template_name src = tmpdirp / "machines" / opts.template_name
has_inventory = (dst / "inventory.json").exists() if not (src / "configuration.nix").exists():
if not (src / "configuration.nix").exists() and not has_inventory: msg = f"Template machine '{opts.template_name}' does not contain a configuration.nix"
msg = f"Template machine '{opts.template_name}' does not contain a configuration.nix or inventory.json" description = "Template machine must contain a configuration.nix"
description = (
"Template machine must contain a configuration.nix or inventory.json"
)
raise ClanError(msg, description=description) raise ClanError(msg, description=description)
def log_copy(src: str, dst: str) -> None: def log_copy(src: str, dst: str) -> None:
@@ -125,40 +121,27 @@ def create_machine(opts: CreateOptions) -> None:
shutil.copytree(src, dst, ignore_dangling_symlinks=True, copy_function=log_copy) shutil.copytree(src, dst, ignore_dangling_symlinks=True, copy_function=log_copy)
inventory = load_inventory_json(clan_dir)
target_host = opts.target_host
# TODO: We should allow the template to specify machine metadata if not defined by user
new_machine = opts.machine
if target_host:
new_machine["deploy"] = {"targetHost": target_host}
inventory["machines"] = inventory.get("machines", {})
inventory["machines"][machine_name] = new_machine
# Commit at the end in that order to avoid commiting halve-baked machines
# TODO: automatic rollbacks if something goes wrong
set_inventory(inventory, clan_dir, "Imported machine from template")
commit_file( commit_file(
clan_dir / "machines" / machine_name, clan_dir / "machines" / machine_name,
repo_dir=clan_dir, repo_dir=clan_dir,
commit_message=f"Add machine {machine_name}", commit_message=f"Add machine {machine_name}",
) )
inventory = load_inventory_json(clan_dir)
# Merge the inventory from the template
if has_inventory:
template_inventory = load_inventory_json(dst)
merge_template_inventory(inventory, template_inventory, machine_name)
deploy = MachineDeploy()
target_host = opts.target_host
if target_host:
deploy["targetHost"] = target_host
# TODO: We should allow the template to specify machine metadata if not defined by user
new_machine = InventoryMachine(
name=machine_name, deploy=deploy, tags=opts.machine.get("tags", [])
)
if (
not has_inventory
and len(opts.machine.get("tags", [])) == 0
and new_machine.get("deploy", {}).get("targetHost") is None
):
# no need to update inventory if there are no tags or target host
return
inventory["machines"] = inventory.get("machines", {})
inventory["machines"][machine_name] = new_machine
set_inventory(inventory, clan_dir, "Imported machine from template")
def create_command(args: argparse.Namespace) -> None: def create_command(args: argparse.Namespace) -> None:
if args.flake: if args.flake: