Files
mtusi2026lab4/scripts/stop-one.sh
T
2026-12-01 22:01:52 +03:00

39 lines
1.0 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-реплику с X-Instance-Id = TARGET_INSTANCE_ID.
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
ids=$(docker compose -p gavrilenko-lab4 --env-file .env ps -q api)
if [ -z "$ids" ]; then
echo "no api containers" >&2
exit 1
fi
found=0
for id in $ids; do
hostname=$(docker inspect -f '{{.Config.Hostname}}' "$id")
env_id=$(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$id" | sed -n 's/^INSTANCE_ID=//p' | head -n1)
match=$hostname
if [ -n "$env_id" ]; then
match=$env_id
fi
if [ "$match" = "$TARGET_INSTANCE_ID" ]; then
docker stop -t 8 "$id" >/dev/null
state=$(docker inspect -f '{{.State.Running}}' "$id")
if [ "$state" = "true" ]; then
echo "container still running after stop" >&2
exit 1
fi
found=1
break
fi
done
if [ "$found" -ne 1 ]; then
echo "TARGET_INSTANCE_ID not found among api replicas" >&2
exit 1
fi
exit 0