# -*- coding: utf-8 -*- """Deploy YouTube.js sidecar + API/UI wiring.""" import time from pathlib import Path import paramiko HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REMOTE = "/opt/douyinWEB" LOCAL = Path(__file__).resolve().parents[1] FILES = [ "backend/app/config.py", "backend/app/main.py", "backend/app/youtube_client.py", "frontend/src/components/Shell.tsx", "frontend/src/app/groups/page.tsx", "frontend/src/app/youtube/page.tsx", "youtube-svc/package.json", "youtube-svc/src/server.mjs", "youtube-svc/src/youtube.mjs", ] UNIT = """[Unit] Description=DouyinWEB YouTube.js sidecar (youtubei.js) After=network.target [Service] Type=simple WorkingDirectory=/opt/douyinWEB/youtube-svc Environment=YT_SVC_HOST=127.0.0.1 Environment=YT_SVC_PORT=8020 Environment=NODE_ENV=production ExecStart=/usr/bin/node src/server.mjs Restart=always RestartSec=3 [Install] WantedBy=multi-user.target """ def run(c, cmd, timeout=420): print("$", cmd[:180], flush=True) chan = c.get_transport().open_session() chan.settimeout(timeout) chan.get_pty() chan.exec_command(cmd) buf = [] while True: if chan.recv_ready(): buf.append(chan.recv(65535).decode("utf-8", "replace")) if chan.exit_status_ready() and not chan.recv_ready(): break time.sleep(0.25) while chan.recv_ready(): buf.append(chan.recv(65535).decode("utf-8", "replace")) code = chan.recv_exit_status() print("".join(buf)[-3000:], flush=True) return code def ensure_dir(sftp, path): parts = path.strip("/").split("/") cur = "" for p in parts: cur += "/" + p try: sftp.stat(cur) except FileNotFoundError: sftp.mkdir(cur) def main(): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() ensure_dir(sftp, f"{REMOTE}/youtube-svc/src") ensure_dir(sftp, f"{REMOTE}/frontend/src/app/youtube") for rel in FILES: print("upload", rel, flush=True) sftp.put(str(LOCAL / rel), f"{REMOTE}/{rel}") # write unit via sftp with sftp.file("/etc/systemd/system/douyinweb-yt.service", "w") as f: f.write(UNIT) sftp.close() run(c, "cd /opt/douyinWEB/youtube-svc && npm install --omit=dev", timeout=300) run(c, "systemctl daemon-reload; systemctl enable douyinweb-yt; systemctl restart douyinweb-yt; sleep 2; systemctl is-active douyinweb-yt") # inject env into api if missing run( c, "grep -q DY_YOUTUBE_SVC_URL /etc/systemd/system/douyinweb-api.service || " "sed -i '/Environment=DY_S3_REGION/a Environment=DY_YOUTUBE_SVC_URL=http://127.0.0.1:8020' /etc/systemd/system/douyinweb-api.service; " "systemctl daemon-reload; systemctl restart douyinweb-api --no-block; sleep 3; systemctl is-active douyinweb-api douyinweb-yt", ) run(c, "curl -s http://127.0.0.1:8020/health; echo; curl -s 'http://127.0.0.1:8020/resolve?url=%40YouTube' | head -c 400; echo") run(c, "curl -s http://127.0.0.1:8014/api/youtube/health; echo") if run(c, "cd /opt/douyinWEB/frontend && npm run build", timeout=420) != 0: raise SystemExit(1) run( c, "systemctl restart douyinweb-web --no-block; sleep 3; " "systemctl start douyinweb-api douyinweb-web douyinweb-yt; sleep 2; " "systemctl is-active douyinweb-api douyinweb-web douyinweb-yt; " "curl -s -o /dev/null -w 'web_youtube=%{http_code}\\n' http://127.0.0.1:4015/youtube", ) c.close() print("DONE") if __name__ == "__main__": main()