ЛР 4 - БПИ2302 - Щербацевич - на 4

This commit is contained in:
2026-11-13 00:14:23 +03:00
parent ec540a1a72
commit 18f3986cc6
68 changed files with 5246 additions and 14 deletions
+37
View File
@@ -0,0 +1,37 @@
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSScriptRoot
Set-Location $Root
New-Item -ItemType Directory -Force -Path (Join-Path $Root "secrets") | Out-Null
function Write-Secret([string]$Name, [string]$Value) {
$path = Join-Path $Root "secrets\$Name"
if (-not (Test-Path $path)) {
[System.IO.File]::WriteAllText($path, $Value)
}
}
function Random-Text([int]$Length) {
$bytes = New-Object byte[] $Length
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
return -join ($bytes | ForEach-Object { $_.ToString("x2") })
}
$pg = Random-Text 16
Write-Secret "postgres_password" $pg
Write-Secret "database_url" "postgresql+asyncpg://messenger:${pg}@postgres:5432/messenger"
Write-Secret "host_database_url" "postgresql+asyncpg://messenger:${pg}@127.0.0.1:5433/messenger"
Write-Secret "cache_url" "redis://redis:6379/0"
Write-Secret "host_cache_url" "redis://127.0.0.1:6379/0"
Write-Secret "minio_root_user" "minioadmin"
Write-Secret "minio_root_password" (Random-Text 16)
Write-Secret "s3_access_key_id" "messengerapi"
Write-Secret "s3_secret_access_key" (Random-Text 16)
Write-Secret "host_s3_endpoint" "http://127.0.0.1:9000"
$envFile = Join-Path $Root ".env"
if (-not (Test-Path $envFile)) {
Copy-Item (Join-Path $Root ".env.example") $envFile
}
Write-Host "secrets and .env are ready"
Write-Host "next: docker compose --env-file .env up -d --build --scale api=2"
+36
View File
@@ -0,0 +1,36 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
mkdir -p "$ROOT/secrets"
write_secret() {
name=$1
value=$2
path="$ROOT/secrets/$name"
if [ ! -f "$path" ]; then
printf '%s' "$value" > "$path"
fi
}
rand() {
python3 -c 'import secrets; print(secrets.token_hex(16))'
}
pg=$(rand)
write_secret postgres_password "$pg"
write_secret database_url "postgresql+asyncpg://messenger:${pg}@postgres:5432/messenger"
write_secret host_database_url "postgresql+asyncpg://messenger:${pg}@127.0.0.1:5433/messenger"
write_secret cache_url "redis://redis:6379/0"
write_secret host_cache_url "redis://127.0.0.1:6379/0"
write_secret minio_root_user minioadmin
write_secret minio_root_password "$(rand)"
write_secret s3_access_key_id messengerapi
write_secret s3_secret_access_key "$(rand)"
write_secret host_s3_endpoint "http://127.0.0.1:9000"
if [ ! -f "$ROOT/.env" ]; then
cp "$ROOT/.env.example" "$ROOT/.env"
fi
echo "secrets and .env are ready"
echo "next: docker compose --env-file .env up -d --build --scale api=2"
+5
View File
@@ -0,0 +1,5 @@
@echo off
setlocal
cd /d "%~dp0.."
docker compose --env-file .env restart postgres redis minio api gc 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 minio api gc lb
+21
View File
@@ -0,0 +1,21 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
if "%TARGET_INSTANCE_ID%"=="" (
echo TARGET_INSTANCE_ID is required >&2
exit /b 1
)
for /f "usebackq delims=" %%I in (`docker compose --env-file .env ps -q api`) do (
for /f "usebackq delims=" %%H in (`docker inspect -f "{{.Config.Hostname}}" %%I`) do (
if "%%H"=="%TARGET_INSTANCE_ID%" (
docker stop %%I >nul
exit /b 0
)
)
if "%%I"=="%TARGET_INSTANCE_ID%" (
docker stop %%I >nul
exit /b 0
)
)
echo no api replica matched TARGET_INSTANCE_ID >&2
exit /b 1
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
found=
for id in $(docker compose --env-file .env ps -q api); do
hostname=$(docker inspect -f '{{.Config.Hostname}}' "$id")
instance=$(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$id" | awk -F= '$1=="INSTANCE_ID"{print $2}')
if [ "$hostname" = "$TARGET_INSTANCE_ID" ] || [ "${instance:-}" = "$TARGET_INSTANCE_ID" ] || [ "$id" = "$TARGET_INSTANCE_ID" ]; then
docker stop "$id" >/dev/null
found=1
break
fi
done
if [ -z "$found" ]; then
echo "no api replica matched TARGET_INSTANCE_ID" >&2
exit 1
fi