inspect flake before configure VM
This commit is contained in:
@@ -4,7 +4,7 @@ from fastapi.routing import APIRoute
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from .assets import asset_path
|
||||
from .routers import health, machines, root, vms
|
||||
from .routers import health, machines, root, vms, flake
|
||||
|
||||
origins = [
|
||||
"http://localhost:3000",
|
||||
@@ -20,6 +20,7 @@ def setup_app() -> FastAPI:
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
app.include_router(flake.router)
|
||||
app.include_router(health.router)
|
||||
app.include_router(machines.router)
|
||||
app.include_router(root.router)
|
||||
|
||||
50
pkgs/clan-cli/clan_cli/webui/routers/flake.py
Normal file
50
pkgs/clan-cli/clan_cli/webui/routers/flake.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import asyncio
|
||||
import json
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from pathlib import Path
|
||||
from clan_cli.webui.schemas import FlakeAction, FlakeResponse
|
||||
|
||||
from ...nix import nix_build, nix_eval, nix_command
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/api/flake")
|
||||
async def inspect_flake(
|
||||
url: str,
|
||||
) -> FlakeResponse:
|
||||
actions = []
|
||||
# Extract the flake from the given URL
|
||||
# We do this by running 'nix flake prefetch {url} --json'
|
||||
cmd = nix_command([
|
||||
"flake",
|
||||
"prefetch",
|
||||
url,
|
||||
"--json"
|
||||
])
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
cmd[0],
|
||||
*cmd[1:],
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await proc.communicate()
|
||||
|
||||
if proc.returncode != 0:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=str(stderr))
|
||||
|
||||
|
||||
data: dict[str,str] = json.loads(stdout)
|
||||
|
||||
if data.get("storePath") is None:
|
||||
raise HTTPException(status_code=500,detail="Could not load flake")
|
||||
|
||||
content: str
|
||||
with open(Path(data.get("storePath", "")) / Path("flake.nix")) as f:
|
||||
content = f.read()
|
||||
|
||||
|
||||
# TODO: Figure out some measure when it is insecure to inspect or create a VM
|
||||
actions.append(FlakeAction(id="vms/inspect", uri = f"api/vms/inspect"))
|
||||
actions.append(FlakeAction(id="vms/create", uri = f"api/vms/create"))
|
||||
|
||||
return FlakeResponse(content=content, actions=actions )
|
||||
@@ -1,7 +1,7 @@
|
||||
from enum import Enum
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from typing import List
|
||||
|
||||
class Status(Enum):
|
||||
ONLINE = "online"
|
||||
@@ -45,3 +45,12 @@ class VmConfig(BaseModel):
|
||||
|
||||
class VmInspectResponse(BaseModel):
|
||||
config: VmConfig
|
||||
|
||||
|
||||
class FlakeAction(BaseModel):
|
||||
id: str
|
||||
uri: str
|
||||
|
||||
class FlakeResponse(BaseModel):
|
||||
content: str
|
||||
actions: List[FlakeAction]
|
||||
|
||||
Reference in New Issue
Block a user