From 2bfc33bc312ff00d991946a262ac9371ee61d602 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Tue, 24 Jun 2025 11:37:21 +0200 Subject: [PATCH] clan-cli: Move delete.py to clan_lib/machines --- pkgs/clan-cli/clan_cli/machines/delete.py | 50 +-------------------- pkgs/clan-cli/clan_lib/machines/delete.py | 55 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 49 deletions(-) create mode 100644 pkgs/clan-cli/clan_lib/machines/delete.py diff --git a/pkgs/clan-cli/clan_cli/machines/delete.py b/pkgs/clan-cli/clan_cli/machines/delete.py index 47f3c91bf..680b20b4b 100644 --- a/pkgs/clan-cli/clan_cli/machines/delete.py +++ b/pkgs/clan-cli/clan_cli/machines/delete.py @@ -1,62 +1,14 @@ import argparse import logging -import shutil -from pathlib import Path -from clan_lib import inventory -from clan_lib.api import API -from clan_lib.dirs import specific_machine_dir +from clan_lib.machines.delete import delete_machine from clan_lib.machines.machines import Machine from clan_cli.completions import add_dynamic_completer, complete_machines -from clan_cli.secrets.folders import sops_secrets_folder -from clan_cli.secrets.machines import has_machine as secrets_has_machine -from clan_cli.secrets.machines import remove_machine as secrets_machine_remove -from clan_cli.secrets.secrets import ( - list_secrets, -) log = logging.getLogger(__name__) -@API.register -def delete_machine(machine: Machine) -> None: - inventory_store = inventory.InventoryStore(machine.flake) - try: - inventory_store.delete( - {f"machines.{machine.name}"}, - ) - except KeyError as exc: - # louis@(2025-03-09): test infrastructure does not seem to set the - # inventory properly, but more importantly only one machine in my - # personal clan ended up in the inventory for some reason, so I think - # it makes sense to eat the exception here. - log.warning( - f"{machine.name} was missing or already deleted from the machines inventory: {exc}" - ) - - changed_paths: list[Path] = [] - - folder = specific_machine_dir(machine) - if folder.exists(): - changed_paths.append(folder) - shutil.rmtree(folder) - - # louis@(2025-02-04): clean-up legacy (pre-vars) secrets: - sops_folder = sops_secrets_folder(machine.flake.path) - filter_fn = lambda secret_name: secret_name.startswith(f"{machine.name}-") - for secret_name in list_secrets(machine.flake.path, filter_fn): - secret_path = sops_folder / secret_name - changed_paths.append(secret_path) - shutil.rmtree(secret_path) - - changed_paths.extend(machine.public_vars_store.delete_store()) - changed_paths.extend(machine.secret_vars_store.delete_store()) - # Remove the machine's key, and update secrets & vars that referenced it: - if secrets_has_machine(machine.flake.path, machine.name): - secrets_machine_remove(machine.flake.path, machine.name) - - def delete_command(args: argparse.Namespace) -> None: delete_machine(Machine(flake=args.flake, name=args.name)) diff --git a/pkgs/clan-cli/clan_lib/machines/delete.py b/pkgs/clan-cli/clan_lib/machines/delete.py new file mode 100644 index 000000000..29bf5c473 --- /dev/null +++ b/pkgs/clan-cli/clan_lib/machines/delete.py @@ -0,0 +1,55 @@ +import logging +import shutil +from pathlib import Path + +from clan_cli.secrets.folders import sops_secrets_folder +from clan_cli.secrets.machines import has_machine as secrets_has_machine +from clan_cli.secrets.machines import remove_machine as secrets_machine_remove +from clan_cli.secrets.secrets import ( + list_secrets, +) + +from clan_lib import inventory +from clan_lib.api import API +from clan_lib.dirs import specific_machine_dir +from clan_lib.machines.machines import Machine + +log = logging.getLogger(__name__) + + +@API.register +def delete_machine(machine: Machine) -> None: + inventory_store = inventory.InventoryStore(machine.flake) + try: + inventory_store.delete( + {f"machines.{machine.name}"}, + ) + except KeyError as exc: + # louis@(2025-03-09): test infrastructure does not seem to set the + # inventory properly, but more importantly only one machine in my + # personal clan ended up in the inventory for some reason, so I think + # it makes sense to eat the exception here. + log.warning( + f"{machine.name} was missing or already deleted from the machines inventory: {exc}" + ) + + changed_paths: list[Path] = [] + + folder = specific_machine_dir(machine) + if folder.exists(): + changed_paths.append(folder) + shutil.rmtree(folder) + + # louis@(2025-02-04): clean-up legacy (pre-vars) secrets: + sops_folder = sops_secrets_folder(machine.flake.path) + filter_fn = lambda secret_name: secret_name.startswith(f"{machine.name}-") + for secret_name in list_secrets(machine.flake.path, filter_fn): + secret_path = sops_folder / secret_name + changed_paths.append(secret_path) + shutil.rmtree(secret_path) + + changed_paths.extend(machine.public_vars_store.delete_store()) + changed_paths.extend(machine.secret_vars_store.delete_store()) + # Remove the machine's key, and update secrets & vars that referenced it: + if secrets_has_machine(machine.flake.path, machine.name): + secrets_machine_remove(machine.flake.path, machine.name)