nomadro/deploy/observability/_upload_next_chunked.py
hackrobot 05b9ab3e33 Add admin toggle for WeChat/X follow popup with safe config writes.
Expose socialFollowAuto in /admin/config, default off so live behavior stays unchanged; backup and preserve platforms.tt on save; protect remote-config.json during deploy.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-02 04:51:02 +08:00

138 lines
4.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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
# 发版分支:默认 pre3(可用环境变量 DEPLOY_BRANCH 覆盖)
BRANCH = os.environ.get("DEPLOY_BRANCH", "pre3")
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 重置前备份,重置后还原
CFG=data/remote-config.json
BK=/tmp/remote-config.pre-deploy.json
if [ -f "$CFG" ]; then cp -a "$CFG" "$BK"; echo "backed up $CFG"; fi
git fetch --depth 1 origin {BRANCH}
git reset --hard FETCH_HEAD
git log -1 --oneline
if [ -f "$BK" ]; then
mkdir -p data
cp -a "$BK" "$CFG"
echo "restored $CFG (online data preserved)"
rm -f "$BK"
fi
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 /admin/config; 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 '--- config flags ---'
curl -s -m 25 http://127.0.0.1:4003/api/config | python3 -c "import sys,json; d=json.load(sys.stdin); f=d.get('config',{{}}).get('flags',{{}}); print(f)"
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 branch={BRANCH} $(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())