Files
mtusi2026lab3/scripts/rolling-update.sh

39 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Replace the two original API processes one at a time. No schema changes here.
set -eu
cd "$(dirname "$0")/.."
original=$(docker compose --env-file .env ps -q api)
set -- $original
if [ "$#" -ne 2 ]; then
echo "expected exactly two running API replicas" >&2
exit 1
fi
for container in "$@"; do
if [ "$(docker inspect -f '{{.State.Health.Status}}' "$container")" != healthy ]; then
echo "API replica is not healthy before replacement" >&2
exit 1
fi
done
for container in "$@"; do
docker stop -t 10 "$container" >/dev/null
docker rm "$container" >/dev/null
# --no-recreate preserves the other old replica while Compose fills the gap.
docker compose --env-file .env up -d --no-deps --no-recreate --wait --scale api=2 api >/dev/null
current=$(docker compose --env-file .env ps -q api)
set -- $current
if [ "$#" -ne 2 ]; then
echo "replacement did not restore two API replicas" >&2
exit 1
fi
for running in "$@"; do
if [ "$(docker inspect -f '{{.State.Health.Status}}' "$running")" != healthy ]; then
echo "replacement API is not healthy" >&2
exit 1
fi
done
echo "replaced API container $container; two healthy replicas now serve traffic"
done