wealth/tools/rollback.sh

42 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Rolls one app back to its previous release: stops the container that serves and starts the previous one,
# which tools/deploy.sh keeps stopped with its own image and env. The container that served stays stopped as
# the new previous one, so running this again rolls forward. If the previous release fails its health check
# (/healthz), the one that served starts again. The port refuses connections for a few seconds, as in a
# deploy, and the previous release answers on the port it was deployed with.
#
# bash tools/rollback.sh <app>
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
app=${1:?Usage: tools/rollback.sh <app>}
state="$PWD/.deploy/$app"
[ -d "$state" ] || { echo "$app was never deployed on this host" >&2; exit 1; }
exec 9>"$state/.lock"
flock --nonblock 9 || { echo "A deploy or rollback of $app is running" >&2; exit 1; }
# The container that serves, and the previous one: tools/deploy.sh keeps one, stopped.
containers() { docker ps "$@" --filter "name=^$app-[0-9]{14}\$" --format '{{.Names}}'; }
current=$(containers | head -n 1)
[ -n "$current" ] || { echo "No container of $app serves; deploy it, or start one with docker start" >&2; exit 1; }
previous=$(containers --all | grep -vx "$current" | sort | tail -n 1 || true)
[ -n "$previous" ] || { echo "$app has no previous release to roll back to" >&2; exit 1; }
# Swap. SIGTERM lets Next.js finish what it's serving, for up to 30 s (--stop-timeout). If the previous
# release fails, the one that served starts again.
trap 'docker stop "$previous" >/dev/null 2>&1 || true; docker start "$current" >/dev/null && echo "$current serves again" >&2' EXIT
docker stop "$current" >/dev/null
docker start "$previous" >/dev/null
# Docker turns the image's HEALTHCHECK unhealthy when /healthz keeps failing.
while status=$(docker inspect --format '{{.State.Status}} {{.State.Health.Status}}' "$previous") \
&& [ "$status" = 'running starting' ]; do
sleep 2
done
if [ "$status" != 'running healthy' ]; then
docker logs --tail 50 "$previous" >&2
echo "$previous is $status" >&2
exit 1
fi
trap - EXIT
echo "$app rolled back: $previous serves at $(docker port "$previous" 3000/tcp | head -n 1); $current is stopped, kept to roll forward"