diff --git a/control/Dockerfile b/control/Dockerfile index 8fb1378..5040ee5 100644 --- a/control/Dockerfile +++ b/control/Dockerfile @@ -8,5 +8,6 @@ COPY app_files.py . COPY app_networks.py . COPY app_pods.py . COPY app_containers.py . +COPY app_system.py . COPY common.py . CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/control/app.py b/control/app.py index 16c5471..b994d5f 100644 --- a/control/app.py +++ b/control/app.py @@ -5,6 +5,7 @@ from app_files import init_files_router from app_pods import init_pods_router from app_containers import init_containers_router, start_stats_poller from app_networks import init_networks_router +from app_system import init_system_router from fastapi import FastAPI, HTTPException import requests_unixsocket from common import ( @@ -29,39 +30,9 @@ def _run_systemctl_action(action: str, unit: str): cmd = ["systemctl", "--user", action, unit] return _systemctl(cmd) -@app.get("/health") -def health(): - podman_ok = False - try: - r = SESSION.get(f"{PODMAN_API_BASE}/libpod/info", timeout=2) - if r.status_code == 200: - try: - r.json() - podman_ok = True - except Exception: - podman_ok = False - except Exception: - podman_ok = False - - systemd_reachable = False - try: - res = subprocess.run( - ["systemctl", "--user", "list-units", "--no-pager", "--no-legend"], - capture_output=True, - text=True, - check=False, - timeout=2, - ) - systemd_reachable = (res.returncode == 0) - except Exception: - systemd_reachable = False - - ok = podman_ok and systemd_reachable - return {"ok": ok, "podman": {"ok": podman_ok}, "systemd_user": {"reachable": systemd_reachable}} - - # --- ROUTERS --- # Images API lives in dedicated modules to keep this file from growing further. +app.include_router(init_system_router(SESSION, PODMAN_API_BASE)) app.include_router(init_images_router(SESSION, PODMAN_API_BASE)) app.include_router(init_files_router(SESSION, PODMAN_API_BASE, WORKLOADS_DIR)) app.include_router(init_networks_router(SESSION, PODMAN_API_BASE)) diff --git a/control/app_system.py b/control/app_system.py new file mode 100644 index 0000000..23afe60 --- /dev/null +++ b/control/app_system.py @@ -0,0 +1,43 @@ +import subprocess + +from fastapi import APIRouter + + +def init_system_router(session, podman_api_base: str) -> APIRouter: + router = APIRouter(tags=["system"]) + + @router.get("/health") + def health(): + podman_ok = False + try: + r = session.get(f"{podman_api_base}/libpod/info", timeout=2) + if r.status_code == 200: + try: + r.json() + podman_ok = True + except Exception: + podman_ok = False + except Exception: + podman_ok = False + + systemd_reachable = False + try: + res = subprocess.run( + ["systemctl", "--user", "list-units", "--no-pager", "--no-legend"], + capture_output=True, + text=True, + check=False, + timeout=2, + ) + systemd_reachable = (res.returncode == 0) + except Exception: + systemd_reachable = False + + ok = podman_ok and systemd_reachable + return { + "ok": ok, + "podman": {"ok": podman_ok}, + "systemd_user": {"reachable": systemd_reachable}, + } + + return router