forked from mtusi2026/mtusi2026lab3
Implement Lab 3 grade 4 messenger balancing
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$repo = Split-Path -Parent $PSScriptRoot
|
||||
$secretDir = Join-Path $repo 'secrets'
|
||||
New-Item -ItemType Directory -Path $secretDir -Force | Out-Null
|
||||
$secretNames = @('postgres_password', 'redis_password', 'database_url', 'cache_url')
|
||||
$existing = @($secretNames | Where-Object { Test-Path -LiteralPath (Join-Path $secretDir $_) })
|
||||
if ($existing.Count -eq $secretNames.Count) {
|
||||
Write-Output 'All local secret files already exist; they were left unchanged.'
|
||||
return
|
||||
}
|
||||
if ($existing.Count -gt 0) {
|
||||
throw 'Some but not all local secret files exist. Resolve them manually; this script will not rotate credentials.'
|
||||
}
|
||||
|
||||
function New-Secret {
|
||||
$bytes = New-Object byte[] 32
|
||||
[System.Security.Cryptography.RandomNumberGenerator]::Fill($bytes)
|
||||
return [Convert]::ToBase64String($bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
|
||||
}
|
||||
|
||||
$postgresPassword = New-Secret
|
||||
$redisPassword = New-Secret
|
||||
$files = @{
|
||||
postgres_password = $postgresPassword
|
||||
redis_password = $redisPassword
|
||||
database_url = "postgresql+psycopg://messenger:$postgresPassword@db:5432/messenger"
|
||||
cache_url = "redis://:$redisPassword@redis:6379/0"
|
||||
}
|
||||
foreach ($name in $secretNames) {
|
||||
$path = Join-Path $secretDir $name
|
||||
[System.IO.File]::WriteAllText($path, $files[$name], [System.Text.UTF8Encoding]::new($false))
|
||||
}
|
||||
Write-Output 'Local Postgres and Redis credentials created under secrets/. Values were not displayed.'
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose start api >/dev/null
|
||||
deadline=$((SECONDS + 60))
|
||||
while (( SECONDS < deadline )); do
|
||||
healthy=0
|
||||
for container_id in $(docker compose ps -q api); do
|
||||
state=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container_id" 2>/dev/null || true)
|
||||
[[ "$state" == healthy ]] && healthy=$((healthy + 1))
|
||||
done
|
||||
if (( healthy >= 2 )) && curl --fail --silent http://localhost:8080/health/ready >/dev/null; then
|
||||
printf 'PASS: two API replicas are healthy through the load balancer.\n'
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
printf 'API replicas did not recover within 60 seconds.\n' >&2
|
||||
docker compose ps
|
||||
exit 1
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
# Stop the API before its state stores; named volumes are left intact.
|
||||
docker compose stop api redis db
|
||||
docker compose start db redis
|
||||
|
||||
attempt=0
|
||||
until docker compose exec -T db pg_isready -U messenger -d messenger >/dev/null 2>&1 \
|
||||
&& docker compose exec -T redis sh -ec 'REDISCLI_AUTH="$(cat /run/secrets/redis_password)" redis-cli ping | grep -q PONG' >/dev/null 2>&1; do
|
||||
attempt=$((attempt + 1))
|
||||
if [ "$attempt" -ge 60 ]; then
|
||||
echo 'Database/cache did not become ready within 60 seconds.' >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
docker compose start api
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
image=mtusi-messenger-lab3-api:local
|
||||
revision="roll-$(date -u +%Y%m%d%H%M%S)"
|
||||
|
||||
docker compose build --build-arg "BUILD_REVISION=$revision" api
|
||||
new_image=$(docker image inspect --format '{{.Id}}' "$image")
|
||||
old_ids=$(docker compose ps --all -q api)
|
||||
[[ -n "$old_ids" ]] || { printf 'No API containers found.\n' >&2; exit 1; }
|
||||
|
||||
count_new_healthy() {
|
||||
for container_id in $(docker compose ps --all -q api); do
|
||||
image_id=$(docker inspect --format '{{.Image}}' "$container_id" 2>/dev/null || true)
|
||||
state=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container_id" 2>/dev/null || true)
|
||||
if [[ "$image_id" == "$new_image" && "$state" == healthy ]]; then printf '1\n'; fi
|
||||
done | awk 'END { print NR + 0 }'
|
||||
}
|
||||
|
||||
wait_new_healthy() {
|
||||
expected=$1
|
||||
deadline=$((SECONDS + 90))
|
||||
while (( SECONDS < deadline )); do
|
||||
actual=$(count_new_healthy)
|
||||
if (( actual >= expected )); then return 0; fi
|
||||
sleep 1
|
||||
done
|
||||
printf 'Only %s new replicas became healthy; expected %s.\n' "$(count_new_healthy)" "$expected" >&2
|
||||
docker compose ps --all
|
||||
return 1
|
||||
}
|
||||
|
||||
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
||||
wait_new_healthy 1
|
||||
new_count=1
|
||||
for container_id in $old_ids; do
|
||||
image_id=$(docker inspect --format '{{.Image}}' "$container_id" 2>/dev/null || true)
|
||||
if [[ "$image_id" != "$new_image" ]]; then
|
||||
docker stop --time 6 "$container_id" >/dev/null
|
||||
docker rm "$container_id" >/dev/null
|
||||
docker compose up -d --no-deps --no-recreate --scale api=3 api
|
||||
new_count=$((new_count + 1))
|
||||
wait_new_healthy "$new_count"
|
||||
fi
|
||||
done
|
||||
docker compose up -d --no-deps --no-recreate --scale api=2 api
|
||||
wait_new_healthy 2
|
||||
printf 'PASS: old API containers were replaced one at a time; two new replicas are healthy. revision=%s\n' "$revision"
|
||||
docker compose ps api
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
|
||||
for container_id in $(docker compose ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
|
||||
if [[ "$hostname" == "$TARGET_INSTANCE_ID" ]]; then
|
||||
mkdir -p evidence
|
||||
printf '%s %s\n' "$(date -u +%FT%TZ)" "$TARGET_INSTANCE_ID" >> evidence/stopped-replicas.txt
|
||||
docker stop --time 6 "$container_id" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
printf 'No running API container matches TARGET_INSTANCE_ID.\n' >&2
|
||||
exit 1
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
run_name=${1:-run1}
|
||||
mkdir -p evidence
|
||||
bash scripts/recover-api.sh
|
||||
load_json="evidence/failover-$run_name-load.json"
|
||||
load_log="evidence/failover-$run_name-load.log"
|
||||
failover_log="evidence/failover-$run_name.log"
|
||||
python3 tests/load.py --duration 40 --concurrency 4 --output "$load_json" >"$load_log" 2>&1 &
|
||||
load_pid=$!
|
||||
sleep 2
|
||||
set +e
|
||||
python3 tests/resilience.py --mode failover --action-script scripts/stop-one.sh --base-url http://localhost:8080 2>&1 | tee "$failover_log"
|
||||
scenario_status=${PIPESTATUS[0]}
|
||||
set -e
|
||||
if ! wait "$load_pid"; then
|
||||
printf 'Background load scenario failed; inspect %s.\n' "$load_log" >&2
|
||||
exit 1
|
||||
fi
|
||||
exit "$scenario_status"
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p evidence
|
||||
load_json=evidence/rolling-update-load.json
|
||||
load_log=evidence/rolling-update-load.log
|
||||
update_log=evidence/rolling-update.log
|
||||
python3 tests/load.py --duration 60 --concurrency 4 --output "$load_json" >"$load_log" 2>&1 &
|
||||
load_pid=$!
|
||||
sleep 2
|
||||
set +e
|
||||
bash scripts/rolling-update.sh 2>&1 | tee "$update_log"
|
||||
update_status=${PIPESTATUS[0]}
|
||||
set -e
|
||||
if ! wait "$load_pid"; then
|
||||
printf 'Background load scenario failed; inspect %s.\n' "$load_log" >&2
|
||||
exit 1
|
||||
fi
|
||||
exit "$update_status"
|
||||
Reference in New Issue
Block a user