#!/bin/bash # Pulse worker deploy (#40), run by the webhook listener on a worker host (CT 153). # # Source of truth: deploy/pulse_worker_deploy.sh in the Pulse repo. # Installed copy: /usr/local/bin/pulse_worker_deploy.sh (see README "Worker deploys"). # # Usage: pulse_worker_deploy.sh # sha full 40-char commit CI tested; must be on origin/main # ts unix time CI signed the request at # Both come from the HMAC-signed webhook payload. The timestamp makes every # signature unique: stale or replayed requests are refused. # # Installs worker/worker.js and the npm manifests from exactly , records the # sha in VERSION (the worker reports it), restarts the worker and waits until the # server's /healthz lists that sha among the connected workers. Otherwise it puts # the previous files back. set -euo pipefail # Overridable only so tests can run the script in a sandbox. APP_DIR=${PULSE_WORKER_APP_DIR:-/opt/pulse-worker} REPO_URL=${PULSE_WORKER_REPO_URL:-https://code.lotusguild.org/LotusGuild/pulse.git} STATE_DIR=${PULSE_WORKER_STATE_DIR:-/var/lib/pulse-worker-deploy} LOG=${PULSE_WORKER_LOG:-/var/log/pulse-worker-deploy.log} LOCK=${PULSE_WORKER_LOCK:-/run/pulse-worker-deploy.lock} HEALTH_URL=${PULSE_WORKER_HEALTH_URL:-http://10.10.10.65:8080/healthz} SERVICE=${PULSE_WORKER_SERVICE:-pulse-worker} BRANCH=main MAX_AGE_S=300 HEALTH_TRIES=${PULSE_WORKER_HEALTH_TRIES:-45} # x 2 s = 90 s to reconnect FILES=(worker.js package.json package-lock.json) log() { echo "$(date '+%F %T') [pulse-worker-deploy] $*" | tee -a "$LOG"; } fail() { log "REFUSED: $*"; exit 1; } SHA="${1:-}" TS="${2:-}" exec 9>"$LOCK" flock -w 600 9 || fail "timed out waiting for another deploy" [[ "$SHA" =~ ^[0-9a-f]{40}$ ]] || fail "missing or invalid sha" [[ "$TS" =~ ^[0-9]+$ ]] || fail "missing or invalid timestamp" now=$(date +%s) age=$(( now - TS )) (( age >= -60 && age <= MAX_AGE_S )) || fail "request is ${age}s old (limit ${MAX_AGE_S}s)" mkdir -p "$STATE_DIR" chmod 700 "$STATE_DIR" last_ts=$(cat "$STATE_DIR/last_ts" 2>/dev/null || echo 0) (( TS > last_ts )) || fail "timestamp $TS is not newer than the last accepted request ($last_ts): replay or out of order" echo "$TS" > "$STATE_DIR/last_ts" # A local clone of the (public) repo, used only to read files at . REPO="$STATE_DIR/repo" if [ ! -d "$REPO/.git" ]; then git clone --quiet --no-checkout "$REPO_URL" "$REPO" fi git -C "$REPO" fetch --quiet origin "$BRANCH" git -C "$REPO" cat-file -e "${SHA}^{commit}" 2>/dev/null || fail "commit $SHA not found after fetch" git -C "$REPO" merge-base --is-ancestor "$SHA" "origin/$BRANCH" || fail "commit $SHA is not on $BRANCH" PREV=$(cat "$APP_DIR/VERSION" 2>/dev/null || echo unknown) healthy() { # healthy : the server lists among connected worker versions local want="$1" body for _ in $(seq 1 "$HEALTH_TRIES"); do if body=$(curl -fsS --max-time 3 "$HEALTH_URL" 2>/dev/null) \ && grep -Eq "\"worker_versions\":\[[^]]*\"$want\"" <<<"$body"; then return 0 fi sleep "${PULSE_WORKER_HEALTH_SLEEP:-2}" done return 1 } if [ "$PREV" = "$SHA" ] && healthy "$SHA"; then log "already running $SHA and connected; nothing to do" exit 0 fi # Keep the current files so a failed deploy can be undone. BACKUP="$STATE_DIR/previous" rm -rf "$BACKUP" && mkdir -p "$BACKUP" for f in "${FILES[@]}" VERSION; do if [ -e "$APP_DIR/$f" ]; then cp -p "$APP_DIR/$f" "$BACKUP/$f"; fi done restart() { # Explicit returns: `set -e` does not apply inside a function used in an `if`. (cd "$APP_DIR" && npm ci --omit=dev --no-audit --no-fund >>"$LOG" 2>&1) || { log "npm ci failed"; return 1; } systemctl restart "$SERVICE" || { log "restart failed"; return 1; } } install_sha() { local src for f in "${FILES[@]}"; do case "$f" in worker.js) src="worker/worker.js" ;; *) src="$f" ;; esac git -C "$REPO" show "$SHA:$src" > "$APP_DIR/$f.new" || { log "could not read $src at $SHA"; return 1; } done for f in "${FILES[@]}"; do mv -f "$APP_DIR/$f.new" "$APP_DIR/$f"; done echo "$SHA" > "$APP_DIR/VERSION" restart } log "deploying worker $SHA (previous $PREV)" if install_sha && healthy "$SHA"; then log "deployed worker $SHA: connected" exit 0 fi log "FAILED: worker $SHA did not reconnect; restoring $PREV" rm -f "$APP_DIR"/*.new "$APP_DIR/VERSION" for f in "${FILES[@]}" VERSION; do if [ -e "$BACKUP/$f" ]; then cp -p "$BACKUP/$f" "$APP_DIR/$f"; fi done if restart && { [ "$PREV" = unknown ] || healthy "$PREV"; }; then log "restored $PREV" else log "RESTORE FAILED: check journalctl -u $SERVICE" fi exit 1