48 lines
2.1 KiB
Bash
Executable File
48 lines
2.1 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
# Replace API replicas one at a time with a new build while the LB keeps serving.
|
|
# APP_VERSION=v2 scripts/rolling-update.sh
|
|
# 1. Build messenger-lab3-app:$APP_VERSION.
|
|
# 2. Run migrations ONCE (expand-only, compatible with replicas still on the old build).
|
|
# 3. For each old replica: start one new replica (N+1), wait until Docker reports it healthy AND HAProxy
|
|
# marks it UP, then docker stop the old one (SIGTERM -> drain -> exit) and remove it.
|
|
# Capacity never drops below N. Afterwards put APP_VERSION=$APP_VERSION into .env, otherwise a later
|
|
# `docker compose up` recreates replicas from the version written there.
|
|
: "${APP_VERSION:?set APP_VERSION of the new build}"
|
|
export APP_VERSION
|
|
project="${COMPOSE_PROJECT_NAME:-messenger-lab3}"
|
|
compose() { docker compose --env-file .env "$@"; }
|
|
api_ids() {
|
|
docker ps -q --filter "label=com.docker.compose.project=$project" --filter "label=com.docker.compose.service=api"
|
|
}
|
|
lb_up() { sh scripts/lb-status.sh | grep -c ' UP$' || true; }
|
|
|
|
old=$(api_ids)
|
|
count=$(printf '%s\n' "$old" | grep -c . || true)
|
|
[ "$count" -ge 1 ] || { echo "no running api replicas" >&2; exit 1; }
|
|
echo "$(date +%T) rolling $count replica(s) to messenger-lab3-app:$APP_VERSION"
|
|
|
|
compose build --quiet api migrate
|
|
compose run --rm --no-deps -T migrate
|
|
echo "$(date +%T) migrations applied once"
|
|
|
|
for id in $old; do
|
|
before=$(lb_up)
|
|
compose up -d --no-deps --no-recreate --wait --scale "api=$((count + 1))" api >/dev/null 2>&1
|
|
new=$(api_ids | grep -v -F "$old" || true)
|
|
i=0
|
|
until [ "$(lb_up)" -gt "$before" ]; do
|
|
i=$((i + 1))
|
|
[ "$i" -le 60 ] || { echo "new replica did not become UP in HAProxy" >&2; exit 1; }
|
|
sleep 0.5
|
|
done
|
|
echo "$(date +%T) new replica(s) UP in LB: $(printf '%s ' $new); stopping old $id"
|
|
docker stop --time 9 "$id" >/dev/null
|
|
docker rm "$id" >/dev/null
|
|
echo "$(date +%T) old $id drained and removed"
|
|
done
|
|
|
|
compose ps api --format '{{.Name}} {{.Image}} {{.Status}}'
|
|
echo "$(date +%T) done; set APP_VERSION=$APP_VERSION in .env to keep this version on the next compose up"
|