From 7479fca82b47beff2ffd9eabbc0df0d25e1de0dc Mon Sep 17 00:00:00 2001 From: Qubasa Date: Fri, 22 Sep 2023 19:58:45 +0200 Subject: [PATCH] Started working on vm_create --- pkgs/clan-cli/clan_cli/custom_logger.py | 4 ++-- pkgs/clan-cli/clan_cli/webui/app.py | 8 ++++++-- pkgs/clan-cli/clan_cli/webui/routers/vms.py | 5 ++++- pkgs/clan-cli/tests/test_vms_api.py | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/custom_logger.py b/pkgs/clan-cli/clan_cli/custom_logger.py index 242e8028a..52d3f1ae0 100644 --- a/pkgs/clan-cli/clan_cli/custom_logger.py +++ b/pkgs/clan-cli/clan_cli/custom_logger.py @@ -12,7 +12,7 @@ class CustomFormatter(logging.Formatter): def format_str(color): reset = "\x1b[0m" - return f"%(asctime)s - %(name)s - {color} %(levelname)s {reset} - %(message)s (%(filename)s:%(lineno)d)" + return f"{color}%(levelname)s{reset}:(%(filename)s:%(lineno)d): %(message)s" FORMATS = { logging.DEBUG: format_str(blue), @@ -36,7 +36,7 @@ class CustomFormatter(logging.Formatter): def register(level): ch = logging.StreamHandler() - ch.setLevel(logging.DEBUG) + ch.setLevel(level) ch.setFormatter(CustomFormatter()) logging.basicConfig(level=level, handlers=[ch]) diff --git a/pkgs/clan-cli/clan_cli/webui/app.py b/pkgs/clan-cli/clan_cli/webui/app.py index 8b7f645a9..f14180632 100644 --- a/pkgs/clan-cli/clan_cli/webui/app.py +++ b/pkgs/clan-cli/clan_cli/webui/app.py @@ -11,6 +11,8 @@ from .routers import flake, health, machines, root, vms origins = [ "http://localhost:3000", ] +# Logging setup +log = logging.getLogger(__name__) def setup_app() -> FastAPI: @@ -37,6 +39,8 @@ def setup_app() -> FastAPI: return app -custom_logger.register(logging.getLogger('uvicorn').level) +#TODO: How do I get the log level from the command line in here? +custom_logger.register(logging.DEBUG) app = setup_app() - +log.warn("log warn") +log.debug("log debug") diff --git a/pkgs/clan-cli/clan_cli/webui/routers/vms.py b/pkgs/clan-cli/clan_cli/webui/routers/vms.py index 8a55350b8..95688b23a 100644 --- a/pkgs/clan-cli/clan_cli/webui/routers/vms.py +++ b/pkgs/clan-cli/clan_cli/webui/routers/vms.py @@ -2,7 +2,7 @@ import asyncio import json import shlex from typing import Annotated, AsyncIterator - +import logging from fastapi import APIRouter, Body, HTTPException, Request, status, logger from fastapi.encoders import jsonable_encoder @@ -11,6 +11,8 @@ from fastapi.responses import JSONResponse, StreamingResponse from ...nix import nix_build, nix_eval from ..schemas import VmConfig, VmInspectResponse +# Logging setup +log = logging.getLogger(__name__) router = APIRouter() @@ -94,6 +96,7 @@ command output: async def vm_build(vm: VmConfig) -> AsyncIterator[str]: cmd = nix_build_vm(vm.flake_attr, flake_url=vm.flake_url) + log.debug(f"Running command: {shlex.join(cmd)}") proc = await asyncio.create_subprocess_exec( cmd[0], *cmd[1:], diff --git a/pkgs/clan-cli/tests/test_vms_api.py b/pkgs/clan-cli/tests/test_vms_api.py index 8935e6cc0..7d202a1c5 100644 --- a/pkgs/clan-cli/tests/test_vms_api.py +++ b/pkgs/clan-cli/tests/test_vms_api.py @@ -20,6 +20,7 @@ def test_inspect(api: TestClient, test_flake_with_core: Path) -> None: @pytest.mark.impure def test_create(api: TestClient, test_flake_with_core: Path) -> None: + print(f"flake_url: {test_flake_with_core} ") response = api.post( "/api/vms/create", json=dict(