webui: implement /api/machines/{name}/config #176
@ -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.<module_name>.options
|
||||
# use nix eval to lib.evalModules .#clanModules.machine-{machine_name}
|
||||
proc = subprocess.run(
|
||||
[
|
||||
"nix",
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
@ -26,12 +26,8 @@ class MachineResponse(BaseModel):
|
||||
machine: Machine
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
pass
|
||||
|
||||
|
||||
class ConfigResponse(BaseModel):
|
||||
config: Config
|
||||
config: dict
|
||||
|
||||
|
||||
class SchemaResponse(BaseModel):
|
||||
|
Loading…
Reference in New Issue
Block a user