Перенести рабочий стенд lab3 в репозиторий lab4.

API, Postgres, Redis и HAProxy остаются базой для вложений: сессии и история уже общие для нескольких реплик.
This commit is contained in:
2026-11-09 00:04:59 +03:00
parent ec540a1a72
commit d208a87ae8
28 changed files with 2040 additions and 7 deletions
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
# Restart API replicas and every store they use. Volumes are kept.
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
if [ -f .env ]; then
docker compose --env-file .env restart postgres redis api
else
docker compose restart postgres redis api
fi
+55
View File
@@ -0,0 +1,55 @@
#!/bin/sh
# Replace API replicas one at a time. The other replica keeps serving.
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
compose() {
if [ -f .env ]; then
docker compose --env-file .env "$@"
else
docker compose "$@"
fi
}
if command -v python3 >/dev/null 2>&1; then
PY=python3
else
PY=python
fi
ids=$(compose ps -q api)
count=0
for id in $ids; do
count=$((count + 1))
done
if [ "$count" -lt 2 ]; then
echo "rolling update needs at least 2 running API replicas" >&2
exit 1
fi
for id in $ids; do
echo "replacing $(docker inspect -f '{{.Config.Hostname}}' "$id")"
docker stop -t 8 "$id" >/dev/null
docker rm "$id" >/dev/null
compose up -d --scale "api=$count" --no-deps --no-recreate api >/dev/null
ready=0
i=0
while [ "$i" -lt 40 ]; do
running=0
for cid in $(compose ps -q api); do
running=$((running + 1))
done
if [ "$running" -ge "$count" ] && "$PY" -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" >/dev/null 2>&1; then
ready=1
break
fi
i=$((i + 1))
sleep 1
done
if [ "$ready" -ne 1 ]; then
echo "replacement did not become ready" >&2
exit 1
fi
done
echo "rolling update finished; replicas=$count"
+32
View File
@@ -0,0 +1,32 @@
#!/bin/sh
# Stop the single API replica whose X-Instance-Id is TARGET_INSTANCE_ID.
# Leaves it stopped. Does not touch the database, Redis, the load balancer, or volumes.
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
compose() {
if [ -f .env ]; then
docker compose --env-file .env "$@"
else
docker compose "$@"
fi
}
found=0
for id in $(compose ps -q api); do
iid=$(docker exec "$id" cat /tmp/instance-id 2>/dev/null | tr -d '\r\n' || true)
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
if [ "$iid" = "$TARGET_INSTANCE_ID" ] || [ "$host" = "$TARGET_INSTANCE_ID" ]; then
docker stop -t 5 "$id" >/dev/null
found=1
break
fi
done
if [ "$found" -ne 1 ]; then
echo "no running API replica matched TARGET_INSTANCE_ID" >&2
exit 1
fi