Implement lab4 S3 attachments

This commit is contained in:
2026-11-11 18:55:53 +03:00
parent ec540a1a72
commit d894764b39
44 changed files with 1333 additions and 23 deletions
+10
View File
@@ -0,0 +1,10 @@
@echo off
setlocal
cd /d "%~dp0.."
rem Preserve PostgreSQL named volume, tokens and secrets.
docker compose --env-file .env restart -t 5 db s3 api1 api2
if errorlevel 1 exit /b 1
docker compose --env-file .env up -d --no-deps --wait --wait-timeout 55 api1 api2
if errorlevel 1 exit /b 1
docker compose --env-file .env restart -t 5 lb
exit /b %ERRORLEVEL%
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
# Keep PostgreSQL named volume and the generated DB credentials unchanged.
# Restart shared DB and both API replicas; restart proxy to refresh backends.
docker compose --env-file .env restart -t 5 db s3 api1 api2
docker compose --env-file .env up -d --no-deps --wait --wait-timeout 55 api1 api2
docker compose --env-file .env restart -t 5 lb
+5
View File
@@ -0,0 +1,5 @@
@echo off
setlocal
cd /d "%~dp0.."
docker compose --env-file .env up -d --no-deps --wait --wait-timeout 55 api1 api2
exit /b %ERRORLEVEL%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env start api1 api2
+36
View File
@@ -0,0 +1,36 @@
$ErrorActionPreference = 'Stop'
Set-Location (Join-Path $PSScriptRoot '..')
if (!(Test-Path -LiteralPath '.env')) {
Copy-Item -LiteralPath '.env.example' -Destination '.env'
}
New-Item -ItemType Directory -Path 'secrets' -Force | Out-Null
function New-SecretFile([string]$Name, [int]$Length) {
$path = Join-Path (Get-Location) "secrets/$Name"
if (!(Test-Path -LiteralPath $path)) {
$bytes = [byte[]]::new($Length)
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$rng.GetBytes($bytes)
} finally {
$rng.Dispose()
}
$value = [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant()
[IO.File]::WriteAllText($path, $value, [Text.Encoding]::ASCII)
}
}
New-SecretFile 'db_password' 24
New-SecretFile 's3_root_access' 10
New-SecretFile 's3_root_secret' 20
New-SecretFile 's3_access_key_id' 10
New-SecretFile 's3_secret_access_key' 20
if (!(Test-Path -LiteralPath 'secrets/database_url')) {
$password = (Get-Content -LiteralPath 'secrets/db_password' -Raw).TrimEnd([char]10)
$encoded = [Uri]::EscapeDataString($password)
$url = "postgresql://messenger:${encoded}@db:5432/messenger"
[IO.File]::WriteAllText((Join-Path (Get-Location) 'secrets/database_url'), $url, [Text.Encoding]::ASCII)
}
Write-Host 'Local secrets are ready.'
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
if [ ! -e .env ]; then cp .env.example .env; fi
mkdir -p secrets
if [ ! -e secrets/db_password ]; then
python3 -c 'import secrets;print(secrets.token_urlsafe(36),end="")' > secrets/db_password
fi
if [ ! -e secrets/database_url ]; then
python3 -c 'from pathlib import Path;from urllib.parse import quote; p=Path("secrets/db_password").read_text().strip();print("postgresql://messenger:"+quote(p,safe="")+"@db:5432/messenger",end="")' > secrets/database_url
fi
python3 - <<'PYGEN'
from pathlib import Path
import secrets
root=Path('secrets')
for name,length in [('s3_root_access',10),('s3_root_secret',20),('s3_access_key_id',10),('s3_secret_access_key',20)]:
file=root/name
if not file.exists():
file.write_bytes(secrets.token_hex(length).encode('ascii'))
PYGEN
chmod 600 secrets/* .env
printf 'Local secrets prepared (values not displayed).\n'
+10
View File
@@ -0,0 +1,10 @@
@echo off
setlocal
cd /d "%~dp0.."
if /I "%TARGET_INSTANCE_ID%"=="api1" goto stop
if /I "%TARGET_INSTANCE_ID%"=="api2" goto stop
echo Invalid TARGET_INSTANCE_ID; refusing to stop an arbitrary service 1>&2
exit /b 2
:stop
docker compose --env-file .env stop -t 2 %TARGET_INSTANCE_ID%
exit /b %ERRORLEVEL%
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
case "${TARGET_INSTANCE_ID:-}" in
api1|api2) exec docker compose --env-file .env stop -t 2 "$TARGET_INSTANCE_ID" ;;
*) echo 'Unknown TARGET_INSTANCE_ID; refusing to stop an arbitrary service' >&2; exit 2 ;;
esac