Files
mtusi2026lab3/scripts/rolling-update.sh

43 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
set -eu
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
project_dir=$(dirname -- "$script_dir")
cd "$project_dir"
wait_for_two_healthy() {
deadline=$(( $(date +%s) + 60 ))
while [ "$(date +%s)" -lt "$deadline" ]; do
ids=$(docker compose --env-file .env ps -q api)
set -- $ids
if [ "$#" -eq 2 ]; then
all_healthy=true
for container_id in "$@"; do
status=$(docker inspect --format '{{.State.Health.Status}}' "$container_id")
if [ "$status" != 'healthy' ]; then
all_healthy=false
break
fi
done
if [ "$all_healthy" = true ]; then
return 0
fi
fi
sleep 1
done
printf '%s\n' 'two healthy API replicas did not appear within 60 seconds' >&2
return 1
}
wait_for_two_healthy
original_ids=$(docker compose --env-file .env ps -q api)
for old_id in $original_ids; do
old_instance=$(docker inspect --format '{{.Config.Hostname}}' "$old_id")
docker stop --time 5 "$old_id" >/dev/null
docker rm "$old_id" >/dev/null
docker compose --env-file .env up -d --no-deps --no-recreate --scale api=2 api >/dev/null
wait_for_two_healthy
printf 'replaced API instance %s\n' "$old_instance"
done