You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/virsh_panel/app.py

1108 lines
47 KiB
Python

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.

"""Browser setup, authenticated API, and VM console for one remote host."""
from __future__ import annotations
import asyncio
import hmac
import ipaddress
import json
import logging
import secrets
import subprocess
import threading
import uuid
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager, suppress
from typing import Callable, Literal
from urllib.parse import urlsplit
from fastapi import Depends, FastAPI, HTTPException, Request, WebSocket
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, ConfigDict, Field, SecretStr
from .auth import DatabaseSessionStore, LoginLimiter, Session, SessionStore, verify_password
from .cloud_init import _validate_guest_auth, hash_root_password
from .cloud_templates import (
TemplateError, TemplateStore, download_verified_image, template_id_for,
template_scope, template_volume_name, validate_image_url, validate_template_name,
)
from .config import Settings
from .console import ConsoleRelayError, relay_console
from .database import DatabaseError, make_components
from .database_bootstrap import DatabaseBootstrap, DatabaseSetupError
from .libvirt_service import VMError, VMService
from .panel_state import PanelState, SetupError
from .remote_host import HostProfile, RemoteHostError, RemoteHostManager
from .updater import BRANCH, REPOSITORY_URL, UpdateError, Updater
COOKIE_NAME = "virsh_panel_session"
LOG = logging.getLogger(__name__)
STARTUP_LOG = logging.getLogger("uvicorn.error")
class SetupBody(BaseModel):
model_config = ConfigDict(extra="forbid")
token: str = Field(min_length=1, max_length=256)
username: str = Field(min_length=3, max_length=32)
password: str = Field(min_length=12, max_length=1024)
class DatabaseSetupBody(BaseModel):
model_config = ConfigDict(extra="forbid")
host: str = Field(min_length=1, max_length=253)
port: int = Field(ge=1, le=65535)
database: str = Field(min_length=1, max_length=64)
username: str = Field(min_length=1, max_length=128)
password: SecretStr
setup_token: str = Field(min_length=1, max_length=256)
class UpdateSettingsBody(BaseModel):
model_config = ConfigDict(extra="forbid")
repository_url: str = Field(default=REPOSITORY_URL, max_length=2048)
branch: str = Field(default=BRANCH, max_length=128)
access_token: SecretStr | None = None
class UpdateApplyBody(BaseModel):
model_config = ConfigDict(extra="forbid")
expected_current_commit: str = Field(pattern=r"^(?:[0-9a-f]{40}|[0-9a-f]{64})$")
expected_remote_commit: str = Field(pattern=r"^(?:[0-9a-f]{40}|[0-9a-f]{64})$")
class LoginBody(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str = Field(min_length=1, max_length=64)
password: str = Field(min_length=1, max_length=1024)
class UnlockBody(BaseModel):
model_config = ConfigDict(extra="forbid")
passphrase: str = Field(min_length=1, max_length=1024)
class HostScanBody(BaseModel):
model_config = ConfigDict(extra="forbid")
host: str = Field(min_length=1, max_length=253)
port: int = Field(ge=1, le=65535)
class HostBody(HostScanBody):
private_key: str = Field(default="", max_length=65536)
private_key_passphrase: str = Field(default="", max_length=1024)
host_key: str = Field(min_length=1, max_length=4096)
confirmed_fingerprint: str = Field(min_length=1, max_length=256)
storage_pool: str = Field(default="default", min_length=1, max_length=4096)
iso_pool: str = Field(default="iso", min_length=1, max_length=4096)
default_network: str = Field(default="default", min_length=1, max_length=128)
def profile(self) -> HostProfile:
return HostProfile(
host=self.host,
port=self.port,
private_key=self.private_key,
host_key=self.host_key,
private_key_passphrase=self.private_key_passphrase,
storage_pool=self.storage_pool,
iso_pool=self.iso_pool,
default_network=self.default_network,
)
class CreateISOBody(BaseModel):
model_config = ConfigDict(extra="forbid")
source: Literal["iso"] = "iso"
name: str = Field(min_length=1, max_length=63)
vcpu: int = Field(ge=1, le=64)
memory_mib: int = Field(ge=512, le=262144)
disk_gib: int = Field(ge=1, le=4096)
network: str = Field(min_length=1, max_length=128)
iso: str = Field(min_length=1, max_length=255)
class CreateCloudBody(BaseModel):
model_config = ConfigDict(extra="forbid")
source: Literal["cloud"]
name: str = Field(min_length=1, max_length=63)
vcpu: int = Field(ge=1, le=64)
memory_mib: int = Field(ge=512, le=262144)
disk_gib: int = Field(ge=1, le=4096)
network: str = Field(min_length=1, max_length=128)
template_id: str = Field(min_length=1, max_length=80)
public_ip_cidr: str = Field(min_length=3, max_length=64)
gateway: str = Field(min_length=7, max_length=64)
dns_servers: list[str] = Field(min_length=1, max_length=4)
guest_ssh_public_key: str = Field(default="", max_length=16384)
root_password: SecretStr | None = None
class ImportTemplateBody(BaseModel):
model_config = ConfigDict(extra="forbid")
url: str = Field(min_length=16, max_length=2048)
sha256: str = Field(min_length=64, max_length=64)
name: str = Field(min_length=1, max_length=64)
class ReinstallBody(BaseModel):
model_config = ConfigDict(extra="forbid")
template_id: str = Field(min_length=1, max_length=80)
ssh_public_key: str = Field(default="", max_length=16384)
root_password: SecretStr | None = None
confirmation_name: str = Field(min_length=1, max_length=63)
class ActionBody(BaseModel):
model_config = ConfigDict(extra="forbid")
action: Literal["start", "shutdown", "reboot", "poweroff", "reset"]
def _public_host(profile: dict | None) -> dict:
if profile is None:
return {"configured": False}
keys = (
"host", "port", "key_type", "fingerprint", "storage_pool",
"iso_pool", "default_network", "private_key_configured",
"private_key_encrypted", "key_locked",
)
return {"configured": True, **{key: profile[key] for key in keys if key in profile}}
def _peer(request: Request) -> str:
# Uvicorn has already processed forwarding headers from its trusted local
# reverse proxy. Never read a raw client-supplied X-Real-IP here, because
# that would let a login attacker reset the rate limit.
return request.client.host if request.client else "unknown"
def _origin_matches_host(origin: str | None, host: str | None) -> bool:
if not origin or not host:
return False
try:
parsed = urlsplit(origin)
except ValueError:
return False
if parsed.scheme != "https":
return False
return bool(
parsed.netloc == host
and parsed.hostname
and not parsed.username
and not parsed.password
and not parsed.path
and not parsed.query
and not parsed.fragment
)
def _plain_loopback_allowed(scope: dict, host: str | None) -> bool:
"""Permit local diagnostics only when both peer and requested host are local.
The request scheme is supplied by the ASGI server. Uvicorn's proxy header
handling may upgrade it to HTTPS/WSS only for its explicitly trusted proxy
address; this function never trusts an incoming forwarding header itself.
"""
client = scope.get("client")
if not client or not host:
return False
try:
peer_is_local = ipaddress.ip_address(client[0]).is_loopback
host_is_local = urlsplit("//" + host).hostname in {
"localhost", "127.0.0.1", "::1",
}
except ValueError:
return False
return peer_is_local and host_is_local
def create_app(
settings: Settings | None = None,
*,
panel_state: PanelState | None = None,
remote_manager: RemoteHostManager | None = None,
service_factory: Callable[[dict], VMService] | None = None,
) -> FastAPI:
settings = settings or Settings.from_env()
settings.validate()
# Injected objects keep the existing isolated HTTP test harness working.
# Normal production startup never uses the old JSON-backed stores.
database_mode = panel_state is None and remote_manager is None
bootstrap = DatabaseBootstrap(settings.data_dir) if database_mode else None
data_store = None
updater = None
if database_mode:
configured_database = bootstrap.load_config()
if configured_database is None:
setup_token = bootstrap.ensure_token()
state = None
manager = None
templates = None
else:
state, manager, templates, data_store = make_components(
settings.data_dir, configured_database, bootstrap.master_key(),
settings.ssh_binary,
)
setup_token = (bootstrap.ensure_token()
if state.setup_required() else state.initialize_setup_token())
else:
state = panel_state or PanelState(settings.data_dir)
setup_token = state.initialize_setup_token()
manager = remote_manager or RemoteHostManager(
settings.data_dir, ssh_binary=settings.ssh_binary)
templates = TemplateStore(settings.data_dir)
if setup_token is not None:
# Uvicorn writes this logger to stderr, which the supplied systemd
# service captures in its journal. Never expose the token over HTTP.
STARTUP_LOG.warning("VIRSH_PANEL_SETUP_TOKEN=%s", setup_token)
if database_mode and (settings.data_dir / "generations").exists():
STARTUP_LOG.warning(
"Legacy data/generations detected; its old SSH key files are not migrated "
"or automatically removed. Verify and remove them separately."
)
import_executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="cloud-image-import")
import_lock = threading.RLock()
import_jobs: dict[str, dict] = {}
pending_templates: set[tuple[str, str]] = set()
reinstall_executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="vm-reinstall")
reinstall_lock = threading.RLock()
reinstall_jobs: dict[str, dict] = {}
pending_reinstalls: set[tuple[str, str]] = set()
def save_job(kind: str, job_id: str, job: dict) -> None:
if data_store is not None:
data_store.set(kind, job_id, json.dumps(
job, ensure_ascii=False, separators=(",", ":")))
def delete_job(kind: str, job_id: str) -> None:
if data_store is not None:
data_store.delete(kind, job_id)
def restore_jobs() -> None:
"""Expose interrupted jobs as failed, never pretend work resumed."""
if data_store is None:
return
for kind, destination in (("import_job", import_jobs),
("reinstall_job", reinstall_jobs)):
for job_id, raw in data_store.list(kind).items():
try:
job = json.loads(raw)
except (TypeError, ValueError) as exc:
raise DatabaseError("数据库任务记录损坏") from exc
if not isinstance(job, dict):
raise DatabaseError("数据库任务记录损坏")
if job.get("status") in {"queued", "running"}:
job["status"] = "failed"
job["phase"] = "面板重启,任务已中断"
job["error"] = "面板在任务执行期间重启;请检查宿主机实际状态,勿直接重复提交"
save_job(kind, job_id, job)
destination[job_id] = job
restore_jobs()
sessions = DatabaseSessionStore(data_store) if data_store is not None else SessionStore()
login_limiter = LoginLimiter()
setup_limiter = LoginLimiter()
unlock_limiter = LoginLimiter()
@asynccontextmanager
async def lifespan(_app: FastAPI):
try:
yield
finally:
reinstall_executor.shutdown(wait=True, cancel_futures=False)
import_executor.shutdown(wait=True, cancel_futures=False)
close_manager = getattr(manager, "close", None) if manager is not None else None
if callable(close_manager):
close_manager()
app = FastAPI(title="Virsh Panel API", docs_url=None, redoc_url=None, lifespan=lifespan)
app.mount("/static", StaticFiles(directory=str(settings.static_dir)), name="static")
app.mount(
"/vendor/novnc",
StaticFiles(directory=str(settings.static_dir / "vendor" / "novnc")),
name="novnc",
)
@app.middleware("http")
async def security_headers(request: Request, call_next):
if request.url.scheme != "https" and not (
request.url.path == "/api/v1/health"
and request.url.scheme == "http"
and _plain_loopback_allowed(request.scope, request.headers.get("host"))
):
return JSONResponse(status_code=400, content={"error": "请使用 HTTPS 访问面板"})
response = await call_next(request)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["Referrer-Policy"] = "no-referrer"
response.headers["X-Frame-Options"] = "DENY"
response.headers["Content-Security-Policy"] = (
"default-src 'self'; script-src 'self'; style-src 'self'; "
"img-src 'self' data:; connect-src 'self' wss:; "
"object-src 'none'; frame-ancestors 'none'; base-uri 'self'"
)
if (request.url.path == "/" or request.url.path.startswith("/api/")
or request.url.path in ("/static/app.js", "/static/styles.css")):
response.headers["Cache-Control"] = "no-store"
return response
@app.exception_handler(VMError)
async def handle_vm_error(_request: Request, exc: VMError):
return JSONResponse(status_code=exc.status, content={"error": str(exc)})
@app.exception_handler(RemoteHostError)
async def handle_remote_error(_request: Request, exc: RemoteHostError):
content = {"error": str(exc)}
if exc.status == 423:
content["error_code"] = "key_locked"
return JSONResponse(status_code=exc.status, content=content)
@app.exception_handler(SetupError)
async def handle_setup_error(_request: Request, exc: SetupError):
return JSONResponse(status_code=exc.status, content={"error": str(exc)})
@app.exception_handler(DatabaseSetupError)
async def handle_database_setup_error(_request: Request, exc: DatabaseSetupError):
return JSONResponse(status_code=exc.status, content={"error": str(exc)})
@app.exception_handler(DatabaseError)
async def handle_database_error(_request: Request, exc: DatabaseError):
return JSONResponse(status_code=exc.status, content={"error": str(exc)})
@app.exception_handler(UpdateError)
async def handle_update_error(_request: Request, exc: UpdateError):
return JSONResponse(status_code=exc.status, content={"error": str(exc)})
@app.exception_handler(TemplateError)
async def handle_template_error(_request: Request, exc: TemplateError):
return JSONResponse(status_code=exc.status, content={"error": str(exc)})
def require_session(request: Request) -> Session:
session = sessions.get(request.cookies.get(COOKIE_NAME))
if session is None:
raise HTTPException(status_code=401, detail="请先登录")
return session
def require_write(request: Request) -> Session:
session = require_session(request)
csrf = request.headers.get("X-CSRF-Token", "")
if not hmac.compare_digest(csrf, session.csrf_token):
raise HTTPException(status_code=403, detail="请求验证失败")
return session
def current_updater() -> Updater:
nonlocal updater
if data_store is None:
raise UpdateError("请先完成数据库配置", 409)
if updater is None:
updater = Updater(settings.static_dir.parent, data_store)
return updater
def forbid_long_job_during_update() -> None:
if data_store is None:
return
active_id = data_store.get_value("update.active_job")
if active_id:
job = current_updater().get_job(active_id)
if job is not None and job.get("status") in {"queued", "running"}:
raise HTTPException(status_code=409, detail="面板正在更新,暂时不能开始新任务")
def current_profile() -> dict:
profile = manager.current()
if profile is None:
raise HTTPException(status_code=409, detail="请先在面板中配置宿主机")
if profile.get("key_locked"):
raise RemoteHostError("宿主机 SSH 私钥已锁定,请先在面板解锁", 423)
return profile
def new_service(profile: dict) -> VMService:
if service_factory is not None:
return service_factory(profile)
return VMService(
profile["libvirt_uri"],
profile["storage_pool"],
profile["iso_pool"],
profile["default_network"],
)
def current_service() -> VMService:
return new_service(current_profile())
def validate_staged_libvirt(
staged, body: HostBody, *, ensure_paths: bool = False,
) -> tuple[dict | None, str | None]:
service = new_service({
"libvirt_uri": staged.libvirt_uri,
"storage_pool": body.storage_pool,
"iso_pool": body.iso_pool,
"default_network": body.default_network,
})
# A successful libvirt API call is required before activation. Missing
# storage/network resources should not prevent connecting an otherwise
# healthy host; the UI can guide the operator to fix those separately.
service.list_vms()
if ensure_paths and (body.storage_pool.startswith("/") or body.iso_pool.startswith("/")):
service.ensure_storage_pools()
try:
return service.resources(), None
except VMError as exc:
return None, str(exc)
def session_response(username: str) -> JSONResponse:
session = sessions.issue(username)
response = JSONResponse({
"authenticated": True,
"username": session.username,
"csrf_token": session.csrf_token,
})
response.set_cookie(
COOKIE_NAME,
session.token,
max_age=8 * 60 * 60,
httponly=True,
secure=settings.secure_cookie,
samesite="strict",
path="/",
)
return response
@app.get("/", include_in_schema=False)
def index():
return FileResponse(settings.static_dir / "index.html")
@app.get("/api/v1/health")
def health():
if data_store is not None:
# A running HTTP listener alone is not a usable post-update panel.
# The updater treats this endpoint as readiness for the configured DB.
data_store.get("admin", "singleton")
return {"status": "ok"}
@app.get("/api/v1/database/status")
def database_status():
if bootstrap is None:
return {"configured": True, "setup_required": False}
return {"configured": bootstrap.configured(),
"setup_required": not bootstrap.configured()}
@app.post("/api/v1/database/setup")
def setup_database(body: DatabaseSetupBody, request: Request):
nonlocal state, manager, templates, data_store, updater, sessions
if bootstrap is None:
raise DatabaseSetupError("数据库初始化仅供正常部署使用", 409)
peer = _peer(request)
if not setup_limiter.allowed(peer):
raise HTTPException(status_code=429, detail="初始化尝试过多,请稍后再试")
def connect(config: dict, master_key: bytes):
try:
return make_components(
settings.data_dir, config, master_key, settings.ssh_binary)
except Exception as exc:
# Connector errors may include database host details. Never
# reflect the exception, credentials or driver SQL in HTTP.
LOG.warning("database setup connection failed: %s", type(exc).__name__)
raise DatabaseSetupError(
"无法连接或初始化 MariaDB;请检查地址、账号、数据库和建表权限", 502,
) from exc
try:
components = bootstrap.configure({
"host": body.host,
"port": body.port,
"database": body.database,
"username": body.username,
"password": body.password.get_secret_value(),
}, body.setup_token, connect)
except DatabaseSetupError:
setup_limiter.failed(peer)
raise
state, manager, templates, data_store = components
sessions = DatabaseSessionStore(data_store)
restore_jobs()
setup_limiter.succeeded(peer)
LOG.info("panel database configured")
return {"configured": True}
@app.get("/api/v1/setup/status")
def setup_status():
if state is None:
raise HTTPException(status_code=409, detail="请先配置数据库")
return {"setup_required": state.setup_required()}
@app.post("/api/v1/setup/admin")
def setup_admin(body: SetupBody, request: Request):
if state is None:
raise HTTPException(status_code=409, detail="请先配置数据库")
peer = _peer(request)
if not setup_limiter.allowed(peer):
raise HTTPException(status_code=429, detail="初始化尝试过多,请稍后再试")
try:
username = state.create_admin(body.token, body.username, body.password)
except SetupError:
setup_limiter.failed(peer)
raise
setup_limiter.succeeded(peer)
LOG.info("panel administrator created: username=%s", username)
return session_response(username)
@app.get("/api/v1/session")
def session_info(request: Request):
session = sessions.get(request.cookies.get(COOKIE_NAME))
if session is None:
return {"authenticated": False, "username": None, "csrf_token": None}
return {
"authenticated": True,
"username": session.username,
"csrf_token": session.csrf_token,
}
@app.post("/api/v1/login")
def login(body: LoginBody, request: Request):
if state is None:
raise HTTPException(status_code=409, detail="请先配置数据库")
peer = _peer(request)
if not login_limiter.allowed(peer):
raise HTTPException(status_code=429, detail="登录尝试过多,请稍后再试")
admin = state.admin()
if admin is None:
raise HTTPException(status_code=409, detail="请先完成初始化")
username, encoded = admin
password_ok = verify_password(body.password, encoded)
username_ok = hmac.compare_digest(body.username, username)
if not password_ok or not username_ok:
login_limiter.failed(peer)
raise HTTPException(status_code=401, detail="用户名或密码错误")
login_limiter.succeeded(peer)
return session_response(username)
@app.post("/api/v1/logout")
def logout(request: Request, _session: Session = Depends(require_write)):
sessions.revoke(request.cookies.get(COOKIE_NAME))
response = JSONResponse({"ok": True})
response.delete_cookie(COOKIE_NAME, path="/")
return response
def public_update_settings() -> dict:
info = current_updater().settings()
return {
"repository_url": info["repository"],
"branch": info["branch"],
"token_configured": info["token_configured"],
"checkout_ready": info["checkout_ready"],
"configured": info["token_configured"] and info["checkout_ready"],
"current_version": info["current_commit"],
"active_job_id": info.get("active_job_id"),
"last_job_id": info.get("last_job_id"),
"checkout_error": info.get("checkout_error"),
}
@app.get("/api/v1/update/settings")
def update_settings(_session: Session = Depends(require_session)):
return public_update_settings()
@app.put("/api/v1/update/settings")
def save_update_settings(body: UpdateSettingsBody,
_session: Session = Depends(require_write)):
if body.repository_url != REPOSITORY_URL or body.branch != BRANCH:
raise UpdateError("更新仓库和分支固定为面板指定的 Gitea main 分支", 422)
service = current_updater()
if body.access_token is not None:
token = body.access_token.get_secret_value()
if token:
service.set_token(token)
if not service.settings()["token_configured"]:
raise UpdateError("请填写 Gitea 私有仓库访问令牌", 422)
LOG.info("panel update access token configured: admin=%s", _session.username)
return public_update_settings()
@app.post("/api/v1/update/check")
def check_update(_session: Session = Depends(require_write)):
result = current_updater().check()
return {
"repository_url": result["repository"],
"branch": result["branch"],
"current_version": result["current_commit"],
"latest_version": result["remote_commit"],
"update_available": result["status"] == "update_available",
}
@app.post("/api/v1/update/apply", status_code=202)
def apply_update(body: UpdateApplyBody, _session: Session = Depends(require_write)):
with import_lock, reinstall_lock:
if pending_templates or pending_reinstalls:
raise UpdateError("云镜像导入或虚拟机重装正在执行,请等待完成后再更新面板", 409)
try:
unit = subprocess.run(
["/usr/bin/systemctl", "is-active", "virsh-panel-update.service"],
capture_output=True, text=True, timeout=5, check=False,
)
except (OSError, subprocess.TimeoutExpired):
unit = None
if unit is not None and unit.stdout.strip() in {
"activating", "active", "deactivating", "reloading",
}:
raise UpdateError("独立更新服务仍在运行,请等待其完全结束后再提交", 409)
service = current_updater()
job = service.create_job(
expected_current_commit=body.expected_current_commit,
expected_remote_commit=body.expected_remote_commit,
)
try:
# The web service has a read-only code tree. A separate one-shot
# unit performs the Git update and restarts this service.
trigger = subprocess.run(
["/usr/bin/systemctl", "--no-block", "start", "virsh-panel-update.service"],
capture_output=True, text=True, timeout=10, check=False,
)
except (OSError, subprocess.TimeoutExpired):
trigger = None
if trigger is None or trigger.returncode != 0:
service.fail_job(job["job_id"],
"无法启动独立更新服务;请检查 virsh-panel-update.service 是否已安装")
raise UpdateError("无法启动独立更新服务;请检查 systemd 配置", 503)
LOG.info("panel update queued: job=%s admin=%s", job["job_id"], _session.username)
return {"job_id": job["job_id"], "status": "queued"}
@app.get("/api/v1/update/jobs/{job_id}")
def update_job(job_id: str, _session: Session = Depends(require_session)):
job = current_updater().get_job(job_id)
if job is None:
raise UpdateError("更新任务不存在", 404)
return job
@app.get("/api/v1/host")
def host(_session: Session = Depends(require_session)):
return _public_host(manager.current())
@app.post("/api/v1/host/scan")
def scan_host(body: HostScanBody, _session: Session = Depends(require_write)):
return manager.scan(body.host, body.port)
@app.post("/api/v1/host/unlock")
def unlock_host(body: UnlockBody, request: Request, _session: Session = Depends(require_write)):
peer = _peer(request)
if not unlock_limiter.allowed(peer):
raise HTTPException(status_code=429, detail="私钥口令尝试过多,请稍后再试")
try:
profile = manager.unlock_current(body.passphrase)
except RemoteHostError as exc:
if exc.status == 400:
unlock_limiter.failed(peer)
raise
unlock_limiter.succeeded(peer)
LOG.info("remote host SSH key unlocked: host=%s admin=%s", profile.get("host"), _session.username)
return _public_host(profile)
@app.post("/api/v1/host/test")
def test_host(body: HostBody, _session: Session = Depends(require_write)):
staged = manager.stage(body.profile(), confirmed_fingerprint=body.confirmed_fingerprint)
try:
result = manager.test(staged)
resources, resource_warning = validate_staged_libvirt(staged, body)
return {
**result,
"resources": resources,
"warnings": [resource_warning] if resource_warning else [],
}
finally:
manager.abort(staged)
@app.put("/api/v1/host")
def save_host(body: HostBody, _session: Session = Depends(require_write)):
# A running reinstall uses the saved SSH host identity and paths. Do
# not replace them while its libvirt operation is in flight.
with reinstall_lock:
forbid_long_job_during_update()
if pending_reinstalls or any(
job.get("status") == "succeeded" and "generated_password" in job
for job in reinstall_jobs.values()):
raise VMError("请先完成重装任务并确认保存自动生成的密码,再修改宿主机配置", 409)
staged = manager.stage(body.profile(), confirmed_fingerprint=body.confirmed_fingerprint)
committed = False
try:
manager.test(staged)
_resources, resource_warning = validate_staged_libvirt(staged, body, ensure_paths=True)
profile = manager.commit(staged)
committed = True
finally:
if not committed:
manager.abort(staged)
LOG.info("remote host configured: host=%s port=%s admin=%s", body.host, body.port, _session.username)
return {
**_public_host(profile),
"warnings": [resource_warning] if resource_warning else [],
}
@app.get("/api/v1/resources")
def resources(_session: Session = Depends(require_session)):
return current_service().resources()
@app.get("/api/v1/vm-templates")
def list_templates(_session: Session = Depends(require_session)):
profile = current_profile()
return {"templates": templates.list(template_scope(profile))}
def run_template_import(job_id: str, profile: dict, scope: str,
body: ImportTemplateBody, template_id: str) -> None:
image_path = None
volume_name = template_volume_name(template_id)
uploaded = False
try:
image_path, virtual_size = download_verified_image(
body.url, body.sha256.lower(), settings.data_dir)
result = new_service(profile).import_template(image_path, volume_name, virtual_size)
uploaded = True
record = {
"id": template_id,
"name": body.name,
"sha256": body.sha256.lower(),
"volume_name": volume_name,
"virtual_size": virtual_size,
"image_bytes": result["image_bytes"],
}
templates.add(scope, record)
with import_lock:
import_jobs[job_id] = {"scope": scope, "status": "completed",
"template": record}
save_job("import_job", job_id, import_jobs[job_id])
except Exception as exc:
if isinstance(exc, (TemplateError, VMError)):
error = str(exc)
else:
LOG.exception("Cloud template import failed: job=%s", job_id)
error = "云镜像导入失败,请查看面板日志"
if uploaded:
error += f";宿主机模板卷 {volume_name} 可能需要人工检查"
with import_lock:
import_jobs[job_id] = {"scope": scope, "status": "failed", "error": error}
save_job("import_job", job_id, import_jobs[job_id])
finally:
if image_path is not None:
image_path.unlink(missing_ok=True)
with import_lock:
pending_templates.discard((scope, template_id))
@app.post("/api/v1/vm-templates/import", status_code=202)
def import_template(body: ImportTemplateBody, _session: Session = Depends(require_write)):
profile = current_profile()
validate_image_url(body.url)
name = validate_template_name(body.name)
body = body.model_copy(update={"name": name})
if not all(char in "0123456789abcdefABCDEF" for char in body.sha256):
raise TemplateError("SHA-256 校验值必须是十六进制")
scope = template_scope(profile)
template_id = template_id_for(scope, body.sha256.lower())
with import_lock:
forbid_long_job_during_update()
if any(row["id"] == template_id for row in templates.list(scope)):
raise TemplateError("此云镜像已经导入", 409)
if (scope, template_id) in pending_templates:
raise TemplateError("该云镜像正在导入", 409)
if pending_templates:
raise TemplateError("已有云镜像正在导入,请等待完成后再提交", 409)
job_id = str(uuid.uuid4())
pending_templates.add((scope, template_id))
import_jobs[job_id] = {"scope": scope, "status": "running"}
save_job("import_job", job_id, import_jobs[job_id])
try:
import_executor.submit(run_template_import, job_id, profile,
scope, body, template_id)
except RuntimeError as exc:
pending_templates.discard((scope, template_id))
import_jobs.pop(job_id, None)
delete_job("import_job", job_id)
raise TemplateError("面板正在停止,无法开始导入任务", 503) from exc
LOG.info("cloud template import queued: job=%s admin=%s", job_id, _session.username)
return {"job_id": job_id, "status": "running"}
@app.get("/api/v1/vm-templates/import-jobs/{job_id}")
def import_job(job_id: str, _session: Session = Depends(require_session)):
scope = template_scope(current_profile())
with import_lock:
job = import_jobs.get(job_id)
if job is None or job["scope"] != scope:
raise TemplateError("导入任务不存在", 404)
return {key: value for key, value in job.items() if key != "scope"}
@app.get("/api/v1/vms")
def list_vms(_session: Session = Depends(require_session)):
return {"items": current_service().list_vms()}
@app.get("/api/v1/vms/{vm_id}")
def get_vm(vm_id: str, _session: Session = Depends(require_session)):
return current_service().get_vm(vm_id)
@app.post("/api/v1/vms", status_code=201)
def create_vm(body: CreateISOBody | CreateCloudBody,
_session: Session = Depends(require_write)):
# Keep the update submission lock until libvirt has finished creating
# the VM. Otherwise an update can restart the web process mid-create.
with reinstall_lock:
forbid_long_job_during_update()
if isinstance(body, CreateCloudBody):
public_key = body.guest_ssh_public_key.strip()
root_password = (body.root_password.get_secret_value()
if body.root_password is not None else "")
if bool(public_key) == bool(root_password):
raise HTTPException(
status_code=422,
detail="虚拟机 SSH 公钥和初始 root 密码必须且只能填写一项",
)
password_hash = None
if root_password:
try:
password_hash = hash_root_password(root_password)
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
profile = current_profile()
record = templates.get(template_scope(profile), body.template_id)
spec = body.model_dump(exclude={"source", "template_id", "root_password"})
spec["guest_ssh_public_key"] = public_key
if password_hash is not None:
spec["root_password_hash"] = password_hash
vm = new_service(profile).create_cloud_vm(spec, record["volume_name"])
else:
vm = current_service().create_vm(body.model_dump(exclude={"source"}))
LOG.info("vm created: uuid=%s admin=%s", vm.get("uuid"), _session.username)
return vm
def run_reinstall(
job_id: str, profile: dict, scope: str, vm_id: str,
template_volume_name: str, ssh_public_key: str,
root_password_hash: str | None, generated_password: str | None,
) -> None:
def report_progress(phase: str) -> None:
if not isinstance(phase, str):
return
with reinstall_lock:
job = reinstall_jobs.get(job_id)
if job is not None and job["status"] == "running":
job["phase"] = phase[:200]
save_job("reinstall_job", job_id, job)
try:
with reinstall_lock:
reinstall_jobs[job_id]["status"] = "running"
reinstall_jobs[job_id]["phase"] = "正在检查虚拟机"
save_job("reinstall_job", job_id, reinstall_jobs[job_id])
result = new_service(profile).reinstall_cloud_vm(
vm_id, template_volume_name,
ssh_public_key=ssh_public_key,
root_password_hash=root_password_hash,
progress=report_progress,
)
with reinstall_lock:
job = reinstall_jobs[job_id]
job["status"] = "succeeded"
job["phase"] = "重装完成"
if isinstance(result, dict) and result.get("warning"):
job["warning"] = str(result["warning"])
if generated_password is not None:
# DatabaseStore encrypts this one-time password. Keep it
# until the operator explicitly acknowledges saving it.
job["generated_password"] = generated_password
save_job("reinstall_job", job_id, job)
LOG.info("vm reinstall succeeded: job=%s uuid=%s", job_id, vm_id)
except Exception as exc:
if isinstance(exc, (VMError, TemplateError, ValueError)):
error = str(exc)
else:
LOG.exception("VM reinstall failed: job=%s uuid=%s", job_id, vm_id)
error = "重装系统失败,请查看面板日志"
with reinstall_lock:
job = reinstall_jobs[job_id]
job["status"] = "failed"
job["phase"] = "重装失败"
job["error"] = error
save_job("reinstall_job", job_id, job)
LOG.warning("vm reinstall failed: job=%s uuid=%s", job_id, vm_id)
finally:
with reinstall_lock:
pending_reinstalls.discard((scope, vm_id))
@app.post("/api/v1/vms/{vm_id}/reinstall", status_code=202)
def reinstall_vm(vm_id: str, body: ReinstallBody,
_session: Session = Depends(require_write)):
ssh_public_key = body.ssh_public_key.strip()
supplied_password = (body.root_password.get_secret_value()
if body.root_password is not None else "")
if not supplied_password.strip():
supplied_password = ""
if ssh_public_key and supplied_password:
raise HTTPException(
status_code=422,
detail="SSH 公钥和初始 root 密码只能填写一种",
)
generated_password = None
if not ssh_public_key and not supplied_password:
generated_password = secrets.token_urlsafe(24)
supplied_password = generated_password
try:
if ssh_public_key:
ssh_public_key, _unused_hash = _validate_guest_auth(ssh_public_key, None)
root_password_hash = None
else:
root_password_hash = hash_root_password(supplied_password)
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
with reinstall_lock:
forbid_long_job_during_update()
profile = current_profile()
scope = template_scope(profile)
record = templates.get(scope, body.template_id)
vm = new_service(profile).get_vm(vm_id)
if not vm.get("managed") or not vm.get("public_ip_cidr"):
raise VMError("只有面板创建的公网云镜像虚拟机支持自动重装", 409)
if body.confirmation_name != vm["name"]:
raise HTTPException(status_code=422, detail="确认名称与虚拟机名称不一致")
normalized_id = vm["uuid"]
key = (scope, normalized_id)
if key in pending_reinstalls:
raise VMError("该虚拟机正在重装系统", 409)
job_id = str(uuid.uuid4())
reinstall_jobs[job_id] = {
"owner": _session.username,
"scope": scope,
"status": "queued",
"phase": "等待重装任务开始",
"vm_id": normalized_id,
"template_id": record["id"],
}
save_job("reinstall_job", job_id, reinstall_jobs[job_id])
pending_reinstalls.add(key)
try:
reinstall_executor.submit(
run_reinstall, job_id, profile, scope, normalized_id,
record["volume_name"], ssh_public_key, root_password_hash,
generated_password,
)
except RuntimeError as exc:
pending_reinstalls.discard(key)
reinstall_jobs.pop(job_id, None)
delete_job("reinstall_job", job_id)
raise VMError("面板正在停止,无法开始重装任务", 503) from exc
LOG.info("vm reinstall queued: job=%s uuid=%s admin=%s",
job_id, normalized_id, _session.username)
return {"job_id": job_id, "status": "queued"}
@app.get("/api/v1/reinstall-jobs/{job_id}")
def reinstall_job(job_id: str, _session: Session = Depends(require_write)):
scope = template_scope(current_profile())
with reinstall_lock:
job = reinstall_jobs.get(job_id)
if (job is None or job["scope"] != scope
or job["owner"] != _session.username):
raise VMError("重装任务不存在", 404)
result = {key: value for key, value in job.items()
if key not in ("scope", "owner", "generated_password")}
if job["status"] == "succeeded" and "generated_password" in job:
result["generated_password"] = job["generated_password"]
return result
@app.post("/api/v1/reinstall-jobs/{job_id}/ack-password")
def acknowledge_reinstall_password(job_id: str,
_session: Session = Depends(require_write)):
scope = template_scope(current_profile())
with reinstall_lock:
job = reinstall_jobs.get(job_id)
if (job is None or job["scope"] != scope
or job["owner"] != _session.username):
raise VMError("重装任务不存在", 404)
if job["status"] != "succeeded":
raise VMError("重装任务尚未成功,不能确认密码", 409)
job.pop("generated_password", None)
save_job("reinstall_job", job_id, job)
return {"acknowledged": True}
@app.post("/api/v1/vms/{vm_id}/actions")
def action(vm_id: str, body: ActionBody, _session: Session = Depends(require_write)):
with reinstall_lock:
forbid_long_job_during_update()
profile = current_profile()
try:
normalized_id = str(uuid.UUID(vm_id))
except ValueError:
normalized_id = vm_id
if (template_scope(profile), normalized_id) in pending_reinstalls:
raise VMError("虚拟机正在重装系统,暂时不能执行其他操作", 409)
vm = new_service(profile).action(vm_id, body.action)
LOG.info("vm action: uuid=%s action=%s admin=%s", vm.get("uuid"), body.action, _session.username)
return vm
@app.delete("/api/v1/vms/{vm_id}")
def delete_vm(
vm_id: str,
delete_disks: bool = False,
_session: Session = Depends(require_write),
):
with reinstall_lock:
forbid_long_job_during_update()
profile = current_profile()
try:
normalized_id = str(uuid.UUID(vm_id))
except ValueError:
normalized_id = vm_id
if (template_scope(profile), normalized_id) in pending_reinstalls:
raise VMError("虚拟机正在重装系统,暂时不能删除", 409)
result = new_service(profile).delete_vm(vm_id, delete_disks)
LOG.info(
"vm deleted: uuid=%s delete_disks=%s admin=%s",
result.get("uuid"), delete_disks, _session.username,
)
return result
@app.websocket("/api/v1/vms/{vm_id}/console")
async def console(websocket: WebSocket, vm_id: str):
host = websocket.headers.get("host")
if websocket.url.scheme != "wss":
await websocket.close(code=4403)
return
if not _origin_matches_host(websocket.headers.get("origin"), host):
await websocket.close(code=4403)
return
if await asyncio.to_thread(sessions.get, websocket.cookies.get(COOKIE_NAME)) is None:
await websocket.close(code=4401)
return
profile = manager.current()
if profile is None:
await websocket.close(code=4409)
return
try:
if profile.get("key_locked"):
await websocket.close(code=4423)
return
port = await asyncio.to_thread(new_service(profile).console_port, vm_id)
except VMError:
await websocket.close(code=4404)
return
await websocket.accept()
try:
await relay_console(
websocket,
port=port,
ssh_target=profile["ssh_target"],
ssh_binary=settings.ssh_binary,
known_hosts_file=profile["ssh_known_hosts_file"],
ssh_config_file=profile["ssh_config_file"],
)
except ConsoleRelayError as exc:
LOG.warning("VNC relay failed for vm %r: %s", vm_id, exc)
with suppress(RuntimeError, OSError):
await websocket.close(code=1011)
return app