выполнение ЛР3 на 4

This commit is contained in:
Ilya
2026-10-21 12:20:11 +03:00
parent 0ab3a341aa
commit 6f4f74d691
37 changed files with 2854 additions and 15 deletions
+28
View File
@@ -0,0 +1,28 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
mkdir -p secrets
gen() { python3 -c 'import secrets; print(secrets.token_hex(24))'; }
if [ ! -f secrets/postgres_password ]; then
gen > secrets/postgres_password
echo "created secrets/postgres_password"
fi
PG_PASS=$(cat secrets/postgres_password)
printf 'postgresql://messenger:%s@postgres:5432/messenger\n' "$PG_PASS" > secrets/database_url
if [ ! -f secrets/valkey.conf ]; then
{
printf 'requirepass %s\n' "$(gen)"
printf 'save ""\nappendonly no\n'
printf 'maxmemory 64mb\nmaxmemory-policy volatile-lru\n'
} > secrets/valkey.conf
echo "created secrets/valkey.conf"
fi
VK_PASS=$(sed -n 's/^requirepass //p' secrets/valkey.conf)
printf 'redis://:%s@valkey:6379/0\n' "$VK_PASS" > secrets/cache_url
chmod 644 secrets/*
echo "secrets ready in ./secrets (not tracked by git)"
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose ps --format 'table {{.Name}}\t{{.Status}}'
docker compose stop lb api
docker compose stop valkey postgres
docker compose up -d --no-build --wait api lb
docker compose ps --format 'table {{.Name}}\t{{.Status}}'
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose up -d --no-build --no-recreate --wait api
docker compose ps api --format 'table {{.Name}}\t{{.Status}}'
+31
View File
@@ -0,0 +1,31 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
N=$(docker compose ps -q api | wc -l | tr -d ' ')
[ "$N" -ge 1 ] || { echo "api is not running" >&2; exit 1; }
OLD=$(docker compose ps -q api)
docker compose build api
docker compose run --rm --no-deps migrate
wait_healthy() {
i=0
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$1")" = healthy ]; do
i=$((i + 1)); [ "$i" -le 60 ] || { echo "replica $1 not healthy" >&2; exit 1; }
sleep 1
done
}
for old in $OLD; do
before=$(docker compose ps -q api | sort)
docker compose up -d --no-deps --no-recreate --scale api=$((N + 1)) api >/dev/null 2>&1
new=$(docker compose ps -q api | sort | grep -vxF "$before" || true)
[ -n "$new" ] || { echo "no new replica started" >&2; exit 1; }
wait_healthy "$new"
sleep 2
printf '%s new %s healthy; stopping old %s\n' "$(date -u +%H:%M:%S)" \
"$(docker inspect -f '{{.Name}}' "$new")" "$(docker inspect -f '{{.Name}}' "$old")"
docker stop --time 8 "$old" >/dev/null
docker rm "$old" >/dev/null
done
docker compose ps api --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}'
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
name=${1:?usage: stop-container.sh <project>-api-N}
case "$name" in *-api-*) ;; *) echo "refusing: $name is not an API replica" >&2; exit 2;; esac
alias=$(docker exec "$name" cat /tmp/instance-id 2>/dev/null || echo "?")
docker stop --time 8 "$name" >/dev/null
printf '%s stopped %s instance=%s state=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$name" "$alias" \
"$(docker inspect -f '{{.State.Status}}' "$name")" | tee -a evidence/stop-one.txt
+19
View File
@@ -0,0 +1,19 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
log=evidence/stop-one.txt
for cid in $(docker compose ps -q api); do
alias=$(docker exec "$cid" cat /tmp/instance-id 2>/dev/null || true)
if [ "$alias" = "$TARGET_INSTANCE_ID" ]; then
name=$(docker inspect -f '{{.Name}}' "$cid" | sed 's#^/##')
docker stop --time 8 "$cid" >/dev/null
state=$(docker inspect -f '{{.State.Status}}' "$cid")
printf '%s stopped %s (%s) instance=%s state=%s\n' \
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$name" "$(printf %.12s "$cid")" "$alias" "$state" | tee -a "$log"
exit 0
fi
done
echo "no running api container reports instance id $TARGET_INSTANCE_ID" >&2
exit 1