Files
mtusi2026lab7/scripts/stop-one.sh

39 lines
790 B
Bash

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
if [[ -z "${TARGET_INSTANCE_ID:-}" ]]; then
echo "FAIL: TARGET_INSTANCE_ID is missing" >&2
exit 1
fi
for service in api1 api2; do
instance_id=$(
docker compose exec -T "$service" python -c '
import urllib.request
with urllib.request.urlopen(
"http://127.0.0.1:8080/health/live",
timeout=2,
) as response:
print(response.headers.get("X-Instance-Id", ""))
' 2>/dev/null
) || continue
if [[ "$instance_id" == "$TARGET_INSTANCE_ID" ]]; then
echo "Stopping replica: $service"
docker compose stop -t 2 \
"${service}-mtls" "$service"
echo "PASS: target replica stopped"
exit 0
fi
done
echo "FAIL: target API instance not found" >&2
exit 1