douyinWEB/scripts/deploy_all_features.py
2026-08-10 20:11:23 -05:00

78 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy P1-P5: API key, PB health, group URL probe, tests support."""
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
REMOTE = "/opt/douyinWEB"
LOCAL = Path(__file__).resolve().parents[1]
FILES = [
"backend/app/auth.py",
"backend/app/config.py",
"backend/app/main.py",
"backend/app/pb_sync.py",
"backend/app/db.py",
"backend/app/youtube_monitor.py",
"backend/requirements.txt",
"backend/tests/conftest.py",
"backend/tests/test_reconcile_yt.py",
"backend/tests/test_youtube_live.py",
"frontend/src/lib/api.js",
"frontend/src/app/settings/page.tsx",
"frontend/src/app/groups/page.tsx",
]
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()
text = "".join(buf)
clean = "".join(ch for ch in text if ord(ch) < 128 or "\u4e00" <= ch <= "\u9fff")
print(clean[-2500:], flush=True)
return code
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()
run(c, f"mkdir -p {REMOTE}/backend/tests")
for rel in FILES:
print("upload", rel, flush=True)
sftp.put(str(LOCAL / rel), f"{REMOTE}/{rel}")
sftp.close()
run(c, f"cd {REMOTE}/backend && pip install -q -r requirements.txt", timeout=180)
run(c, "systemctl restart douyinweb-api --no-block; sleep 5; systemctl is-active douyinweb-api")
run(c, "curl -sS http://127.0.0.1:8014/api/auth/status; echo")
run(c, "curl -sS http://127.0.0.1:8014/api/pb/health | head -c 600; 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 is-active douyinweb-api douyinweb-web")
run(c, f"cd {REMOTE}/backend && python -m pytest tests/ -q", timeout=120)
c.close()
print("DONE")
if __name__ == "__main__":
main()