WFDASH-WEBAPP/scripts/pb-hooks/sync-pb-hooks.sh
Dave 6b99f52dac chore(pocketbase): track live quotes hook and add git-based hook sync
quotes.pb.js is copied byte-for-byte from the live WFDASH PocketBase.
DEPLOY lists the hooks the VPS cron syncs into the hooks volume; the
audit hooks stay undeployed for now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 15:34:41 +02:00

72 lines
2.1 KiB
Bash
Executable file

#!/bin/sh
# Sync the WFDASH PocketBase hooks from git into the live hooks volume.
#
# Runs on the VPS from cron. Pulls origin/main into a sparse clone and copies
# every file listed in pocketbase/pb_hooks/DEPLOY into the PocketBase hooks
# volume. PocketBase reloads itself when a file in pb_hooks changes, so no
# container restart is needed. Files are only written when their content
# changed, so an unchanged repo never triggers a reload.
#
# Usage: sync-pb-hooks.sh [--force] (--force re-syncs even if HEAD is current)
set -eu
REPO="${PB_HOOKS_REPO:-/opt/wfdash-pb-hooks/repo}"
TARGET="${PB_HOOKS_TARGET:-/var/lib/docker/volumes/gnqc88suy8fqk6wyryjjdpvl_pocketbase-hooks/_data}"
LOCK="${PB_HOOKS_LOCK:-/tmp/wfdash-pb-hooks-sync.lock}"
log() { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) $*"; }
exec 9>"$LOCK"
if ! flock -n 9; then
log "another sync is running; skipping"
exit 0
fi
cd "$REPO"
git fetch --quiet origin main
if [ "$(git rev-parse HEAD)" = "$(git rev-parse origin/main)" ] && [ "${1:-}" != "--force" ]; then
exit 0
fi
git reset --quiet --hard origin/main
SRC="$REPO/pocketbase/pb_hooks"
LIST="$SRC/DEPLOY"
if [ ! -f "$LIST" ]; then
log "no $LIST; nothing deployed"
exit 1
fi
# Validate the whole list before touching the live folder.
WANTED=$(grep -v '^[[:space:]]*#' "$LIST" | sed 's/[[:space:]]*$//' | grep -v '^$' || true)
for f in $WANTED; do
case "$f" in
*/*|.*) log "refusing unsafe name in DEPLOY: $f"; exit 1 ;;
esac
if [ ! -f "$SRC/$f" ]; then
log "listed file missing in repo: $f — nothing changed"
exit 1
fi
done
changed=0
for f in $WANTED; do
if ! cmp -s "$SRC/$f" "$TARGET/$f"; then
cp "$SRC/$f" "$TARGET/$f.tmp"
mv "$TARGET/$f.tmp" "$TARGET/$f"
log "updated $f"
changed=1
fi
done
# Remove deployed .js files that are no longer listed.
for path in "$TARGET"/*.js; do
[ -e "$path" ] || continue
name=$(basename "$path")
if ! printf '%s\n' $WANTED | grep -qx "$name"; then
rm -f "$path"
log "removed $name (not in DEPLOY)"
changed=1
fi
done
log "synced $(git rev-parse --short HEAD) (changed=$changed)"