#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Physical deploy for studio triad (sudo -S, in-place cat).""" 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 main() -> None: host, port, user, pw = "107.173.30.245", 4014, "eric", "4669805" 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() pairs = [] for rel in FILES: tmp = f"/tmp/_triad_{rel.replace('/', '_')}" with sftp.file(tmp, "wb") as f: f.write((ROOT / rel).read_bytes()) pairs.append((tmp, f"/opt/douyinWEB/{rel}")) print("upload", rel) sh = ["#!/bin/bash", "set -e"] for tmp, dst in pairs: sh.append(f"mkdir -p \"$(dirname '{dst}')\"") # in-place overwrite preserves bind-mount inode when dst already exists sh.append(f"cat '{tmp}' > '{dst}'") sh.append(f"echo updated {dst}") sh.append("systemctl restart douyinweb-api || systemctl restart douyinweb || true") sh.append("sleep 3") sh.append("systemctl is-active douyinweb-api || systemctl is-active douyinweb || true") sh.append("curl -sS -m 5 -o /dev/null -w 'health=%{http_code}\\n' http://127.0.0.1:8014/api/health || true") sh.append("grep -c encoder_lost /opt/douyinWEB/backend/neko/cdp_studio_scrape.py") sh.append( "docker cp /opt/douyinWEB/backend/neko/cdp_studio_scrape.py douyinweb-ops-neko-1:/etc/neko/cdp_studio_scrape.py 2>/dev/null || true" ) sh.append( "docker exec douyinweb-ops-neko-1 python3 /etc/neko/cdp_studio_scrape.py 2>/dev/null | tail -1 | head -c 1200; echo" ) with sftp.file("/tmp/_deploy_triad.sh", "w") as f: f.write("\n".join(sh) + "\n") sftp.close() _, o, e = c.exec_command(f"echo '{pw}' | sudo -S -p '' bash /tmp/_deploy_triad.sh", timeout=120) print((o.read() + e.read()).decode("utf-8", "replace")) c.close() if __name__ == "__main__": main()