Files
mtusi2026lab3/scripts/stop-one.sh
T

33 lines
981 B
Bash
Executable File

#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
target_container=
for container in $(docker compose --env-file .env ps -q api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
target_container=$container
break
fi
done
if [ -z "$target_container" ]; then
printf '%s\n' 'No running API container matches TARGET_INSTANCE_ID.' >&2
exit 1
fi
if [ -n "${FAILOVER_EVIDENCE_FILE:-}" ]; then
case "$FAILOVER_EVIDENCE_FILE" in
/*) evidence_file=$FAILOVER_EVIDENCE_FILE ;;
*) evidence_file="$ROOT/$FAILOVER_EVIDENCE_FILE" ;;
esac
printf '%s target_instance=%s container=%s\n' \
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$TARGET_INSTANCE_ID" "$target_container" \
>> "$evidence_file"
fi
docker stop --time 2 "$target_container" >/dev/null