# -*- coding: utf-8 -*- """Fix PB binary, VPS RAM pressure, enable dy shorts/relay, deploy health+FE, verify.""" 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") PB_URL = "https://github.com/pocketbase/pocketbase/releases/download/v0.22.34/pocketbase_0.22.34_linux_amd64.zip" FILES = [ ("backend/app/main.py", "/opt/douyinWEB/backend/app/main.py"), ("frontend/src/app/settings/page.tsx", "/opt/douyinWEB/frontend/src/app/settings/page.tsx"), ] 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: full = f"echo '{pw}' | sudo -S -p '' bash -lc {repr(cmd)}" else: full = cmd _, o, e = c.exec_command(full, timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") return c, run def put_files(c, run, sudo=False): sftp = c.open_sftp() for rel, dest in FILES: data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n") tmp = f"/tmp/_dep_{Path(rel).name}" with sftp.file(tmp, "wb") as f: f.write(data) if sudo: print(run(f"install -D -m 644 {tmp} {dest}; wc -c {dest}")) else: print(run(f"install -D -m 644 {tmp} {dest}; wc -c {dest}")) sftp.close() def install_pb(run): print(run( "set -e; " "mkdir -p /opt/douyinWEB/pb; " "if [ -x /opt/douyinWEB/pb/pocketbase ]; then echo PB_EXISTS; /opt/douyinWEB/pb/pocketbase --version || true; exit 0; fi; " f"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; " "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 || true" )) def enable_shorts_relay(run): # kv_settings print(run( "python3 - <<'PY'\n" "import sqlite3\n" "con=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db')\n" "con.execute('create table if not exists kv_settings (key text primary key, value text not null)')\n" "for k,v in [('shorts_enabled','1'),('youtube_relay_enabled','1')]:\n" " con.execute('insert into kv_settings(key,value) values(?,?) on conflict(key) do update set value=excluded.value',(k,v))\n" "con.commit()\n" "rows=con.execute(\"select key,value from kv_settings where key in ('shorts_enabled','youtube_relay_enabled','yt_long_enabled','s3_enabled')\").fetchall()\n" "print('kv', rows)\n" "n=con.execute('select count(1) from shorts_channels where enabled=1').fetchone()[0]\n" "print('shorts_enabled_channels', n)\n" "g=con.execute('select count(1) from yt_assign_groups where enabled=1').fetchone()[0]\n" "print('assign_enabled_groups', g)\n" "PY" )) def call(base, path, opener=None, method="GET", body=None, timeout=45): 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("===== VPS =====", flush=True) v, vrun = ssh("107.173.30.245", 22, "root", "Xiao4669805") print("mem before", vrun("free -h; ps -eo rss,cmd --sort=-rss | head -20")) # stop crash-loop noise + pause foreign tiktokWEB if present (frees RAM for shorts) print(vrun( "systemctl stop douyinweb-pb 2>/dev/null || true; " "systemctl stop tiktokweb-api 2>/dev/null || true; " "systemctl stop tiktokWEB-api 2>/dev/null || true; " "pkill -f '/opt/tiktokWEB/backend' 2>/dev/null || true; " "docker ps --format '{{.Names}} {{.Status}}' | head -40" )) print("mem mid", vrun("free -h")) install_pb(vrun) put_files(v, vrun, sudo=False) # ensure s3 env / seaweed print(vrun( "systemctl list-units --all | grep -iE 's3|seaweed|weed' | head; " "ss -ltn | grep 8333 || true; " "grep -E 'DY_S3|S3_' /etc/systemd/system/douyinweb-api.service /etc/systemd/system/douyinweb-api.service.d/* 2>/dev/null | head -30" )) enable_shorts_relay(vrun) print(vrun( "systemctl restart douyinweb-yt; " "systemctl restart douyinweb-api; " # FE: rebuild if next build dir; otherwise restart web to pick tsx via next if source-mounted "systemctl restart douyinweb-web; " "sleep 2; systemctl is-active douyinweb-api douyinweb-yt douyinweb-web douyinweb-pb" )) for i in range(24): time.sleep(5) code = vrun("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health; echo").strip() print(f"vps health[{i}] {code}", flush=True) if code.endswith("200"): break print(vrun("curl -sS -m 5 'http://127.0.0.1:8014/api/health' | head -c 500; echo")) print(vrun("curl -sS -m 5 http://127.0.0.1:8020/health; echo; curl -sS -m 3 http://127.0.0.1:8092/api/health; echo; free -h")) v.close() print("===== PHYS =====", flush=True) p, prun = ssh("107.173.30.245", 4014, "eric", "4669805", sudo=True) install_pb(prun) put_files(p, prun, sudo=True) enable_shorts_relay(prun) print(prun( "systemctl restart douyinweb-yt; " "systemctl restart douyinweb-api; " "systemctl restart douyinweb-web; " "sleep 2; systemctl is-active douyinweb-api douyinweb-yt douyinweb-web douyinweb-pb douyinweb-s3" )) for i in range(30): time.sleep(5) code = prun("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health; echo").strip() print(f"phys health[{i}] {code}", flush=True) if code.endswith("200"): break print(prun("curl -sS -m 5 'http://127.0.0.1:8014/api/health' | head -c 500; echo; free -h")) p.close() print("===== PUBLIC VERIFY =====", flush=True) for base in ["https://dylive.nomadro.com", "https://dy.nomadro.com"]: print(f"\n--- {base} ---") try: st, h = call(base, "/api/health", timeout=20) print("health", st, {k: h.get(k) for k in ("ok", "youtube", "pocketbase", "s3", "youtube_monitor", "shorts", "monitor")}) except Exception as ex: print("health ERR", ex) continue op = login(base) shorts = call(base, "/api/shorts/status", op)[1] print("shorts", {k: shorts.get(k) for k in ("enabled", "running", "quota_gap", "processed", "sampled", "last_error")}) ch = call(base, "/api/shorts/channels", op)[1] rows = ch.get("channels") or [] en = [r for r in rows if r.get("enabled")] print("channels", len(rows), "enabled", len(en)) 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")}) ytmon = call(base, "/api/youtube-monitor/status", op)[1] print("yt_svc", ytmon.get("youtube_svc")) try: live = call(base, "/api/youtube/live-map", op, timeout=90)[1] print("live_map", live.get("live_count"), live.get("source")) except Exception as ex: print("live_map ERR", ex)