Files
GarminHealthLab/deploy/push.sh
ericwyuan 14b6bbecb4 fix(deploy): 群晖没有 pgrep,那三处「守卫」一直是空转
上一版给 push.sh 加的 pid 比对刚跑就露馅了:`sh: pgrep: command not found`,
于是 BEFORE 和 AFTER 都是空字符串,`[ -n "$BEFORE" ]` 直接跳过检查——我刚加
的安全网自己就是个摆设。

顺手查了一圈,stop.sh 和 start.sh 里同样的 pgrep 也一直在空转:
- stop.sh 的 `pgrep ... || exit 0` 每次都以 127 失败,永远匹配不到那个提前
  返回,所以每次停服都白等满 15 秒,也从没真正确认过残留 worker 已经没了。
- start.sh 的等待循环同理,白等 20 秒。

DSM 有 pkill 没有 pgrep,而且非特权的 ps 看不见 root 起的进程(服务是开机
以 root 启动的),两者任一都会让检查静默返回「没有」——这正是最危险的答案,
因为它长得和「已经停干净了」一模一样。

- push.sh 改用 `sudo ps -eo pid,args | grep`;并且 AFTER 为空时直接报错退出,
  不再把「看不见」当成「通过」
- stop.sh / start.sh 改用 `ps -eo args | grep -q`

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-01 14:34:27 +08:00

113 lines
4.8 KiB
Bash
Executable File

#!/bin/sh
# Push this working tree to the NAS and restart it.
#
# The NAS only accepts password auth, so one ssh master connection is opened
# up front and every later step rides on it: you type the password once, not
# five times. (macOS's bundled rsync 2.6.9 cannot carry a password through
# -e at all — hence tar over ssh.)
#
# ./deploy/push.sh [user@host] [port]
#
# The password is never stored here — ssh asks for it on your terminal. To
# stop being asked at all, run `ssh-copy-id -p 2222 ericwyuan@192.168.50.64`
# once; after that this script runs unattended.
#
# Never touches .env, .venv or the database on the far side.
set -e
HOST="${1:-ericwyuan@192.168.50.64}"
PORT="${2:-2222}"
# Needed for the sudo restart below. Prompted for rather than stored, and only
# used over the already-authenticated ssh master connection.
PASS="${NAS_PASSWORD:-}"
REPO="$(cd "$(dirname "$0")/.." && pwd)"
CTL="$(mktemp -u /tmp/garmin-deploy-XXXXXX)"
sh_() { ssh -S "$CTL" -o BatchMode=yes "$HOST" "$@"; }
cleanup() { ssh -S "$CTL" -O exit "$HOST" 2>/dev/null || true; }
trap cleanup EXIT
echo "==> connecting to $HOST:$PORT (password prompt follows, once)"
ssh -M -S "$CTL" -fN -p "$PORT" -o ControlPersist=300 "$HOST"
if [ -z "$PASS" ]; then
# Same password as the ssh login; asked for separately because ssh consumed
# the first one itself and sudo on the far side needs it on stdin.
printf 'sudo password for %s (needed to restart the root-owned service): ' "$HOST" >&2
stty -echo 2>/dev/null; read PASS; stty echo 2>/dev/null; echo >&2
fi
# The app dir has moved before; find it rather than assume it.
APP=$(sh_ 'for d in ~/apps/garmin-health-lab /volume1/web/garmin-health-lab; do
[ -d "$d/backend" ] && { echo "$d"; break; }; done')
[ -n "$APP" ] || { echo "cannot find the app dir on $HOST" >&2; exit 1; }
echo "==> app dir: $APP"
# STATIC_DIR is ./static relative to backend/, which is where start.sh cds to.
STATIC="$APP/backend/static"
# `cmd && VAR=x` would trip `set -e` when cmd fails, so spell it out.
if sh_ "[ -f '$APP/static/index.html' ]" 2>/dev/null; then
STATIC="$APP/static"
fi
echo "==> static dir: $STATIC"
# macOS bsdtar writes com.apple.provenance xattrs and ._ resource forks that
# the NAS's tar cannot read; it warns once per file and copies nothing useful.
TAR="tar czf - --no-xattrs"
export COPYFILE_DISABLE=1
echo "==> backend"
$TAR --exclude .venv --exclude .env --exclude __pycache__ \
--exclude '*.db' --exclude tests --exclude .pytest_cache \
-C "$REPO/backend" . | sh_ "tar xzf - -C '$APP/backend'"
echo "==> static (cleared first, so stale JS chunks do not pile up)"
if [ ! -f "$REPO/client/build/index.html" ]; then
echo "client/build is missing — run 'npm run build' first" >&2
exit 1
fi
sh_ "rm -rf '$STATIC' && mkdir -p '$STATIC'"
$TAR -C "$REPO/client/build" . | sh_ "tar xzf - -C '$STATIC'"
# The service is started at boot as root (DSM Task Scheduler -> S99garmin.sh),
# so logs/error.log and logs/access.log are root-owned. Restarting as
# ericwyuan therefore fails instantly — gunicorn cannot open its own error log
# — and this went unnoticed for a whole deploy: stop.sh could not kill a root
# process either, so the OLD master kept serving :8124, start.sh's health
# check saw a 200 and reported "started", and the new code was never loaded.
# Hence sudo, matching how the service actually runs.
SUDO="echo '$PASS' | sudo -S"
echo "==> restart (sudo: the service runs as root, started at boot)"
# DSM has no pgrep, and an unprivileged `ps` cannot see a root-owned process
# — either one silently returns nothing, which would make the pid comparison
# below always "pass" and put the check right back where it started.
pids_() {
sh_ "echo '$PASS' | sudo -S ps -eo pid,args 2>/dev/null \
| grep '$APP/backend/.venv/bin/gunicorn' | grep -v grep \
| awk '{print \$1}' | sort -n | tr '\n' ','"
}
BEFORE=$(pids_ || true)
sh_ "cd '$APP' && $SUDO sh deploy/stop.sh >/dev/null 2>&1; sleep 3; $SUDO sh deploy/start.sh" \
|| { echo "restart failed" >&2; exit 1; }
echo "==> health"
sh_ "curl -sf -m 5 -o /dev/null -w 'local api: %{http_code}\n' \
http://127.0.0.1:8124/api/health/status" || echo "local api: unreachable"
# A 200 alone proves nothing: it is exactly what a surviving old master
# returns. The master pid must have changed for the new code to be loaded.
AFTER=$(pids_ || true)
if [ -z "$AFTER" ]; then
echo "cannot see any gunicorn process - the pid check could not run." >&2
echo "Verify by hand before trusting this deploy." >&2
exit 1
fi
if [ "$BEFORE" = "$AFTER" ]; then
echo "the gunicorn pids did not change ($AFTER) - the old process is still" >&2
echo "serving and your changes are NOT live. Check $APP/logs/error.log." >&2
exit 1
fi
echo "==> gunicorn restarted: $BEFORE -> $AFTER"
echo "done. The public URL takes a few seconds longer (frp reconnecting)."