Merge pull request 'add stub api for machines' (#166) from Mic92-main into main

This commit is contained in:
clan-bot
2023-08-24 14:27:26 +00:00
7 changed files with 93 additions and 10 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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(

View File

@@ -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)

View File

@@ -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())

View File

@@ -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"])

View File

@@ -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