webui: implement /api/machines/{name}/config
All checks were successful
build / test (pull_request) Successful in 1m4s
All checks were successful
build / test (pull_request) Successful in 1m4s
This commit is contained in:
parent
b303665989
commit
868c49acea
@ -7,10 +7,36 @@ from typing import Optional
|
|||||||
from ..dirs import get_clan_flake_toplevel
|
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:
|
def schema_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict:
|
||||||
if flake is None:
|
if flake is None:
|
||||||
flake = get_clan_flake_toplevel()
|
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(
|
proc = subprocess.run(
|
||||||
[
|
[
|
||||||
"nix",
|
"nix",
|
||||||
|
@ -2,11 +2,14 @@ from typing import Annotated
|
|||||||
|
|
||||||
from fastapi import APIRouter, Body
|
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.create import create_machine as _create_machine
|
||||||
from ...machines.list import list_machines as _list_machines
|
from ...machines.list import list_machines as _list_machines
|
||||||
from ..schemas import (
|
from ..schemas import (
|
||||||
Config,
|
|
||||||
ConfigResponse,
|
ConfigResponse,
|
||||||
Machine,
|
Machine,
|
||||||
MachineCreate,
|
MachineCreate,
|
||||||
@ -41,14 +44,15 @@ async def get_machine(name: str) -> MachineResponse:
|
|||||||
|
|
||||||
@router.get("/api/machines/{name}/config")
|
@router.get("/api/machines/{name}/config")
|
||||||
async def get_machine_config(name: str) -> ConfigResponse:
|
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")
|
@router.put("/api/machines/{name}/config")
|
||||||
async def set_machine_config(
|
async def set_machine_config(
|
||||||
name: str, config: Annotated[Config, Body()]
|
name: str, config: Annotated[dict, Body()]
|
||||||
) -> ConfigResponse:
|
) -> ConfigResponse:
|
||||||
print("TODO")
|
set_config_for_machine(name, config)
|
||||||
return ConfigResponse(config=config)
|
return ConfigResponse(config=config)
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,12 +26,8 @@ class MachineResponse(BaseModel):
|
|||||||
machine: Machine
|
machine: Machine
|
||||||
|
|
||||||
|
|
||||||
class Config(BaseModel):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigResponse(BaseModel):
|
class ConfigResponse(BaseModel):
|
||||||
config: Config
|
config: dict
|
||||||
|
|
||||||
|
|
||||||
class SchemaResponse(BaseModel):
|
class SchemaResponse(BaseModel):
|
||||||
|
Loading…
Reference in New Issue
Block a user