#!/usr/bin/env python3 """Deploy CPU optimizations + harden Netdata / Neko / systemd on VPS.""" from __future__ import annotations import sys import time from pathlib import Path import paramiko if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST = "108.174.50.68" USER = "root" PASS = "Xiao4669805" ROOT = Path(__file__).resolve().parents[1] FILES = [ "backend/app/shorts_clip.py", "backend/app/yt_long_clip.py", "backend/app/clip_cascade.py", "backend/app/youtube_relay.py", "backend/app/youtube_relay_pipeline.py", "backend/app/youtube_relay_presets.py", "backend/app/host_metrics.py", "backend/app/db.py", "backend/app/config.py", "backend/app/main.py", "backend/app/shorts_pipeline.py", "backend/app/tiktok_pipeline.py", "backend/app/x_clips_pipeline.py", "backend/app/yt_long_pipeline.py", "frontend/src/components/HostResourceBar.tsx", "frontend/src/components/YtAssignPanel.tsx", "frontend/src/app/x/page.tsx", "frontend/src/app/web/page.tsx", "frontend/src/app/ops/page.tsx", "frontend/src/app/rooms/page.tsx", "frontend/src/app/shorts/page.tsx", "frontend/src/app/yt-long/page.tsx", "frontend/src/app/tiktok/page.tsx", "frontend/src/lib/douyinLive.ts", "frontend/src/lib/youtubeLive.ts", ] def run(c: paramiko.SSHClient, cmd: str, timeout: int = 300) -> str: _, out, err = c.exec_command(cmd, timeout=timeout) text = out.read().decode("utf-8", "replace") e = err.read().decode("utf-8", "replace").strip() if e and "Warning" not in e[:40]: text += ("\nERR: " + e[:800]) if text else ("ERR: " + e[:800]) return text.strip() def main() -> None: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASS, timeout=45, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() for rel in FILES: local = ROOT / rel remote = f"/opt/douyinWEB/{rel.replace(chr(92), '/')}" print(f"upload {rel}") sftp.put(str(local), remote) sftp.close() # DB preset + Netdata + API CPUQuota + stop idle neko if possible harden = r""" set -e cd /opt/douyinWEB/backend .venv/bin/python - <<'PY' from app import db db.update_settings_map({ 'youtube_relay_quality_preset': 'vps_ultra', 'priority_interval_sec': 60.0, 'yt_live_interval_sec': 120.0, }) print('settings ok', db.get_settings_map().get('youtube_relay_quality_preset')) PY # Netdata: slow collectors mkdir -p /etc/netdata if [ -f /etc/netdata/netdata.conf ]; then grep -q 'update every' /etc/netdata/netdata.conf || true fi cat > /etc/netdata/netdata.conf <<'EOF' [global] update every = 5 memory mode = dbengine page cache size = 32 dbengine multihost disk space = 128 [plugins] python.d = no charts.d = no node.d = no go.d = yes apps = yes cgroups = no diskspace = yes proc = yes tc = no [web] bind to = 127.0.0.1 EOF # apps.plugin every 5s if override exists mkdir -p /etc/netdata/apps_groups.conf.d 2>/dev/null || true systemctl restart netdata 2>/dev/null || true # API: CPU weight / nice via drop-in mkdir -p /etc/systemd/system/douyinweb-api.service.d cat > /etc/systemd/system/douyinweb-api.service.d/cpu.conf <<'EOF' [Service] Nice=5 CPUWeight=80 EOF mkdir -p /etc/systemd/system/douyinweb-web.service.d cat > /etc/systemd/system/douyinweb-web.service.d/cpu.conf <<'EOF' [Service] Nice=10 CPUWeight=40 EOF mkdir -p /etc/systemd/system/netdata.service.d cat > /etc/systemd/system/netdata.service.d/cpu.conf <<'EOF' [Service] Nice=15 CPUWeight=20 CPUQuota=25% EOF systemctl daemon-reload systemctl restart douyinweb-api sleep 3 systemctl is-active douyinweb-api systemctl restart netdata 2>/dev/null || true # Stop idle neko containers that are not actively needed — leave running ones; lower is better docker ps --format '{{.Names}}' | grep neko || true uptime """ print(run(c, harden, timeout=120)) print(run(c, "cd /opt/douyinWEB/frontend && npm run build 2>&1 | tail -15", timeout=600)) c.exec_command("systemctl restart douyinweb-web", timeout=5) time.sleep(4) print("web:", run(c, "systemctl is-active douyinweb-web")) print( run( c, "uptime; pgrep -a ffmpeg | head -2 || echo no_ffmpeg; " "ps aux --sort=-%cpu | head -8; " "docker stats --no-stream 2>/dev/null | head -6", timeout=40, ) ) c.close() if __name__ == "__main__": main()