forked from mtusi2026/mtusi2026lab4
142 lines
4.2 KiB
Bash
Executable File
142 lines
4.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
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) + 120))
|
|
|
|
while [ "$(date +%s)" -lt "$deadline" ]; do
|
|
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
|
|
healthy=$((healthy + 1))
|
|
fi
|
|
done
|
|
if [ "$count" -eq "$expected" ] && [ "$healthy" -eq "$expected" ]; then
|
|
return 0
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
printf 'API replicas did not become healthy: expected %s\n' "$expected" >&2
|
|
return 1
|
|
}
|
|
|
|
smoke_check() {
|
|
compose exec -T nginx wget -q -O /dev/null http://127.0.0.1:8080/health/ready
|
|
}
|
|
|
|
running_image() {
|
|
containers=$(compose ps -q "$1")
|
|
for container in $containers; do
|
|
docker inspect --format '{{.Config.Image}}' "$container"
|
|
return 0
|
|
done
|
|
return 1
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
# 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
|
|
|
|
compose up -d --no-build swagger postgres-exporter valkey-exporter nginx-exporter prometheus grafana
|
|
|
|
cp "$manifest" "$current_manifest"
|
|
trap - EXIT INT TERM
|