AkurAI Build
Menu

AkurAI-Build

public

Latest change e7a1a89ddab5d1f27fad067498f11157de4d156c - feat(deploy): shared cross-environment health-sweep by Ólafur Búi Ólafsson

#!/usr/bin/env sh
# akurai-health-sweep — probe every AkurAI environment's public health endpoint.
#
# Canonical source: AkurAI-Build repo (scripts/health-sweep.sh). Installed to
# ~/.local/bin/akurai-health-sweep by scripts/install-host-tools.sh, so the CI
# worker (which bind-mounts ~/.local/bin) can run it as the final post-deploy
# step of any pipeline. Deploys therefore prove the health of the deployment
# AND every other environment: a regression elsewhere surfaces as a warning.
#
# Usage: akurai-health-sweep [--self NAME]
#   --self NAME  the environment just deployed. An unhealthy NAME fails the
#                sweep (exit 1); any OTHER unhealthy environment only warns
#                (exit 0). With no --self, any unhealthy environment warns and
#                the sweep still exits 0.
#
# Manifest: $AKURAI_ENV_MANIFEST or ~/.config/akurai-build/environments.tsv
#   Tab-separated lines: NAME<TAB>URL[<TAB>EXPECT]   (EXPECT default 200)
#   Blank lines and lines starting with '#' are ignored.
set -eu

SELF=""
while [ $# -gt 0 ]; do
  case "$1" in
    --self) SELF="${2:-}"; shift 2 ;;
    --self=*) SELF="${1#--self=}"; shift ;;
    -h|--help) sed -n '1,20p' "$0"; exit 0 ;;
    *) echo "unknown argument: $1" >&2; exit 2 ;;
  esac
done

MANIFEST="${AKURAI_ENV_MANIFEST:-$HOME/.config/akurai-build/environments.tsv}"
[ -f "$MANIFEST" ] || { echo "health-sweep: manifest not found: $MANIFEST" >&2; exit 2; }

tab="$(printf '\t')"
self_seen=0
self_bad=0
others_bad=0

printf '%-16s %-10s %s\n' "ENV" "STATUS" "URL"
while IFS="$tab" read -r name url expect || [ -n "${name:-}" ]; do
  case "$name" in ''|\#*) continue ;; esac
  [ -n "${url:-}" ] || continue
  expect="${expect:-200}"
  code="$(curl -sS -o /dev/null -m 10 -w '%{http_code}' "$url" 2>/dev/null || echo 000)"
  if [ "$code" = "$expect" ]; then
    state="ok"
  else
    state="FAIL($code)"
  fi
  printf '%-16s %-10s %s\n' "$name" "$state" "$url"
  if [ "$state" != "ok" ]; then
    if [ -n "$SELF" ] && [ "$name" = "$SELF" ]; then
      self_bad=1
    else
      others_bad=1
    fi
  fi
  [ "$name" = "$SELF" ] && self_seen=1
done < "$MANIFEST"

if [ -n "$SELF" ] && [ "$self_seen" -eq 0 ]; then
  echo "health-sweep: --self '$SELF' is not listed in $MANIFEST" >&2
  exit 2
fi

if [ "$self_bad" -ne 0 ]; then
  [ "$others_bad" -ne 0 ] && echo "WARNING: other environment(s) also regressed (see FAIL rows)" >&2
  echo "ERROR: deployed environment '$SELF' is unhealthy" >&2
  exit 1
fi

if [ "$others_bad" -ne 0 ]; then
  echo "WARNING: one or more OTHER environments regressed (see FAIL rows above); deployment of '${SELF:-?}' is healthy" >&2
  exit 0
fi

echo "health-sweep: all environments healthy${SELF:+ (self=$SELF)}"