fix: deploy verified images atomically

This commit is contained in:
2026-10-09 13:14:11 +03:00
parent cc4ac1ddd7
commit f8c31de7c0
6 changed files with 238 additions and 37 deletions
+110 -27
View File
@@ -2,18 +2,44 @@
set -eu
if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
printf 'usage: %s <image-manifest>\n' "$0" >&2
exit 2
fi
manifest=$1
deploy_dir=${DEPLOY_STATE_DIR:-"$HOME/.local/state/mtusi2026lab4"}
current_manifest=$deploy_dir/current-images.env
previous_manifest=$deploy_dir/previous-images.env
replicas=${API_REPLICAS:-2}
rollback_ready=false
switched=false
mkdir -p "$deploy_dir"
load_manifest() {
set -a
# shellcheck disable=SC1090
. "$1"
set +a
}
compose() {
docker compose -f compose.yaml -f deploy/compose.images.yaml "$@"
}
wait_for_replicas() {
expected=$1
deadline=$(($(date +%s) + 90))
deadline=$(($(date +%s) + 120))
while [ "$(date +%s)" -lt "$deadline" ]; do
containers=$(docker compose ps -q api)
containers=$(compose ps -q api)
count=0
healthy=0
for container in $containers; do
count=$((count + 1))
status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container")
if [ "$status" = "healthy" ]; then
if [ "$status" = healthy ]; then
healthy=$((healthy + 1))
fi
done
@@ -23,36 +49,93 @@ wait_for_replicas() {
sleep 1
done
printf '%s\n' "API replicas did not become healthy: expected $expected" >&2
printf 'API replicas did not become healthy: expected %s\n' "$expected" >&2
return 1
}
old_containers=$(docker compose ps -q api)
old_count=0
for container in $old_containers; do
old_count=$((old_count + 1))
done
smoke_check() {
compose exec -T nginx wget -q -O /dev/null http://127.0.0.1:8080/health/ready
}
docker compose build
docker compose up -d postgres valkey s3
docker compose run --rm s3-bootstrap
docker compose run --rm migrate
running_image() {
containers=$(compose ps -q "$1")
for container in $containers; do
docker inspect --format '{{.Config.Image}}' "$container"
return 0
done
return 1
}
if [ "$old_count" -lt 2 ]; then
docker compose up -d --scale api=2 --wait --wait-timeout 120
exit 0
capture_running_images() {
output=$1
api_image=$(running_image api) || return 1
nginx_image=$(running_image nginx) || return 1
swagger_image=$(running_image swagger) || swagger_image=$SWAGGER_IMAGE
prometheus_image=$(running_image prometheus) || prometheus_image=$PROMETHEUS_IMAGE
grafana_image=$(running_image grafana) || grafana_image=$GRAFANA_IMAGE
s3_image=$(running_image s3) || s3_image=$S3_IMAGE
s3_bootstrap_image=$(running_image s3-bootstrap) || s3_bootstrap_image=$S3_BOOTSTRAP_IMAGE
{
printf 'API_IMAGE=%s\n' "$api_image"
printf 'NGINX_IMAGE=%s\n' "$nginx_image"
printf 'SWAGGER_IMAGE=%s\n' "$swagger_image"
printf 'PROMETHEUS_IMAGE=%s\n' "$prometheus_image"
printf 'GRAFANA_IMAGE=%s\n' "$grafana_image"
printf 'S3_IMAGE=%s\n' "$s3_image"
printf 'S3_BOOTSTRAP_IMAGE=%s\n' "$s3_bootstrap_image"
} > "$output"
}
rollback() {
status=$?
trap - EXIT INT TERM
if [ "$switched" = true ] && [ "$rollback_ready" = true ]; then
printf '%s\n' 'Deployment failed; restoring the previous images.' >&2
compose stop nginx api >/dev/null 2>&1 || true
compose rm -f nginx api >/dev/null 2>&1 || true
load_manifest "$previous_manifest"
compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api
wait_for_replicas "$replicas"
compose up -d --no-build --no-deps --force-recreate nginx
smoke_check
elif [ "$switched" = true ]; then
printf '%s\n' 'Deployment failed and no previous image manifest is available.' >&2
fi
exit "$status"
}
trap rollback EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
load_manifest "$manifest"
if [ -f "$current_manifest" ]; then
cp "$current_manifest" "$previous_manifest"
rollback_ready=true
elif capture_running_images "$previous_manifest"; then
rollback_ready=true
fi
docker compose up -d --no-deps --no-recreate --scale api=3 api
wait_for_replicas 3
compose pull api nginx swagger prometheus grafana s3 s3-bootstrap
compose up -d --no-build --wait --wait-timeout 120 postgres valkey s3
compose run --rm --no-deps s3-bootstrap
compose run --rm --no-deps migrate
for container in $old_containers; do
docker stop --time 6 "$container" >/dev/null
docker rm "$container" >/dev/null
docker compose up -d --no-deps --no-recreate --scale api=3 api
wait_for_replicas 3
done
# Stop the entry point and every old API replica before starting the new pool.
# This gives an atomic version boundary at the cost of a short maintenance window.
switched=true
compose stop nginx api
compose rm -f nginx api
compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api
wait_for_replicas "$replicas"
compose up -d --no-build --no-deps --force-recreate nginx
smoke_check
docker compose up -d --no-deps --no-recreate --scale api=2 api
wait_for_replicas 2
docker compose up -d --no-build --scale api=2 --wait --wait-timeout 120
compose up -d --no-build swagger postgres-exporter valkey-exporter nginx-exporter prometheus grafana
cp "$manifest" "$current_manifest"
trap - EXIT INT TERM