Files
mtusi2026lab5/scripts/stop-one.sh
T
2026-11-10 16:21:46 +03:00

41 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
project_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$project_root"
env_file=${ENV_FILE:-.env}
matched_container=
compose() {
if [ -f "$env_file" ]; then
docker compose --env-file "$env_file" "$@"
else
docker compose "$@"
fi
}
for container_id in $(compose ps -a -q api); do
instance_id=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
if [ "$instance_id" = "$TARGET_INSTANCE_ID" ]; then
if [ -n "$matched_container" ]; then
printf '%s\n' "multiple API containers match instance $TARGET_INSTANCE_ID" >&2
exit 1
fi
matched_container=$container_id
fi
done
if [ -z "$matched_container" ]; then
printf '%s\n' "API instance not found: $TARGET_INSTANCE_ID" >&2
exit 1
fi
# A manual docker stop suppresses the container restart policy until the next
# explicit start, so the selected replica remains down for the whole test.
docker stop --time 5 "$matched_container" >/dev/null
printf '%s\n' "stopped API instance $TARGET_INSTANCE_ID"