compute_os/backend/app/main.py

615 lines
24 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""Local service manager API backed by the current user's systemd instance."""
from __future__ import annotations
import asyncio
import csv
import hmac
import json
import os
import platform
import re
import shlex
import shutil
import sqlite3
import socket
import subprocess
import time
import uuid
from contextlib import asynccontextmanager
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Literal
import psutil
from fastapi import Depends, FastAPI, Header, HTTPException, Query, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field, field_validator
BASE_DIR = Path(__file__).resolve().parents[1]
DATABASE_PATH = Path(os.getenv("PANEL_DATABASE_PATH", str(Path.home() / ".local" / "share" / "compute-service-manager" / "panel.db")))
SERVICE_HOME = Path(os.getenv("PANEL_SERVICE_HOME", str(Path.home() / ".local" / "share" / "compute-service-manager")))
UNIT_DIR = Path(os.getenv("PANEL_UNIT_DIR", str(Path.home() / ".config" / "systemd" / "user")))
API_TOKEN = os.getenv("PANEL_API_TOKEN", "")
ENV_KEY_PATTERN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
_operation_lock = asyncio.Lock()
class AppInput(BaseModel):
"""Fields needed to run a Python service without accepting arbitrary shell text."""
name: str = Field(min_length=1, max_length=80)
description: str = Field(default="", max_length=240)
python_path: str = Field(min_length=1, max_length=1000)
script_path: str = Field(min_length=1, max_length=1000)
working_dir: str = Field(min_length=1, max_length=1000)
args: list[str] = Field(default_factory=list, max_length=100)
env: dict[str, str] = Field(default_factory=dict, max_length=100)
@field_validator("name")
@classmethod
def trim_name(cls, value: str) -> str:
value = value.strip()
if not value or any(ord(char) < 32 for char in value):
raise ValueError("应用名称不能为空,也不能包含控制字符")
return value
@field_validator("python_path", "script_path", "working_dir")
@classmethod
def require_absolute_path(cls, value: str) -> str:
if any(ord(char) < 32 for char in value):
raise ValueError("路径不能包含控制字符")
candidate = Path(value).expanduser()
if not candidate.is_absolute():
raise ValueError("路径必须是绝对路径")
return str(candidate)
@field_validator("env")
@classmethod
def validate_environment(cls, value: dict[str, str]) -> dict[str, str]:
for key in value:
if not ENV_KEY_PATTERN.fullmatch(key):
raise ValueError(f"无效的环境变量名:{key}")
if "\x00" in value[key]:
raise ValueError(f"环境变量 {key} 不能包含 NUL 字符")
return value
@field_validator("args")
@classmethod
def validate_args(cls, value: list[str]) -> list[str]:
if any("\x00" in part for part in value):
raise ValueError("启动参数不能包含 NUL 字符")
return value
class AppRecord(AppInput):
id: str
status: Literal["running", "stopped", "failed", "unknown"] = "stopped"
pid: int | None = None
active_since: str | None = None
created_at: str
updated_at: str
class AppAction(BaseModel):
action: Literal["start", "stop", "restart"]
def utc_now() -> str:
return datetime.now(timezone.utc).isoformat()
class LocalSystemMonitor:
"""Collect host metrics in-process using local OS counters, not a remote monitor API."""
def __init__(self) -> None:
self.latest: dict[str, Any] | None = None
self.error: str | None = None
self._last_network = psutil.net_io_counters()
self._last_network_at = time.monotonic()
@staticmethod
def _cpu_names() -> list[str]:
name = platform.processor().strip()
if not name and Path("/proc/cpuinfo").exists():
try:
for line in Path("/proc/cpuinfo").read_text(errors="replace").splitlines():
if line.lower().startswith(("model name", "hardware")) and ":" in line:
name = line.split(":", 1)[1].strip()
if name:
break
except OSError:
pass
return [name or platform.machine() or "CPU"]
@staticmethod
def _gpu_metrics() -> tuple[list[str], list[dict[str, Any]]]:
"""Read NVIDIA telemetry when the vendor utility is installed; other GPUs stay empty."""
command = shutil.which("nvidia-smi")
if not command:
return [], []
try:
result = subprocess.run(
[command, "--query-gpu=index,name,utilization.gpu,memory.used,memory.total", "--format=csv,noheader,nounits"],
check=False,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
timeout=3,
)
except (OSError, subprocess.TimeoutExpired):
return [], []
if result.returncode != 0:
return [], []
names: list[str] = []
gpus: list[dict[str, Any]] = []
for row in csv.reader(result.stdout.splitlines(), skipinitialspace=True):
if len(row) < 5:
continue
try:
index = int(row[0].strip())
utilization = float(row[2].strip())
memory_used = float(row[3].strip())
memory_total = float(row[4].strip())
except ValueError:
continue
name = row[1].strip()
names.append(name)
gpus.append({
"index": index,
"name": name,
"utilization": utilization,
"memory_used_mb": memory_used,
"memory_total_mb": memory_total,
})
return names, gpus
def collect(self) -> dict[str, Any]:
"""Build a Nezha-shaped local snapshot so the existing dashboard can render it."""
# A short blocking sample is safe here because collect() runs in a worker thread.
cpu_percent = psutil.cpu_percent(interval=0.25)
captured_at = datetime.now(timezone.utc)
memory = psutil.virtual_memory()
disk_path = Path.home().anchor or os.path.abspath(os.sep)
disk = psutil.disk_usage(disk_path)
network = psutil.net_io_counters()
now = time.monotonic()
elapsed = max(now - self._last_network_at, 0.001)
incoming = max(network.bytes_recv - self._last_network.bytes_recv, 0)
outgoing = max(network.bytes_sent - self._last_network.bytes_sent, 0)
self._last_network, self._last_network_at = network, now
cpu_names = self._cpu_names()
gpu_names, gpus = self._gpu_metrics()
try:
load_1m = os.getloadavg()[0]
except (AttributeError, OSError):
load_1m = None
try:
temperatures = [
{"name": name, "temperature": entry.current}
for name, entries in psutil.sensors_temperatures(fahrenheit=False).items()
for entry in entries
if entry.current is not None
]
except (AttributeError, NotImplementedError, OSError):
temperatures = []
host_name = socket.gethostname()
uptime = max(int(time.time() - psutil.boot_time()), 0)
return {
"source": "local",
"connection_status": "connected",
"captured_at": captured_at.isoformat(),
"server": {
"id": None,
"name": host_name,
"platform": f"{platform.system()} {platform.release()}".strip(),
"cpu_names": cpu_names,
"gpu_names": gpu_names,
"uptime_seconds": uptime,
"last_active": captured_at.isoformat(),
"is_online": True,
},
"cpu": {
"percent": cpu_percent,
"load_1m": load_1m,
"cores": psutil.cpu_count(logical=True),
"cpu_names": cpu_names,
},
"memory": {"used_bytes": memory.used, "total_bytes": memory.total, "percent": memory.percent},
"disk": {"path": disk_path, "used_bytes": disk.used, "total_bytes": disk.total, "percent": disk.percent},
"gpus": gpus,
"network": {"in_bytes_per_second": incoming / elapsed, "out_bytes_per_second": outgoing / elapsed},
"temperatures": temperatures,
"error": None,
}
async def run(self) -> None:
"""Sample outside the event loop and refresh the snapshot every five seconds."""
while True:
try:
self.latest = await asyncio.to_thread(self.collect)
self.error = None
except asyncio.CancelledError:
raise
except Exception as exc:
self.latest = None
self.error = f"本机资源采集失败:{str(exc)[:240]}"
await asyncio.sleep(5)
def snapshot(self) -> dict[str, Any]:
if self.latest is not None:
return dict(self.latest)
return {
"source": "local",
"connection_status": "unavailable" if self.error else "sampling",
"captured_at": None,
"server": None,
"cpu": None,
"memory": None,
"disk": None,
"gpus": [],
"network": None,
"temperatures": [],
"error": self.error,
}
system_monitor = LocalSystemMonitor()
def connect_db() -> sqlite3.Connection:
DATABASE_PATH.parent.mkdir(parents=True, exist_ok=True)
connection = sqlite3.connect(DATABASE_PATH, timeout=10)
connection.row_factory = sqlite3.Row
return connection
def initialize_database() -> None:
with connect_db() as connection:
connection.execute(
"""CREATE TABLE IF NOT EXISTS apps (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
python_path TEXT NOT NULL,
script_path TEXT NOT NULL,
working_dir TEXT NOT NULL,
args_json TEXT NOT NULL,
env_json TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
)"""
)
# SQLite 配置包含应用环境变量,限制数据库文件只对管理账号可读写。
DATABASE_PATH.chmod(0o600)
def require_token(authorization: str | None = Header(default=None)) -> None:
# API 操作统一使用服务端密钥鉴权,密钥不进入服务配置或日志。
if not API_TOKEN:
raise HTTPException(status_code=503, detail="请先配置 PANEL_API_TOKEN")
supplied = authorization.removeprefix("Bearer ") if authorization else ""
if not supplied or not hmac.compare_digest(supplied, API_TOKEN):
raise HTTPException(status_code=401, detail="管理密钥无效")
def unit_name(app_id: str) -> str:
return f"compute-app-{app_id}.service"
def service_paths(app_id: str) -> tuple[Path, Path]:
return UNIT_DIR / unit_name(app_id), SERVICE_HOME / "launchers" / f"{app_id}.sh"
def fetch_row(app_id: str) -> sqlite3.Row:
with connect_db() as connection:
row = connection.execute("SELECT * FROM apps WHERE id = ?", (app_id,)).fetchone()
if row is None:
raise HTTPException(status_code=404, detail="应用不存在")
return row
def to_record(row: sqlite3.Row, service_state: dict[str, str] | None = None) -> AppRecord:
state = service_state or {}
active = state.get("ActiveState", "unknown")
status: Literal["running", "stopped", "failed", "unknown"]
if active == "active":
status = "running"
elif active in {"inactive", "deactivating"}:
status = "stopped"
elif active == "failed":
status = "failed"
else:
status = "unknown"
return AppRecord(
id=row["id"],
name=row["name"],
description=row["description"],
python_path=row["python_path"],
script_path=row["script_path"],
working_dir=row["working_dir"],
args=json.loads(row["args_json"]),
env=json.loads(row["env_json"]),
status=status,
pid=int(state["MainPID"]) if state.get("MainPID", "0").isdigit() and state["MainPID"] != "0" else None,
active_since=state.get("ActiveEnterTimestamp"),
created_at=row["created_at"],
updated_at=row["updated_at"],
)
def systemctl(*args: str, check: bool = True) -> subprocess.CompletedProcess[str]:
command = ["systemctl", "--user", *args]
try:
result = subprocess.run(command, text=True, capture_output=True, timeout=30, check=False)
except FileNotFoundError as exc:
raise HTTPException(status_code=503, detail="当前系统找不到 systemctl") from exc
except subprocess.TimeoutExpired as exc:
raise HTTPException(status_code=504, detail="systemd 操作超时") from exc
if check and result.returncode != 0:
message = (result.stderr or result.stdout).strip() or "systemd 操作失败"
raise HTTPException(status_code=409, detail=message[-1200:])
return result
def journalctl(*args: str) -> subprocess.CompletedProcess[str]:
try:
return subprocess.run(
["journalctl", "--user", *args],
text=True,
capture_output=True,
timeout=20,
check=False,
)
except FileNotFoundError as exc:
raise HTTPException(status_code=503, detail="当前系统找不到 journalctl") from exc
except subprocess.TimeoutExpired as exc:
raise HTTPException(status_code=504, detail="读取服务日志超时") from exc
def read_unit_state(app_id: str) -> dict[str, str]:
result = systemctl(
"show",
unit_name(app_id),
"--property=ActiveState,MainPID,ActiveEnterTimestamp",
check=False,
)
if result.returncode != 0:
return {"ActiveState": "inactive", "MainPID": "0"}
return dict(line.split("=", 1) for line in result.stdout.splitlines() if "=" in line)
def check_runtime(app: AppInput) -> tuple[Path, Path, Path]:
python = Path(app.python_path).expanduser().resolve()
script = Path(app.script_path).expanduser().resolve()
working_dir = Path(app.working_dir).expanduser().resolve()
if not python.is_file() or not os.access(python, os.X_OK):
raise HTTPException(status_code=422, detail="Python 解释器不存在或不可执行")
if not script.is_file():
raise HTTPException(status_code=422, detail="启动脚本不存在")
if not working_dir.is_dir():
raise HTTPException(status_code=422, detail="工作目录不存在")
return python, script, working_dir
def write_service_files(app_id: str, app: AppInput) -> None:
python, script, working_dir = check_runtime(app)
unit_path, launcher_path = service_paths(app_id)
unit_path.parent.mkdir(parents=True, exist_ok=True)
launcher_path.parent.mkdir(parents=True, exist_ok=True)
# 用 argv 列表和 shlex 转义生成启动器,不拼接用户提供的 shell 片段。
command = [str(python), str(script), *app.args]
lines = ["#!/usr/bin/env bash", "set -euo pipefail", f"cd -- {shlex.quote(str(working_dir))}"]
lines.extend(f"export {key}={shlex.quote(value)}" for key, value in app.env.items())
lines.append("exec " + " ".join(shlex.quote(part) for part in command))
launcher_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
launcher_path.chmod(0o700)
# 每个应用使用独立 user unit;重启面板进程不会丢失服务的托管状态。
unit_path.write_text(
"\n".join(
[
"[Unit]",
f"Description=Managed app: {app.name.replace('%', '%%')}",
"After=network.target",
"",
"[Service]",
"Type=simple",
f"WorkingDirectory={systemd_quote(str(working_dir))}",
f"ExecStart=/usr/bin/bash {systemd_quote(str(launcher_path))}",
"Restart=on-failure",
"RestartSec=5s",
"TimeoutStopSec=30s",
"KillMode=control-group",
"",
"[Install]",
"WantedBy=default.target",
"",
]
),
encoding="utf-8",
)
unit_path.chmod(0o600)
def systemd_quote(value: str) -> str:
"""Quote a single systemd unit argument, including spaces and quote marks."""
return '"' + value.replace("\\", "\\\\").replace('"', '\\"').replace("%", "%%") + '"'
def save_app(app_id: str, app: AppInput) -> None:
timestamp = utc_now()
with connect_db() as connection:
connection.execute(
"""INSERT INTO apps
(id, name, description, python_path, script_path, working_dir, args_json, env_json, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(
app_id,
app.name,
app.description,
app.python_path,
app.script_path,
app.working_dir,
json.dumps(app.args),
json.dumps(app.env),
timestamp,
timestamp,
),
)
def update_app(app_id: str, app: AppInput) -> None:
with connect_db() as connection:
connection.execute(
"""UPDATE apps SET name=?, description=?, python_path=?, script_path=?, working_dir=?,
args_json=?, env_json=?, updated_at=? WHERE id=?""",
(
app.name,
app.description,
app.python_path,
app.script_path,
app.working_dir,
json.dumps(app.args),
json.dumps(app.env),
utc_now(),
app_id,
),
)
@asynccontextmanager
async def lifespan(_: FastAPI):
initialize_database()
monitor_task = asyncio.create_task(system_monitor.run())
try:
yield
finally:
monitor_task.cancel()
await asyncio.gather(monitor_task, return_exceptions=True)
app = FastAPI(title="Compute Service Manager API", version="0.1.0", lifespan=lifespan)
origins = [item.strip() for item in os.getenv("PANEL_CORS_ORIGINS", "http://localhost:5173").split(",") if item.strip()]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=False,
allow_methods=["GET", "POST", "PUT", "DELETE"],
allow_headers=["Authorization", "Content-Type"],
)
# Production deployments serve the separately built frontend from the same origin.
FRONTEND_DIST = BASE_DIR.parent / "frontend" / "dist"
if (FRONTEND_DIST / "index.html").is_file():
app.mount("/assets", StaticFiles(directory=FRONTEND_DIST / "assets"), name="frontend-assets")
@app.get("/api/health")
def health() -> dict[str, str]:
return {"status": "ok"}
@app.get("/api/system/metrics", dependencies=[Depends(require_token)])
def system_metrics() -> dict[str, Any]:
"""Return the latest local system-monitor snapshot without calling another service."""
return system_monitor.snapshot()
@app.get("/api/apps", response_model=list[AppRecord], dependencies=[Depends(require_token)])
def list_apps() -> list[AppRecord]:
with connect_db() as connection:
rows = connection.execute("SELECT * FROM apps ORDER BY created_at DESC").fetchall()
return [to_record(row, read_unit_state(row["id"])) for row in rows]
@app.post("/api/apps", response_model=AppRecord, status_code=201, dependencies=[Depends(require_token)])
async def create_app(payload: AppInput) -> AppRecord:
check_runtime(payload)
app_id = str(uuid.uuid4())
async with _operation_lock:
await asyncio.to_thread(save_app, app_id, payload)
await asyncio.to_thread(write_service_files, app_id, payload)
await asyncio.to_thread(systemctl, "daemon-reload")
return to_record(fetch_row(app_id), {"ActiveState": "inactive", "MainPID": "0"})
@app.put("/api/apps/{app_id}", response_model=AppRecord, dependencies=[Depends(require_token)])
async def edit_app(app_id: str, payload: AppInput) -> AppRecord:
row = fetch_row(app_id)
if read_unit_state(app_id).get("ActiveState") == "active":
raise HTTPException(status_code=409, detail="请先停止应用,再修改配置")
check_runtime(payload)
async with _operation_lock:
await asyncio.to_thread(update_app, app_id, payload)
await asyncio.to_thread(write_service_files, app_id, payload)
await asyncio.to_thread(systemctl, "daemon-reload")
return to_record(fetch_row(app_id), {"ActiveState": "inactive", "MainPID": "0"})
@app.post("/api/apps/{app_id}/actions", response_model=AppRecord, dependencies=[Depends(require_token)])
async def app_action(app_id: str, payload: AppAction) -> AppRecord:
row = fetch_row(app_id)
async with _operation_lock:
if payload.action in {"start", "restart"}:
saved = AppInput(
name=row["name"],
description=row["description"],
python_path=row["python_path"],
script_path=row["script_path"],
working_dir=row["working_dir"],
args=json.loads(row["args_json"]),
env=json.loads(row["env_json"]),
)
await asyncio.to_thread(write_service_files, app_id, saved)
await asyncio.to_thread(systemctl, "daemon-reload")
if payload.action == "restart":
await asyncio.to_thread(systemctl, "restart", unit_name(app_id))
elif payload.action == "start":
await asyncio.to_thread(systemctl, "start", unit_name(app_id))
else:
await asyncio.to_thread(systemctl, "stop", unit_name(app_id), check=False)
return to_record(fetch_row(app_id), read_unit_state(app_id))
@app.get("/api/apps/{app_id}/logs", dependencies=[Depends(require_token)])
def app_logs(app_id: str, lines: int = Query(default=120, ge=1, le=1000)) -> dict[str, Any]:
fetch_row(app_id)
result = journalctl("--no-pager", "--output=short-iso", "-u", unit_name(app_id), "-n", str(lines))
return {"lines": result.stdout.splitlines(), "error": result.stderr.strip() if result.returncode else None}
@app.delete("/api/apps/{app_id}", status_code=204, dependencies=[Depends(require_token)])
async def delete_app(app_id: str) -> Response:
fetch_row(app_id)
if read_unit_state(app_id).get("ActiveState") == "active":
raise HTTPException(status_code=409, detail="请先停止应用,再删除")
unit_path, launcher_path = service_paths(app_id)
async with _operation_lock:
await asyncio.to_thread(unit_path.unlink, missing_ok=True)
await asyncio.to_thread(launcher_path.unlink, missing_ok=True)
def remove_row() -> None:
with connect_db() as connection:
connection.execute("DELETE FROM apps WHERE id = ?", (app_id,))
await asyncio.to_thread(remove_row)
await asyncio.to_thread(systemctl, "daemon-reload")
return Response(status_code=204)
@app.get("/{frontend_path:path}", include_in_schema=False)
def frontend_fallback(frontend_path: str) -> FileResponse:
"""Return the SPA entry for browser routes after API routes have been matched."""
if frontend_path == "api" or frontend_path.startswith("api/"):
raise HTTPException(status_code=404, detail="Not Found")
index_file = FRONTEND_DIST / "index.html"
if not index_file.is_file():
raise HTTPException(status_code=404, detail="前端尚未构建")
return FileResponse(index_file)