Files
mtusi2026lab3-fork/scripts/rolling-update.sh
T

72 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
# Запоминаем именно исходные контейнеры.
mapfile -t old_ids < <(docker compose ps -q api)
if [[ "${#old_ids[@]}" -ne 2 ]]; then
echo "ERROR: expected exactly two API replicas"
exit 1
fi
wait_healthy() {
local id="$1"
local deadline=$((SECONDS + 60))
local status=""
while (( SECONDS < deadline )); do
status=$(docker inspect \
-f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' \
"$id" 2>/dev/null || true)
if [[ "$status" == "healthy" ]]; then
return 0
fi
sleep 1
done
echo "ERROR: container $id did not become healthy"
return 1
}
for old_id in "${old_ids[@]}"; do
echo "Replacing $old_id..."
docker stop --timeout 10 "$old_id"
docker rm "$old_id"
docker compose up -d \
--no-deps \
--no-recreate \
--scale api=2 \
api
mapfile -t current_ids < <(docker compose ps -q api)
if [[ "${#current_ids[@]}" -ne 2 ]]; then
echo "ERROR: expected two API replicas after replacement"
exit 1
fi
new_id=""
for id in "${current_ids[@]}"; do
if [[ "$id" != "${old_ids[0]}" && "$id" != "${old_ids[1]}" ]]; then
new_id="$id"
fi
done
if [[ -z "$new_id" ]]; then
echo "ERROR: new API replica not found"
exit 1
fi
wait_healthy "$new_id"
echo "New replica $new_id is healthy"
done
echo "PASS: sequential API replica replacement completed"