90 lines
2.7 KiB
Bash
Executable File
90 lines
2.7 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
project_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
|
cd "$project_root"
|
|
|
|
env_file=${ENV_FILE:-.env}
|
|
desired_replicas=${DESIRED_REPLICAS:-2}
|
|
wait_timeout=${ROLLING_WAIT_TIMEOUT:-90}
|
|
stop_timeout=${ROLLING_STOP_TIMEOUT:-15}
|
|
dns_settle_delay=${ROLLING_DNS_SETTLE_DELAY:-2}
|
|
|
|
case "$desired_replicas" in
|
|
''|*[!0-9]*)
|
|
printf '%s\n' "DESIRED_REPLICAS must be an integer" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
case "$dns_settle_delay" in
|
|
''|*[!0-9]*)
|
|
printf '%s\n' "ROLLING_DNS_SETTLE_DELAY must be a non-negative integer" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
if [ "$desired_replicas" -lt 2 ]; then
|
|
printf '%s\n' "rolling update requires at least two desired replicas" >&2
|
|
exit 2
|
|
fi
|
|
|
|
compose() {
|
|
docker compose --env-file "$env_file" "$@"
|
|
}
|
|
|
|
healthy_api_count() {
|
|
healthy=0
|
|
for container_id in $(compose ps -q api); do
|
|
status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id")
|
|
if [ "$status" = "healthy" ] || [ "$status" = "running" ]; then
|
|
healthy=$((healthy + 1))
|
|
fi
|
|
done
|
|
printf '%s\n' "$healthy"
|
|
}
|
|
|
|
wait_for_healthy() {
|
|
expected=$1
|
|
deadline=$(( $(date +%s) + wait_timeout ))
|
|
while [ "$(healthy_api_count)" -lt "$expected" ]; do
|
|
if [ "$(date +%s)" -ge "$deadline" ]; then
|
|
printf '%s\n' "timed out waiting for $expected healthy API replicas" >&2
|
|
return 1
|
|
fi
|
|
sleep 1
|
|
done
|
|
}
|
|
|
|
old_containers=$(compose ps -q api)
|
|
old_count=$(printf '%s\n' "$old_containers" | awk 'NF { count++ } END { print count+0 }')
|
|
if [ "$old_count" -ne "$desired_replicas" ]; then
|
|
printf '%s\n' "expected $desired_replicas running API containers, found $old_count" >&2
|
|
exit 1
|
|
fi
|
|
|
|
surge_replicas=$((desired_replicas + 1))
|
|
|
|
printf '%s\n' "building replacement API image"
|
|
compose build api
|
|
|
|
printf '%s\n' "starting one surge replica"
|
|
compose up -d --no-deps --scale api="$surge_replicas" --no-recreate api
|
|
wait_for_healthy "$surge_replicas"
|
|
sleep "$dns_settle_delay"
|
|
|
|
for old_container in $old_containers; do
|
|
old_instance=$(docker inspect --format '{{.Config.Hostname}}' "$old_container")
|
|
printf '%s\n' "draining and replacing API instance $old_instance"
|
|
docker stop --time "$stop_timeout" "$old_container" >/dev/null
|
|
docker rm "$old_container" >/dev/null
|
|
compose up -d --no-deps --scale api="$surge_replicas" --no-recreate api
|
|
wait_for_healthy "$surge_replicas"
|
|
sleep "$dns_settle_delay"
|
|
done
|
|
|
|
printf '%s\n' "removing surge capacity"
|
|
compose up -d --no-deps --scale api="$desired_replicas" --no-recreate api
|
|
wait_for_healthy "$desired_replicas"
|
|
sleep "$dns_settle_delay"
|
|
|
|
printf '%s\n' "rolling update completed with $desired_replicas healthy API replicas"
|