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

120 lines
4.7 KiB
Python

# -*- coding: utf-8 -*-
"""Create PB admin, re-bootstrap, explain dy relay pushing=0."""
from __future__ import annotations
import json
import sys
import http.cookiejar
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
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=40, allow_agent=False, look_for_keys=False)
def run(cmd, t=120):
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
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:
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
ADMIN_SH = r'''
set +e
cd /opt/douyinWEB/pb
./pocketbase --help 2>&1 | head -40
echo '---'
# 0.22.x uses: ./pocketbase admin create EMAIL PASS
./pocketbase admin create "hackrobot@dy.nomadro.com" "Xiao4669805@" --dir=./pb_data 2>&1
./pocketbase admin update "hackrobot@dy.nomadro.com" "Xiao4669805@" --dir=./pb_data 2>&1
# also try env emails from systemd
EMAIL=$(grep -h DY_PB_ADMIN_EMAIL /etc/systemd/system/douyinweb-api.service /etc/systemd/system/douyinweb-api.service.d/* 2>/dev/null | head -1 | sed 's/.*=//')
PASS=$(grep -h DY_PB_ADMIN_PASSWORD /etc/systemd/system/douyinweb-api.service /etc/systemd/system/douyinweb-api.service.d/* 2>/dev/null | head -1 | sed 's/.*=//')
echo "env_email=$EMAIL"
if [ -n "$EMAIL" ] && [ -n "$PASS" ]; then
./pocketbase admin create "$EMAIL" "$PASS" --dir=./pb_data 2>&1
./pocketbase admin update "$EMAIL" "$PASS" --dir=./pb_data 2>&1
fi
systemctl restart douyinweb-pb
sleep 2
systemctl is-active douyinweb-pb
curl -sS -m 3 http://127.0.0.1:8092/api/health; echo
'''
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} admin =====", flush=True)
c, run = ssh(host, port, user, pw, sudo)
sftp = c.open_sftp()
with sftp.file("/tmp/_pb_admin.sh", "w") as f:
f.write(ADMIN_SH.replace("\r\n", "\n"))
sftp.close()
print(run("bash /tmp/_pb_admin.sh", 60))
c.close()
for base in ["https://dylive.nomadro.com", "https://dy.nomadro.com"]:
print(f"\n===== {base} =====")
op = login(base)
print("bootstrap", call(base, "/api/pb/bootstrap", op, "POST", timeout=120)[1])
print("pb_health", str(call(base, "/api/pb/health", op)[1])[:400])
st, h = call(base, "/api/health", op)
print("ops", {
"youtube": h.get("youtube"),
"pb": (h.get("pocketbase") or {}).get("ok"),
"s3": h.get("s3"),
"ytmon": h.get("youtube_monitor"),
"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")})
# dig relay groups why not pushing on dy
try:
st, groups = call(base, "/api/youtube/relay/groups", op, timeout=40)
rows = groups.get("groups") or groups.get("items") or groups
if isinstance(rows, list):
for g in rows[:12]:
if not isinstance(g, dict):
continue
print(
" g",
(g.get("name") or g.get("id") or "")[:28],
"en",
g.get("enabled"),
"st",
(g.get("relay") or g.get("state") or {}).get("status") if isinstance(g.get("relay") or g.get("state"), dict) else g.get("status"),
"prefer",
g.get("neko_open_prefer"),
)
except Exception as ex:
print("groups ERR", ex)
live = call(base, "/api/youtube/live-map", op, timeout=90)[1]
print("live_map", live.get("live_count"), live.get("source"))