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 collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any, TypedDict
from uuid import uuid4 from uuid import uuid4
from clan_cli.api import API from clan_cli.api import API
@@ -124,6 +124,11 @@ def get_disk_schemas(
return disk_schemas return disk_schemas
class MachineDiskMatter(TypedDict):
schema_name: str
placeholders: dict[str, str]
@API.register @API.register
def set_machine_disk_schema( def set_machine_disk_schema(
base_path: Path, base_path: Path,
@@ -134,7 +139,7 @@ def set_machine_disk_schema(
placeholders: dict[str, str], placeholders: dict[str, str],
force: bool = False, force: bool = False,
) -> None: ) -> None:
""" " """
Set the disk placeholders of the template Set the disk placeholders of the template
""" """
# Assert the hw-config must exist before setting the disk # 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}") 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"""# --- header = f"""# ---
# schema = "{schema_name}" # schema = "{schema_name}"
# [placeholders]
{placeholders_toml}
# --- # ---
# This file was automatically generated! # This file was automatically generated!
# CHANGING this configuration requires wiping and reinstalling the machine # CHANGING this configuration requires wiping and reinstalling the machine

View File

@@ -6,6 +6,7 @@ from pathlib import Path
from typing import Literal from typing import Literal
from clan_cli.api import API 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.modules import parse_frontmatter
from clan_cli.api.serde import dataclass_to_dict from clan_cli.api.serde import dataclass_to_dict
from clan_cli.cmd import RunOpts, run_no_stdout 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: class MachineDetails:
machine: Machine machine: Machine
hw_config: HardwareConfig | None = None hw_config: HardwareConfig | None = None
disk_schema: str | None = None disk_schema: MachineDiskMatter | None = None
import re 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) hw_config = HardwareConfig.detect_type(flake_url, machine_name)
machine_dir = specific_machine_dir(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" disk_path = machine_dir / "disko.nix"
if disk_path.exists(): if disk_path.exists():
with disk_path.open() as f: 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) header = extract_header(content)
data, _rest = parse_frontmatter(header) data, _rest = parse_frontmatter(header)
if data: if data:
disk_schema = data.get("schema") disk_schema = data # type: ignore
return MachineDetails(machine=machine, hw_config=hw_config, disk_schema=disk_schema) return MachineDetails(machine=machine, hw_config=hw_config, disk_schema=disk_schema)