refactor: verplaats run() duplicaat naar common.py
run() stond identiek in app.py en app_system.py. Verplaatst naar common.py als single source of truth; beide modules importeren nu de centrale versie. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+1
-9
@@ -1,4 +1,3 @@
|
||||
import subprocess
|
||||
from app_images import init_images_router
|
||||
from app_files import init_files_router
|
||||
from app_pods import init_pods_router
|
||||
@@ -9,6 +8,7 @@ from fastapi import FastAPI
|
||||
import requests_unixsocket
|
||||
from common import (
|
||||
_systemctl as _common_systemctl,
|
||||
run,
|
||||
)
|
||||
import uvicorn
|
||||
|
||||
@@ -44,13 +44,5 @@ app.include_router(init_pods_router(
|
||||
app.include_router(init_system_router(SESSION, PODMAN_API_BASE, WORKLOADS_DIR))
|
||||
|
||||
|
||||
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)
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
|
||||
@@ -5,6 +5,7 @@ from fastapi import APIRouter, HTTPException
|
||||
from common import (
|
||||
_podman_get_json as _common_podman_get_json,
|
||||
_systemctl as _common_systemctl,
|
||||
run,
|
||||
)
|
||||
|
||||
|
||||
@@ -68,14 +69,6 @@ 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,
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
import subprocess
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
|
||||
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 _podman_get_json_checked(session, url: str):
|
||||
r = session.get(url)
|
||||
if r.status_code >= 400:
|
||||
|
||||
Reference in New Issue
Block a user