# -*- coding: utf-8 -*- """Safe code-only deploy for X activity feed UI. Never touch business data.""" from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REPO = "/opt/douyinWEB" DB = f"{REPO}/backend/data/monitor.db" c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False) def run(cmd: str, timeout: int = 420) -> str: _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace").strip() print("=== SAFE DEPLOY (code only) ===") before = run(f"stat -c '%s' {DB}") print("[db before]", before) print("[status]", run(f"cd {REPO} && git status -sb | head -8")) # Only pull if clean tracked files; leave untracked alone dirty = run(f"cd {REPO} && git diff --name-only && git diff --cached --name-only") if dirty.strip(): print("[WARN] tracked dirty — stash code files only") print( run( "cd /opt/douyinWEB && git stash push -m 'auto-before-x-feed-7b3c926' -- " "$(git diff --name-only) 2>&1 | tail -20" ) ) print("[pull]", run(f"cd {REPO} && git pull --ff-only origin test1 && git log --oneline -1")) run("systemctl restart douyinweb-api") for i in range(25): st = run("systemctl is-active douyinweb-api", timeout=20) if st == "active": print("[api]", st) break time.sleep(2) else: print("[api]", run("systemctl is-active douyinweb-api", timeout=20)) build = run(f"cd {REPO}/frontend && npm run build", timeout=360) for line in build.splitlines()[-12:]: print(line.encode("ascii", "replace").decode("ascii")) run("systemctl restart douyinweb-web") time.sleep(4) after = run(f"stat -c '%s' {DB}") print("[db after]", after, "delta", int(after) - int(before) if before.isdigit() and after.isdigit() else "?") print("[svc]", run("systemctl is-active douyinweb-api douyinweb-web")) print("[fingerprint]", run(f"du -sb {REPO}/backend/data/covers {REPO}/backend/data/clips 2>/dev/null")) c.close() print("DONE")