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

103 lines
3.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy remote ops-neko Live-page gate for auto-relay."""
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
LOCAL = Path(__file__).resolve().parents[1]
FILES = [
"backend/app/youtube_neko.py",
"backend/app/youtube_relay_pipeline.py",
"backend/app/ops_neko.py",
"backend/app/ops_neko_sync.py",
]
def put(c, root: str, sudo: str | None = None) -> None:
sftp = c.open_sftp()
for rel in FILES:
src = LOCAL / rel
tmp = f"/tmp/_ops_live_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(src.read_bytes())
dst = f"{root}/{rel}".replace("\\", "/")
if sudo:
cmd = (
f"echo '{sudo}' | sudo -S -p '' bash -lc "
f"\"mkdir -p $(dirname {dst}); cat {tmp} > {dst}; sed -i 's/\\r$//' {dst}; chmod 644 {dst}\""
)
else:
cmd = f"mkdir -p $(dirname {dst}); cat {tmp} > {dst}; sed -i 's/\\r$//' {dst}; chmod 644 {dst}"
_i, o, e = c.exec_command(cmd, timeout=40)
err = (e.read() or b"").decode("utf-8", "replace")
print("ok", rel, err.strip()[:80], flush=True)
sftp.close()
def restart_api(c, sudo: str | None = None) -> None:
if sudo:
c.exec_command(f"echo {sudo} | sudo -S -p '' systemctl restart douyinweb-api", timeout=60)[1].read()
time.sleep(3)
_i, o, e = c.exec_command(
f"echo {sudo} | sudo -S -p '' systemctl is-active douyinweb-api; "
"grep -c wait_group_ready_for_live /opt/douyinWEB/backend/app/youtube_relay_pipeline.py; "
"grep -c ensure_slot_live_studio /opt/douyinWEB/backend/app/ops_neko.py",
timeout=30,
)
else:
c.exec_command("systemctl restart douyinweb-api", timeout=60)[1].read()
time.sleep(3)
_i, o, e = c.exec_command(
"systemctl is-active douyinweb-api; "
"grep -c wait_group_ready_for_live /opt/douyinWEB/backend/app/youtube_relay_pipeline.py; "
"grep -c ensure_slot_live_studio /opt/douyinWEB/backend/app/ops_neko.py",
timeout=30,
)
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
def main() -> None:
p = paramiko.SSHClient()
p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
p.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False)
print("=== PHYS ===", flush=True)
put(p, "/opt/douyinWEB", "4669805")
restart_api(p, "4669805")
# write ensure script then run
sftp = p.open_sftp()
with sftp.file("/tmp/_ensure_ops_live.py", "wb") as f:
f.write(
b"import sys\n"
b"sys.path.insert(0,'/opt/douyinWEB/backend')\n"
b"from app import ops_neko\n"
b"print(ops_neko.ensure_bound_slots_live_studio())\n"
)
sftp.close()
_i, o, e = p.exec_command(
"echo 4669805 | sudo -S -p '' bash -lc '"
"cd /opt/douyinWEB/backend; "
"if [ -x .venv/bin/python ]; then .venv/bin/python /tmp/_ensure_ops_live.py; "
"else python3 /tmp/_ensure_ops_live.py; fi"
"'",
timeout=90,
)
print("PHYS_ENSURE", (o.read() + e.read()).decode("utf-8", "replace"), flush=True)
p.close()
v = paramiko.SSHClient()
v.set_missing_host_key_policy(paramiko.AutoAddPolicy())
v.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=40, allow_agent=False, look_for_keys=False)
print("=== VPS ===", flush=True)
put(v, "/opt/douyinWEB")
restart_api(v)
v.close()
print("DONE", flush=True)
if __name__ == "__main__":
main()