ЛР 3: балансировка, идемпотентность и failover — на 3

This commit is contained in:
2026-11-20 16:20:41 +03:00
parent 0ab3a341aa
commit be96c39563
31 changed files with 1490 additions and 21 deletions
+27
View File
@@ -0,0 +1,27 @@
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot
$utf8 = [Text.UTF8Encoding]::new($false)
$secretDir = Join-Path $root 'secrets'
[void][IO.Directory]::CreateDirectory($secretDir)
$passwordFile = Join-Path $secretDir 'postgres_password'
if (-not (Test-Path -LiteralPath $passwordFile)) {
$bytes = New-Object byte[] 32
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
$password = [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant()
[IO.File]::WriteAllText($passwordFile, $password, $utf8)
}
$password = [IO.File]::ReadAllText($passwordFile).Trim()
if ($password -cnotmatch '^[a-f0-9]{64}$') {
throw 'Expected the locally generated PostgreSQL password in secrets/postgres_password.'
}
[IO.File]::WriteAllText(
(Join-Path $secretDir 'database_url'),
"postgresql://messenger:${password}@postgres:5432/messenger",
$utf8
)
$envFile = Join-Path $root '.env'
if (-not (Test-Path -LiteralPath $envFile)) {
Copy-Item -LiteralPath (Join-Path $root '.env.example') -Destination $envFile
}
Write-Output 'Local configuration ready.'
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
if [ -n "${LAB3_PROJECT:-}" ]; then
set -- --project-name "$LAB3_PROJECT"
fi
docker compose "$@" restart --no-deps --timeout 4 postgres
docker compose "$@" restart --no-deps --timeout 4 api1 api2
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
set -eu
case "${TARGET_INSTANCE_ID:-}" in
api1|api2) ;;
*) echo 'TARGET_INSTANCE_ID must be api1 or api2' >&2; exit 2 ;;
esac
cd "$(dirname "$0")/.."
if [ -n "${LAB3_PROJECT:-}" ]; then
set -- --project-name "$LAB3_PROJECT"
fi
docker compose "$@" stop --timeout 4 "$TARGET_INSTANCE_ID"