Files
mtusi2026lab4/scripts/deploy.sh
T
2026-11-06 18:05:29 +03:00

52 lines
2.1 KiB
Bash

#!/bin/sh
# Build the application image, create local Swarm secrets once, deploy the stack.
#
# Prerequisites: Docker Engine with Swarm mode (docker swarm init is done here if
# needed) and the files created by scripts/bootstrap-secrets.sh. No secret value
# is printed. This is the one documented bootstrap sequence for stack.yaml.
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$root"
# The public failover test invokes the action script without a shell; it must be
# executable on POSIX. Git may not preserve the mode on a Windows checkout.
chmod +x scripts/stop-one.sh scripts/restore.sh scripts/restart.sh scripts/rolling-update.sh scripts/deploy.sh 2>/dev/null || true
stack=${STACK_NAME:-lab4}
image=${API_IMAGE:-mtusi-messenger-lab4:4.0.0}
state=$(docker info --format '{{.Swarm.LocalNodeState}}')
if [ "$state" != "active" ]; then
docker swarm init >/dev/null
fi
sh scripts/bootstrap-secrets.sh
for name in database_url cache_url postgres_password cursor_secret s3_root_user s3_root_password s3_access_key_id s3_secret_access_key; do
if ! docker secret inspect "${stack}_${name}" >/dev/null 2>&1; then
docker secret create "${stack}_${name}" "secrets/${name}" >/dev/null
fi
done
docker build -t "$image" .
# Swarm configs неизменяемы. Перед повторным deploy убираем завершившийся
# minio-init и его config, иначе stack deploy попытается обновить config и
# получит "only updates to Labels are allowed". На первом deploy это no-op.
docker service rm "${stack}_minio-init" >/dev/null 2>&1 || true
docker config rm "${stack}_lab4_minio_init" >/dev/null 2>&1 || true
API_IMAGE="$image" docker stack deploy --resolve-image never -c stack.yaml "$stack"
i=0
while [ "$i" -lt 90 ]; do
if curl -fsS http://127.0.0.1:8080/health/ready >/dev/null 2>&1; then
printf 'deployed: http://127.0.0.1:8080 (image %s)\n' "$image"
docker stack services "$stack"
exit 0
fi
i=$((i + 1))
sleep 2
done
printf 'stack did not become ready within 180s\n' >&2
docker stack services "$stack" >&2 || true
exit 1