Implement lab 2 messenger with PostgreSQL and Docker
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# Creates local secrets and .env (Windows PowerShell 5.1 / PowerShell 7).
|
||||
# Existing files are NEVER overwritten: the password of an already initialised database volume
|
||||
# must keep matching secrets/postgres_password.
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
$db = if ($env:POSTGRES_DB) { $env:POSTGRES_DB } else { 'messenger' }
|
||||
$user = if ($env:POSTGRES_USER) { $env:POSTGRES_USER } else { 'messenger' }
|
||||
New-Item -ItemType Directory -Force -Path 'secrets' | Out-Null
|
||||
# UTF-8 WITHOUT BOM and without a trailing newline: readable by the PostgreSQL image and by the app.
|
||||
$utf8 = New-Object System.Text.UTF8Encoding($false)
|
||||
$pwFile = Join-Path (Get-Location) 'secrets/postgres_password'
|
||||
$urlFile = Join-Path (Get-Location) 'secrets/database_url'
|
||||
if (-not (Test-Path $pwFile)) {
|
||||
$bytes = New-Object byte[] 24
|
||||
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
|
||||
$password = ($bytes | ForEach-Object { $_.ToString('x2') }) -join ''
|
||||
[System.IO.File]::WriteAllText($pwFile, $password, $utf8)
|
||||
Write-Host 'created secrets/postgres_password'
|
||||
}
|
||||
if (-not (Test-Path $urlFile)) {
|
||||
$password = [System.IO.File]::ReadAllText($pwFile).Trim()
|
||||
[System.IO.File]::WriteAllText($urlFile, "postgres://${user}:${password}@postgres:5432/${db}?sslmode=disable", $utf8)
|
||||
Write-Host 'created secrets/database_url'
|
||||
}
|
||||
if (-not (Test-Path '.env')) { Copy-Item '.env.example' '.env'; Write-Host 'created .env' }
|
||||
@@ -0,0 +1,23 @@
|
||||
@echo off
|
||||
rem Full restart of API and PostgreSQL, volumes are preserved (Windows). No secrets are printed.
|
||||
setlocal
|
||||
cd /d "%~dp0.." || exit /b 1
|
||||
|
||||
docker compose stop api postgres >nul 2>&1
|
||||
if errorlevel 1 exit /b 1
|
||||
docker compose start postgres >nul 2>&1
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
set /a tries=0
|
||||
:waitpg
|
||||
docker compose exec -T postgres sh -c "pg_isready -q -U $POSTGRES_USER -d $POSTGRES_DB" >nul 2>&1
|
||||
if not errorlevel 1 goto pgready
|
||||
set /a tries+=1
|
||||
if %tries% GEQ 45 exit /b 1
|
||||
rem ping instead of timeout: timeout fails when stdin is redirected (as in tests/resilience.py)
|
||||
ping -n 2 127.0.0.1 >nul
|
||||
goto waitpg
|
||||
|
||||
:pgready
|
||||
docker compose start api >nul 2>&1
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
# Полный перезапуск API и ВСЕХ используемых хранилищ (PostgreSQL) с сохранением volumes.
|
||||
# Не использует down -v / удаление volumes / повторный seed. Вывод секретов отсутствует.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
# stop → SIGTERM (graceful): API дожидается запросов, PostgreSQL делает fast shutdown и сбрасывает WAL.
|
||||
docker compose stop api postgres >/dev/null 2>&1
|
||||
docker compose start postgres >/dev/null 2>&1
|
||||
|
||||
tries=0
|
||||
until docker compose exec -T postgres sh -c 'pg_isready -q -U "$POSTGRES_USER" -d "$POSTGRES_DB"' >/dev/null 2>&1; do
|
||||
tries=$((tries + 1))
|
||||
if [ "$tries" -ge 45 ]; then
|
||||
echo "postgres did not become ready in time" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
docker compose start api >/dev/null 2>&1
|
||||
exit 0
|
||||
Reference in New Issue
Block a user