API: Disk templates, persist original values

This commit is contained in:
Johannes Kirschbauer
2025-01-07 09:18:57 +01:00
parent 4cd28f257a
commit d5e54d262b
2 changed files with 16 additions and 5 deletions

View File

@@ -3,7 +3,7 @@ import logging
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from typing import Any, TypedDict
from uuid import uuid4
from clan_cli.api import API
@@ -124,6 +124,11 @@ def get_disk_schemas(
return disk_schemas
class MachineDiskMatter(TypedDict):
schema_name: str
placeholders: dict[str, str]
@API.register
def set_machine_disk_schema(
base_path: Path,
@@ -134,7 +139,7 @@ def set_machine_disk_schema(
placeholders: dict[str, str],
force: bool = False,
) -> None:
""" "
"""
Set the disk placeholders of the template
"""
# Assert the hw-config must exist before setting the disk
@@ -183,8 +188,13 @@ def set_machine_disk_schema(
)
raise ClanError(msg, description=f"Valid options: {ph.options}")
placeholders_toml = "\n".join(
[f"""# {k} = "{v}""" for k, v in placeholders.items() if v is not None]
)
header = f"""# ---
# schema = "{schema_name}"
# [placeholders]
{placeholders_toml}
# ---
# This file was automatically generated!
# CHANGING this configuration requires wiping and reinstalling the machine

View File

@@ -6,6 +6,7 @@ from pathlib import Path
from typing import Literal
from clan_cli.api import API
from clan_cli.api.disk import MachineDiskMatter
from clan_cli.api.modules import parse_frontmatter
from clan_cli.api.serde import dataclass_to_dict
from clan_cli.cmd import RunOpts, run_no_stdout
@@ -41,7 +42,7 @@ def list_inventory_machines(flake_url: str | Path) -> dict[str, Machine]:
class MachineDetails:
machine: Machine
hw_config: HardwareConfig | None = None
disk_schema: str | None = None
disk_schema: MachineDiskMatter | None = None
import re
@@ -69,7 +70,7 @@ def get_inventory_machine_details(flake_url: Path, machine_name: str) -> Machine
hw_config = HardwareConfig.detect_type(flake_url, machine_name)
machine_dir = specific_machine_dir(flake_url, machine_name)
disk_schema: str | None = None
disk_schema: MachineDiskMatter | None = None
disk_path = machine_dir / "disko.nix"
if disk_path.exists():
with disk_path.open() as f:
@@ -77,7 +78,7 @@ def get_inventory_machine_details(flake_url: Path, machine_name: str) -> Machine
header = extract_header(content)
data, _rest = parse_frontmatter(header)
if data:
disk_schema = data.get("schema")
disk_schema = data # type: ignore
return MachineDetails(machine=machine, hw_config=hw_config, disk_schema=disk_schema)