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/update_worker.py

110 lines
4.1 KiB
Python

"""Entry point for the separate virsh-panel-update.service one-shot unit."""
from __future__ import annotations
import argparse
import json
import logging
import subprocess
import time
from urllib.error import URLError
from urllib.request import urlopen
from .config import PROJECT_DIR, Settings
from .database import DatabaseError, DatabaseStore
from .database_bootstrap import DatabaseBootstrap
from .updater import UpdateError, Updater
LOG = logging.getLogger(__name__)
_WEB_UNIT = "virsh-panel.service"
_HEALTH_URL = "http://127.0.0.1:8000/api/v1/health"
def restart_web_service() -> None:
"""Restart the main unit and wait for its local health endpoint."""
try:
result = subprocess.run(
["/usr/bin/systemctl", "restart", _WEB_UNIT],
capture_output=True,
text=True,
timeout=90,
check=False,
)
except (OSError, subprocess.TimeoutExpired) as exc:
raise UpdateError("面板服务重启失败,请检查 systemd 日志", 500) from exc
if result.returncode != 0:
# systemctl output is intentionally not returned: it may contain
# environment details from another unit or configuration.
raise UpdateError("面板服务重启失败,请检查 systemd 日志", 500)
deadline = time.monotonic() + 45
while time.monotonic() < deadline:
try:
with urlopen(_HEALTH_URL, timeout=2) as response:
payload = json.load(response)
if response.status == 200 and isinstance(payload, dict) and payload.get("status") == "ok":
return
except (OSError, URLError, ValueError):
pass
time.sleep(1)
raise UpdateError("新版本服务未通过本机健康检查,请检查面板日志", 500)
def _load_updater() -> tuple[Updater, DatabaseStore]:
settings = Settings.from_env()
bootstrap = DatabaseBootstrap(settings.data_dir)
config = bootstrap.load_config()
if config is None:
raise UpdateError("尚未完成数据库配置,无法运行在线更新", 409)
store = DatabaseStore(config, bootstrap.master_key())
return Updater(PROJECT_DIR, store), store
def finalize_incomplete_job(updater: Updater, store: DatabaseStore) -> bool:
"""Mark an interrupted job failed after the one-shot unit has exited.
This is invoked by systemd ExecStopPost regardless of the main worker's
exit code. Successful/failed jobs are untouched. It is intentionally
idempotent so operators may rerun it while diagnosing a crashed worker.
"""
job_id = store.get_value("update.active_job")
if not job_id:
return False
job = updater.get_job(job_id)
if job is None or job.get("status") not in {"queued", "running"}:
return False
updater.fail_job(
job_id,
"独立更新服务异常退出或超时;请检查更新服务日志和当前 Git 版本,勿直接重复提交",
)
return True
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Run or finalize the Virsh Panel update job")
parser.add_argument("--finalize-incomplete", action="store_true")
options = parser.parse_args(argv)
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
try:
updater, store = _load_updater()
if options.finalize_incomplete:
if finalize_incomplete_job(updater, store):
LOG.warning("interrupted panel update marked failed")
return 0
result = updater.run_pending_job(restart=restart_web_service)
except (UpdateError, DatabaseError, ValueError) as exc:
# Public errors are deliberately credential-free. Never log the DB
# config, Gitea PAT, subprocess output or captured environment.
LOG.error("panel update failed: %s", exc)
return 1
LOG.info(
"panel update finished: job=%s old=%s new=%s updated=%s",
result["job_id"], result.get("old_commit"), result.get("new_commit"), result.get("updated"),
)
return 0
if __name__ == "__main__":
raise SystemExit(main())