5e7d1b887c
Backend (/api/health):
- Importeer HELPER_SOCKET uit common.py
- Voeg helper-check toe: connect() op /run/podman-helper.sock, timeout=2s
- ok blijft true als alleen de helper ontbreekt (waarschuwing, geen fout)
- Nieuwe response key: "helper": {"ok": bool}
Frontend (pingApi / setApiState):
- pingApi() roept nu /api/health aan i.p.v. /pods-dashboard
- setApiState(state, msg) accepteert 'ok' / 'warn' / 'error'
- Gele dot met --warn kleur als helper.ok=false maar core OK
- refreshActive() delegeert statusupdate aan pingApi()
- Detailbericht bij fout: toont welk component (podman/systemd) faalt
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
116 lines
3.7 KiB
Python
116 lines
3.7 KiB
Python
import os
|
|
import socket
|
|
import subprocess
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from common import (
|
|
HELPER_SOCKET,
|
|
_helper_call,
|
|
_podman_get_json as _common_podman_get_json,
|
|
_systemctl as _common_systemctl,
|
|
run,
|
|
)
|
|
|
|
|
|
def init_system_router(session, podman_api_base: str, workloads_dir: 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
|
|
|
|
helper_ok = False
|
|
try:
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
|
|
s.settimeout(2)
|
|
s.connect(HELPER_SOCKET)
|
|
helper_ok = True
|
|
except Exception:
|
|
helper_ok = False
|
|
|
|
ok = podman_ok and systemd_reachable
|
|
return {
|
|
"ok": ok,
|
|
"podman": {"ok": podman_ok},
|
|
"systemd_user": {"reachable": systemd_reachable},
|
|
"helper": {"ok": helper_ok},
|
|
}
|
|
|
|
@router.get("/test-hybrid")
|
|
def test_hybrid():
|
|
# 1. Check filesystem
|
|
try:
|
|
bestanden = []
|
|
for root, _, files in os.walk(workloads_dir):
|
|
for f in files:
|
|
bestanden.append(os.path.join(root, f))
|
|
except Exception as e:
|
|
bestanden = f"FS Fout: {str(e)}"
|
|
|
|
# 2. Check Podman API
|
|
try:
|
|
api_containers = _common_podman_get_json(session, f"{podman_api_base}/libpod/containers/json?all=true")
|
|
except Exception as e:
|
|
api_containers = f"API Fout: {str(e)}"
|
|
|
|
return {
|
|
"bestanden_gevonden": bestanden if isinstance(bestanden, list) else [],
|
|
"api_containers_aantal": len(api_containers) if isinstance(api_containers, list) else -1,
|
|
"api_raw_sample": api_containers[0] if isinstance(api_containers, list) and api_containers else api_containers,
|
|
}
|
|
|
|
def _systemctl(cmd):
|
|
return _common_systemctl(cmd, run)
|
|
|
|
def _run_systemctl_action(action: str, unit: str):
|
|
cmd = ["systemctl", "--user", action, unit]
|
|
return _systemctl(cmd)
|
|
|
|
@router.post("/daemon-reload")
|
|
def api_daemon_reload():
|
|
try:
|
|
code, out = _systemctl(["systemctl", "--user", "daemon-reload"])
|
|
return {
|
|
"cmd": "systemctl --user daemon-reload",
|
|
"exit": code,
|
|
"output": out,
|
|
}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.post("/{action}/{unit}")
|
|
def api_action(action: str, unit: str):
|
|
if action not in ("status", "start", "stop", "restart"):
|
|
raise HTTPException(status_code=400, detail="Invalid action")
|
|
cmd = ["systemctl", "--user", action, unit]
|
|
if action in ("start", "stop", "restart"):
|
|
code, out = _helper_call(action, unit)
|
|
else:
|
|
code, out = _run_systemctl_action(action, unit)
|
|
return {"cmd": " ".join(cmd), "exit": code, "output": out}
|
|
|
|
return router
|