nomadro/deploy/observability/_upload_next_deploy.py
eric 9f8fec1723 Ship portal PC/H5 acceptance, VIP ScrollReader, admin analytics and ops anchors.
Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 23:52:00 -05:00

126 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Pull source on server, upload local .next, restart nomadro.service."""
from __future__ import annotations
import os
import sys
import tarfile
import tempfile
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805"
APP = "/var/www/nomadro.cn"
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
NEXT_DIR = os.path.join(ROOT, ".next")
GIT_URL = "https://hackrobot:Xiao4669805%40@git.nomadro.cn/hackrobot/nomadro.git"
def run(c: paramiko.SSHClient, cmd: str, timeout: int = 600) -> int:
print(f"\n$ {cmd[:160]}", flush=True)
_, stdout, stderr = 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"\n[exit {code}]", flush=True)
return code
def main() -> int:
if not os.path.isfile(os.path.join(NEXT_DIR, "BUILD_ID")):
print("Missing local .next/BUILD_ID — run pnpm build first", flush=True)
return 1
print("Packing .next ...", flush=True)
tar_path = os.path.join(tempfile.gettempdir(), "nomadro-next.tgz")
with tarfile.open(tar_path, "w:gz") as tar:
tar.add(NEXT_DIR, arcname=".next")
size_mb = os.path.getsize(tar_path) / (1024 * 1024)
print(f"Packed {tar_path} ({size_mb:.1f} MB)", flush=True)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PASS, timeout=30, allow_agent=False, look_for_keys=False, banner_timeout=60)
# sync source (no build)
code = run(
c,
f"""
set -euo pipefail
cd {APP}
export CI=1
git remote set-url origin '{GIT_URL}' || true
git fetch --depth 1 origin pre1
git reset --hard FETCH_HEAD
git log -1 --oneline
# install deps only if package.json changed / missing modules
if [ ! -d node_modules/next ]; then
rm -rf node_modules
pnpm install --force
else
pnpm install --force
fi
echo SOURCE_OK
""",
timeout=900,
)
if code != 0:
c.close()
return code
print("Uploading .next tarball via SFTP ...", flush=True)
sftp = c.open_sftp()
remote_tar = "/tmp/nomadro-next.tgz"
sftp.put(tar_path, remote_tar)
sftp.close()
print("Upload done", flush=True)
code = run(
c,
f"""
set -euo pipefail
cd {APP}
rm -rf .next
tar -xzf /tmp/nomadro-next.tgz -C {APP}
rm -f /tmp/nomadro-next.tgz
test -f .next/BUILD_ID
echo BUILD_ID=$(cat .next/BUILD_ID)
sudo systemctl restart nomadro.service
sleep 3
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 /nav /book /community /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
# ntfy deploy ping (best-effort)
if [ -x /usr/local/bin/notify-ntfy ]; then
/usr/local/bin/notify-ntfy "Nomadro deploy" "portal restarted $(date -u +%Y-%m-%dT%H:%MZ)" default "rocket,nomadro" || true
fi
echo DONE
""",
timeout=180,
)
c.close()
try:
os.remove(tar_path)
except OSError:
pass
return code
if __name__ == "__main__":
raise SystemExit(main())