From 3452ca54e638376f241fb7727db336efb4667231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 3 Dec 2024 21:11:04 +0100 Subject: [PATCH] skip writing the empty inventory json --- pkgs/clan-cli/clan_cli/machines/create.py | 15 ++++++++++----- pkgs/clan-cli/tests/test_machines_cli.py | 10 +++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index 05e9e3a3e..2ac95337f 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -109,10 +109,8 @@ def create_machine(opts: CreateOptions) -> None: src = tmpdirp / "machines" / opts.template_name - if ( - not (src / "configuration.nix").exists() - and not (src / "inventory.json").exists() - ): + has_inventory = (dst / "inventory.json").exists() + if not (src / "configuration.nix").exists() and not has_inventory: msg = f"Template machine '{opts.template_name}' does not contain a configuration.nix or inventory.json" description = ( "Template machine must contain a configuration.nix or inventory.json" @@ -131,7 +129,7 @@ def create_machine(opts: CreateOptions) -> None: inventory = load_inventory_json(clan_dir) # Merge the inventory from the template - if (dst / "inventory.json").exists(): + if has_inventory: template_inventory = load_inventory_json(dst) merge_template_inventory(inventory, template_inventory, machine_name) @@ -141,6 +139,13 @@ def create_machine(opts: CreateOptions) -> None: new_machine = InventoryMachine( name=machine_name, deploy=deploy, tags=opts.machine.tags ) + if ( + not has_inventory + and len(opts.machine.tags) == 0 + and new_machine.deploy.targetHost is None + ): + # no need to update inventory if there are no tags or target host + return inventory.machines.update({new_machine.name: dataclass_to_dict(new_machine)}) set_inventory(inventory, clan_dir, "Imported machine from template") diff --git a/pkgs/clan-cli/tests/test_machines_cli.py b/pkgs/clan-cli/tests/test_machines_cli.py index f8994471b..b79223909 100644 --- a/pkgs/clan-cli/tests/test_machines_cli.py +++ b/pkgs/clan-cli/tests/test_machines_cli.py @@ -10,7 +10,15 @@ def test_machine_subcommands( capture_output: CaptureOutput, ) -> None: cli.run( - ["machines", "create", "--flake", str(test_flake_with_core.path), "machine1"] + [ + "machines", + "create", + "--flake", + str(test_flake_with_core.path), + "machine1", + "--tags", + "vm", + ] ) with capture_output as output: