137 lines
4.7 KiB
Python
137 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Copy PB 0.25 binary VPS->PHYS via local; fix shorts; verify live."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import time
|
|
import tempfile
|
|
import http.cookiejar
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
PHYS_SH = r'''
|
|
set -e
|
|
systemctl stop douyinweb-pb || true
|
|
install -m 755 /tmp/pocketbase /opt/douyinWEB/pb/pocketbase
|
|
/opt/douyinWEB/pb/pocketbase --version
|
|
rm -rf /opt/douyinWEB/pb/pb_data
|
|
mkdir -p /opt/douyinWEB/pb/pb_data
|
|
/opt/douyinWEB/pb/pocketbase superuser upsert "hackrobot@dy.nomadro.com" "Xiao4669805@" --dir=/opt/douyinWEB/pb/pb_data
|
|
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):
|
|
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)
|
|
return c
|
|
|
|
|
|
print("===== fetch binary from VPS =====", flush=True)
|
|
v = ssh("107.173.30.245", 22, "root", "Xiao4669805")
|
|
vsftp = v.open_sftp()
|
|
local = Path(tempfile.gettempdir()) / "pocketbase_0.25.9"
|
|
vsftp.get("/opt/douyinWEB/pb/pocketbase", str(local))
|
|
vsftp.close()
|
|
v.close()
|
|
print("local size", local.stat().st_size, flush=True)
|
|
|
|
print("===== push to PHYS =====", flush=True)
|
|
p = ssh("107.173.30.245", 4014, "eric", "4669805")
|
|
psftp = p.open_sftp()
|
|
psftp.put(str(local), "/tmp/pocketbase")
|
|
with psftp.file("/tmp/_pb_phys.sh", "w") as f:
|
|
f.write(PHYS_SH.replace("\r\n", "\n"))
|
|
# also pb_sync
|
|
pb_sync = Path(r"c:\Users\admin\Desktop\douyinWEB\backend\app\pb_sync.py").read_bytes().replace(b"\r\n", b"\n")
|
|
with psftp.file("/tmp/_pb_sync.py", "wb") as f:
|
|
f.write(pb_sync)
|
|
psftp.close()
|
|
pw = "4669805"
|
|
|
|
|
|
def pr(cmd, t=120):
|
|
_, o, e = p.exec_command(f"echo '{pw}' | sudo -S -p '' bash -lc {repr(cmd)}", timeout=t)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
print(pr("bash /tmp/_pb_phys.sh", 60))
|
|
print(pr("install -m 644 /tmp/_pb_sync.py /opt/douyinWEB/backend/app/pb_sync.py"))
|
|
p.close()
|
|
|
|
|
|
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
|
|
|
|
|
|
print("===== enable + verify =====", 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"]:
|
|
try:
|
|
st, body = call(base, path, op, "POST", timeout=180)
|
|
print(path, st, str(body)[:180])
|
|
except Exception as ex:
|
|
print(path, "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"),
|
|
})
|
|
try:
|
|
pb = call(base, "/api/pb/health", op)[1]
|
|
print("pb_health ok", pb.get("ok"), "in_sync", pb.get("in_sync"), "delta", pb.get("max_abs_delta"))
|
|
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", "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?force=1", op, timeout=120)[1]
|
|
print("live", live.get("live_count"), live.get("source"), "entries", len(live.get("entries") or {}))
|