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

143 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy nodata/on_air gate fix to VPS + physical; stop invalid push; no profile wipe."""
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",
"backend/app/youtube_relay_pipeline.py",
"backend/app/ops_neko.py",
"backend/neko/cdp_studio_scrape.py",
"backend/neko/live-ops-extension/content.js",
]
def put_tree(sftp, local: Path, remote: str) -> None:
data = local.read_bytes()
# write to /tmp then sudo install keeps perms
tmp = f"/tmp/_deploy_{local.name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
return tmp
def deploy_host(host, port, user, pw, sudo: bool) -> None:
print(f"\n===== {user}@{host}:{port} =====")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=40, allow_agent=False, look_for_keys=False, banner_timeout=120)
sftp = c.open_sftp()
tmps = []
for rel in FILES:
local = ROOT / rel
tmp = f"/tmp/_dyfix_{rel.replace('/', '_')}"
with sftp.file(tmp, "wb") as f:
f.write(local.read_bytes())
tmps.append((tmp, f"/opt/douyinWEB/{rel}"))
print("upload", rel, local.stat().st_size)
sftp.close()
lines = ["set -e"]
for tmp, dst in tmps:
lines.append(f"cp -a '{dst}' '{dst}.bak.nodata_$(date +%Y%m%d_%H%M%S)' 2>/dev/null || true")
# in-place for bind-mounted files
lines.append(f"cat '{tmp}' > '{dst}'")
lines.append(f"chmod --reference='{dst}.bak.nodata_'* '{dst}' 2>/dev/null || chmod 644 '{dst}'")
lines.append("echo FILES_OK")
body = "\n".join(lines)
if sudo:
cmd = f"echo '{pw}' | sudo -S -p '' bash -lc {body!r}"
else:
cmd = f"bash -lc {body!r}"
# body!r may break — use script file
sftp = c.open_sftp()
with sftp.file("/tmp/_deploy_nodata_gate.sh", "w") as f:
f.write("#!/bin/bash\nset -e\n")
for tmp, dst in tmps:
f.write(f"test -f '{dst}' || mkdir -p \"$(dirname '{dst}')\"\n")
f.write(f"cp -a '{dst}' \"{dst}.bak.nodata_$(date +%Y%m%d_%H%M%S)\" 2>/dev/null || true\n")
f.write(f"cat '{tmp}' > '{dst}'\n")
f.write(f"echo updated {dst}\n")
f.write("systemctl restart douyinweb-api || systemctl restart douyinweb || true\n")
f.write("sleep 2\n")
f.write("systemctl is-active douyinweb-api || systemctl is-active douyinweb || true\n")
sftp.close()
run = (
f"echo '{pw}' | sudo -S -p '' bash /tmp/_deploy_nodata_gate.sh"
if sudo
else "bash /tmp/_deploy_nodata_gate.sh"
)
_i, o, e = c.exec_command(run, timeout=120)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
def stop_invalid_vps() -> None:
print("\n===== stop invalid push on VPS =====")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False)
script = r"""
python3 - <<'PY'
import sqlite3, time, signal, os
con=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db')
con.row_factory=sqlite3.Row
gid='3fb8cbf7-2180-45bc-99f0-836169f0bf97'
r=con.execute('select * from yt_relay_states where group_id=?',(gid,)).fetchone()
if r:
d=dict(r)
print('before', d.get('status'), 'pid', d.get('ffmpeg_pid'), 'push_sec', d.get('push_seconds'))
pid=int(d.get('ffmpeg_pid') or 0)
if pid>0:
try:
os.kill(pid, signal.SIGTERM)
print('killed', pid)
except Exception as e:
print('kill_err', e)
now=time.time()
con.execute("update yt_relay_states set status=?, error=?, ffmpeg_pid=0, updated_at=? where group_id=?",
('waiting','手动停止:Studio 仍为 NODATA/待开播(未 on_air)的无效推流', now, gid))
con.commit()
print('db set waiting')
else:
print('no state')
# also kill any leftover ffmpeg for youtube rtmp
import subprocess
out=subprocess.check_output("ps -ef | grep -E '[f]fmpeg.*rtmp' || true", shell=True, text=True)
print(out[:500])
PY
systemctl restart douyinweb-api
sleep 3
systemctl is-active douyinweb-api
curl -sS -m 5 http://127.0.0.1:8014/api/health | head -c 120; echo
"""
sftp = c.open_sftp()
with sftp.file("/tmp/_stop_invalid_push.sh", "w") as f:
f.write(script)
sftp.close()
_i, o, e = c.exec_command("bash /tmp/_stop_invalid_push.sh", timeout=60)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
def main() -> None:
deploy_host("107.173.30.245", 22, "root", "Xiao4669805", sudo=False)
deploy_host("107.173.30.245", 4014, "eric", "4669805", sudo=True)
stop_invalid_vps()
print(
"\nNOTE: live-ops content.js is bind-mounted; next Studio navigation picks up nodata signals. "
"No Chrome restart (protect login)."
)
if __name__ == "__main__":
main()