实例页增加远程更新客户端:下发 git pull + pm2 restart,并轮询版本确认上线。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-04 07:11:15 +08:00
parent f7ffd08274
commit 8e2576661a
6 changed files with 370 additions and 55 deletions
+89
View File
@@ -269,6 +269,95 @@ def pull_all_stats(window: str = "7d") -> list[dict[str, Any]]:
return results
def fetch_client_version(inst: dict[str, Any]) -> dict[str, Any]:
"""探测客户端 git 版本 / 在线."""
base = (inst.get("base_url") or "").rstrip("/")
token = (inst.get("bridge_token") or "").strip()
out: dict[str, Any] = {
"ok": False,
"online": False,
"sha": "",
"short_sha": "",
"branch": "",
"dirty": False,
"msg": "",
"instance_id": inst.get("id"),
"instance_name": inst.get("name"),
}
if not base or not token:
out["msg"] = "缺少 URL 或 Token"
return out
url = f"{base}/api/hub/version"
try:
r = requests.get(
url,
headers={"X-Hub-Token": token, "Accept": "application/json"},
timeout=_timeout(),
)
data = r.json() if r.content else {}
if not isinstance(data, dict):
out["msg"] = f"HTTP {r.status_code}"
return out
for key in ("sha", "short_sha", "branch", "dirty", "msg", "repo"):
if key in data:
out[key] = data.get(key)
out["ok"] = bool(data.get("ok")) and r.status_code == 200
out["online"] = out["ok"]
if not out["ok"]:
out["msg"] = data.get("msg") or f"HTTP {r.status_code}"
return out
except requests.Timeout:
out["msg"] = "超时"
return out
except Exception as e:
out["msg"] = str(e) or "探测失败"
return out
def post_client_update(inst: dict[str, Any]) -> dict[str, Any]:
"""触发客户端快速更新(git pull + pm2 restart)."""
base = (inst.get("base_url") or "").rstrip("/")
token = (inst.get("bridge_token") or "").strip()
if not base or not token:
return {
"ok": False,
"msg": "缺少 URL 或 Token",
"instance_id": inst.get("id"),
"instance_name": inst.get("name"),
}
url = f"{base}/api/hub/update"
try:
r = requests.post(
url,
headers=_headers(token),
json={},
timeout=_timeout(),
)
data = r.json() if r.content else {}
if not isinstance(data, dict):
data = {"ok": False, "msg": f"HTTP {r.status_code}"}
data["instance_id"] = inst.get("id")
data["instance_name"] = inst.get("name")
if r.status_code >= 400 and "ok" not in data:
data["ok"] = False
data.setdefault("msg", f"HTTP {r.status_code}")
return data
except requests.Timeout:
return {
"ok": False,
"msg": "客户端超时(更新可能仍在进行,请刷新版本确认)",
"instance_id": inst.get("id"),
"instance_name": inst.get("name"),
}
except Exception as e:
return {
"ok": False,
"msg": str(e) or "下发失败",
"instance_id": inst.get("id"),
"instance_name": inst.get("name"),
}
def fetch_backup_list(inst: dict[str, Any]) -> dict[str, Any]:
base = (inst.get("base_url") or "").rstrip("/")
token = (inst.get("bridge_token") or "").strip()