ЛР3 - БПИ2303 - Стругов - на 4

This commit is contained in:
wanderinho
2026-11-10 15:19:31 +03:00
parent 0ab3a341aa
commit bdccb3ad7d
33 changed files with 3591 additions and 23 deletions
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
mkdir -p secrets
if [ -e secrets/postgres_password.txt ]; then echo 'Secrets already exist; leaving unchanged'; exit 0; fi
password=$(od -An -N24 -tx1 /dev/urandom | tr -d ' \n')
printf '%s' "$password" > secrets/postgres_password.txt
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable' "$password" > secrets/database_url.txt
printf '%s' 'redis://redis:6379/0' > secrets/cache_url.txt
chmod 600 secrets/*.txt
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose down
docker compose up -d --scale api=2 --build --wait
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose up -d --scale api=2 --no-recreate api
# Explicitly start any stopped API container (Compose does not necessarily do so).
for id in $(docker compose ps -a -q api); do
if [ "$(docker inspect --format '{{.State.Running}}' "$id")" = false ]; then docker start "$id" >/dev/null; fi
done
+71
View File
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
# Запоминаем именно исходные контейнеры.
mapfile -t old_ids < <(docker compose ps -q api)
if [[ "${#old_ids[@]}" -ne 2 ]]; then
echo "ERROR: expected exactly two API replicas"
exit 1
fi
wait_healthy() {
local id="$1"
local deadline=$((SECONDS + 60))
local status=""
while (( SECONDS < deadline )); do
status=$(docker inspect \
-f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' \
"$id" 2>/dev/null || true)
if [[ "$status" == "healthy" ]]; then
return 0
fi
sleep 1
done
echo "ERROR: container $id did not become healthy"
return 1
}
for old_id in "${old_ids[@]}"; do
echo "Replacing $old_id..."
docker stop --timeout 10 "$old_id"
docker rm "$old_id"
docker compose up -d \
--no-deps \
--no-recreate \
--scale api=2 \
api
mapfile -t current_ids < <(docker compose ps -q api)
if [[ "${#current_ids[@]}" -ne 2 ]]; then
echo "ERROR: expected two API replicas after replacement"
exit 1
fi
new_id=""
for id in "${current_ids[@]}"; do
if [[ "$id" != "${old_ids[0]}" && "$id" != "${old_ids[1]}" ]]; then
new_id="$id"
fi
done
if [[ -z "$new_id" ]]; then
echo "ERROR: new API replica not found"
exit 1
fi
wait_healthy "$new_id"
echo "New replica $new_id is healthy"
done
echo "PASS: sequential API replica replacement completed"
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides instance alias}"
# Default Docker hostname is the container's short ID, used as X-Instance-Id.
for id in $(docker compose ps -q api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$id")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
docker stop --time 2 "$id" >/dev/null
exit 0
fi
done
printf '%s\n' 'No running API container matches TARGET_INSTANCE_ID' >&2
exit 1