#!/bin/sh # Поочерёдная замена API: сначала поднимается новая реплика, затем останавливается старая. set -eu cd "$(dirname "$0")/.." compose() { docker compose --env-file .env -f compose.yaml "$@" } bid=$(date +%s) docker build --build-arg "BUILD_ID=${bid}" -t mtusi-messenger:lab3 . old=$(compose ps -q api) if [ -z "$old" ]; then echo "no running api replicas" >&2 exit 1 fi healthy_count() { n=0 for id in $(compose ps -q api); do status=$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$id") if [ "$status" = "healthy" ]; then n=$((n + 1)) fi done printf '%s' "$n" } wait_healthy() { want=$1 i=0 while [ "$i" -lt 45 ]; do n=$(healthy_count) if [ "$n" -ge "$want" ]; then return 0 fi i=$((i + 1)) sleep 1 done echo "wanted ${want} healthy api, saw $(healthy_count)" >&2 return 1 } for cid in $old; do echo "replacement before stop ${cid}" compose up -d --no-deps --scale api=3 --no-recreate api wait_healthy 3 docker stop -t 8 "$cid" >/dev/null docker rm "$cid" >/dev/null compose up -d --no-deps --scale api=2 --no-recreate api wait_healthy 2 done echo "rolling update done" compose ps api