Files
GarminHealthLab/deploy/push.sh
2026-09-01 09:09:21 +08:00

70 lines
2.7 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}"
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"
# 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'"
echo "==> restart"
sh_ "sh '$APP/deploy/stop.sh' >/dev/null 2>&1; sleep 2; sh '$APP/deploy/start.sh'"
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"
echo "done. The public URL takes a few seconds longer (frp reconnecting)."