From 868c49acea8d803e7d7376fdad9189a4c17d0e68 Mon Sep 17 00:00:00 2001 From: DavHau Date: Fri, 25 Aug 2023 23:18:06 +0200 Subject: [PATCH] webui: implement /api/machines/{name}/config --- pkgs/clan-cli/clan_cli/config/machine.py | 28 ++++++++++++++++++- .../clan_cli/webui/routers/machines.py | 14 ++++++---- pkgs/clan-cli/clan_cli/webui/schemas.py | 6 +--- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/config/machine.py b/pkgs/clan-cli/clan_cli/config/machine.py index 3787bf6c..0bf2de59 100644 --- a/pkgs/clan-cli/clan_cli/config/machine.py +++ b/pkgs/clan-cli/clan_cli/config/machine.py @@ -7,10 +7,36 @@ from typing import Optional from ..dirs import get_clan_flake_toplevel +def config_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict: + # find the flake root + if flake is None: + flake = get_clan_flake_toplevel() + # read the config from a json file located at {flake}/machines/{machine_name}.json + config_path = flake / "machines" / f"{machine_name}.json" + if not config_path.exists(): + raise Exception( + f"Machine {machine_name} does not exist in {flake / 'machines'}" + ) + with open(config_path) as f: + return json.load(f) + + +def set_config_for_machine( + machine_name: str, config: dict, flake: Optional[Path] = None +) -> None: + # find the flake root + if flake is None: + flake = get_clan_flake_toplevel() + # write the config to a json file located at {flake}/machines/{machine_name}.json + config_path = flake / "machines" / f"{machine_name}.json" + with open(config_path, "w") as f: + json.dump(config, f) + + def schema_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict: if flake is None: flake = get_clan_flake_toplevel() - # use nix eval to read from .#clanModules..options + # use nix eval to lib.evalModules .#clanModules.machine-{machine_name} proc = subprocess.run( [ "nix", diff --git a/pkgs/clan-cli/clan_cli/webui/routers/machines.py b/pkgs/clan-cli/clan_cli/webui/routers/machines.py index 9c7ebacf..61fd5edc 100644 --- a/pkgs/clan-cli/clan_cli/webui/routers/machines.py +++ b/pkgs/clan-cli/clan_cli/webui/routers/machines.py @@ -2,11 +2,14 @@ from typing import Annotated from fastapi import APIRouter, Body -from ...config.machine import schema_for_machine +from ...config.machine import ( + config_for_machine, + schema_for_machine, + set_config_for_machine, +) from ...machines.create import create_machine as _create_machine from ...machines.list import list_machines as _list_machines from ..schemas import ( - Config, ConfigResponse, Machine, MachineCreate, @@ -41,14 +44,15 @@ async def get_machine(name: str) -> MachineResponse: @router.get("/api/machines/{name}/config") async def get_machine_config(name: str) -> ConfigResponse: - return ConfigResponse(config=Config()) + config = config_for_machine(name) + return ConfigResponse(config=config) @router.put("/api/machines/{name}/config") async def set_machine_config( - name: str, config: Annotated[Config, Body()] + name: str, config: Annotated[dict, Body()] ) -> ConfigResponse: - print("TODO") + set_config_for_machine(name, config) return ConfigResponse(config=config) diff --git a/pkgs/clan-cli/clan_cli/webui/schemas.py b/pkgs/clan-cli/clan_cli/webui/schemas.py index 00258630..90c5437a 100644 --- a/pkgs/clan-cli/clan_cli/webui/schemas.py +++ b/pkgs/clan-cli/clan_cli/webui/schemas.py @@ -26,12 +26,8 @@ class MachineResponse(BaseModel): machine: Machine -class Config(BaseModel): - pass - - class ConfigResponse(BaseModel): - config: Config + config: dict class SchemaResponse(BaseModel):