Files
OpenVPN-Monitoring-Simple/APP_PROFILER/routers/server_process.py
T
ayurishchevandClaude Sonnet 5.5 56dae90ff5 Guard server setup behind an initialized PKI
- Profiler: server_files_missing/require_* helpers; 409 for
  /server/configure, start/restart and POST /profiles when the PKI is
  not initialized or server files (honouring fqdn_server) are missing;
  stop stays allowed; DELETE /system/pki returns 409 while OpenVPN runs;
  GET /system/init/status adds pki_ready, server_ready, missing.
- UI: shared usePkiStatus; VPN menu item locked until the PKI is ready,
  route guard for /config/vpn, Start/Restart and New Client disabled
  with hints, lock notice on the VPN page.
- Test: one pytest (test_pki_guard.py).
- Docs: plan and summary in DOCS/Changes, API reference, README, Index.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-04 21:52:40 +03:00

54 lines
1.6 KiB
Python

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from services import process, pki
from database import get_db
from sqlalchemy.orm import Session
from utils.auth import verify_token
from typing import Optional
from fastapi import Depends
router = APIRouter(dependencies=[Depends(verify_token)])
class ProcessActionResponse(BaseModel):
status: str
message: str
stdout: Optional[str] = None
stderr: Optional[str] = None
class ProcessStats(BaseModel):
status: str
pid: Optional[int] = None
cpu_percent: float
memory_mb: float
uptime: Optional[str] = None
@router.post("/server/process/{action}", response_model=ProcessActionResponse)
def manage_process(action: str, db: Session = Depends(get_db)):
"""
Control the OpenVPN server process.
Action: start, stop, restart
"""
if action not in ["start", "stop", "restart"]:
raise HTTPException(status_code=400, detail="Invalid action. Use start, stop, or restart")
# Stop stays allowed: it must work after Clear PKI or with a broken setup
if action in ("start", "restart"):
try:
pki.require_server_ready(db)
except pki.PkiNotReady as e:
raise HTTPException(status_code=409, detail=str(e))
result = process.control_service(action)
if result["status"] == "error":
raise HTTPException(status_code=500, detail=result["message"])
return result
@router.get("/server/process/stats", response_model=ProcessStats)
def get_process_stats():
"""
Get current telemetry for the OpenVPN process.
"""
return process.get_process_stats()