forked from mtusi2026/mtusi2026lab3
39 lines
1.2 KiB
Bash
Executable File
39 lines
1.2 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
# Sequential (rolling) replacement of the API replicas under load.
|
|
#
|
|
# The image is rebuilt once, then each running replica is stopped (the balancer
|
|
# drains it and keeps serving from the peers) and recreated from the new image.
|
|
# Before the next replica is touched we wait until every running replica is
|
|
# healthy, so the balancer always has somewhere to send traffic.
|
|
cd "$(dirname "$0")/.."
|
|
|
|
N="${API_REPLICAS:-2}"
|
|
|
|
wait_healthy() {
|
|
for _ in $(seq 1 90); do
|
|
total=$(docker compose ps -q api | wc -l)
|
|
healthy=$(docker ps --filter label=com.docker.compose.service=api --filter health=healthy --format '{{.ID}}' | wc -l)
|
|
if [ "$total" -ge "$N" ] && [ "$healthy" -ge "$total" ]; then
|
|
return 0
|
|
fi
|
|
sleep 1
|
|
done
|
|
}
|
|
|
|
echo "Building new API image..."
|
|
docker compose build api
|
|
|
|
# Wait for the current generation to be healthy before starting.
|
|
wait_healthy
|
|
|
|
for id in $(docker compose ps -q api); do
|
|
echo "Replacing replica $id ..."
|
|
docker stop -t 10 "$id" >/dev/null
|
|
docker rm "$id" >/dev/null
|
|
docker compose up -d --no-recreate api >/dev/null
|
|
wait_healthy
|
|
done
|
|
|
|
echo "Rolling update complete; all replicas healthy."
|