douyinWEB/scripts/_deploy_continue_live.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

147 lines
5.6 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy scrape fix + extension ingest order; optional soft Chrome restart for SW reload."""
from __future__ import annotations
import time
from pathlib import Path
import paramiko
PASS = "4669805"
ROOT = Path(__file__).resolve().parents[1]
SOFT_CHROME = False # NEVER restart Chrome on deploy — signs Google out
FILES = [
("backend/app/youtube_neko.py", "/opt/douyinWEB/backend/app/youtube_neko.py", "_c3_neko.py"),
("backend/neko/cdp_studio_scrape.py", "/opt/douyinWEB/backend/neko/cdp_studio_scrape.py", "_c3_scrape.py"),
(
"backend/neko/live-ops-extension/background.js",
"/opt/douyinWEB/backend/neko/live-ops-extension/background.js",
"_c3_bg.js",
),
(
"backend/neko/live-ops-extension/content.js",
"/opt/douyinWEB/backend/neko/live-ops-extension/content.js",
"_c3_ct.js",
),
("scripts/_probe_telemetry_detail.py", "/tmp/_probe_telemetry_detail.py", "_probe_telemetry_detail.py"),
]
SH = r"""#!/bin/bash
set -euo pipefail
install -m 644 /tmp/_c3_neko.py /opt/douyinWEB/backend/app/youtube_neko.py
install -m 644 /tmp/_c3_scrape.py /opt/douyinWEB/backend/neko/cdp_studio_scrape.py
install -m 644 /tmp/_c3_bg.js /opt/douyinWEB/backend/neko/live-ops-extension/background.js
install -m 644 /tmp/_c3_ct.js /opt/douyinWEB/backend/neko/live-ops-extension/content.js
cd /opt/douyinWEB/backend && .venv/bin/python -m py_compile app/youtube_neko.py
BEFORE=$(docker ps -q --filter name=douyinweb-neko- | wc -l | tr -d ' ')
systemctl restart douyinweb-api
sleep 4
systemctl is-active douyinweb-api
AFTER=$(docker ps -q --filter name=douyinweb-neko- | wc -l | tr -d ' ')
echo "NEKO_BEFORE=$BEFORE NEKO_AFTER=$AFTER"
# Smoke scrape via stdin (no docker cp)
echo SMOKE_SCRAPE
python3 - <<'PY'
import subprocess
from pathlib import Path
script = Path('/opt/douyinWEB/backend/neko/cdp_studio_scrape.py').read_text(encoding='utf-8')
for idx in (1, 5, 6):
name = f'douyinweb-neko-{idx}'
p = subprocess.run(['docker','exec','-i',name,'python3','-'], input=script, capture_output=True, text=True, timeout=30)
line = (p.stdout or p.stderr or '').strip().splitlines()[-1] if (p.stdout or p.stderr) else ''
print(name, 'rc', p.returncode, line[:220])
PY
SOFT_CHROME=__SOFT__
if [ "$SOFT_CHROME" = "1" ]; then
echo "=== soft restart google-chrome (profile kept) ==="
for name in $(docker ps --filter name=douyinweb-neko- --format '{{.Names}}' | sort -V); do
echo -n "$name "
docker exec "$name" supervisorctl restart google-chrome 2>/dev/null \
|| docker exec "$name" supervisorctl restart chromium 2>/dev/null \
|| echo "no_supervisor_chrome"
done
sleep 20
# Re-open Studio URLs if session restore missed livestreaming
python3 - <<'PY'
import json, subprocess, urllib.parse
URLS = {
1: "https://studio.youtube.com/video/9tMkUp0iUH8/livestreaming",
2: "https://studio.youtube.com/video/zHthtNjrbeo/livestreaming",
3: "https://studio.youtube.com/video/8NeMVAjx0r4/livestreaming",
4: "https://studio.youtube.com/video/-kkCblUC8XM/livestreaming",
5: "https://studio.youtube.com/video/31s_VgR215k/livestreaming",
6: "https://studio.youtube.com/video/IKGJwKAZGWM/livestreaming",
7: "https://studio.youtube.com/video/i6szUMA25RM/livestreaming",
}
def run(cmd, timeout=25):
p = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
return p.returncode, (p.stdout or "") + (p.stderr or "")
for idx, url in URLS.items():
name = f"douyinweb-neko-{idx}"
# if already has livestreaming tab, skip
py_check = "import json,urllib.request; tabs=json.loads(urllib.request.urlopen('http://127.0.0.1:9222/json/list',timeout=4).read().decode()); print(sum(1 for t in tabs if 'livestreaming' in (t.get('url') or '')))"
code, out = run(["docker", "exec", name, "python3", "-c", py_check])
if (out or "").strip().splitlines()[-1:] == ["1"] or (out or "").strip().endswith("1"):
print(name, "already_live_tab")
continue
py = f'''
import json, urllib.request
url = {url!r}
req = urllib.request.Request("http://127.0.0.1:9222/json/new?" + url, method="PUT")
with urllib.request.urlopen(req, timeout=10) as r:
print(r.read()[:80])
'''
code, out = run(["docker", "exec", name, "python3", "-c", py])
print(name, "reopen", (out or "")[:80].replace("\\n"," "))
PY
sleep 15
fi
echo PROBE
/opt/douyinWEB/backend/.venv/bin/python /tmp/_probe_telemetry_detail.py
docker ps --filter name=douyinweb-neko- --format '{{.Names}} {{.Status}}'
echo DONE
""".replace("__SOFT__", "1" if SOFT_CHROME else "0")
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(10):
try:
c.connect(
"107.173.30.245",
4014,
"eric",
PASS,
timeout=60,
banner_timeout=120,
allow_agent=False,
look_for_keys=False,
)
break
except Exception as ex:
print("ssh", i, type(ex).__name__)
time.sleep(2)
else:
return 1
sftp = c.open_sftp()
for rel, _dst, tmp in FILES:
with sftp.file(f"/tmp/{tmp}", "wb") as f:
f.write((ROOT / rel).read_bytes())
print("up", rel)
with sftp.file("/tmp/_c3_deploy.sh", "w") as f:
f.write(SH)
sftp.close()
_, o, e = c.exec_command(f"echo '{PASS}' | sudo -S -p '' bash /tmp/_c3_deploy.sh", timeout=600)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())