forked from mtusi2026/mtusi2026lab3
50 lines
1.8 KiB
Bash
Executable File
50 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
image=mtusi-messenger-lab3-api:local
|
|
revision="roll-$(date -u +%Y%m%d%H%M%S)"
|
|
|
|
docker compose build --build-arg "BUILD_REVISION=$revision" api
|
|
new_image=$(docker image inspect --format '{{.Id}}' "$image")
|
|
old_ids=$(docker compose ps --all -q api)
|
|
[[ -n "$old_ids" ]] || { printf 'No API containers found.\n' >&2; exit 1; }
|
|
|
|
count_new_healthy() {
|
|
for container_id in $(docker compose ps --all -q api); do
|
|
image_id=$(docker inspect --format '{{.Image}}' "$container_id" 2>/dev/null || true)
|
|
state=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container_id" 2>/dev/null || true)
|
|
if [[ "$image_id" == "$new_image" && "$state" == healthy ]]; then printf '1\n'; fi
|
|
done | awk 'END { print NR + 0 }'
|
|
}
|
|
|
|
wait_new_healthy() {
|
|
expected=$1
|
|
deadline=$((SECONDS + 90))
|
|
while (( SECONDS < deadline )); do
|
|
actual=$(count_new_healthy)
|
|
if (( actual >= expected )); then return 0; fi
|
|
sleep 1
|
|
done
|
|
printf 'Only %s new replicas became healthy; expected %s.\n' "$(count_new_healthy)" "$expected" >&2
|
|
docker compose ps --all
|
|
return 1
|
|
}
|
|
|
|
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
|
wait_new_healthy 1
|
|
new_count=1
|
|
for container_id in $old_ids; do
|
|
image_id=$(docker inspect --format '{{.Image}}' "$container_id" 2>/dev/null || true)
|
|
if [[ "$image_id" != "$new_image" ]]; then
|
|
docker stop --time 6 "$container_id" >/dev/null
|
|
docker rm "$container_id" >/dev/null
|
|
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
|
new_count=$((new_count + 1))
|
|
wait_new_healthy "$new_count"
|
|
fi
|
|
done
|
|
docker compose up -d --no-deps --no-recreate --scale api=2 api
|
|
wait_new_healthy 2
|
|
printf 'PASS: old API containers were replaced one at a time; two new replicas are healthy. revision=%s\n' "$revision"
|
|
docker compose ps api
|