Added state directory.

This commit is contained in:
Qubasa
2023-10-13 19:56:10 +02:00
parent 58b54bb9df
commit 2ad87785c0
10 changed files with 74 additions and 35 deletions

View File

@@ -1,5 +1,7 @@
# mypy: ignore-errors
import logging
from pathlib import Path
from typing import Any
from pydantic import AnyUrl, BaseModel, validator
@@ -28,7 +30,7 @@ class ClanDataPath(BaseModel):
dest: Path
@validator("dest")
def check_data_path(cls, v: Path) -> Path:
def check_data_path(cls: Any, v: Path) -> Path: # type: ignore
return validate_path(clan_data_dir(), v)
@@ -36,7 +38,7 @@ class ClanFlakePath(BaseModel):
dest: Path
@validator("dest")
def check_dest(cls, v: Path) -> Path:
def check_dest(cls: Any, v: Path) -> Path: # type: ignore
return validate_path(clan_flake_dir(), v)

View File

@@ -6,15 +6,15 @@ from typing import Annotated
from fastapi import APIRouter, Body, HTTPException, status
from pydantic import AnyUrl
from clan_cli.webui.api_inputs import (
FlakeCreateInput,
)
from clan_cli.webui.api_outputs import (
FlakeAction,
FlakeAttrResponse,
FlakeCreateResponse,
FlakeResponse,
)
from clan_cli.webui.api_inputs import (
FlakeCreateInput,
)
from ...async_cmd import run
from ...flake import create
@@ -25,11 +25,11 @@ router = APIRouter()
# TODO: Check for directory traversal
async def get_attrs(url: AnyUrl | Path) -> list[str]:
cmd = nix_flake_show(url)
stdout, stderr = await run(cmd)
out = await run(cmd)
data: dict[str, dict] = {}
try:
data = json.loads(stdout)
data = json.loads(out.stdout)
except JSONDecodeError:
raise HTTPException(status_code=422, detail="Could not load flake.")
@@ -57,8 +57,8 @@ async def inspect_flake(
# Extract the flake from the given URL
# We do this by running 'nix flake prefetch {url} --json'
cmd = nix_command(["flake", "prefetch", str(url), "--json", "--refresh"])
stdout, stderr = await run(cmd)
data: dict[str, str] = json.loads(stdout)
out = await run(cmd)
data: dict[str, str] = json.loads(out.stdout)
if data.get("storePath") is None:
raise HTTPException(status_code=500, detail="Could not load flake")

View File

@@ -37,7 +37,8 @@ async def list_machines() -> MachinesResponse:
@router.post("/api/machines", status_code=201)
async def create_machine(machine: Annotated[MachineCreate, Body()]) -> MachineResponse:
_create_machine(machine.name)
out = await _create_machine(machine.name)
log.debug(out)
return MachineResponse(machine=Machine(name=machine.name, status=Status.UNKNOWN))

View File

@@ -1,4 +1,5 @@
import logging
from pathlib import Path
from typing import Annotated, Iterator
from uuid import UUID
@@ -6,13 +7,17 @@ from fastapi import APIRouter, Body, status
from fastapi.exceptions import HTTPException
from fastapi.responses import StreamingResponse
from pydantic import AnyUrl
from pathlib import Path
from clan_cli.webui.routers.flake import get_attrs
from ...task_manager import get_task
from ...vms import create, inspect
from ..api_outputs import VmConfig, VmCreateResponse, VmInspectResponse, VmStatusResponse
from ..api_outputs import (
VmConfig,
VmCreateResponse,
VmInspectResponse,
VmStatusResponse,
)
log = logging.getLogger(__name__)
router = APIRouter()