Files
mtusi2026lab3/scripts/rolling-update.sh
T

22 lines
1004 B
Bash
Executable File

#!/bin/sh
# Rolling replacement for Compose: rebuild the image, then for each old replica start one new replica,
# wait until it is healthy (HAProxy picks it up via DNS), and only then stop/remove the old one.
# Migrations run before (expand-only, compatible with the running version).
set -eu
cd "$(dirname "$0")/.."
docker compose build api >/dev/null
docker compose run --rm migrate >/dev/null
old=$(docker compose ps -q api)
known=$old
n=$(printf '%s\n' "$old" | grep -c .)
for cid in $old; do
docker compose up -d --no-deps --no-recreate --scale api=$((n + 1)) api >/dev/null 2>&1
new=$(docker compose ps -q api | grep -v -F "$known")
known="$known
$new"
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$new")" = healthy ]; do sleep 1; done
sleep 2 # rise 2 x inter 1 s: LB marks the new replica UP
docker stop "$cid" >/dev/null && docker rm "$cid" >/dev/null
echo "replaced $(echo "$cid" | cut -c1-12) -> $(echo "$new" | cut -c1-12)"
done