ЛР 3 — БПИ2302 — Щербацевич — на 5

This commit is contained in:
2026-11-11 23:55:42 +03:00
parent 0ab3a341aa
commit 7aa4ea541b
76 changed files with 4627 additions and 34 deletions
+21
View File
@@ -0,0 +1,21 @@
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSScriptRoot
if (-not $Root) { $Root = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path }
Set-Location $Root
New-Item -ItemType Directory -Force -Path "secrets" | Out-Null
$passFile = Join-Path $Root "secrets\postgres_password"
if (-not (Test-Path $passFile)) {
$bytes = New-Object byte[] 24
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
$password = [Convert]::ToBase64String($bytes).TrimEnd("=").Replace("+","-").Replace("/","_")
Set-Content -Path $passFile -Value $password -NoNewline -Encoding ascii
}
$password = ((Get-Content -Raw $passFile) -replace "`r","" -replace "`n","").Trim()
Set-Content -Path (Join-Path $Root "secrets\database_url") -Value "postgresql+asyncpg://messenger:${password}@postgres:5432/messenger" -NoNewline -Encoding ascii
Set-Content -Path (Join-Path $Root "secrets\host_database_url") -Value "postgresql+asyncpg://messenger:${password}@127.0.0.1:5433/messenger" -NoNewline -Encoding ascii
Set-Content -Path (Join-Path $Root "secrets\cache_url") -Value "redis://redis:6379/0" -NoNewline -Encoding ascii
Set-Content -Path (Join-Path $Root "secrets\host_cache_url") -Value "redis://127.0.0.1:6379/0" -NoNewline -Encoding ascii
if (-not (Test-Path (Join-Path $Root ".env"))) {
Copy-Item (Join-Path $Root ".env.example") (Join-Path $Root ".env")
}
Write-Output "secrets and .env are ready"
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
mkdir -p secrets
if [ ! -f secrets/postgres_password ]; then
python -c "import secrets,pathlib; pathlib.Path('secrets/postgres_password').write_text(secrets.token_urlsafe(24)+'\n', encoding='utf-8')"
fi
PASSWORD=$(tr -d '\r\n' < secrets/postgres_password)
printf '%s\n' "postgresql+asyncpg://messenger:${PASSWORD}@postgres:5432/messenger" > secrets/database_url
printf '%s\n' "postgresql+asyncpg://messenger:${PASSWORD}@127.0.0.1:5433/messenger" > secrets/host_database_url
printf '%s\n' "redis://redis:6379/0" > secrets/cache_url
printf '%s\n' "redis://127.0.0.1:6379/0" > secrets/host_cache_url
if [ ! -f .env ]; then
cp .env.example .env
fi
printf '%s\n' "secrets and .env are ready"
+5
View File
@@ -0,0 +1,5 @@
@echo off
setlocal
cd /d "%~dp0.."
docker compose --env-file .env restart postgres redis api lb
exit /b %ERRORLEVEL%
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
docker compose --env-file .env restart postgres redis api lb
+7
View File
@@ -0,0 +1,7 @@
@echo off
setlocal
cd /d "%~dp0.."
if "%API_REPLICAS%"=="" set API_REPLICAS=2
docker compose --env-file .env start api
docker compose --env-file .env up -d --no-deps --scale api=%API_REPLICAS% api
exit /b %ERRORLEVEL%
+6
View File
@@ -0,0 +1,6 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
docker compose --env-file .env start api
docker compose --env-file .env up -d --no-deps --scale "api=${API_REPLICAS:-2}" api
+45
View File
@@ -0,0 +1,45 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
REPLICAS=$(docker compose ps -q api | wc -l | tr -d ' ')
if [ "$REPLICAS" -lt 2 ]; then
printf '%s\n' "need at least two API replicas" >&2
exit 1
fi
docker compose --env-file .env build api
old_ids=$(docker compose ps -q api)
for old in $old_ids; do
next=$((REPLICAS + 1))
docker compose --env-file .env up -d --no-deps --scale "api=${next}" --no-recreate api
deadline=$(( $(date +%s) + 60 ))
while [ "$(docker compose ps -q api | wc -l | tr -d ' ')" -lt "$next" ]; do
if [ "$(date +%s)" -ge "$deadline" ]; then
printf '%s\n' "new replica did not appear" >&2
exit 1
fi
sleep 1
done
docker stop -t 5 "$old" >/dev/null
docker rm "$old" >/dev/null
docker compose --env-file .env up -d --no-deps --scale "api=${REPLICAS}" api
done
deadline=$(( $(date +%s) + 60 ))
until docker compose exec -T lb sh -c 'true' >/dev/null 2>&1; do
sleep 1
done
python -c "
import json,urllib.request,time
deadline=time.time()+60
last=''
while time.time()<deadline:
try:
urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).read()
break
except Exception as exc:
last=type(exc).__name__
time.sleep(0.5)
else:
raise SystemExit('ready wait failed: '+last)
"
printf '%s\n' "rolling update finished replicas=${REPLICAS}"
+17
View File
@@ -0,0 +1,17 @@
@echo off
setlocal
cd /d "%~dp0.."
if "%TARGET_INSTANCE_ID%"=="" (
echo TARGET_INSTANCE_ID is required 1>&2
exit /b 2
)
for /f "delims=" %%I in ('docker compose ps -aq -- api') do (
for /f "delims=" %%H in ('docker inspect -f "{{.Config.Hostname}}" %%I') do (
if /I "%%H"=="%TARGET_INSTANCE_ID%" (
docker stop -t 5 %%I >nul
exit /b 0
)
)
)
echo no API replica matches TARGET_INSTANCE_ID 1>&2
exit /b 1
+27
View File
@@ -0,0 +1,27 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
found=""
for id in $(docker compose ps -aq -- api); do
hostname=$(docker inspect -f '{{.Config.Hostname}}' "$id")
name=$(docker inspect -f '{{.Name}}' "$id")
name=${name#/}
cid=$(docker inspect -f '{{.Id}}' "$id")
short=$(printf '%s' "$cid" | cut -c1-12)
if [ "$TARGET_INSTANCE_ID" = "$hostname" ] \
|| [ "$TARGET_INSTANCE_ID" = "$name" ] \
|| [ "$TARGET_INSTANCE_ID" = "$id" ] \
|| [ "$TARGET_INSTANCE_ID" = "$cid" ] \
|| [ "$TARGET_INSTANCE_ID" = "$short" ]; then
found=$id
break
fi
done
if [ -z "$found" ]; then
printf '%s\n' "no API replica matches TARGET_INSTANCE_ID" >&2
exit 1
fi
docker stop -t 5 "$found" >/dev/null
exit 0
+6
View File
@@ -0,0 +1,6 @@
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSScriptRoot
Set-Location $Root
if (-not $env:API_REPLICAS) { $env:API_REPLICAS = "2" }
docker compose --env-file .env up -d --build --wait --scale "api=$($env:API_REPLICAS)"
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
+6
View File
@@ -0,0 +1,6 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
REPLICAS=${API_REPLICAS:-2}
docker compose --env-file .env up -d --build --wait --scale "api=${REPLICAS}"