Files
mtusi2026lab5/scripts/rollout.sh
T

45 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# Rolling update of the API with docker rollout (github.com/wowu/docker-rollout,
# MIT): new replicas start next to the old ones, and the old ones go once all
# new ones are healthy; if one is not, the new ones are removed instead.
#
# With a stopped replica docker rollout would replace only that one and leave
# two versions running, so the script refuses. The migrate container is
# recreated from the new image without starting: the `up` of docker rollout
# then applies the new migrations once, before any new replica starts.
set -eu
cd "$(dirname "$0")/.."
idle=$(docker compose ps -aq --status exited --status created --status paused --status dead api)
if [ -n "$idle" ]; then
echo "some API replicas are not running; start them first: docker compose start api" >&2
exit 2
fi
version=v0.14
checksum=cdeaba6ae9eee3b0b606286e585bbda6787283d801a6ad6d9b9d2bc347fda05b
tool=bin/docker-rollout
if ! printf '%s %s\n' "$checksum" "$tool" | sha256sum -c - >/dev/null 2>&1; then
mkdir -p bin
curl -fsSL -o "$tool.download" "https://github.com/wowu/docker-rollout/releases/download/$version/docker-rollout"
if ! printf '%s %s\n' "$checksum" "$tool.download" | sha256sum -c - >/dev/null 2>&1; then
rm -f "$tool.download"
echo "docker-rollout $version: checksum mismatch, not used" >&2
exit 1
fi
mv "$tool.download" "$tool"
fi
docker compose build api
docker compose up --no-start --no-deps --force-recreate migrate
# Run through sh: Windows cannot use the script as a docker CLI plugin.
if ! sh "$tool" rollout --wait-after-healthy 4 api; then
# A failed migration leaves the new containers created but not started.
created=$(docker compose ps -aq --status created api)
if [ -n "$created" ]; then
docker rm $created >/dev/null
fi
exit 1
fi