Files
mtusi2026lab3-fork/scripts/rolling-restart.sh
T
2026-10-27 13:50:56 +03:00

69 lines
1.7 KiB
Bash

#!/bin/sh
set -eu
# Safely replace API replicas one at a time.
# Temporarily scale to 3 so there is no need to stop a replica
# before its replacement becomes healthy.
service="api"
target=2
temporary=3
healthy_count() {
count=0
for id in $(docker compose ps -q "$service"); do
status=$(docker inspect --format '{{.State.Health.Status}}' "$id" 2>/dev/null || echo starting)
[ "$status" = healthy ] && count=$((count + 1))
done
echo "$count"
}
wait_for_healthy() {
tries=0
while [ "$tries" -lt 60 ]; do
healthy=$(healthy_count)
if [ "$healthy" -ge "$1" ]; then
return 0
fi
tries=$((tries + 1))
sleep 1
done
echo "Timed out waiting for $1 healthy API replicas" >&2
return 1
}
docker compose up -d --scale api="$temporary" api
wait_for_healthy "$temporary"
# Replace two original replicas. Each iteration starts with 3 healthy
# replicas, stops one, then recreates it to restore the count to 3.
for iteration in 1 2; do
old_id=""
for id in $(docker compose ps -q "$service"); do
status=$(docker inspect --format '{{.State.Health.Status}}' "$id" 2>/dev/null || echo starting)
if [ "$status" = healthy ]; then
old_id="$id"
break
fi
done
[ -n "$old_id" ] || {
echo "No healthy API replica available to replace" >&2
exit 1
}
docker stop --time 10 "$old_id" >/dev/null
docker rm "$old_id" >/dev/null
docker compose up -d --scale api="$temporary" api
wait_for_healthy "$target"
# Wait until the third healthy replica is available before next iteration.
wait_for_healthy "$temporary"
done
docker compose up -d --scale api="$target" api
wait_for_healthy "$target"
echo "PASS: rolling restart completed with $target healthy API replicas"