Files
mtusi2026lab3/scripts/stop-one.sh
T
dawudukandCursor cc7147f458 Поставить реплики API за HAProxy и описать остановку одной из них.
Внешний порт остаётся только у балансировщика, миграции применяются один раз, а stop-one оставляет выбранную реплику остановленной.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-07 12:46:37 +03:00

33 lines
868 B
Bash
Executable File

#!/bin/sh
# Stop the single API replica whose X-Instance-Id is TARGET_INSTANCE_ID.
# Leaves it stopped. Does not touch the database, Redis, the load balancer, or volumes.
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
compose() {
if [ -f .env ]; then
docker compose --env-file .env "$@"
else
docker compose "$@"
fi
}
found=0
for id in $(compose ps -q api); do
iid=$(docker exec "$id" cat /tmp/instance-id 2>/dev/null | tr -d '\r\n' || true)
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
if [ "$iid" = "$TARGET_INSTANCE_ID" ] || [ "$host" = "$TARGET_INSTANCE_ID" ]; then
docker stop -t 5 "$id" >/dev/null
found=1
break
fi
done
if [ "$found" -ne 1 ]; then
echo "no running API replica matched TARGET_INSTANCE_ID" >&2
exit 1
fi