Files
mtusi2026lab3/scripts/stop-one.sh
T
Mlvn b1972bcfd8 Реализовать lab3: HAProxy, API×N, идемпотентность и трек 1.
Горизонтальное масштабирование за LB с общими сессиями Redis, X-Instance-Id,
обязательным Idempotency-Key в PostgreSQL, сценариями failover/persistence и
измерением цены масштабирования при фиксированном CPU/RAM бюджете.
2026-11-06 21:07:00 +03:00

45 lines
1.6 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.
# Реплика остаётся остановленной; восстановление — scripts/restore-one.sh или compose up.
# Не трогает postgres/redis/lb/volumes. Должен завершиться за <10 с.
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
ids=$(docker compose --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
# Совпадает с config.InstanceID: hostname контейнера, если INSTANCE_ID не задан.
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
# Явный stop контейнера; restart: "no" у api гарантирует, что он не вернётся сам.
docker stop -t 8 "$id" >/dev/null
# Подтверждаем, что контейнер действительно не running — иначе failover-тест врёт.
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