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

126 lines
4.8 KiB
Python

# -*- coding: utf-8 -*-
"""Upgrade PB to 0.25.x (matches pb_sync _superusers), bootstrap both hosts."""
from __future__ import annotations
import json
import sys
import http.cookiejar
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
PB_URL = "https://github.com/pocketbase/pocketbase/releases/download/v0.25.9/pocketbase_0.25.9_linux_amd64.zip"
UPGRADE = rf'''
set -e
systemctl stop douyinweb-pb || true
cd /tmp
curl -fsSL -o pb.zip "{PB_URL}"
unzip -o pb.zip pocketbase -d /opt/douyinWEB/pb
chmod +x /opt/douyinWEB/pb/pocketbase
/opt/douyinWEB/pb/pocketbase --version
# fresh data already; recreate superuser for API env email
EMAIL=$(grep -h '^Environment=DY_PB_ADMIN_EMAIL=' /etc/systemd/system/douyinweb-api.service /etc/systemd/system/douyinweb-api.service.d/* 2>/dev/null | head -1 | cut -d= -f3)
PASS=$(grep -h '^Environment=DY_PB_ADMIN_PASSWORD=' /etc/systemd/system/douyinweb-api.service /etc/systemd/system/douyinweb-api.service.d/* 2>/dev/null | head -1 | cut -d= -f3)
EMAIL=${{EMAIL:-hackrobot@douyin.local}}
PASS=${{PASS:-Xiao4669805@}}
echo "admin $EMAIL"
/opt/douyinWEB/pb/pocketbase superuser upsert "$EMAIL" "$PASS" --dir=/opt/douyinWEB/pb/pb_data
# also common aliases
/opt/douyinWEB/pb/pocketbase superuser upsert "hackrobot@dy.nomadro.com" "$PASS" --dir=/opt/douyinWEB/pb/pb_data || true
/opt/douyinWEB/pb/pocketbase superuser upsert "hackrobot@douyin.local" "$PASS" --dir=/opt/douyinWEB/pb/pb_data || true
cat >/etc/systemd/system/douyinweb-pb.service <<'EOF'
[Unit]
Description=DouyinWEB PocketBase
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/douyinWEB/pb
ExecStart=/opt/douyinWEB/pb/pocketbase serve --http=127.0.0.1:8092 --dir=/opt/douyinWEB/pb/pb_data
Restart=always
RestartSec=3
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
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
'''
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")
def put(text, remote):
sftp = c.open_sftp()
with sftp.file(remote, "w") as f:
f.write(text.replace("\r\n", "\n"))
sftp.close()
return c, run, put
def call(base, path, opener=None, method="GET", body=None, timeout=90):
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
for label, host, port, user, pw, sudo in [
("VPS", "107.173.30.245", 22, "root", "Xiao4669805", False),
("PHYS", "107.173.30.245", 4014, "eric", "4669805", True),
]:
print(f"===== {label} upgrade PB =====", flush=True)
c, run, put = ssh(host, port, user, pw, sudo)
put(UPGRADE, "/tmp/_pb_up.sh")
print(run("bash /tmp/_pb_up.sh", 120))
c.close()
print("===== bootstrap =====", flush=True)
for base in ["https://dylive.nomadro.com", "https://dy.nomadro.com"]:
op = login(base)
print(base, "bootstrap", call(base, "/api/pb/bootstrap", op, "POST", timeout=180)[1])
pb = call(base, "/api/pb/health", op)[1]
print(base, "pb_ok", pb.get("ok"), "in_sync", pb.get("in_sync"), "max", pb.get("max_abs_delta"))
h = call(base, "/api/health", op)[1]
print(base, "ops", {
"yt": (h.get("youtube") or {}).get("ok"),
"pb": (h.get("pocketbase") or {}).get("ok"),
"s3": h.get("s3"),
"ytmon": h.get("youtube_monitor"),
"shorts_run": (h.get("shorts") or {}).get("running"),
})
shorts = call(base, "/api/shorts/status", op)[1]
print(base, "shorts", {k: shorts.get(k) for k in ("enabled", "running", "busy", "quota_gap", "processed", "sampled", "active_jobs", "quota_filled")})
relay = call(base, "/api/youtube/relay/status", op)[1]
print(base, "relay", {k: relay.get(k) for k in ("enabled", "running", "pushing_count", "enabled_groups")})
live = call(base, "/api/youtube/live-map", op, timeout=90)[1]
print(base, "live", live.get("live_count"), live.get("source"))