# -*- coding: utf-8 -*- """Safe deploy: TikTok Cookie upload test — code only, never touch monitor.db.""" from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REPO = "/opt/douyinWEB" VENV = f"{REPO}/backend/.venv/bin/python" PIP = f"{REPO}/backend/.venv/bin/pip" CODE_CONFLICT_FILES = [ "backend/app/main.py", "backend/app/models.py", "backend/app/tiktok_db.py", "backend/app/tiktok_pipeline.py", "backend/app/tiktok_schedule.py", "backend/app/tiktok_upload.py", "backend/requirements.txt", "frontend/src/app/tiktok/page.tsx", ".gitignore", ] def run(c, cmd: str, timeout: int = 300) -> tuple[str, str]: print("$", cmd[:200]) _s, out, err = c.exec_command(cmd, timeout=timeout) o = out.read().decode("utf-8", "replace").strip() e = err.read().decode("utf-8", "replace").strip() if o: print(o) if e and "warning" not in e.lower(): print("ERR:", e[:500]) return o, e def main() -> int: 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) print("=== pre-check (business health) ===") run(c, "systemctl is-active douyinweb-api douyinweb-web douyinweb-pb") run(c, f"ls -lh {REPO}/backend/data/monitor.db") run( c, f"{VENV} -c \"import sqlite3; c=sqlite3.connect('{REPO}/backend/data/monitor.db'); " "print('shorts_channels', c.execute('SELECT COUNT(*) FROM shorts_channels').fetchone()[0]); " "print('rooms', c.execute('SELECT COUNT(*) FROM rooms').fetchone()[0])\"", ) print("\n=== git pull (stash code conflicts only, not db) ===") run(c, f"cd {REPO} && git status --short | head -15") files = " ".join(CODE_CONFLICT_FILES) run(c, f"cd {REPO} && git stash push -m 'pre-tiktok-cookie-deploy' -- {files} 2>&1 || true") o, e = run(c, f"cd {REPO} && git pull origin dev 2>&1", timeout=90) if "error:" in (o + e).lower() and "already up to date" not in (o + e).lower(): print("ABORT: git pull failed — not restarting services") c.close() return 1 run(c, f"cd {REPO} && git log --oneline -1") print("\n=== tiktok vendor + python deps ===") run(c, f"cd {REPO} && python3 scripts/setup_tiktok_uploader.py", timeout=600) run(c, f"{PIP} install -r {REPO}/backend/requirements.txt -q", timeout=180) print("\n=== restart API ===") c.exec_command("systemctl restart douyinweb-api", timeout=5) time.sleep(12) run(c, "systemctl is-active douyinweb-api") print("\n=== verify core business APIs ===") run(c, "curl -sS http://127.0.0.1:8014/api/health") run(c, "curl -sS http://127.0.0.1:8014/api/monitor/status | head -c 400") run(c, "curl -sS http://127.0.0.1:8014/api/shorts/status | head -c 400") run(c, "curl -sS http://127.0.0.1:8014/api/tiktok/setup | head -c 500") print("\n=== confirm tiktok still disabled ===") run( c, f"{VENV} -c \"from app import db; print('tiktok_enabled', db.get_settings_map().get('tiktok_enabled'))\"", cwd=f"{REPO}/backend", ) print("\n=== frontend build + restart web ===") run(c, f"cd {REPO}/frontend && npm run build 2>&1 | tail -6", timeout=420) c.exec_command("systemctl restart douyinweb-web", timeout=5) time.sleep(8) run(c, "systemctl is-active douyinweb-web") print("\n=== post-check ===") run(c, "curl -sS -o /dev/null -w 'health=%{http_code}\\n' http://127.0.0.1:8014/api/health") run(c, "curl -sS -o /dev/null -w 'tiktok=%{http_code}\\n' http://127.0.0.1:8014/api/tiktok/status") c.close() print("\nDONE — TikTok Cookie 已部署;tiktok_enabled 默认关,不影响监控/Shorts") return 0 if __name__ == "__main__": raise SystemExit(main())