Complete Lab 4 attachment service

This commit is contained in:
2026-11-14 02:11:47 +03:00
parent ec540a1a72
commit 1c10525136
36 changed files with 2740 additions and 73 deletions
+57
View File
@@ -0,0 +1,57 @@
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$secretDir = Join-Path $repo 'secrets'
New-Item -ItemType Directory -Path $secretDir -Force | Out-Null
function New-Secret {
$bytes = New-Object byte[] 32
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
return [Convert]::ToBase64String($bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
}
function New-AccessKey {
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
$bytes = New-Object byte[] 20
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
$value = -join ($bytes | ForEach-Object { $alphabet[($_ % $alphabet.Length)] })
return $value
}
function Read-Secret([string]$name) {
$path = Join-Path $secretDir $name
if (-not (Test-Path -LiteralPath $path)) { return $null }
return [System.IO.File]::ReadAllText($path).Trim()
}
function Write-Secret([string]$name, [string]$value) {
$path = Join-Path $secretDir $name
[System.IO.File]::WriteAllText($path, $value, [System.Text.UTF8Encoding]::new($false))
}
function Ensure-Pair([string]$first, [string]$second, [scriptblock]$newFirst, [scriptblock]$newSecond) {
$left = Read-Secret $first
$right = Read-Secret $second
if (($null -eq $left) -ne ($null -eq $right)) {
throw "Only one file of credential pair '$first'/'$second' exists. Resolve it manually; credentials are never rotated automatically."
}
if ($null -eq $left) {
$left = & $newFirst
$right = & $newSecond $left
Write-Secret $first $left
Write-Secret $second $right
}
return @($left, $right)
}
$pg = Ensure-Pair 'postgres_password' 'database_url' { New-Secret } {
param($password)
"postgresql+psycopg://messenger:$password@db:5432/messenger"
}
$redis = Ensure-Pair 'redis_password' 'cache_url' { New-Secret } {
param($password)
"redis://:$password@redis:6379/0"
}
$s3root = Ensure-Pair 's3_root_access_key_id' 's3_root_secret_access_key' { New-AccessKey } { New-Secret }
$s3api = Ensure-Pair 's3_access_key_id' 's3_secret_access_key' { New-AccessKey } { New-Secret }
Write-Output 'Local Postgres, Redis, RustFS root and limited API credentials are ready under secrets/. Existing complete pairs were left unchanged; values were not displayed.'
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
docker compose --env-file .env up -d --scale api=2 api lb >/dev/null
deadline=$((SECONDS + 60))
while (( SECONDS < deadline )); do
healthy=0
for container_id in $(docker compose --env-file .env 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 --env-file .env ps
exit 1
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env sh
set -eu
cd "$(dirname "$0")/.."
# Stop the entry points before every state store; named volumes are left intact.
docker compose stop lb api redis db s3
docker compose start db redis s3
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 \
&& docker compose exec -T s3 curl -fsS http://127.0.0.1:9000/health/ready >/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 --env-file .env up -d migrate s3-bootstrap
docker compose --env-file .env up -d --scale api=2 api lb
+17
View File
@@ -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