forked from mtusi2026/mtusi2026lab5
43 lines
1.1 KiB
Bash
Executable File
43 lines
1.1 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
COMPOSE="docker compose --env-file .env"
|
|
N=${1:-$($COMPOSE ps -q api | wc -l)}
|
|
[ "$N" -ge 2 ] || {
|
|
echo "need at least two API replicas; got $N" >&2
|
|
exit 2
|
|
}
|
|
|
|
wait_healthy() {
|
|
attempts=0
|
|
while [ "$attempts" -lt 90 ]; do
|
|
ids=$($COMPOSE ps -q api)
|
|
count=$(printf '%s\n' "$ids" | sed '/^$/d' | wc -l)
|
|
healthy=$(for id in $ids; do docker inspect -f '{{.State.Health.Status}}' "$id"; done | grep -c '^healthy$' || true)
|
|
if [ "$count" -eq "$N" ] && [ "$healthy" -eq "$N" ]; then
|
|
return 0
|
|
fi
|
|
attempts=$((attempts + 1))
|
|
sleep 1
|
|
done
|
|
echo "API replicas did not become healthy in time" >&2
|
|
return 1
|
|
}
|
|
|
|
$COMPOSE build api
|
|
i=0
|
|
while [ "$i" -lt "$N" ]; do
|
|
target=$($COMPOSE ps -q api | head -n 1)
|
|
[ -n "$target" ] || {
|
|
echo "no API container to replace" >&2
|
|
exit 2
|
|
}
|
|
echo "replacing $target"
|
|
docker stop --time 10 "$target" >/dev/null
|
|
docker rm "$target" >/dev/null
|
|
$COMPOSE up -d --no-deps --no-recreate --scale api="$N" api
|
|
wait_healthy
|
|
i=$((i + 1))
|
|
done
|
|
echo "rolling replacement complete for $N replicas"
|