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

118 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy Shorts side-path (default off) without disrupting monitor."""
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/db.py",
"backend/app/models.py",
"backend/app/shorts_pipeline.py",
"backend/app/main.py",
"backend/app/timeutil.py",
"backend/app/shorts_oauth.py",
"backend/app/shorts_clip.py",
"backend/app/shorts_schedule.py",
"backend/app/shorts_upload.py",
"backend/app/youtube_live.py",
"frontend/src/lib/youtubeLive.ts",
"frontend/src/components/YouTubeLiveBadge.tsx",
"frontend/src/components/Shell.tsx",
"frontend/src/components/ShortsUploadUx.tsx",
"frontend/src/components/ShortsJobDetailModal.tsx",
"frontend/src/components/GlobalEventAlerts.tsx",
"frontend/src/app/settings/page.tsx",
"frontend/src/app/shorts/page.tsx",
"backend/app/notify.py",
"docs/SHORTS.md",
"README.md",
]
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[-2000:], 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()
# ensure remote dirs for new paths
for d in ("frontend/src/app/shorts", "backend/app", "docs"):
try:
sftp.stat(f"{REMOTE}/{d}")
except FileNotFoundError:
print("mkdir", d, flush=True)
sftp.mkdir(f"{REMOTE}/{d}")
for rel in FILES:
print("upload", rel, flush=True)
sftp.put(str(LOCAL / rel), f"{REMOTE}/{rel}")
sftp.close()
run(
c,
"systemctl kill -s SIGKILL douyinweb-api || true; sleep 1; "
"systemctl reset-failed douyinweb-api || true; "
"systemctl start douyinweb-api; sleep 4; systemctl is-active douyinweb-api",
)
# Isolation checks: shorts off by default; monitor still healthy
run(
c,
"cd /opt/douyinWEB/backend && .venv/bin/python -c "
"\"from app import db; from app.shorts_pipeline import shorts_pipeline; "
"db.init_db(); "
"n=db.enforce_shorts_daily_force(); "
"db.update_settings_map({'shorts_clip_sec': 15}); "
"cfg=db.get_settings_map(); "
"print('enforced', n); "
"print('shorts_enabled', cfg.get('shorts_enabled')); "
"print('clip_sec', cfg.get('shorts_clip_sec')); "
"print('live_sample', cfg.get('shorts_live_sample')); "
"print('status_enabled', shorts_pipeline.status().get('enabled'))\"",
timeout=60,
)
run(
c,
"curl -sS http://127.0.0.1:8014/api/health | head -c 900; echo; "
"curl -sS http://127.0.0.1:8014/api/shorts/status; echo; "
"curl -sS http://127.0.0.1:8014/api/monitor/status | head -c 400; echo",
timeout=60,
)
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")
c.close()
print("DONE")
if __name__ == "__main__":
main()