Add service restart API and modal restart action
Agent-Logs-Url: https://github.com/naturallaw777/staging_alpha/sessions/8e6c98f7-8b24-4ec0-944b-0310e0989495 Co-authored-by: naturallaw777 <99053422+naturallaw777@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
8fd08057d8
commit
fce4608647
@@ -2963,6 +2963,35 @@ async def api_ping():
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@app.post("/api/service/{unit}/restart")
|
||||
async def api_service_restart(unit: str):
|
||||
cfg = load_config()
|
||||
services = cfg.get("services", [])
|
||||
allowed_units = {
|
||||
str(s.get("unit", "")).strip()
|
||||
for s in services
|
||||
if s.get("unit")
|
||||
}
|
||||
if unit not in allowed_units:
|
||||
raise HTTPException(status_code=404, detail="Service not found")
|
||||
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"systemctl", "restart", unit,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
_, stderr = await proc.communicate()
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to restart service: {exc}")
|
||||
|
||||
if proc.returncode != 0:
|
||||
detail = stderr.decode(errors="ignore").strip() or "systemctl restart failed"
|
||||
raise HTTPException(status_code=500, detail=detail)
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@app.post("/api/reboot")
|
||||
async def api_reboot():
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user