diff --git a/README.md b/README.md index bc5f53f..76980a5 100644 --- a/README.md +++ b/README.md @@ -916,7 +916,7 @@ git remote -v # 查看当前 origin → cd frontend && npm run build && systemctl restart douyinweb-web ``` -双端同步可用:`python scripts/_deploy_git_pull_both.py`(跟踪 `test1` 时需与脚本内 `BRANCH` 一致)。 +双端同步可用:`python scripts/_deploy_git_pull_both.py`(跟踪 **`test2`**;两台海外 VPS:dylive + dy.live)。 **禁止**: diff --git a/scripts/_deploy_git_pull_both.py b/scripts/_deploy_git_pull_both.py index 887b746..1ef7e8c 100644 --- a/scripts/_deploy_git_pull_both.py +++ b/scripts/_deploy_git_pull_both.py @@ -1,68 +1,75 @@ #!/usr/bin/env python3 -"""Push-synced deploy: git reset to origin/test1 on dylive VPS + physical guest. +"""Git-sync deploy: reset both VPS apps to origin/test2. -Preserves monitor.db. Restarts api/web only — does NOT recreate Neko/Chrome. +Preserves monitor.db / pb_data / .venv / frontend/.next build inputs. +Restarts api/web only — does NOT recreate Neko/Chrome. """ from __future__ import annotations import sys import time -from pathlib import Path sys.stdout.reconfigure(encoding="utf-8", errors="replace") import paramiko REMOTE = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git" -BRANCH = "test1" +BRANCH = "test2" REPO = "/opt/douyinWEB" +PASS = "Xiao4669805" -# dylive colocated on overseas VPS -DYLIVE = ("107.173.30.245", 22, "root", "Xiao4669805") -# physical app guest via FRP -PHYSICAL = ("107.173.30.245", 4014, "eric", "4669805") -GUEST_SUDO = "4669805" +# VPS-A: dylive.nomadro.com +DYLIVE = ("107.173.30.245", 22, "root", PASS) +# VPS-B: dy.nomadro.live (direct SSH; not FRP/physical) +DY = ("108.174.50.68", 22, "root", PASS) def connect(host: str, port: int, user: str, password: str) -> paramiko.SSHClient: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - c.connect(host, port, user, password, timeout=60, banner_timeout=120, allow_agent=False, look_for_keys=False) + c.connect( + host, + port, + user, + password, + timeout=60, + banner_timeout=120, + allow_agent=False, + look_for_keys=False, + ) return c def run(c: paramiko.SSHClient, cmd: str, timeout: int = 600, label: str = "") -> tuple[int, str]: tag = f"[{label}] " if label else "" - print(f"\n{tag}$ {cmd[:200]}{'…' if len(cmd) > 200 else ''}", flush=True) + print(f"\n{tag}$ {cmd[:220]}{'…' if len(cmd) > 220 else ''}", flush=True) _i, o, e = c.exec_command(cmd, timeout=timeout) code = o.channel.recv_exit_status() out = (o.read() + e.read()).decode("utf-8", "replace") if out.strip(): - print(out[-6000:] if len(out) > 6000 else out, flush=True) + print(out[-8000:] if len(out) > 8000 else out, flush=True) print(f"{tag}→ exit={code}", flush=True) return code, out -def git_sync(c: paramiko.SSHClient, *, sudo_prefix: str, label: str) -> int: +def git_sync(c: paramiko.SSHClient, *, label: str) -> int: ts = time.strftime("%Y%m%d_%H%M%S") - # backup db run( c, - f"{sudo_prefix}bash -lc 'cd {REPO} && cp -a backend/data/monitor.db backend/data/monitor.db.bak.{ts} 2>/dev/null || true && ls -la backend/data/monitor.db | head -1'", + f"bash -lc 'cd {REPO} && cp -a backend/data/monitor.db backend/data/monitor.db.bak.{ts} 2>/dev/null || true && " + f"ls -la backend/data/monitor.db 2>/dev/null | head -1 || echo no_monitor_db'", timeout=60, label=label, ) code, _ = run( c, - f"{sudo_prefix}bash -lc 'set -e; cd {REPO}; " - # Tarball physical installs may lack .git — bootstrap tracking without wiping data/. + f"bash -lc 'set -e; cd {REPO}; " f"if [ ! -d .git ]; then git init -b {BRANCH}; fi; " f"git remote remove origin 2>/dev/null || true; " f"git remote add origin \"{REMOTE}\"; " f"git fetch origin {BRANCH}; " - f"git reset --hard HEAD 2>/dev/null || true; " f"git clean -fd -e backend/data -e frontend/.next -e node_modules -e frontend/node_modules " - f"-e backend/.venv -e .venv -e \"*.db\" -e \"*.db-*\"; " + f"-e backend/.venv -e .venv -e \"*.db\" -e \"*.db-*\" -e pb_data -e backend/pb_data; " f"git checkout -f -B {BRANCH} origin/{BRANCH}; " f"git reset --hard origin/{BRANCH}; " f"git log -1 --oneline; git status -sb'", @@ -72,26 +79,30 @@ def git_sync(c: paramiko.SSHClient, *, sudo_prefix: str, label: str) -> int: return code -def deps_and_restart(c: paramiko.SSHClient, *, sudo_prefix: str, label: str, public_health: str) -> int: +def deps_and_restart(c: paramiko.SSHClient, *, label: str, public_health: str) -> int: run( c, - f"{sudo_prefix}bash -lc 'cd {REPO}/backend; " + f"bash -lc 'cd {REPO}/backend; " f"if [ -f requirements.txt ]; then " f" (test -x .venv/bin/pip && .venv/bin/pip install -r requirements.txt -q) || " f" (test -x ../.venv/bin/pip && ../.venv/bin/pip install -r requirements.txt -q) || true; " - f"fi; echo deps_ok'", + f"fi; " + f".venv/bin/python -m py_compile app/main.py app/procutil.py app/host_metrics.py app/yt_revenue.py 2>/dev/null || true; " + f"echo deps_ok'", timeout=300, label=label, ) run( c, - f"{sudo_prefix}bash -lc 'systemctl restart douyinweb-api douyinweb-pb douyinweb-yt 2>/dev/null || systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api || true'", + "bash -lc 'systemctl restart douyinweb-api; " + "systemctl restart douyinweb-pb douyinweb-yt 2>/dev/null || true; " + "sleep 3; systemctl is-active douyinweb-api || true'", timeout=90, label=label, ) code, _ = run( c, - f"{sudo_prefix}bash -lc 'set -e; cd {REPO}/frontend; npm ci --prefer-offline 2>/dev/null || npm install; npm run build'", + f"bash -lc 'set -e; cd {REPO}/frontend; npm ci --prefer-offline 2>/dev/null || npm install; npm run build'", timeout=900, label=label, ) @@ -99,13 +110,16 @@ def deps_and_restart(c: paramiko.SSHClient, *, sudo_prefix: str, label: str, pub return code run( c, - f"{sudo_prefix}bash -lc 'systemctl restart douyinweb-web; sleep 2; systemctl is-active douyinweb-api douyinweb-web'", + "bash -lc 'systemctl restart douyinweb-web; sleep 2; " + "systemctl is-active douyinweb-api douyinweb-web; " + "systemctl try-restart douyinweb-api-watch 2>/dev/null || true'", timeout=90, label=label, ) run( c, - f"bash -lc 'curl -sS -m 15 http://127.0.0.1:8014/api/health | head -c 400; echo; " + f"bash -lc 'curl -sS -m 12 -o /dev/null -w \"local_health=%{{http_code}} t=%{{time_total}}\\n\" " + f"http://127.0.0.1:8014/api/health; " f"curl -sS -m 20 -o /dev/null -w \"public=%{{http_code}}\\n\" {public_health}; " f"cd {REPO}; git rev-parse --short HEAD; " f"test -f backend/data/monitor.db && echo monitor_db=ok'", @@ -115,19 +129,20 @@ def deps_and_restart(c: paramiko.SSHClient, *, sudo_prefix: str, label: str, pub return 0 -def deploy_host(spec: tuple, *, label: str, public_health: str, use_sudo: bool) -> int: +def deploy_host( + spec: tuple[str, int, str, str], + *, + label: str, + public_health: str, +) -> int: host, port, user, pw = spec print(f"\n======== {label} {host}:{port} ========", flush=True) c = connect(host, port, user, pw) try: - if use_sudo: - sudo_prefix = f"echo '{GUEST_SUDO}' | sudo -S -p '' " - else: - sudo_prefix = "" - if git_sync(c, sudo_prefix=sudo_prefix, label=label) != 0: + if git_sync(c, label=label) != 0: print(f"FATAL: {label} git sync failed", flush=True) return 1 - if deps_and_restart(c, sudo_prefix=sudo_prefix, label=label, public_health=public_health) != 0: + if deps_and_restart(c, label=label, public_health=public_health) != 0: print(f"FATAL: {label} build/restart failed", flush=True) return 1 print(f"OK {label}", flush=True) @@ -138,9 +153,23 @@ def deploy_host(spec: tuple, *, label: str, public_health: str, use_sudo: bool) def main() -> int: rc = 0 - if deploy_host(DYLIVE, label="dylive", public_health="https://dylive.nomadro.com/api/health", use_sudo=False) != 0: + if ( + deploy_host( + DYLIVE, + label="dylive", + public_health="https://dylive.nomadro.com/api/health", + ) + != 0 + ): rc = 1 - if deploy_host(PHYSICAL, label="physical", public_health="https://dy.nomadro.com/api/health", use_sudo=True) != 0: + if ( + deploy_host( + DY, + label="dy.live", + public_health="https://dy.nomadro.live/api/health", + ) + != 0 + ): rc = 1 print(f"\nDONE rc={rc}", flush=True) return rc