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

56 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy Studio-not-on-air circuit breaker for YouTube relay auto flap."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
FILES = [
("backend/app/youtube_relay_pipeline.py", "/tmp/_dy_yt_relay_pipeline.py"),
]
REMOTE = r'''#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from pathlib import Path
src = Path('/tmp/_dy_yt_relay_pipeline.py')
dst = Path('/opt/douyinWEB/backend/app/youtube_relay_pipeline.py')
dst.write_bytes(src.read_bytes().replace(b'\r\n', b'\n').replace(b'\r', b'\n'))
print('ok', dst, dst.stat().st_size)
PY
python3 -m py_compile /opt/douyinWEB/backend/app/youtube_relay_pipeline.py && echo py_ok
grep -n '_note_studio_not_on_air\|paused_studio\|studio_cooldown' /opt/douyinWEB/backend/app/youtube_relay_pipeline.py | head -20
systemctl restart douyinweb-api.service
sleep 6
systemctl is-active douyinweb-api.service
curl -sS -m 8 http://127.0.0.1:8014/api/ping; echo
'''
def deploy(label, host, port, user, pw, sudo=False):
print(f"\n===== {label} =====")
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)
sftp = c.open_sftp()
for local, remote in FILES:
data = (ROOT / local).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file(remote, "wb") as f:
f.write(data)
with sftp.file("/tmp/_dy_studio_breaker.sh", "w") as f:
f.write(REMOTE.replace("\r\n", "\n"))
sftp.close()
cmd = f"echo '{pw}' | sudo -S -p '' bash /tmp/_dy_studio_breaker.sh" if sudo else "bash /tmp/_dy_studio_breaker.sh"
_, o, e = c.exec_command(cmd, timeout=120)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
deploy("vps-dylive", "107.173.30.245", 22, "root", "Xiao4669805", sudo=False)
deploy("phys-dy", "107.173.30.245", 4014, "eric", "4669805", sudo=True)
print("DONE")