Files
mtusi2026lab4-Chinyaeva/scripts/stop-one.sh
Чиняева f39ae06ef3 Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
2026-11-07 01:37:21 +03:00

43 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
# Stop exactly ONE API replica identified by the public X-Instance-Id alias.
# The public failover test sets TARGET_INSTANCE_ID and expects this script to
# return within 10 seconds while leaving the replica stopped. It never touches
# PostgreSQL, Redis, the LB or named volumes.
set -eu
: "${TARGET_INSTANCE_ID:?test provides the target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
ENV_FILE=${ENV_FILE:-.env}
COMPOSE="docker compose --env-file $ENV_FILE"
target=$TARGET_INSTANCE_ID
found=''
for id in $($COMPOSE ps -q api); do
host=$(docker inspect --format '{{.Config.Hostname}}' "$id")
name=$(docker inspect --format '{{.Name}}' "$id")
case "$host" in
"$target"|"$target"*) found=$id; break ;;
esac
case "$target" in
"$host"|"$host"*) found=$id; break ;;
esac
# INSTANCE_ID may be set explicitly by an operator; then it is not the
# container hostname and the default mapping cannot be used.
if [ "$host" != "$name" ] && [ "${name#/}" = "$target" ]; then
found=$id
break
fi
done
if [ -z "$found" ]; then
echo "stop-one: no API replica matches TARGET_INSTANCE_ID" >&2
exit 3
fi
# -t 5 bounds the graceful drain; gunicorn exits earlier when idle. Docker
# does not restart a container stopped explicitly, even with restart policy.
docker stop --time 5 "$found" >/dev/null
exit 0