feat(app) lab 3 implemented

This commit is contained in:
imadev
2026-10-30 18:11:30 +03:00
parent f731f9b460
commit 48a616e910
57 changed files with 1390 additions and 303 deletions
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env restart postgres redis haproxy api
+42
View File
@@ -0,0 +1,42 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
COMPOSE="docker compose --env-file .env"
N=${1:-$($COMPOSE ps -q api | wc -l)}
[ "$N" -ge 2 ] || {
echo "need at least two API replicas; got $N" >&2
exit 2
}
wait_healthy() {
attempts=0
while [ "$attempts" -lt 90 ]; do
ids=$($COMPOSE ps -q api)
count=$(printf '%s\n' "$ids" | sed '/^$/d' | wc -l)
healthy=$(for id in $ids; do docker inspect -f '{{.State.Health.Status}}' "$id"; done | grep -c '^healthy$' || true)
if [ "$count" -eq "$N" ] && [ "$healthy" -eq "$N" ]; then
return 0
fi
attempts=$((attempts + 1))
sleep 1
done
echo "API replicas did not become healthy in time" >&2
return 1
}
$COMPOSE build api
i=0
while [ "$i" -lt "$N" ]; do
target=$($COMPOSE ps -q api | head -n 1)
[ -n "$target" ] || {
echo "no API container to replace" >&2
exit 2
}
echo "replacing $target"
docker stop --time 10 "$target" >/dev/null
docker rm "$target" >/dev/null
$COMPOSE up -d --no-deps --no-recreate --scale api="$N" api
wait_healthy
i=$((i + 1))
done
echo "rolling replacement complete for $N replicas"
+20
View File
@@ -0,0 +1,20 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
cd "$(dirname "$0")/.."
target=""
for id in $(docker compose --env-file .env ps -q api); do
hostname=$(docker inspect -f '{{.Config.Hostname}}' "$id")
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
target="$id"
break
fi
done
if [ -z "$target" ]; then
echo "no running API replica has instance id $TARGET_INSTANCE_ID" >&2
exit 2
fi
docker stop --time 5 "$target" >/dev/null