Merge pull request 'Refactor: replace direct references to inventory' (#3674) from hsjobeki/clan-core:chores-1 into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3674
This commit is contained in:
hsjobeki
2025-05-16 10:43:24 +00:00
6 changed files with 37 additions and 27 deletions

View File

@@ -6,11 +6,11 @@ from pathlib import Path
from clan_lib.api import API
from clan_lib.nix_models.inventory import Inventory
from clan_lib.persist.inventory_store import InventoryStore
from clan_cli.cmd import CmdOut, RunOpts, run
from clan_cli.errors import ClanError
from clan_cli.flake import Flake
from clan_cli.inventory import set_inventory
from clan_cli.nix import nix_command, nix_metadata, nix_shell
from clan_cli.templates import (
InputPrio,
@@ -108,11 +108,8 @@ def create_clan(opts: CreateOptions) -> CreateClanResponse:
response.flake_update = flake_update
if opts.initial:
set_inventory(
flake=Flake(str(opts.dest)),
inventory=opts.initial,
message="Init inventory",
)
inventory_store = InventoryStore(flake=Flake(str(opts.dest)))
inventory_store.write(opts.initial, message="Init inventory")
return response

View File

@@ -2,9 +2,10 @@ from dataclasses import dataclass
from clan_lib.api import API
from clan_lib.nix_models.inventory import Inventory, Meta
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch
from clan_cli.flake import Flake
from clan_cli.inventory import load_inventory_json, set_inventory
@dataclass
@@ -15,9 +16,9 @@ class UpdateOptions:
@API.register
def update_clan_meta(options: UpdateOptions) -> Inventory:
inventory = load_inventory_json(options.flake)
inventory["meta"] = options.meta
set_inventory(inventory, options.flake, "Update clan metadata")
inventory_store = InventoryStore(options.flake)
inventory = inventory_store.read()
apply_patch(inventory, "meta", options.meta)
inventory_store.write(inventory, message="Update clan metadata")
return inventory

View File

@@ -5,22 +5,20 @@ from dataclasses import dataclass
from pathlib import Path
from clan_lib.api import API
from clan_lib.api.serde import dataclass_to_dict
from clan_lib.nix_models.inventory import (
Machine as InventoryMachine,
)
from clan_lib.nix_models.inventory import (
MachineDeploy,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch
from clan_cli.completions import add_dynamic_completer, complete_tags
from clan_cli.dirs import get_clan_flake_toplevel_or_env
from clan_cli.errors import ClanError
from clan_cli.flake import Flake
from clan_cli.git import commit_file
from clan_cli.inventory import (
patch_inventory_with,
)
from clan_cli.machines.list import list_machines
from clan_cli.templates import (
InputPrio,
@@ -111,9 +109,14 @@ def create_machine(opts: CreateOptions, commit: bool = True) -> None:
if target_host:
new_machine["deploy"] = {"targetHost": target_host}
patch_inventory_with(
opts.clan_dir, f"machines.{machine_name}", dataclass_to_dict(new_machine)
inventory_store = InventoryStore(opts.clan_dir)
inventory = inventory_store.read()
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
# TODO: automatic rollbacks if something goes wrong

View File

@@ -1,11 +1,9 @@
from typing import Any
from typing import Any, cast
import pytest
from clan_cli.flake import Flake
# Functions to test
from clan_cli.inventory import load_inventory_eval
from clan_cli.tests.fixtures_flakes import FlakeForTest
from clan_lib.persist.inventory_store import InventoryStore
@pytest.mark.parametrize(
@@ -44,7 +42,11 @@ def test_inventory_deserialize_variants(
Testing different inventory deserializations
Inventory should always be deserializable to a dict
"""
inventory: dict[str, Any] = load_inventory_eval(Flake(test_flake_with_core.path)) # type: ignore
inventory_store = InventoryStore(Flake(str(test_flake_with_core.path)))
# Cast the inventory to a dict for the following assertions
inventory = cast(dict[str, Any], inventory_store.read())
# Check that the inventory is a dict
assert isinstance(inventory, dict)

View File

@@ -4,9 +4,6 @@ from typing import TYPE_CHECKING
import pytest
from clan_cli.flake import Flake
from clan_cli.inventory import (
set_inventory,
)
from clan_cli.machines.create import CreateOptions, create_machine
from clan_cli.nix import nix_eval, run
from clan_cli.tests.fixtures_flakes import FlakeForTest
@@ -16,6 +13,7 @@ from clan_lib.nix_models.inventory import (
Machine,
MachineDeploy,
)
from clan_lib.persist.inventory_store import InventoryStore
if TYPE_CHECKING:
from .age_keys import KeyPair
@@ -88,7 +86,12 @@ def test_add_module_to_inventory(
}
}
set_inventory(inventory, Flake(str(base_path)), "Add borgbackup service")
inventory_store = InventoryStore(Flake(str(test_flake_with_core.path)))
inventory_store.write(
inventory,
message="Add borgbackup service",
commit=False,
)
# cmd = ["facts", "generate", "--flake", str(test_flake_with_core.path), "machine1"]
cmd = [

View File

@@ -162,4 +162,8 @@ class InventoryStore:
json.dump(persisted, f, indent=2)
if commit:
commit_file(self.inventory_file, self._flake.path, commit_message=message)
commit_file(
self.inventory_file,
self._flake.path,
commit_message=f"update({self.inventory_file.name}): {message}",
)