#!/bin/sh # Rolling update of the API without downtime, with docker rollout # (https://github.com/wowu/docker-rollout, MIT): it starts as many new # replicas as are running, waits until all of them are healthy, then stops # and removes the old ones. If a new replica does not become healthy it # removes the new ones and leaves the old ones serving, so the stand is never # left with a mix of versions. # # Every replica must be running. With a stopped one (say, after stop-one), # docker rollout only replaces the stopped one and reports success, which # would leave old and new versions side by side; the script refuses instead. # # The migrations of the new version run first, once, while the old replicas # serve: they only add to the schema. docker rollout scales with `compose up`, # which also starts the dependencies of the API and would rerun the old, # exited migrate container. Recreating it beforehand from the new image, # without starting it, makes that `up` run the new migrations exactly 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 stops the scale-up after the new containers were # created but before they started; remove them for a clean next attempt. created=$(docker compose ps -aq --status created api) if [ -n "$created" ]; then docker rm $created >/dev/null fi exit 1 fi