#!/bin/bash # Pulse production deploy (#45 phase 2), run by the webhook listener on CT 122. # # Source of truth: deploy/pulse_deploy.sh in the Pulse repo. # Installed copy: /usr/local/bin/pulse_deploy.sh (install it from the repo; see README "Deploys"). # # Usage: pulse_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, so they can't be altered. The # timestamp makes every signature unique: stale or replayed requests are refused. # # Deploys exactly , then waits for /healthz to report that sha with a working # database. Otherwise it rolls back to the previous commit. set -euo pipefail # Overridable only so tests can run the script in a sandbox. APP_ROOT=${PULSE_DEPLOY_APP_ROOT:-/opt/pulse-server} BRANCH=main STATE_DIR=${PULSE_DEPLOY_STATE_DIR:-/var/lib/pulse-deploy} LOG=${PULSE_DEPLOY_LOG:-/var/log/pulse-deploy.log} LOCK=${PULSE_DEPLOY_LOCK:-/run/pulse-deploy.lock} HEALTH_URL=${PULSE_DEPLOY_HEALTH_URL:-http://127.0.0.1:8080/healthz} MAX_AGE_S=300 # refuse requests signed more than 5 minutes ago HEALTH_TRIES=${PULSE_DEPLOY_HEALTH_TRIES:-45} # x 2 s = 90 s for the new process to report healthy log() { echo "$(date '+%F %T') [pulse-deploy] $*" | tee -a "$LOG"; } fail() { log "REFUSED: $*"; exit 1; } SHA="${1:-}" TS="${2:-}" # One deploy at a time (CI also queues deploys, this covers manual runs). 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" cd "$APP_ROOT" git fetch --quiet origin "$BRANCH" git cat-file -e "${SHA}^{commit}" 2>/dev/null || fail "commit $SHA not found after fetch" git merge-base --is-ancestor "$SHA" "origin/$BRANCH" || fail "commit $SHA is not on $BRANCH" PREV=$(git rev-parse HEAD) deploy_to() { # .env and node_modules are untracked, so a forced checkout leaves .env alone. # Explicit returns: `set -e` does not apply inside a function used in an `if`. git checkout --quiet --force --detach "$1" || { log "checkout of $1 failed"; return 1; } npm ci --omit=dev --no-audit --no-fund >>"$LOG" 2>&1 || { log "npm ci failed for $1"; return 1; } systemctl restart pulse || { log "restart failed for $1"; return 1; } } healthy() { local want="$1" body for _ in $(seq 1 "$HEALTH_TRIES"); do if body=$(curl -fsS --max-time 3 "$HEALTH_URL" 2>/dev/null) \ && grep -q "\"sha\":\"$want\"" <<<"$body" \ && grep -q '"db":true' <<<"$body"; then return 0 fi sleep "${PULSE_DEPLOY_HEALTH_SLEEP:-2}" done return 1 } if [ "$PREV" = "$SHA" ] && healthy "$SHA"; then log "already running $SHA and healthy; nothing to do" exit 0 fi log "deploying $SHA (previous $PREV)" if deploy_to "$SHA" && healthy "$SHA"; then echo "$SHA" > "$STATE_DIR/current" log "deployed $SHA: healthy" exit 0 fi log "FAILED: $SHA did not become healthy; rolling back to $PREV" if deploy_to "$PREV" && healthy "$PREV"; then log "rolled back to $PREV: healthy" else log "ROLLBACK FAILED: check journalctl -u pulse" fi exit 1