Files
mtusi2026lab5/scripts/stop-one.sh
T
2026-11-18 20:33:27 +03:00

30 lines
950 B
Bash

#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PROJECT_DIR=$(dirname -- "$SCRIPT_DIR")
cd "$PROJECT_DIR"
: "${ENV_FILE:=.env}"
target=""
matches=0
for container_id in $(docker compose --env-file "$ENV_FILE" ps -q api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
short_id=$(printf '%s' "$container_id" | cut -c1-12)
if [ "$TARGET_INSTANCE_ID" = "$hostname" ] || [ "$TARGET_INSTANCE_ID" = "$short_id" ]; then
target=$container_id
matches=$((matches + 1))
fi
done
if [ "$matches" -ne 1 ]; then
printf '%s\n' "Expected one API replica for X-Instance-Id, found $matches" >&2
exit 2
fi
# Five seconds keeps the operator hook below the test's ten-second budget while
# still delivering SIGTERM and allowing Spring's graceful shutdown phase.
docker stop --time 5 "$target" >/dev/null