forked from mtusi2026/mtusi2026lab6
56 lines
1.3 KiB
Bash
56 lines
1.3 KiB
Bash
#!/bin/sh
|
|
# Replace API replicas one at a time. The other replica keeps serving.
|
|
set -eu
|
|
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
|
cd "$ROOT"
|
|
|
|
compose() {
|
|
if [ -f .env ]; then
|
|
docker compose --env-file .env "$@"
|
|
else
|
|
docker compose "$@"
|
|
fi
|
|
}
|
|
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
PY=python3
|
|
else
|
|
PY=python
|
|
fi
|
|
|
|
ids=$(compose ps -q api)
|
|
count=0
|
|
for id in $ids; do
|
|
count=$((count + 1))
|
|
done
|
|
if [ "$count" -lt 2 ]; then
|
|
echo "rolling update needs at least 2 running API replicas" >&2
|
|
exit 1
|
|
fi
|
|
|
|
for id in $ids; do
|
|
echo "replacing $(docker inspect -f '{{.Config.Hostname}}' "$id")"
|
|
docker stop -t 8 "$id" >/dev/null
|
|
docker rm "$id" >/dev/null
|
|
compose up -d --scale "api=$count" --no-deps --no-recreate api >/dev/null
|
|
ready=0
|
|
i=0
|
|
while [ "$i" -lt 40 ]; do
|
|
running=0
|
|
for cid in $(compose ps -q api); do
|
|
running=$((running + 1))
|
|
done
|
|
if [ "$running" -ge "$count" ] && "$PY" -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" >/dev/null 2>&1; then
|
|
ready=1
|
|
break
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 1
|
|
done
|
|
if [ "$ready" -ne 1 ]; then
|
|
echo "replacement did not become ready" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
echo "rolling update finished; replicas=$count"
|