#!/usr/bin/env python3 """Upload packed .next and restart — assumes source already on pre1.""" from __future__ import annotations import os import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805" TAR = os.path.join(os.environ.get("TEMP", "/tmp"), "nomadro-next.tgz") REMOTE = "/tmp/nomadro-next.tgz" APP = "/var/www/nomadro.cn" CHUNK = 256 * 1024 def connect(): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( HOST, username=USER, password=PASS, timeout=60, allow_agent=False, look_for_keys=False, banner_timeout=120, ) t = c.get_transport() if t: t.set_keepalive(10) return c def run(c: paramiko.SSHClient, cmd: str, timeout: int = 300) -> int: print(f"$ {cmd[:140]}", flush=True) _, stdout, _ = c.exec_command(cmd, timeout=timeout, get_pty=True) while not stdout.channel.exit_status_ready(): if stdout.channel.recv_ready(): print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True) time.sleep(0.05) while stdout.channel.recv_ready(): print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True) code = stdout.channel.recv_exit_status() print(f"[exit {code}]", flush=True) return code def main() -> int: if not os.path.isfile(TAR): print("missing", TAR) return 1 size = os.path.getsize(TAR) print(f"upload {size/1024/1024:.1f}MB from {TAR}", flush=True) c = connect() # light housekeeping first run( c, "pkill -f 'next build' || true; pkill -f 'pnpm build' || true; " "systemctl is-active nomadro.service || true", 60, ) sftp = c.open_sftp() with open(TAR, "rb") as f, sftp.file(REMOTE, "wb") as rf: rf.set_pipelined(True) sent = 0 t0 = time.time() while True: data = f.read(CHUNK) if not data: break rf.write(data) sent += len(data) if sent % (2 * 1024 * 1024) < CHUNK: rate = sent / max(time.time() - t0, 0.1) / 1024 print(f" {sent/1024/1024:.1f}/{size/1024/1024:.1f} MB ({rate:.0f} KB/s)", flush=True) sftp.close() print("uploaded ok", flush=True) code = run( c, f""" set -euo pipefail cd {APP} git fetch --depth 1 origin pre1 git reset --hard FETCH_HEAD git log -1 --oneline rm -rf .next tar -xzf {REMOTE} -C {APP} rm -f {REMOTE} test -f .next/BUILD_ID echo BUILD_ID=$(cat .next/BUILD_ID) sudo systemctl restart nomadro.service sleep 4 systemctl is-active nomadro.service echo '--- local ---' for p in / /api/health /api/config /nav /book /book/read /community /vip/ebook/read /admin; do code=$(curl -s -o /dev/null -w '%{{http_code}}' -m 25 http://127.0.0.1:4003$p || echo ERR) echo "$p -> $code" done echo '--- public ---' for p in / /api/health /api/config /book/read /vip/ebook/read; do code=$(curl -sk -o /dev/null -w '%{{http_code}}' -m 25 https://nomadro.cn$p || echo ERR) echo "https://nomadro.cn$p -> $code" done if [ -x /usr/local/bin/notify-ntfy ]; then /usr/local/bin/notify-ntfy "Nomadro deploy" "portal .next uploaded $(date -u +%Y-%m-%dT%H:%MZ)" default "rocket,nomadro" || true fi echo DONE """, 240, ) c.close() return code if __name__ == "__main__": raise SystemExit(main())