refactor(api): remove DI callables, routers import common directly
This commit is contained in:
+24
-22
@@ -1,20 +1,22 @@
|
||||
import os
|
||||
|
||||
from fastapi import APIRouter
|
||||
from common import (
|
||||
_build_pod_to_containers_map,
|
||||
_map_pod_to_unit,
|
||||
_podman_action_post,
|
||||
_podman_delete,
|
||||
_podman_get_json,
|
||||
_podman_post,
|
||||
_systemd_then_podman,
|
||||
)
|
||||
|
||||
|
||||
def init_pods_router(
|
||||
session,
|
||||
podman_api_base: str,
|
||||
workloads_dir: str,
|
||||
podman_get_json,
|
||||
podman_post,
|
||||
podman_delete,
|
||||
systemctl_func,
|
||||
podman_action_post,
|
||||
map_pod_to_unit,
|
||||
systemd_then_podman,
|
||||
build_pod_to_containers_map,
|
||||
) -> APIRouter:
|
||||
router = APIRouter(tags=["pods"])
|
||||
|
||||
@@ -23,7 +25,7 @@ def init_pods_router(
|
||||
for p in api_pods:
|
||||
name = p.get("Name")
|
||||
status = p.get("Status", "unknown")
|
||||
unit = map_pod_to_unit(name) if name else ""
|
||||
unit = _map_pod_to_unit(name) if name else ""
|
||||
dashboard.append({
|
||||
"Name": name,
|
||||
"Status": status,
|
||||
@@ -39,7 +41,7 @@ def init_pods_router(
|
||||
if f.endswith((".yaml", ".yml")):
|
||||
base = os.path.splitext(os.path.basename(f))[0]
|
||||
pod_name = f"pod{base}"
|
||||
unit_name = map_pod_to_unit(pod_name)
|
||||
unit_name = _map_pod_to_unit(pod_name)
|
||||
|
||||
if pod_name not in by_name:
|
||||
code, out = systemctl_func(["systemctl", "--user", "is-active", unit_name])
|
||||
@@ -54,7 +56,7 @@ def init_pods_router(
|
||||
|
||||
def try_systemd_pod_action(action: str, podname: str):
|
||||
# If systemd unit exists/allowed, prefer it.
|
||||
unit = map_pod_to_unit(podname)
|
||||
unit = _map_pod_to_unit(podname)
|
||||
if not unit:
|
||||
return None
|
||||
code, out = systemctl_func(["systemctl", "--user", action, unit])
|
||||
@@ -71,7 +73,7 @@ def init_pods_router(
|
||||
def list_pods():
|
||||
# Cruciaal: ?all=true zorgt dat EXIT_STATE pods ook getoond worden
|
||||
url = f"{podman_api_base}/libpod/pods/json?all=true"
|
||||
return podman_get_json(url)
|
||||
return _podman_get_json(session, url)
|
||||
|
||||
@router.post("/actions/{action}/{name}")
|
||||
def take_action(action: str, name: str):
|
||||
@@ -81,7 +83,7 @@ def init_pods_router(
|
||||
if action == "start":
|
||||
# STAP 1: Probeer direct de pod te starten (de 'Cockpit' methode)
|
||||
for target in possible_names:
|
||||
res = podman_post(f"{podman_api_base}/libpod/pods/{target}/start")
|
||||
res = _podman_post(session, f"{podman_api_base}/libpod/pods/{target}/start")
|
||||
if res.status_code in (200, 204):
|
||||
return {"status": "started", "target": target, "method": "direct"}
|
||||
|
||||
@@ -96,15 +98,15 @@ def init_pods_router(
|
||||
if target_path:
|
||||
with open(target_path, 'r') as file:
|
||||
yaml_content = file.read()
|
||||
res = podman_post(f"{podman_api_base}/libpod/kube/play", data=yaml_content)
|
||||
res = _podman_post(session, f"{podman_api_base}/libpod/kube/play", data=yaml_content)
|
||||
|
||||
# SPECIALE CASE: Pod bestaat al, forceer dan restart
|
||||
if res.status_code == 500 and "already exists" in res.text:
|
||||
print(f"DEBUG: Forceer herstart voor {name} wegens conflict")
|
||||
for target in possible_names:
|
||||
podman_delete(f"{podman_api_base}/libpod/pods/{target}?force=true")
|
||||
_podman_delete(session, f"{podman_api_base}/libpod/pods/{target}?force=true")
|
||||
# Probeer het nu opnieuw
|
||||
retry_res = podman_post(f"{podman_api_base}/libpod/kube/play", data=yaml_content)
|
||||
retry_res = _podman_post(session, f"{podman_api_base}/libpod/kube/play", data=yaml_content)
|
||||
return retry_res.json()
|
||||
|
||||
return res.json()
|
||||
@@ -113,7 +115,7 @@ def init_pods_router(
|
||||
|
||||
if action == "stop":
|
||||
for target in possible_names:
|
||||
res = podman_post(f"{podman_api_base}/libpod/pods/{target}/stop")
|
||||
res = _podman_post(session, f"{podman_api_base}/libpod/pods/{target}/stop")
|
||||
if res.status_code in (200, 204):
|
||||
return {"status": "stopped", "target": target}
|
||||
return {"status": "not found"}
|
||||
@@ -125,11 +127,11 @@ def init_pods_router(
|
||||
dashboard = []
|
||||
|
||||
# 0) Bouw mapping: pod_name -> [container_names...]
|
||||
containers = podman_get_json(f"{podman_api_base}/libpod/containers/json?all=true")
|
||||
pod_to_containers = build_pod_to_containers_map(containers)
|
||||
containers = _podman_get_json(session, f"{podman_api_base}/libpod/containers/json?all=true")
|
||||
pod_to_containers = _build_pod_to_containers_map(containers)
|
||||
|
||||
# 1) A) echte pods
|
||||
api_pods = podman_get_json(f"{podman_api_base}/libpod/pods/json?all=true")
|
||||
api_pods = _podman_get_json(session, f"{podman_api_base}/libpod/pods/json?all=true")
|
||||
by_name = {p.get("Name"): p for p in api_pods}
|
||||
|
||||
_append_podman_pods_dashboard_rows(dashboard, api_pods, pod_to_containers)
|
||||
@@ -151,10 +153,10 @@ def init_pods_router(
|
||||
def _podman_call(systemd_res):
|
||||
if systemd_res:
|
||||
note = "systemd failed; falling back to podman"
|
||||
podman = podman_action_post("pods", podname, action).json()
|
||||
podman = _podman_action_post(session, podman_api_base, "pods", podname, action).json()
|
||||
return {"method": "systemd_then_podman", "note": note, "systemd": systemd_res, "podman": podman}
|
||||
return {"method": "podman", "result": podman_action_post("pods", podname, action).json()}
|
||||
return {"method": "podman", "result": _podman_action_post(session, podman_api_base, "pods", podname, action).json()}
|
||||
|
||||
return systemd_then_podman(_systemd_call, _podman_call)
|
||||
return _systemd_then_podman(_systemd_call, _podman_call)
|
||||
|
||||
return router
|
||||
|
||||
Reference in New Issue
Block a user