Files
mtusi2026lab7/scripts/stop-one.sh
T

39 lines
1.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# Останавливает ровно одну API-реплику стенда — ту, которая обслужила текущую
# сессию. В lab7 X-Instance-Id — непрозрачный случайный алиас (без hostname/IP),
# который API записывает в /tmp/instance-id внутри tmpfs контейнера.
# Ничего, кроме целевой реплики, скрипт не трогает.
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
project="${LAB_PROJECT:-mtusi-lab7}"
target=''
for container in $(
docker ps \
--filter "label=com.docker.compose.project=${project}" \
--filter 'label=com.docker.compose.service=api' \
--format '{{.ID}}'
); do
alias_val="$(docker exec "$container" cat /tmp/instance-id 2>/dev/null || true)"
if [ -n "$alias_val" ] && [ "$alias_val" = "$TARGET_INSTANCE_ID" ]; then
target="$container"
break
fi
case "$container" in
"$TARGET_INSTANCE_ID"*)
target="$container"
break
;;
esac
done
if [ -z "$target" ]; then
printf 'stop-one: no running api replica of project %s matching instance alias %s\n' \
"$project" "$TARGET_INSTANCE_ID" >&2
exit 1
fi
printf 'stop-one: stopping api replica %s (alias=%s)\n' "$target" "$TARGET_INSTANCE_ID" >&2
docker stop --time 10 "$target" >/dev/null