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

117 lines
3.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy Studio 开播/断播/停播 classification to VPS + physical."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
FILES = [
("backend/app/youtube_neko.py", "/opt/douyinWEB/backend/app/youtube_neko.py"),
("backend/app/youtube_relay_pipeline.py", "/opt/douyinWEB/backend/app/youtube_relay_pipeline.py"),
("backend/app/ops_neko.py", "/opt/douyinWEB/backend/app/ops_neko.py"),
("backend/neko/cdp_studio_scrape.py", "/opt/douyinWEB/backend/neko/cdp_studio_scrape.py"),
(
"backend/neko/live-ops-extension/content.js",
"/opt/douyinWEB/backend/neko/live-ops-extension/content.js",
),
]
VPS = ("107.173.30.245", 22, "root", "Xiao4669805")
PHYS = ("107.173.30.245", 4014, "eric", "4669805")
def put_inplace(sftp, local: Path, remote: str) -> None:
"""Overwrite existing file in-place (preserve inode for bind mounts)."""
data = local.read_bytes()
try:
with sftp.file(remote, "r+") as f:
f.write(data)
f.truncate(len(data))
except OSError:
with sftp.file(remote, "wb") as f:
f.write(data)
def deploy(host, port, user, pwd, *, restart_api: bool, sync_ops_ext: bool) -> None:
print(f"\n=== {user}@{host}:{port} ===")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pwd, timeout=30, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
for rel, remote in FILES:
put_inplace(sftp, ROOT / rel, remote)
print("put", remote)
sftp.close()
def run(cmd: str, t: int = 90) -> str:
_, o, e = c.exec_command(cmd, timeout=t)
o.channel.settimeout(t)
try:
return (o.read() + e.read()).decode("utf-8", "replace")
except Exception as ex:
return f"ERR {type(ex).__name__}: {ex}"
if sync_ops_ext:
# In-place into running ops-neko containers if extension is bind-mounted or copied.
print(
run(
r"""
for i in 1 2 3 4 5 6; do
name=douyinweb-ops-neko-$i
docker inspect "$name" >/dev/null 2>&1 || continue
# Prefer host bind path already updated; also copy into container path if present.
for p in /opt/douyinWEB/backend/neko/live-ops-extension/content.js; do
docker cp "$p" "$name:/home/neko/.config/chromium/live-ops-extension/content.js" 2>/dev/null || true
docker cp "$p" "$name:/etc/neko/live-ops-extension/content.js" 2>/dev/null || true
done
docker cp /opt/douyinWEB/backend/neko/cdp_studio_scrape.py "$name:/etc/neko/cdp_studio_scrape.py" 2>/dev/null || true
done
echo ops_ext_synced
""",
60,
).strip()
)
if restart_api:
unit = "douyinweb-api"
print("restart", run(f"systemctl restart {unit}; sleep 2; systemctl is-active {unit}", 50).strip())
print(
"health",
run(
"curl -sS -m 5 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo fail",
20,
).strip(),
)
c.close()
def main() -> None:
deploy(*VPS, restart_api=True, sync_ops_ext=False)
deploy(*PHYS, restart_api=True, sync_ops_ext=True)
# Verify CDP classification on physical ops-1
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(*PHYS, timeout=30, allow_agent=False, look_for_keys=False)
_, o, e = c.exec_command(
r"""
# find venv
PY=/opt/douyinWEB/backend/.venv/bin/python
[ -x "$PY" ] || PY=python3
docker exec douyinweb-ops-neko-1 python3 /etc/neko/cdp_studio_scrape.py 2>/dev/null | tail -1
""",
timeout=40,
)
print("\n=== CDP ops1 after deploy ===")
print((o.read() + e.read()).decode("utf-8", "replace")[:1500])
c.close()
if __name__ == "__main__":
main()