515 lines
20 KiB
Python
515 lines
20 KiB
Python
"""Local service manager API backed by the current user's systemd instance."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
import hmac
|
||
import json
|
||
import os
|
||
import re
|
||
import shlex
|
||
import sqlite3
|
||
import subprocess
|
||
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
|
||
|
||
from .metrics import LocalSystemMonitor
|
||
|
||
|
||
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
|
||
ports: list[int] = Field(default_factory=list)
|
||
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()
|
||
|
||
|
||
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"
|
||
main_pid = int(state["MainPID"]) if state.get("MainPID", "0").isdigit() and state["MainPID"] != "0" else None
|
||
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=main_pid,
|
||
ports=read_listening_ports(main_pid),
|
||
active_since=state.get("ActiveEnterTimestamp"),
|
||
created_at=row["created_at"],
|
||
updated_at=row["updated_at"],
|
||
)
|
||
|
||
|
||
def read_listening_ports(main_pid: int | None) -> list[int]:
|
||
"""Report TCP listener ports owned by the service process tree."""
|
||
if main_pid is None:
|
||
return []
|
||
|
||
try:
|
||
root = psutil.Process(main_pid)
|
||
processes = [root, *root.children(recursive=False)]
|
||
except (psutil.Error, OSError):
|
||
return []
|
||
|
||
# Service wrappers often launch the actual server as one child. Limit
|
||
# discovery to that first level so worker IPC sockets are not shown as ports.
|
||
ports: set[int] = set()
|
||
for process in processes:
|
||
try:
|
||
connections = process.net_connections(kind="inet")
|
||
except (psutil.Error, OSError):
|
||
continue
|
||
for connection in connections:
|
||
if connection.status == psutil.CONN_LISTEN and connection.laddr:
|
||
ports.add(connection.laddr.port)
|
||
return sorted(ports)
|
||
|
||
|
||
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]:
|
||
# Keep the configured interpreter path intact: venv Python executables may
|
||
# be symlinks, and resolving one can bypass the environment's site-packages.
|
||
python = Path(os.path.abspath(Path(app.python_path).expanduser()))
|
||
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",
|
||
# Stop crash loops after five starts in five minutes; recovery then
|
||
# requires an explicit manual start from the service manager.
|
||
"StartLimitIntervalSec=5min",
|
||
"StartLimitBurst=5",
|
||
"",
|
||
"[Service]",
|
||
"Type=simple",
|
||
# Unlike ExecStart, WorkingDirectory is a single path value; quotes
|
||
# are literal here, so escape path characters in systemd syntax.
|
||
f"WorkingDirectory={systemd_path(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 systemd_path(value: str) -> str:
|
||
"""Encode a path as a systemd setting value, not as an ExecStart argument."""
|
||
# WorkingDirectory does not use ExecStart's quote parser. Encode whitespace
|
||
# and backslashes with systemd's C-style escapes, and escape specifiers.
|
||
escaped: list[str] = []
|
||
for char in value:
|
||
if char == "%":
|
||
escaped.append("%%")
|
||
elif char == "\\" or char.isspace() or ord(char) < 32 or ord(char) == 127:
|
||
escaped.append(f"\\x{ord(char):02x}")
|
||
else:
|
||
escaped.append(char)
|
||
return "".join(escaped)
|
||
|
||
|
||
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())
|
||
process_task = asyncio.create_task(system_monitor.run_processes())
|
||
try:
|
||
yield
|
||
finally:
|
||
monitor_task.cancel()
|
||
process_task.cancel()
|
||
await asyncio.gather(monitor_task, process_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/system/history", dependencies=[Depends(require_token)])
|
||
def system_history(minutes: int = Query(default=10, ge=1, le=30)) -> dict[str, Any]:
|
||
"""Return the rolling 1-second sample ring buffer that seeds the detail charts."""
|
||
return {"window_seconds": minutes * 60, "samples": system_monitor.history_snapshot(minutes * 60)}
|
||
|
||
|
||
@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)
|