150 lines
5.1 KiB
Python
150 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Reset incompatible PB data, bootstrap, kick shorts both hosts."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import time
|
|
import http.cookiejar
|
|
import urllib.request
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
PB_RESET = r'''
|
|
set -e
|
|
systemctl stop douyinweb-pb || true
|
|
ts=$(date +%Y%m%d%H%M%S)
|
|
if [ -d /opt/douyinWEB/pb/pb_data ]; then
|
|
mv /opt/douyinWEB/pb/pb_data /opt/douyinWEB/pb/pb_data.bak.$ts
|
|
fi
|
|
mkdir -p /opt/douyinWEB/pb/pb_data
|
|
# drop broken JS migrations that fight binary migrations
|
|
if [ -d /opt/douyinWEB/pb/pb_migrations ]; then
|
|
mv /opt/douyinWEB/pb/pb_migrations /opt/douyinWEB/pb/pb_migrations.bak.$ts || true
|
|
fi
|
|
mkdir -p /opt/douyinWEB/pb/pb_migrations
|
|
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 3
|
|
systemctl is-active douyinweb-pb
|
|
curl -sS -m 5 http://127.0.0.1:8092/api/health; echo
|
|
# create admin if needed (pb first boot)
|
|
# use env from api unit if present
|
|
'''
|
|
|
|
|
|
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=60):
|
|
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:
|
|
raw = r.read().decode()
|
|
try:
|
|
return r.status, json.loads(raw)
|
|
except Exception:
|
|
return r.status, raw[:300]
|
|
|
|
|
|
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} PB reset =====", flush=True)
|
|
c, run, put = ssh(host, port, user, pw, sudo=sudo)
|
|
put(PB_RESET, "/tmp/_pb_reset.sh")
|
|
print(run("bash /tmp/_pb_reset.sh", 60))
|
|
# ensure admin exists via pocketbase superuser upsert (0.22+)
|
|
print(run(
|
|
"/opt/douyinWEB/pb/pocketbase superuser upsert hackrobot@dy.nomadro.com 'Xiao4669805@' "
|
|
"--dir=/opt/douyinWEB/pb/pb_data 2>&1 || "
|
|
"/opt/douyinWEB/pb/pocketbase admin upsert hackrobot@dy.nomadro.com 'Xiao4669805@' "
|
|
"--dir=/opt/douyinWEB/pb/pb_data 2>&1 || true; "
|
|
"systemctl restart douyinweb-pb; sleep 2; "
|
|
"systemctl is-active douyinweb-pb; curl -sS -m 3 http://127.0.0.1:8092/api/health; echo"
|
|
))
|
|
c.close()
|
|
|
|
print("===== bootstrap + kick =====", flush=True)
|
|
for base in ["https://dylive.nomadro.com", "https://dy.nomadro.com"]:
|
|
print(f"\n--- {base} ---")
|
|
op = login(base)
|
|
for path in [
|
|
"/api/pb/bootstrap",
|
|
"/api/shorts/start",
|
|
"/api/youtube/relay/start",
|
|
"/api/youtube-monitor/start",
|
|
"/api/shorts/sample-now",
|
|
]:
|
|
try:
|
|
st, body = call(base, path, op, "POST", timeout=90)
|
|
print(path, st, str(body)[:200])
|
|
except Exception as ex:
|
|
print(path, "ERR", ex)
|
|
st, h = call(base, "/api/health", op)
|
|
print("health", {
|
|
"yt": h.get("youtube"),
|
|
"pb": h.get("pocketbase"),
|
|
"s3": h.get("s3"),
|
|
"ytmon": h.get("youtube_monitor"),
|
|
"shorts": h.get("shorts"),
|
|
})
|
|
try:
|
|
st, pb = call(base, "/api/pb/health", op, timeout=30)
|
|
print("pb_health", st, str(pb)[:250])
|
|
except Exception as ex:
|
|
print("pb_health ERR", ex)
|
|
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", "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_map", live.get("live_count"), live.get("source"))
|