#!/bin/sh set -eu cd "$(dirname "$0")/.." ids=$(docker ps -q --filter "label=com.docker.compose.project=messenger-lab3" --filter "label=com.docker.compose.service=api") count=$(printf '%s\n' "$ids" | grep -c . || true) if [ "$count" -lt 2 ]; then echo "need at least 2 running api replicas" >&2 exit 1 fi for id in $ids; do short=$(printf '%s' "$id" | cut -c1-12) echo "rolling $short stop $(date -u +%Y-%m-%dT%H:%M:%SZ)" docker stop -t 6 "$id" >/dev/null ready=0 i=0 while [ "$i" -lt 40 ]; do if curl -fsS http://127.0.0.1:8080/health/ready >/dev/null; then ready=1 break fi i=$((i + 1)) sleep 0.5 done if [ "$ready" -ne 1 ]; then echo "ready did not survive stopping $short" >&2 docker start "$id" >/dev/null || true exit 1 fi echo "rolling $short start $(date -u +%Y-%m-%dT%H:%M:%SZ)" docker start "$id" >/dev/null i=0 healthy=0 while [ "$i" -lt 90 ]; do status=$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$id") if [ "$status" = "healthy" ]; then healthy=1 break fi i=$((i + 1)) sleep 1 done if [ "$healthy" -ne 1 ]; then echo "replica $short did not become healthy" >&2 exit 1 fi echo "rolling $short healthy $(date -u +%Y-%m-%dT%H:%M:%SZ)" done echo "rolling update finished $(date -u +%Y-%m-%dT%H:%M:%SZ)"