refactor(api): move /health into app_system router

This commit is contained in:
kodi
2026-02-28 10:51:22 +01:00
parent 492edc2ec0
commit e61f2ccf76
3 changed files with 46 additions and 31 deletions
+1
View File
@@ -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"]
+2 -31
View File
@@ -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))
+43
View File
@@ -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