Files
mtusi2026lab3/scripts/stop-one.sh
T

32 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
# Failover hook of `make test-failover` (tests/resilience.py): stops the one
# API replica whose X-Instance-Id is $TARGET_INSTANCE_ID and leaves it
# stopped. The stop is graceful: on SIGTERM the replica turns unready, keeps
# serving for its drain delay while HAProxy takes it out of rotation, then
# exits; `docker stop` returns once it has. Nothing else is touched, and an
# alias that does not name exactly one running replica stops nothing.
# Restore the replica with: docker compose start api
set -eu
cd "$(dirname "$0")/.."
target=${TARGET_INSTANCE_ID:-}
case "$target" in
'' | *[!0-9a-f]*)
echo "TARGET_INSTANCE_ID must be the 16 lowercase hex characters of an X-Instance-Id" >&2
exit 2
;;
esac
if [ ${#target} -ne 16 ]; then
echo "TARGET_INSTANCE_ID must be the 16 lowercase hex characters of an X-Instance-Id" >&2
exit 2
fi
matches=$(sh scripts/api-instances.sh | awk -v t="$target" '$3 == "running" && $4 == t { print $1 }')
count=$(printf '%s\n' "$matches" | grep -c . || true)
if [ "$count" -ne 1 ]; then
echo "expected one running API replica with X-Instance-Id $target, found $count" >&2
exit 2
fi
docker stop "$matches" >/dev/null
echo "stopped API container $matches (X-Instance-Id $target)"