# -*- coding: utf-8 -*- """Deploy physical bugfix pack — remote script avoids nested-quote hell.""" from __future__ import annotations import sys import time from pathlib import Path import httpx import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] VPS = "107.173.30.245" GUEST_PW = "4669805" ADMIN_USER = "hackrobot" ADMIN_PASS = "Xiao4669805@" FILES = [ "backend/app/egress_hub.py", "backend/app/db.py", "backend/app/main.py", "backend/app/shorts_upload.py", "backend/app/shorts_oauth.py", "backend/app/shorts_schedule.py", "backend/app/tiktok_oauth.py", "backend/app/youtube_manage.py", "backend/app/yt_long_monetize.py", "backend/app/youtube_neko.py", "backend/app/platform_dashboard.py", "youtube-svc/src/youtube.mjs", "frontend/src/middleware.ts", "frontend/src/components/YtChannelMetaBadges.tsx", "frontend/src/app/youtube/health/page.tsx", "scripts/bootstrap_physical.sh", ] REMOTE_SH = r"""#!/bin/bash set -euo pipefail install -m 644 /tmp/_full_egress_hub.py /opt/douyinWEB/backend/app/egress_hub.py install -m 644 /tmp/_full_db.py /opt/douyinWEB/backend/app/db.py install -m 644 /tmp/_full_main.py /opt/douyinWEB/backend/app/main.py install -m 644 /tmp/_full_shorts_upload.py /opt/douyinWEB/backend/app/shorts_upload.py install -m 644 /tmp/_full_shorts_oauth.py /opt/douyinWEB/backend/app/shorts_oauth.py install -m 644 /tmp/_full_shorts_schedule.py /opt/douyinWEB/backend/app/shorts_schedule.py install -m 644 /tmp/_full_tiktok_oauth.py /opt/douyinWEB/backend/app/tiktok_oauth.py install -m 644 /tmp/_full_youtube_manage.py /opt/douyinWEB/backend/app/youtube_manage.py install -m 644 /tmp/_full_yt_long_monetize.py /opt/douyinWEB/backend/app/yt_long_monetize.py install -m 644 /tmp/_full_youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py install -m 644 /tmp/_full_platform_dashboard.py /opt/douyinWEB/backend/app/platform_dashboard.py install -m 644 /tmp/_full_youtube.mjs /opt/douyinWEB/youtube-svc/src/youtube.mjs install -m 644 /tmp/_full_middleware.ts /opt/douyinWEB/frontend/src/middleware.ts install -m 644 /tmp/_full_YtChannelMetaBadges.tsx /opt/douyinWEB/frontend/src/components/YtChannelMetaBadges.tsx install -m 644 /tmp/_full_page.tsx /opt/douyinWEB/frontend/src/app/youtube/health/page.tsx install -m 644 /tmp/_full_bootstrap_physical.sh /opt/douyinWEB/scripts/bootstrap_physical.sh chmod a+rx /opt/douyinWEB/backend/neko/browser-launch.sh 2>/dev/null || true cd /opt/douyinWEB/backend .venv/bin/python -m py_compile \ app/egress_hub.py app/db.py app/main.py app/shorts_upload.py app/shorts_oauth.py \ app/shorts_schedule.py app/tiktok_oauth.py app/youtube_manage.py \ app/yt_long_monetize.py app/youtube_neko.py app/platform_dashboard.py echo COMPILE_OK YT=/etc/systemd/system/douyinweb-yt.service if ! grep -q 'HTTPS_PROXY=' "$YT"; then awk ' {print} /Environment=NODE_ENV=production/ && !done { print "Environment=HTTP_PROXY=http://127.0.0.1:7890" print "Environment=HTTPS_PROXY=http://127.0.0.1:7890" print "Environment=NO_PROXY=127.0.0.1,localhost,10.0.0.0/8,192.168.0.0/16" done=1 } ' "$YT" > /tmp/_yt.unit.new install -m 644 /tmp/_yt.unit.new "$YT" fi systemctl daemon-reload systemctl restart douyinweb-api douyinweb-yt sleep 3 systemctl is-active douyinweb-api douyinweb-yt systemctl stop douyinweb-web || true chmod 755 /tmp/_fe_full.sh rm -f /tmp/fe-full.log nohup /bin/bash /tmp/_fe_full.sh >/dev/null 2>&1 & echo BUILD """ def ssh_guest(tries: int = 12): last = None for i in range(tries): try: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( VPS, 4014, "eric", GUEST_PW, timeout=40, banner_timeout=80, allow_agent=False, look_for_keys=False, ) return c except Exception as ex: last = ex print(f"frp fail{i} {type(ex).__name__}", flush=True) time.sleep(2 + i) raise last # type: ignore[misc] def run(c, cmd: str, timeout: int = 300) -> str: _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace") def main() -> int: g = ssh_guest() sftp = g.open_sftp() for rel in FILES: data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") tmp = f"/tmp/_full_{Path(rel).name}" with sftp.file(tmp, "wb") as f: f.write(data) print("put", rel, flush=True) with sftp.file("/tmp/_deploy_full.sh", "wb") as f: f.write(REMOTE_SH.replace("\r\n", "\n").encode()) fe = ( "#!/bin/bash\nset -e\n" "cd /opt/douyinWEB/frontend\n" "export DY_INTERNAL_API_URL=http://127.0.0.1:8014\n" "export DY_PUBLIC_BASE_URL=https://dy.nomadro.com\n" "npm run build >/tmp/fe-full.log 2>&1\n" "echo EXIT:$? >>/tmp/fe-full.log\n" ) with sftp.file("/tmp/_fe_full.sh", "wb") as f: f.write(fe.encode()) sftp.close() print("=== install ===", flush=True) print( run( g, f"echo '{GUEST_PW}' | sudo -S -p '' bash /tmp/_deploy_full.sh", 180, ), flush=True, ) g.close() for i in range(45): time.sleep(10) g = ssh_guest() st = run(g, "grep '^EXIT:' /tmp/fe-full.log 2>/dev/null | tail -1") g.close() print(f"fe[{i}] {st.strip()}", flush=True) if "EXIT:0" in st: break if "EXIT:" in st and "EXIT:0" not in st: g = ssh_guest() print(run(g, "tail -60 /tmp/fe-full.log"), flush=True) g.close() raise SystemExit("fe fail") else: raise SystemExit("fe timeout") g = ssh_guest() print( run( g, f"echo '{GUEST_PW}' | sudo -S -p '' systemctl start douyinweb-web; " "sleep 3; systemctl is-active douyinweb-web douyinweb-api douyinweb-yt; " "test -x /opt/douyinWEB/backend/neko/browser-launch.sh && echo LAUNCH_X_OK || echo LAUNCH_X_BAD; " "grep HTTPS_PROXY /etc/systemd/system/douyinweb-yt.service || true; " "curl -sS -m 5 http://127.0.0.1:8014/api/health || true", ), flush=True, ) g.close() with httpx.Client(base_url="https://dy.nomadro.com", timeout=180.0, follow_redirects=True) as client: r = client.post("/api/auth/login", json={"username": ADMIN_USER, "password": ADMIN_PASS}) print("login", r.status_code, flush=True) r.raise_for_status() r = client.post("/api/youtube/manage/profiles/refresh?max_age_sec=0&limit=50") print("profiles", r.status_code, r.text[:280], flush=True) r = client.post("/api/yt-long/channels/probe-ypp?limit=50") print("ypp", r.status_code, r.text[:400], flush=True) r = client.get("/api/youtube/manage/channels") for ch in r.json().get("channels") or []: print("CH", ch.get("name"), "ypp=", ch.get("ypp_ok"), ch.get("custom_url") or "", flush=True) print("DONE", flush=True) return 0 if __name__ == "__main__": raise SystemExit(main())