ЛР 4: вложения в S3 и двусторонняя сверка хранилищ

This commit is contained in:
2026-11-15 11:50:57 +03:00
parent ec540a1a72
commit b58e703546
160 changed files with 16598 additions and 25 deletions
+37
View File
@@ -0,0 +1,37 @@
#!/bin/sh
set -eu
umask 077
cd "$(dirname "$0")/.."
command -v openssl >/dev/null 2>&1 || { echo 'openssl is required' >&2; exit 1; }
mkdir -p secrets
if [ ! -e secrets/postgres_password ] && [ ! -e secrets/database_url ]; then
password=$(openssl rand -hex 24)
printf '%s' "$password" > secrets/postgres_password
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable' "$password" > secrets/database_url
elif [ ! -s secrets/postgres_password ] || [ ! -s secrets/database_url ]; then
echo 'incomplete PostgreSQL secrets; restore the matching pair' >&2
exit 1
fi
if [ ! -e secrets/rustfs_root_access_key ] && [ ! -e secrets/rustfs_root_secret_key ]; then
openssl rand -hex 16 > secrets/rustfs_root_access_key
openssl rand -hex 32 > secrets/rustfs_root_secret_key
elif [ ! -s secrets/rustfs_root_access_key ] || [ ! -s secrets/rustfs_root_secret_key ]; then
echo 'incomplete RustFS root secrets; restore the matching pair' >&2
exit 1
fi
if [ ! -e secrets/s3_access_key_id ] && [ ! -e secrets/s3_secret_access_key ]; then
openssl rand -hex 16 > secrets/s3_access_key_id
openssl rand -hex 32 > secrets/s3_secret_access_key
elif [ ! -s secrets/s3_access_key_id ] || [ ! -s secrets/s3_secret_access_key ]; then
echo 'incomplete API S3 secrets; restore the matching pair' >&2
exit 1
fi
if [ ! -e .env ]; then
cp .env.example .env
fi
echo 'Local .env and secret files are present. Existing values were kept.'
+45
View File
@@ -0,0 +1,45 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
if [ -f .env ]; then
compose() { docker compose --env-file .env "$@"; }
else
: "${COMPOSE_PROJECT_NAME:?COMPOSE_PROJECT_NAME is required when .env is absent}"
compose() { docker compose "$@"; }
fi
wait_healthy() {
for service in "$@"; do
containers=$(compose ps -q "$service")
[ -n "$containers" ] || { echo "$service has no running containers" >&2; return 1; }
for container in $containers; do
attempts=0
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container")" = healthy ]; do
attempts=$((attempts + 1))
[ "$attempts" -lt 30 ] || { echo "$service did not become healthy" >&2; return 1; }
sleep 1
done
done
done
}
restart_services() {
for service in "$@"; do
containers=$(compose ps -a -q "$service")
[ -n "$containers" ] || { echo "$service has no containers" >&2; return 1; }
docker restart --timeout 20 $containers >/dev/null
done
}
# Restart only long-running services and preserve their containers and volumes.
cleanup=$(compose ps -a -q attachment-cleanup)
[ -n "$cleanup" ] || { echo 'attachment-cleanup has no container' >&2; exit 1; }
docker stop --timeout 20 "$cleanup" >/dev/null
restart_services postgres redis s3
wait_healthy postgres redis s3
restart_services api
wait_healthy api
restart_services lb
docker start "$cleanup" >/dev/null
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
cd "$(dirname "$0")/.."
for container_id in $(docker compose --env-file .env ps -q api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
docker stop --timeout 5 "$container_id" >/dev/null
exit 0
fi
done
printf 'API replica %s not found\n' "$TARGET_INSTANCE_ID" >&2
exit 1