#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Deploy receiving-first live-map + probe youtube-svc on both hosts.""" 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_live.py", "backend/app/youtube_neko.py", ] def deploy(host, port, user, pw, sudo: bool) -> None: print(f"\n===== deploy {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) sftp = c.open_sftp() pairs = [] for rel in FILES: tmp = f"/tmp/_recv_{rel.replace('/', '_')}" with sftp.file(tmp, "wb") as f: f.write((ROOT / rel).read_bytes()) pairs.append((tmp, f"/opt/douyinWEB/{rel}")) sh = ["#!/bin/bash", "set -e"] for tmp, dst in pairs: sh.append(f"cat '{tmp}' > '{dst}'") sh.append(f"echo updated {dst}") sh.append("systemctl restart douyinweb-api || true") sh.append("sleep 2") sh.append("systemctl is-active douyinweb-api || true") sh.append("curl -sS -m 4 -o /dev/null -w 'health=%{http_code}\\n' http://127.0.0.1:8014/api/health || true") # youtube-svc probe sh.append("systemctl is-active youtube-svc 2>/dev/null || systemctl is-active douyinweb-youtube-svc 2>/dev/null || echo 'no youtube-svc unit'") sh.append("curl -sS -m 3 http://127.0.0.1:8099/health 2>/dev/null | head -c 120; echo") sh.append("curl -sS -m 3 http://127.0.0.1:8089/health 2>/dev/null | head -c 120; echo") with sftp.file("/tmp/_deploy_recv.sh", "w") as f: f.write("\n".join(sh) + "\n") sftp.close() cmd = f"echo '{pw}' | sudo -S -p '' bash /tmp/_deploy_recv.sh" if sudo else "bash /tmp/_deploy_recv.sh" _, o, e = c.exec_command(cmd, timeout=90) print((o.read() + e.read()).decode("utf-8", "replace")) c.close() def main() -> None: deploy("107.173.30.245", 22, "root", "Xiao4669805", sudo=False) deploy("107.173.30.245", 4014, "eric", "4669805", sudo=True) if __name__ == "__main__": main()