format python
This commit is contained in:
@@ -4,7 +4,7 @@ from fastapi.routing import APIRoute
|
|||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
from .assets import asset_path
|
from .assets import asset_path
|
||||||
from .routers import health, machines, root, vms, flake
|
from .routers import flake, health, machines, root, vms
|
||||||
|
|
||||||
origins = [
|
origins = [
|
||||||
"http://localhost:3000",
|
"http://localhost:3000",
|
||||||
|
|||||||
@@ -1,26 +1,24 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
from fastapi import APIRouter, HTTPException, status
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException, status
|
||||||
|
|
||||||
from clan_cli.webui.schemas import FlakeAction, FlakeResponse
|
from clan_cli.webui.schemas import FlakeAction, FlakeResponse
|
||||||
|
|
||||||
from ...nix import nix_build, nix_eval, nix_command
|
from ...nix import nix_command
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/flake")
|
@router.get("/api/flake")
|
||||||
async def inspect_flake(
|
async def inspect_flake(
|
||||||
url: str,
|
url: str,
|
||||||
) -> FlakeResponse:
|
) -> FlakeResponse:
|
||||||
actions = []
|
actions = []
|
||||||
# Extract the flake from the given URL
|
# Extract the flake from the given URL
|
||||||
# We do this by running 'nix flake prefetch {url} --json'
|
# We do this by running 'nix flake prefetch {url} --json'
|
||||||
cmd = nix_command([
|
cmd = nix_command(["flake", "prefetch", url, "--json"])
|
||||||
"flake",
|
|
||||||
"prefetch",
|
|
||||||
url,
|
|
||||||
"--json"
|
|
||||||
])
|
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
cmd[0],
|
cmd[0],
|
||||||
*cmd[1:],
|
*cmd[1:],
|
||||||
@@ -30,21 +28,19 @@ async def inspect_flake(
|
|||||||
stdout, stderr = await proc.communicate()
|
stdout, stderr = await proc.communicate()
|
||||||
|
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=str(stderr))
|
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:
|
data: dict[str, str] = json.loads(stdout)
|
||||||
raise HTTPException(status_code=500,detail="Could not load flake")
|
|
||||||
|
if data.get("storePath") is None:
|
||||||
|
raise HTTPException(status_code=500, detail="Could not load flake")
|
||||||
|
|
||||||
content: str
|
content: str
|
||||||
with open(Path(data.get("storePath", "")) / Path("flake.nix")) as f:
|
with open(Path(data.get("storePath", "")) / Path("flake.nix")) as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
|
|
||||||
# TODO: Figure out some measure when it is insecure to inspect or create a VM
|
# 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/inspect", uri="api/vms/inspect"))
|
||||||
actions.append(FlakeAction(id="vms/create", uri = f"api/vms/create"))
|
actions.append(FlakeAction(id="vms/create", uri="api/vms/create"))
|
||||||
|
|
||||||
return FlakeResponse(content=content, actions=actions )
|
return FlakeResponse(content=content, actions=actions)
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import List
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ONLINE = "online"
|
ONLINE = "online"
|
||||||
@@ -47,10 +48,11 @@ class VmInspectResponse(BaseModel):
|
|||||||
config: VmConfig
|
config: VmConfig
|
||||||
|
|
||||||
|
|
||||||
class FlakeAction(BaseModel):
|
class FlakeAction(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
uri: str
|
uri: str
|
||||||
|
|
||||||
|
|
||||||
class FlakeResponse(BaseModel):
|
class FlakeResponse(BaseModel):
|
||||||
content: str
|
content: str
|
||||||
actions: List[FlakeAction]
|
actions: List[FlakeAction]
|
||||||
|
|||||||
Reference in New Issue
Block a user