get rid of pydantic in cli
This commit is contained in:
@@ -3,20 +3,14 @@ import argparse
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
from pydantic import AnyUrl
|
||||
from pydantic.tools import parse_obj_as
|
||||
|
||||
from ..async_cmd import CmdOut, run, runforcli
|
||||
from ..errors import ClanError
|
||||
from ..nix import nix_command, nix_shell
|
||||
|
||||
DEFAULT_URL: AnyUrl = parse_obj_as(
|
||||
AnyUrl,
|
||||
"git+https://git.clan.lol/clan/clan-core?new-clan",
|
||||
)
|
||||
DEFAULT_URL: str = "git+https://git.clan.lol/clan/clan-core?new-clan"
|
||||
|
||||
|
||||
async def create_flake(directory: Path, url: AnyUrl) -> Dict[str, CmdOut]:
|
||||
async def create_flake(directory: Path, url: str) -> Dict[str, CmdOut]:
|
||||
if not directory.exists():
|
||||
directory.mkdir()
|
||||
else:
|
||||
|
||||
@@ -5,8 +5,6 @@ import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from pydantic import AnyUrl
|
||||
|
||||
from .dirs import nixpkgs_flake, nixpkgs_source
|
||||
|
||||
|
||||
@@ -14,7 +12,7 @@ def nix_command(flags: list[str]) -> list[str]:
|
||||
return ["nix", "--extra-experimental-features", "nix-command flakes"] + flags
|
||||
|
||||
|
||||
def nix_flake_show(flake_url: AnyUrl | Path) -> list[str]:
|
||||
def nix_flake_show(flake_url: str | Path) -> list[str]:
|
||||
return nix_command(
|
||||
[
|
||||
"flake",
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
from pydantic import AnyUrl
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
FlakeUrl = Union[AnyUrl, Path]
|
||||
@@ -1,16 +1,16 @@
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import AnyUrl, BaseModel
|
||||
|
||||
from ..async_cmd import run
|
||||
from ..nix import nix_config, nix_eval
|
||||
|
||||
|
||||
class VmConfig(BaseModel):
|
||||
flake_url: AnyUrl | Path
|
||||
@dataclass
|
||||
class VmConfig:
|
||||
flake_url: str | Path
|
||||
flake_attr: str
|
||||
|
||||
cores: int
|
||||
@@ -18,7 +18,7 @@ class VmConfig(BaseModel):
|
||||
graphics: bool
|
||||
|
||||
|
||||
async def inspect_vm(flake_url: AnyUrl | Path, flake_attr: str) -> VmConfig:
|
||||
async def inspect_vm(flake_url: str | Path, flake_attr: str) -> VmConfig:
|
||||
config = nix_config()
|
||||
system = config["system"]
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import logging
|
||||
|
||||
from pydantic import AnyUrl, BaseModel, Extra
|
||||
from pydantic import AnyUrl, BaseModel, Extra, parse_obj_as
|
||||
|
||||
from ..flakes.create import DEFAULT_URL
|
||||
|
||||
@@ -8,7 +8,7 @@ log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FlakeCreateInput(BaseModel):
|
||||
url: AnyUrl = DEFAULT_URL
|
||||
url: AnyUrl = parse_obj_as(AnyUrl, DEFAULT_URL)
|
||||
|
||||
|
||||
class MachineConfig(BaseModel):
|
||||
|
||||
@@ -5,7 +5,6 @@ from pydantic import BaseModel, Extra, Field
|
||||
|
||||
from ..async_cmd import CmdOut
|
||||
from ..task_manager import TaskStatus
|
||||
from ..vms.inspect import VmConfig
|
||||
|
||||
|
||||
class Status(Enum):
|
||||
@@ -62,10 +61,6 @@ class FlakeAttrResponse(BaseModel):
|
||||
flake_attrs: list[str]
|
||||
|
||||
|
||||
class VmInspectResponse(BaseModel):
|
||||
config: VmConfig
|
||||
|
||||
|
||||
class FlakeAction(BaseModel):
|
||||
id: str
|
||||
uri: str
|
||||
|
||||
@@ -7,7 +7,7 @@ from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from .assets import asset_path
|
||||
from .error_handlers import clan_error_handler
|
||||
from .routers import clan_modules, flake, health, machines, root, vms
|
||||
from .routers import clan_modules, flake, health, machines, root
|
||||
from .settings import settings
|
||||
from .tags import tags_metadata
|
||||
|
||||
@@ -31,7 +31,6 @@ def setup_app() -> FastAPI:
|
||||
app.include_router(flake.router)
|
||||
app.include_router(health.router)
|
||||
app.include_router(machines.router)
|
||||
app.include_router(vms.router)
|
||||
|
||||
# Needs to be last in register. Because of wildcard route
|
||||
app.include_router(root.router)
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Iterator
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Body, status
|
||||
from fastapi.exceptions import HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import AnyUrl
|
||||
|
||||
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 ..tags import Tags
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
# TODO: Check for directory traversal
|
||||
@router.post("/api/vms/inspect", tags=[Tags.vm])
|
||||
async def inspect_vm(
|
||||
flake_url: Annotated[AnyUrl | Path, Body()], flake_attr: Annotated[str, Body()]
|
||||
) -> VmInspectResponse:
|
||||
config = await inspect.inspect_vm(flake_url, flake_attr)
|
||||
return VmInspectResponse(config=config)
|
||||
|
||||
|
||||
@router.get("/api/vms/{uuid}/status", tags=[Tags.vm])
|
||||
async def get_vm_status(uuid: UUID) -> VmStatusResponse:
|
||||
task = get_task(uuid)
|
||||
log.debug(msg=f"error: {task.error}, task.status: {task.status}")
|
||||
error = str(task.error) if task.error is not None else None
|
||||
return VmStatusResponse(status=task.status, error=error)
|
||||
|
||||
|
||||
@router.get("/api/vms/{uuid}/logs", tags=[Tags.vm])
|
||||
async def get_vm_logs(uuid: UUID) -> StreamingResponse:
|
||||
# Generator function that yields log lines as they are available
|
||||
def stream_logs() -> Iterator[str]:
|
||||
task = get_task(uuid)
|
||||
|
||||
yield from task.log_lines()
|
||||
|
||||
return StreamingResponse(
|
||||
content=stream_logs(),
|
||||
media_type="text/plain",
|
||||
)
|
||||
|
||||
|
||||
# TODO: Check for directory traversal
|
||||
@router.post("/api/vms/create", tags=[Tags.vm])
|
||||
async def create_vm(vm: Annotated[VmConfig, Body()]) -> VmCreateResponse:
|
||||
flake_attrs = await get_attrs(vm.flake_url)
|
||||
if vm.flake_attr not in flake_attrs:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Provided attribute '{vm.flake_attr}' does not exist.",
|
||||
)
|
||||
task = create.create_vm(vm)
|
||||
return VmCreateResponse(uuid=str(task.uuid))
|
||||
Reference in New Issue
Block a user