refactor(api): move system endpoints into app_system router
This commit is contained in:
+1
-37
@@ -5,7 +5,7 @@ from app_pods import init_pods_router
|
|||||||
from app_containers import init_containers_router, start_stats_poller
|
from app_containers import init_containers_router, start_stats_poller
|
||||||
from app_networks import init_networks_router
|
from app_networks import init_networks_router
|
||||||
from app_system import init_system_router
|
from app_system import init_system_router
|
||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI
|
||||||
import requests_unixsocket
|
import requests_unixsocket
|
||||||
from common import (
|
from common import (
|
||||||
_systemctl as _common_systemctl,
|
_systemctl as _common_systemctl,
|
||||||
@@ -24,10 +24,6 @@ async def _startup_stats_poller():
|
|||||||
def _systemctl(cmd):
|
def _systemctl(cmd):
|
||||||
return _common_systemctl(cmd, run)
|
return _common_systemctl(cmd, run)
|
||||||
|
|
||||||
def _run_systemctl_action(action: str, unit: str):
|
|
||||||
cmd = ["systemctl", "--user", action, unit]
|
|
||||||
return _systemctl(cmd)
|
|
||||||
|
|
||||||
# --- ROUTERS ---
|
# --- ROUTERS ---
|
||||||
# Images API lives in dedicated modules to keep this file from growing further.
|
# Images API lives in dedicated modules to keep this file from growing further.
|
||||||
app.include_router(init_system_router(SESSION, PODMAN_API_BASE, WORKLOADS_DIR))
|
app.include_router(init_system_router(SESSION, PODMAN_API_BASE, WORKLOADS_DIR))
|
||||||
@@ -48,38 +44,6 @@ app.include_router(init_pods_router(
|
|||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
@app.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))
|
|
||||||
|
|
||||||
|
|
||||||
@app.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]
|
|
||||||
code, out = _run_systemctl_action(action, unit)
|
|
||||||
return {"cmd": " ".join(cmd), "exit": code, "output": out}
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/<action>/<unit>")
|
|
||||||
def legacy_api_action(action: str, unit: str):
|
|
||||||
# legacy flask-like path; keep behavior (even if not used by index.html)
|
|
||||||
if action not in ("status", "start", "stop", "restart"):
|
|
||||||
return {"error": "Invalid action"}, 400
|
|
||||||
cmd = ["systemctl", "--user", action, unit]
|
|
||||||
code, out = _run_systemctl_action(action, unit)
|
|
||||||
return {"cmd": " ".join(cmd), "exit": code, "output": out}
|
|
||||||
|
|
||||||
|
|
||||||
def run(cmd):
|
def run(cmd):
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
|
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
|
||||||
|
|||||||
+49
-2
@@ -1,8 +1,11 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, HTTPException
|
||||||
from common import _podman_get_json as _common_podman_get_json
|
from common import (
|
||||||
|
_podman_get_json as _common_podman_get_json,
|
||||||
|
_systemctl as _common_systemctl,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def init_system_router(session, podman_api_base: str, workloads_dir: str) -> APIRouter:
|
def init_system_router(session, podman_api_base: str, workloads_dir: str) -> APIRouter:
|
||||||
@@ -65,4 +68,48 @@ def init_system_router(session, podman_api_base: str, workloads_dir: str) -> API
|
|||||||
"api_raw_sample": api_containers[0] if isinstance(api_containers, list) and api_containers else api_containers,
|
"api_raw_sample": api_containers[0] if isinstance(api_containers, list) and api_containers else api_containers,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def run(cmd):
|
||||||
|
try:
|
||||||
|
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
|
||||||
|
output = (result.stdout or "") + (result.stderr or "")
|
||||||
|
return result.returncode, output.strip()
|
||||||
|
except Exception as e:
|
||||||
|
return 1, str(e)
|
||||||
|
|
||||||
|
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("/api/<action>/<unit>")
|
||||||
|
def legacy_api_action(action: str, unit: str):
|
||||||
|
# legacy flask-like path; keep behavior (even if not used by index.html)
|
||||||
|
if action not in ("status", "start", "stop", "restart"):
|
||||||
|
return {"error": "Invalid action"}, 400
|
||||||
|
cmd = ["systemctl", "--user", action, unit]
|
||||||
|
code, out = _run_systemctl_action(action, unit)
|
||||||
|
return {"cmd": " ".join(cmd), "exit": code, "output": out}
|
||||||
|
|
||||||
|
@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]
|
||||||
|
code, out = _run_systemctl_action(action, unit)
|
||||||
|
return {"cmd": " ".join(cmd), "exit": code, "output": out}
|
||||||
|
|
||||||
return router
|
return router
|
||||||
|
|||||||
Reference in New Issue
Block a user