diff --git a/pkgs/clan-cli/clan_cli/clan/create.py b/pkgs/clan-cli/clan_cli/clan/create.py index a669e4a95..a9c70b613 100644 --- a/pkgs/clan-cli/clan_cli/clan/create.py +++ b/pkgs/clan-cli/clan_cli/clan/create.py @@ -1,12 +1,11 @@ # !/usr/bin/env python3 import argparse import os -from dataclasses import dataclass, fields +from dataclasses import dataclass from pathlib import Path from clan_cli.api import API -from clan_cli.arg_actions import AppendOptionAction -from clan_cli.inventory import Meta, load_inventory_json, save_inventory +from clan_cli.inventory import Inventory, init_inventory from ..cmd import CmdOut, run from ..errors import ClanError @@ -29,11 +28,9 @@ class CreateClanResponse: @dataclass class CreateOptions: directory: Path | str - # Metadata for the clan - # Metadata can be shown with `clan show` - meta: Meta | None = None # URL to the template to use. Defaults to the "minimal" template template_url: str = minimal_template_url + initial: Inventory | None = None def git_command(directory: Path, *args: str) -> list[str]: @@ -88,17 +85,13 @@ def create_clan(options: CreateOptions) -> CreateClanResponse: git_command(directory, "config", "user.email", "clan@example.com") ) - # Write inventory.json file - inventory = load_inventory_json(directory) - if options.meta is not None: - inventory.meta = options.meta - # Persist creates a commit message for each change - save_inventory(inventory, directory, "Init inventory") - flake_update = run( nix_shell(["nixpkgs#nix"], ["nix", "flake", "update"]), cwd=directory ) + if options.initial: + init_inventory(options.directory, init=options.initial) + response = CreateClanResponse( flake_init=flake_init, git_init=git_init, @@ -118,15 +111,6 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None: default=default_template_url, ) - parser.add_argument( - "--meta", - help=f"""Metadata to set for the clan. Available options are: {", ".join([f.name for f in fields(Meta)]) }""", - nargs=2, - metavar=("name", "value"), - action=AppendOptionAction, - default=[], - ) - parser.add_argument( "path", type=Path, help="Path to the clan directory", default=Path(".") ) diff --git a/pkgs/clan-cli/clan_cli/inventory/__init__.py b/pkgs/clan-cli/clan_cli/inventory/__init__.py index 0f409316d..68a8e8bf9 100644 --- a/pkgs/clan-cli/clan_cli/inventory/__init__.py +++ b/pkgs/clan-cli/clan_cli/inventory/__init__.py @@ -19,7 +19,8 @@ from pathlib import Path from types import UnionType from typing import Any, get_args, get_origin -from clan_cli.errors import ClanError +from clan_cli.api import API +from clan_cli.errors import ClanCmdError, ClanError from clan_cli.git import commit_file from ..cmd import run_no_stdout @@ -216,7 +217,7 @@ def load_inventory_json( Load the inventory file from the flake directory If not file is found, returns the default inventory """ - inventory = default_inventory + inventory = default inventory_file = get_path(flake_dir) if inventory_file.exists(): @@ -228,6 +229,10 @@ def load_inventory_json( # Error decoding the inventory file raise ClanError(f"Error decoding inventory file: {e}") + if not inventory_file.exists(): + # Copy over the meta from the flake if the inventory is not initialized + inventory.meta = load_inventory_eval(flake_dir).meta + return inventory @@ -242,3 +247,22 @@ def save_inventory(inventory: Inventory, flake_dir: str | Path, message: str) -> json.dump(dataclass_to_dict(inventory), f, indent=2) commit_file(inventory_file, Path(flake_dir), commit_message=message) + + +@API.register +def init_inventory(directory: str, init: Inventory | None = None) -> None: + inventory = None + # Try reading the current flake + if init is None: + try: + inventory = load_inventory_eval(directory) + except ClanCmdError: + pass + + if init is not None: + inventory = init + + # Write inventory.json file + if inventory is not None: + # Persist creates a commit message for each change + save_inventory(inventory, directory, "Init inventory") diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index 062d7bfd0..cea2412a7 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -25,6 +25,7 @@ def create_machine(flake: FlakeId, machine: Machine) -> None: ) inventory = load_inventory_json(flake.path) + full_inventory = load_inventory_eval(flake.path) if machine.name in full_inventory.machines.keys(): diff --git a/pkgs/clan-cli/tests/test_create_flake.py b/pkgs/clan-cli/tests/test_create_flake.py index db4c7c8ef..d0e920c80 100644 --- a/pkgs/clan-cli/tests/test_create_flake.py +++ b/pkgs/clan-cli/tests/test_create_flake.py @@ -20,12 +20,12 @@ def test_create_flake( cli.run(["flakes", "create", str(flake_dir), f"--url={url}"]) assert (flake_dir / ".clan-flake").exists() - # Replace the inputs.clan.url in the template flake.nix substitute( flake_dir / "flake.nix", clan_core, ) + # Dont evaluate the inventory before the substitute call monkeypatch.chdir(flake_dir) cli.run(["machines", "create", "machine1"]) diff --git a/pkgs/installer/flake-module.nix b/pkgs/installer/flake-module.nix index bfd4dc551..b00c22a79 100644 --- a/pkgs/installer/flake-module.nix +++ b/pkgs/installer/flake-module.nix @@ -32,7 +32,7 @@ let } // flashDiskoConfig; - # Important: The partition names need to be different to the clan install + # Important: The partition names need to be different to the clan install flashDiskoConfig = { boot.loader.grub.efiSupport = lib.mkDefault true; boot.loader.grub.efiInstallAsRemovable = lib.mkDefault true; diff --git a/pkgs/webview-ui/app/src/routes/clan/clanDetails.tsx b/pkgs/webview-ui/app/src/routes/clan/clanDetails.tsx index eb0356b44..fa6e872fe 100644 --- a/pkgs/webview-ui/app/src/routes/clan/clanDetails.tsx +++ b/pkgs/webview-ui/app/src/routes/clan/clanDetails.tsx @@ -42,7 +42,15 @@ export const ClanForm = () => { await toast.promise( (async () => { await callApi("create_clan", { - options: { directory: target_dir, meta, template_url }, + options: { + directory: target_dir, + template_url, + initial: { + meta, + services: {}, + machines: {}, + }, + }, }); setActiveURI(target_dir); setRoute("machines"); diff --git a/templates/minimal/flake.nix b/templates/minimal/flake.nix index db76be471..78301b9c9 100644 --- a/templates/minimal/flake.nix +++ b/templates/minimal/flake.nix @@ -5,10 +5,7 @@ { self, clan-core, ... }: let # Usage see: https://docs.clan.lol - clan = clan-core.lib.buildClan { - meta.name = "miniclan"; - directory = self; - }; + clan = clan-core.lib.buildClan { directory = self; }; in { # all machines managed by Clan diff --git a/templates/minimal/inventory.json b/templates/minimal/inventory.json new file mode 100644 index 000000000..40109ebd5 --- /dev/null +++ b/templates/minimal/inventory.json @@ -0,0 +1,5 @@ +{ + "meta": { "name": "__CHANGE_ME__" }, + "machines": {}, + "services": {} +}