#!/bin/sh set -eu cd "$(dirname "$0")/.." case "${1:-}" in app1|app2) docker compose stop --timeout 3 "$1" exit ;; "") ;; *) printf '%s\n' 'Expected app1 or app2' >&2 exit 2 ;; esac : "${TARGET_INSTANCE_ID:?Expected target API instance ID}" for replica in app1 app2; do headers=$(docker compose exec -T nginx curl -fsS --noproxy '*' --max-time 3 \ --cacert /run/ca/ca.crt --cert /run/identity/lb.crt --key /run/identity/lb.key \ --connect-to "api.internal:8443:$replica:8443" \ -D - -o /dev/null https://api.internal:8443/health/live) instance=$(printf '%s\n' "$headers" | awk 'tolower($1)=="x-instance-id:" {print $2}' | tr -d '\r') if [ "$instance" = "$TARGET_INSTANCE_ID" ]; then docker compose stop --timeout 3 "$replica" exit fi done printf '%s\n' 'Target API instance was not found' >&2 exit 1