feat: server console (Feature 2) & complete roadmap
This commit is contained in:
+28
-1
@@ -8,8 +8,9 @@ Server-Wartung hinein (siehe Roadmap: Server-Management).
|
||||
|
||||
import shlex
|
||||
import subprocess
|
||||
import asyncio
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, WebSocket, WebSocketDisconnect
|
||||
from pydantic import BaseModel
|
||||
|
||||
from auth import auth
|
||||
@@ -46,3 +47,29 @@ def reboot(req: PwdReq):
|
||||
cmd = f"echo {shlex.quote(req.password)} | sudo -S reboot"
|
||||
subprocess.Popen(["bash", "-c", cmd])
|
||||
return {"ok": True, "note": "Reboot getriggert."}
|
||||
|
||||
@router.websocket("/logs/{service}")
|
||||
async def stream_logs(websocket: WebSocket, service: str):
|
||||
await websocket.accept()
|
||||
if service not in ("llama-swap", "mission-control"):
|
||||
await websocket.close(code=1008)
|
||||
return
|
||||
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
"sudo", "journalctl", "-u", service, "-n", "100", "-f",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE
|
||||
)
|
||||
try:
|
||||
while True:
|
||||
line = await process.stdout.readline()
|
||||
if not line:
|
||||
break
|
||||
await websocket.send_text(line.decode("utf-8", errors="replace"))
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
finally:
|
||||
try:
|
||||
process.terminate()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user