forked from mtusi2026/mtusi2026lab3
ops: add balanced replicated stack
This commit is contained in:
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
wait_for_replicas() {
|
||||
expected=$1
|
||||
deadline=$(($(date +%s) + 90))
|
||||
|
||||
while [ "$(date +%s)" -lt "$deadline" ]; do
|
||||
containers=$(docker 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 '%s\n' "API replicas did not become healthy: expected $expected" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
old_containers=$(docker compose ps -q api)
|
||||
old_count=0
|
||||
for container in $old_containers; do
|
||||
old_count=$((old_count + 1))
|
||||
done
|
||||
|
||||
docker compose build
|
||||
docker compose up -d postgres valkey
|
||||
docker compose run --rm migrate
|
||||
|
||||
if [ "$old_count" -lt 2 ]; then
|
||||
docker compose up -d --scale api=2 --wait --wait-timeout 120
|
||||
exit 0
|
||||
fi
|
||||
|
||||
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
||||
wait_for_replicas 3
|
||||
|
||||
for container in $old_containers; do
|
||||
docker stop --time 6 "$container" >/dev/null
|
||||
docker rm "$container" >/dev/null
|
||||
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
||||
wait_for_replicas 3
|
||||
done
|
||||
|
||||
docker compose up -d --no-deps --no-recreate --scale api=2 api
|
||||
wait_for_replicas 2
|
||||
docker compose up -d --no-build --scale api=2 --wait --wait-timeout 120
|
||||
Reference in New Issue
Block a user