#!/usr/bin/env bash # Verify the API origin through CloudFront /api/health. set -euo pipefail SITE_URL="${SITE_URL:-}" EXPECTED_SHA="${EXPECTED_SHA:-}" BUDGET="${BUDGET:-20}" INTERVAL="${INTERVAL:-5}" if [[ -z "${SITE_URL}" || -z "${EXPECTED_SHA}" ]]; then echo "Usage: SITE_URL EXPECTED_SHA must be set." >&2 exit 2 fi SITE_URL="${SITE_URL%/}" last_code="unreachable" last_sha="unreachable" attempt=0 while [[ "${attempt}" -lt "${BUDGET}" ]]; do attempt=$((attempt + 1)) tmp="$(mktemp)" last_code="$(curl -sS --max-time 30 -o "${tmp}" -w '%{http_code}' "${SITE_URL}/api/health" || printf '000')" last_sha="$(python3 -c 'import json,sys try: print(json.load(open(sys.argv[1], encoding="utf-8")).get("sha") or "") except Exception: print("") ' "${tmp}")" rm -f "${tmp}" echo "poll ${attempt}/${BUDGET}: http=${last_code} sha=${last_sha}" if [[ "${last_code}" == "200" && "${last_sha}" == "${EXPECTED_SHA}" ]]; then echo "PASS: GET /api/health is 200 with sha ${EXPECTED_SHA}" exit 0 fi sleep "${INTERVAL}" done echo "FAIL: GET /api/health did not converge to sha ${EXPECTED_SHA} (last http=${last_code} sha=${last_sha})." >&2 exit 1