From 80d3349ce0edad88013bf22e417c3cf8b4ba951c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 24 Aug 2023 16:11:08 +0200 Subject: [PATCH 1/3] add stub api for machines --- pkgs/clan-cli/clan_cli/webui/app.py | 3 +- .../clan_cli/webui/routers/machines.py | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 pkgs/clan-cli/clan_cli/webui/routers/machines.py diff --git a/pkgs/clan-cli/clan_cli/webui/app.py b/pkgs/clan-cli/clan_cli/webui/app.py index 52de7cc1b..7ad39bbb2 100644 --- a/pkgs/clan-cli/clan_cli/webui/app.py +++ b/pkgs/clan-cli/clan_cli/webui/app.py @@ -1,7 +1,8 @@ from fastapi import FastAPI -from .routers import health, root +from .routers import health, machines, root app = FastAPI() app.include_router(health.router) +app.include_router(machines.router) app.include_router(root.router) diff --git a/pkgs/clan-cli/clan_cli/webui/routers/machines.py b/pkgs/clan-cli/clan_cli/webui/routers/machines.py new file mode 100644 index 000000000..69cd42313 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/webui/routers/machines.py @@ -0,0 +1,65 @@ +from enum import Enum + +from fastapi import APIRouter +from pydantic import BaseModel, Field + +router = APIRouter() + + +class Status(Enum): + ONLINE = "online" + OFFLINE = "offline" + + +class Machine(BaseModel): + name: str + status: Status + + +class MachinesResponse(BaseModel): + machines: list[Machine] + + +class MachineResponse(BaseModel): + machine: Machine + + +class Config(BaseModel): + pass + + +class ConfigResponse(BaseModel): + config: Config + + +class Schema(BaseModel): + pass + + +class SchemaResponse(BaseModel): + schema_: Schema = Field(alias="schema") + + +@router.get("/api/machines") +async def list_machines() -> MachinesResponse: + return MachinesResponse(machines=[]) + + +@router.get("/api/machines/{machine}") +async def get_machine(machine: str) -> MachineResponse: + return MachineResponse(machine=Machine(name=machine, status=Status.ONLINE)) + + +@router.get("/api/machines/{machine}/config") +async def get_machine_config(machine: str) -> ConfigResponse: + return ConfigResponse(config=Config()) + + +@router.post("/api/machines/{machine}/config") +async def set_machine_config(machine: str, config: Config) -> ConfigResponse: + return ConfigResponse(config=config) + + +@router.get("/api/machines/{machine}/schema") +async def get_machine_schema(machine: str) -> SchemaResponse: + return SchemaResponse(schema=Schema()) From 56542ca5ef26dd53a2d42a3f5eb5c448380ab231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 24 Aug 2023 16:25:06 +0200 Subject: [PATCH 2/3] mv update command to machines update command --- pkgs/clan-cli/clan_cli/__init__.py | 8 ++++---- pkgs/clan-cli/clan_cli/machines/__init__.py | 17 +++++++++++++++++ pkgs/clan-cli/clan_cli/{ => machines}/update.py | 4 ++-- ...pdate_cli.py => test_machines_update_cli.py} | 4 ++-- 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 pkgs/clan-cli/clan_cli/machines/__init__.py rename pkgs/clan-cli/clan_cli/{ => machines}/update.py (96%) rename pkgs/clan-cli/tests/{test_update_cli.py => test_machines_update_cli.py} (91%) diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 52777182a..fa8573b2e 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -3,7 +3,7 @@ import sys from types import ModuleType from typing import Optional -from . import admin, secrets, update, webui +from . import admin, machines, secrets, webui # from . import admin, config, secrets, update, webui from .errors import ClanError @@ -34,10 +34,10 @@ def main() -> None: parser_secrets = subparsers.add_parser("secrets", help="manage secrets") secrets.register_parser(parser_secrets) - parser_update = subparsers.add_parser( - "update", help="update the machines in the clan" + parser_machine = subparsers.add_parser( + "machines", help="Manage machines and their configuration" ) - update.register_parser(parser_update) + machines.register_parser(parser_machine) parser_webui = subparsers.add_parser("webui", help="start webui") webui.register_parser(parser_webui) diff --git a/pkgs/clan-cli/clan_cli/machines/__init__.py b/pkgs/clan-cli/clan_cli/machines/__init__.py new file mode 100644 index 000000000..ed51575f3 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/machines/__init__.py @@ -0,0 +1,17 @@ +# !/usr/bin/env python3 +import argparse + +from .update import register_update_parser + + +# takes a (sub)parser and configures it +def register_parser(parser: argparse.ArgumentParser) -> None: + subparser = parser.add_subparsers( + title="command", + description="the command to run", + help="the command to run", + required=True, + ) + + groups_parser = subparser.add_parser("update", help="Update a machine") + register_update_parser(groups_parser) diff --git a/pkgs/clan-cli/clan_cli/update.py b/pkgs/clan-cli/clan_cli/machines/update.py similarity index 96% rename from pkgs/clan-cli/clan_cli/update.py rename to pkgs/clan-cli/clan_cli/machines/update.py index 2e791d2d7..11a26d361 100644 --- a/pkgs/clan-cli/clan_cli/update.py +++ b/pkgs/clan-cli/clan_cli/machines/update.py @@ -2,7 +2,7 @@ import argparse import json import subprocess -from .ssh import Host, HostGroup, HostKeyCheck +from ..ssh import Host, HostGroup, HostKeyCheck def deploy_nixos(hosts: HostGroup) -> None: @@ -94,7 +94,7 @@ def update(args: argparse.Namespace) -> None: deploy_nixos(HostGroup([Host(args.host, user=args.user, meta=meta)])) -def register_parser(parser: argparse.ArgumentParser) -> None: +def register_update_parser(parser: argparse.ArgumentParser) -> None: # TODO pass all args we don't parse into ssh_args, currently it fails if arg starts with - parser.add_argument("--flake-uri", type=str, default=".#", help="nix flake uri") parser.add_argument( diff --git a/pkgs/clan-cli/tests/test_update_cli.py b/pkgs/clan-cli/tests/test_machines_update_cli.py similarity index 91% rename from pkgs/clan-cli/tests/test_update_cli.py rename to pkgs/clan-cli/tests/test_machines_update_cli.py index 707283a62..b8fb676a6 100644 --- a/pkgs/clan-cli/tests/test_update_cli.py +++ b/pkgs/clan-cli/tests/test_machines_update_cli.py @@ -8,12 +8,12 @@ import pytest from environment import mock_env from host_group import HostGroup -from clan_cli.update import deploy_nixos, register_parser +from clan_cli.machines.update import deploy_nixos, register_update_parser def test_cli() -> None: parser = argparse.ArgumentParser() - register_parser(parser) + register_update_parser(parser) with pytest.raises(SystemExit): parser.parse_args(["--help"]) From be78e65b111ac7e2518384e6eb75971b2b1785e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 24 Aug 2023 16:26:03 +0200 Subject: [PATCH 3/3] pre-commit: allow treefmt cache cache should work now and speeds up things --- scripts/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pre-commit b/scripts/pre-commit index 5760641b8..1590d565e 100755 --- a/scripts/pre-commit +++ b/scripts/pre-commit @@ -46,7 +46,7 @@ git stash push --quiet --keep-index --message "treefmt pre-commit" trap restore_stash EXIT # Run treefmt on the files in the index and record the result. -nix fmt -- --no-cache "${commit_files[@]}" +nix fmt -- "${commit_files[@]}" # Check if there is a diff git diff --name-only --exit-code