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

This commit is contained in:
2026-11-20 19:54:38 +03:00
parent 276dbdad8a
commit 1844c21198
88 changed files with 7499 additions and 30 deletions
+52
View File
@@ -0,0 +1,52 @@
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSScriptRoot
$Secrets = Join-Path $Root "secrets"
New-Item -ItemType Directory -Force -Path $Secrets | Out-Null
$utf8 = New-Object System.Text.UTF8Encoding $false
function Write-Secret([string]$Name, [string]$Value) {
$path = Join-Path $Secrets $Name
if (-not (Test-Path $path)) {
[System.IO.File]::WriteAllText($path, $Value, $utf8)
}
}
function Read-Secret([string]$Name) {
$path = Join-Path $Secrets $Name
return ([System.IO.File]::ReadAllText($path, $utf8)).Trim()
}
function New-Hex([int]$Bytes) {
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$buf = New-Object byte[] $Bytes
$rng.GetBytes($buf)
} finally {
$rng.Dispose()
}
return ([System.BitConverter]::ToString($buf) -replace "-", "").ToLower()
}
Write-Secret "postgres_password" (New-Hex 18)
Write-Secret "minio_root_user" ("lab" + (New-Hex 4))
Write-Secret "minio_root_password" (New-Hex 18)
Write-Secret "s3_access_key_id" ("s3" + (New-Hex 6))
Write-Secret "s3_secret_access_key" (New-Hex 24)
Write-Secret "rabbitmq_user" "messenger"
Write-Secret "rabbitmq_password" (New-Hex 18)
$pg = Read-Secret "postgres_password"
$rabbitUser = Read-Secret "rabbitmq_user"
$rabbitPassword = Read-Secret "rabbitmq_password"
Write-Secret "database_url" ("postgresql+asyncpg://messenger:{0}@postgres:5432/messenger" -f $pg)
Write-Secret "host_database_url" ("postgresql+asyncpg://messenger:{0}@127.0.0.1:5433/messenger" -f $pg)
Write-Secret "cache_url" "redis://redis:6379/0"
Write-Secret "host_cache_url" "redis://127.0.0.1:6379/0"
Write-Secret "host_s3_endpoint" "http://127.0.0.1:9000"
Write-Secret "broker_url" ("amqp://{0}:{1}@rabbitmq:5672/" -f $rabbitUser, $rabbitPassword)
$envFile = Join-Path $Root ".env"
if (-not (Test-Path $envFile)) {
Copy-Item (Join-Path $Root ".env.example") $envFile
}
Write-Output "local secrets are ready"
+45
View File
@@ -0,0 +1,45 @@
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
SECRETS="$ROOT/secrets"
mkdir -p "$SECRETS"
umask 077
write_secret() {
name=$1
value=$2
if [ ! -f "$SECRETS/$name" ]; then
printf '%s' "$value" > "$SECRETS/$name"
fi
}
read_secret() {
tr -d '\r\n' < "$SECRETS/$1"
}
hex_bytes() {
od -An -N "$1" -tx1 /dev/urandom | tr -d ' \n'
}
write_secret postgres_password "$(hex_bytes 18)"
write_secret minio_root_user "lab$(hex_bytes 4)"
write_secret minio_root_password "$(hex_bytes 18)"
write_secret s3_access_key_id "s3$(hex_bytes 6)"
write_secret s3_secret_access_key "$(hex_bytes 24)"
write_secret rabbitmq_user messenger
write_secret rabbitmq_password "$(hex_bytes 18)"
pg=$(read_secret postgres_password)
rabbit_user=$(read_secret rabbitmq_user)
rabbit_password=$(read_secret rabbitmq_password)
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 host_s3_endpoint "http://127.0.0.1:9000"
write_secret broker_url "amqp://${rabbit_user}:${rabbit_password}@rabbitmq:5672/"
if [ ! -f "$ROOT/.env" ]; then
cp "$ROOT/.env.example" "$ROOT/.env"
fi
echo "local secrets are ready"
+4
View File
@@ -0,0 +1,4 @@
@echo off
cd /d "%~dp0\.."
docker compose --env-file .env restart postgres redis minio rabbitmq api worker relay gc lb
exit /b %ERRORLEVEL%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env restart postgres redis minio rabbitmq api worker relay gc lb
+3
View File
@@ -0,0 +1,3 @@
@echo off
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0stop-one.ps1"
exit /b %ERRORLEVEL%
+23
View File
@@ -0,0 +1,23 @@
$ErrorActionPreference = "Stop"
$target = $env:TARGET_INSTANCE_ID
if (-not $target) {
Write-Error "TARGET_INSTANCE_ID is required"
exit 1
}
$ids = docker ps -q --filter label=com.docker.compose.service=api --filter label=com.docker.compose.project=mtusi-lab5
$found = ""
foreach ($id in $ids) {
if (-not $id) { continue }
$hostName = docker inspect -f "{{.Config.Hostname}}" $id
$short = $id.Substring(0, [Math]::Min(12, $id.Length))
if ($hostName -eq $target -or $short -eq $target -or $id -eq $target) {
$found = $id
break
}
}
if (-not $found) {
Write-Error "api replica not found"
exit 1
}
docker stop -t 5 $found | Out-Null
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
set -eu
target="${TARGET_INSTANCE_ID:?test provides target API instance alias}"
found=""
for id in $(docker ps -q --filter label=com.docker.compose.service=api --filter label=com.docker.compose.project=mtusi-lab5); do
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
short=$(printf '%s' "$id" | cut -c1-12)
if [ "$host" = "$target" ] || [ "$short" = "$target" ] || [ "$id" = "$target" ]; then
found=$id
break
fi
done
if [ -z "$found" ]; then
echo "api replica not found" >&2
exit 1
fi
docker stop -t 5 "$found" >/dev/null