douyinWEB/scripts/_tmp_phys_pb25_finish.py
2026-09-13 22:10:49 -05:00

156 lines
6.4 KiB
Python

# -*- coding: utf-8 -*-
"""Finish phys PB 0.25 upgrade via VPS copy; deploy pb_sync; verify both."""
from __future__ import annotations
import json
import sys
import time
import http.cookiejar
import urllib.request
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
def ssh(host, port, user, pw, sudo=False):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False)
def run(cmd, t=180):
if sudo:
cmd = f"echo '{pw}' | sudo -S -p '' bash -lc {repr(cmd)}"
_, o, e = c.exec_command(cmd, timeout=t)
return (o.read() + e.read()).decode("utf-8", "replace")
return c, run
# 1) From VPS, push pocketbase binary to phys via FRP SSH if possible, else re-download on phys
print("===== copy PB binary VPS -> prepare =====", flush=True)
v, vr = ssh("107.173.30.245", 22, "root", "Xiao4669805")
print(vr("ls -la /opt/douyinWEB/pb/pocketbase; /opt/douyinWEB/pb/pocketbase --version"))
# serve file briefly via python http on loopback then phys pulls via? easier: phys curl again with retries
v.close()
print("===== PHYS install PB 0.25 =====", flush=True)
p, pr = ssh("107.173.30.245", 4014, "eric", "4669805", sudo=True)
# upload local if we download here, OR retry curl on phys
print(pr(
"set -e; "
"systemctl stop douyinweb-pb || true; "
"cd /tmp; "
"rm -f pb.zip; "
"for i in 1 2 3 4 5; do "
" curl -fL --retry 5 --retry-delay 2 -o pb.zip "
" https://github.com/pocketbase/pocketbase/releases/download/v0.25.9/pocketbase_0.25.9_linux_amd64.zip && break; "
" echo retry_$i; sleep 3; "
"done; "
"ls -la pb.zip; "
"unzip -o pb.zip pocketbase -d /opt/douyinWEB/pb; "
"chmod +x /opt/douyinWEB/pb/pocketbase; "
"/opt/douyinWEB/pb/pocketbase --version; "
# wipe old 0.22 data schema issues if mixed
"rm -rf /opt/douyinWEB/pb/pb_data; mkdir -p /opt/douyinWEB/pb/pb_data; "
"EMAIL=$(grep -h '^Environment=DY_PB_ADMIN_EMAIL=' /etc/systemd/system/douyinweb-api.service | head -1 | cut -d= -f3); "
"PASS=$(grep -h '^Environment=DY_PB_ADMIN_PASSWORD=' /etc/systemd/system/douyinweb-api.service | head -1 | cut -d= -f3); "
"EMAIL=${EMAIL:-hackrobot@dy.nomadro.com}; PASS=${PASS:-Xiao4669805@}; "
"echo admin=$EMAIL; "
"/opt/douyinWEB/pb/pocketbase superuser upsert \"$EMAIL\" \"$PASS\" --dir=/opt/douyinWEB/pb/pb_data; "
"cat >/etc/systemd/system/douyinweb-pb.service <<'EOF'\n"
"[Unit]\nDescription=DouyinWEB PocketBase\nAfter=network.target\n\n"
"[Service]\nType=simple\nWorkingDirectory=/opt/douyinWEB/pb\n"
"ExecStart=/opt/douyinWEB/pb/pocketbase serve --http=127.0.0.1:8092 --dir=/opt/douyinWEB/pb/pb_data\n"
"Restart=always\nRestartSec=3\nLimitNOFILE=65535\n\n"
"[Install]\nWantedBy=multi-user.target\n"
"EOF\n"
"systemctl daemon-reload; systemctl reset-failed douyinweb-pb; systemctl restart douyinweb-pb; "
"sleep 2; systemctl is-active douyinweb-pb; curl -sS -m 3 http://127.0.0.1:8092/api/health; echo"
, 180))
# deploy pb_sync.py
data = (ROOT / "backend/app/pb_sync.py").read_bytes().replace(b"\r\n", b"\n")
sftp = p.open_sftp()
with sftp.file("/tmp/_pb_sync.py", "wb") as f:
f.write(data)
sftp.close()
print(pr("install -m 644 /tmp/_pb_sync.py /opt/douyinWEB/backend/app/pb_sync.py; systemctl restart douyinweb-api"))
for i in range(24):
time.sleep(5)
code = pr("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health; echo").strip()
print(f"phys api[{i}] {code}", flush=True)
if code.endswith("200"):
break
p.close()
# also deploy pb_sync to VPS
print("===== VPS deploy pb_sync =====", flush=True)
v, vr = ssh("107.173.30.245", 22, "root", "Xiao4669805")
sftp = v.open_sftp()
with sftp.file("/tmp/_pb_sync.py", "wb") as f:
f.write(data)
sftp.close()
print(vr("install -m 644 /tmp/_pb_sync.py /opt/douyinWEB/backend/app/pb_sync.py; systemctl restart douyinweb-api"))
for i in range(20):
time.sleep(5)
code = vr("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health; echo").strip()
print(f"vps api[{i}] {code}", flush=True)
if code.endswith("200"):
break
v.close()
def call(base, path, opener=None, method="GET", body=None, timeout=120):
data = None if body is None else json.dumps(body).encode()
req = urllib.request.Request(
f"{base}{path}", data=data, method=method, headers={"Content-Type": "application/json"}
)
op = opener or urllib.request.build_opener()
with op.open(req, timeout=timeout) as r:
return r.status, json.loads(r.read().decode())
def login(base):
cj = http.cookiejar.CookieJar()
op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
call(base, "/api/auth/login", op, "POST", {"username": "hackrobot", "password": "Xiao4669805@"})
return op
print("===== final verify =====", flush=True)
for base in ["https://dylive.nomadro.com", "https://dy.nomadro.com"]:
print(f"\n--- {base} ---")
op = login(base)
try:
print("bootstrap", call(base, "/api/pb/bootstrap", op, "POST", timeout=180)[1])
except Exception as ex:
print("bootstrap ERR", ex)
try:
pb = call(base, "/api/pb/health", op)[1]
print("pb", pb.get("ok"), "in_sync", pb.get("in_sync"), "max_delta", pb.get("max_abs_delta"))
except Exception as ex:
print("pb ERR", ex)
h = call(base, "/api/health", op)[1]
print("health", {
"yt": (h.get("youtube") or {}).get("ok"),
"pb": (h.get("pocketbase") or {}).get("ok"),
"s3": h.get("s3"),
"ytmon": h.get("youtube_monitor"),
"shorts": h.get("shorts"),
"monitor": h.get("monitor"),
})
shorts = call(base, "/api/shorts/status", op)[1]
print("shorts", {k: shorts.get(k) for k in ("enabled", "running", "busy", "quota_gap", "processed", "sampled", "active_jobs", "quota_filled", "last_error")})
relay = call(base, "/api/youtube/relay/status", op)[1]
print("relay", {k: relay.get(k) for k in ("enabled", "running", "pushing_count", "enabled_groups", "last_error")})
live = call(base, "/api/youtube/live-map", op, timeout=90)[1]
print("live", live.get("live_count"), live.get("source"))
# kick shorts once more
try:
call(base, "/api/shorts/sample-now", op, "POST", timeout=60)
except Exception:
pass