Files
mtusi2026lab4-implementation/scripts/rolling-update.sh
T

38 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
PROJECT=${PROJECT:-messenger-lab4}
replicas() {
$ENGINE ps -aq --no-trunc --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api" | sort
}
wait_healthy() {
for _ in $(seq 1 60); do
if [ "$($ENGINE inspect -f '{{.State.Health.Status}}' "$1" 2>/dev/null)" = "healthy" ]; then
return 0
fi
sleep 1
done
echo "replica $1 did not become healthy" >&2
exit 1
}
$ENGINE build --build-arg "BUILD_ID=${BUILD_ID:-$(date +%s)}" -t localhost/messenger-api:lab4 .
$COMPOSE run --rm migrate
for old in $(replicas); do
before=$(replicas)
$ENGINE stop -t 10 "$old"
$ENGINE rm "$old"
$COMPOSE up -d --no-deps --no-recreate api
for id in $(replicas); do
if ! echo "$before" | grep -q "$id"; then
wait_healthy "$id"
fi
done
sleep 2
done