ЛР 2 — БПИ2304 — Кирюпина — на 3
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
SashaKirupina
2026-11-05 13:46:46 +03:00
parent 65003b6119
commit 0c179a44a3
28 changed files with 984 additions and 20 deletions
+24
View File
@@ -0,0 +1,24 @@
param([string]$PythonCommand = 'py')
$ErrorActionPreference = 'Stop'
Push-Location (Split-Path $PSScriptRoot -Parent)
$containerId = $null
try {
# An extra temporary API shares PostgreSQL but issues only two-second sessions.
# The main API and its existing sessions are not reconfigured.
$containerId = docker compose run -d --rm --no-deps -p 127.0.0.1:18083:8080 -e SESSION_TTL_SECONDS=2 api
if ($LASTEXITCODE -ne 0) { throw 'Temporary API failed to start' }
$ready = $false
for ($i=0; $i -lt 30; $i++) {
try {
$null = Invoke-RestMethod http://127.0.0.1:18083/health/ready -TimeoutSec 2
$ready = $true
break
} catch { Start-Sleep -Milliseconds 500 }
}
if (-not $ready) { throw 'Temporary API not ready' }
& $PythonCommand -X utf8 student-tests/check.py --base-url http://127.0.0.1:18083 --expiry
if ($LASTEXITCODE -ne 0) { throw 'Expiry test failed' }
} finally {
if ($containerId) { docker stop $containerId | Out-Null }
Pop-Location
}
+3
View File
@@ -0,0 +1,3 @@
@echo off
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0restart.ps1"
exit /b %errorlevel%
+20
View File
@@ -0,0 +1,20 @@
$ErrorActionPreference = 'Stop'
Push-Location (Split-Path $PSScriptRoot -Parent)
try {
function Snapshot {
foreach ($service in @('api', 'db')) {
$containerId = docker compose ps -q $service
if ($LASTEXITCODE -ne 0 -or -not $containerId) { throw 'Container not found' }
docker inspect --format '{{.Name}} started={{.State.StartedAt}} status={{.State.Status}} volumes={{range .Mounts}}{{.Name}} {{end}}' $containerId
if ($LASTEXITCODE -ne 0) { throw 'Inspect failed' }
}
}
Snapshot | Set-Content evidence/restart-scope.txt
docker compose stop api db
if ($LASTEXITCODE -ne 0) { throw 'Stop failed' }
docker compose up -d --wait --wait-timeout 30 db
if ($LASTEXITCODE -ne 0) { throw 'Database start failed' }
docker compose up -d --wait --wait-timeout 30 api
if ($LASTEXITCODE -ne 0) { throw 'API start failed' }
Snapshot | Add-Content evidence/restart-scope.txt
} finally { Pop-Location }
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
mkdir -p evidence
snapshot() {
for service in api db; do
id=$(docker compose ps -q "$service")
docker inspect --format '{{.Name}} started={{.State.StartedAt}} status={{.State.Status}} mounts={{range .Mounts}}{{if eq .Type "volume"}}{{.Name}} {{end}}{{end}}' "$id"
done
}
snapshot > evidence/restart-scope.txt
docker compose stop api db
docker compose up -d --wait --wait-timeout 30 db
docker compose up -d --wait --wait-timeout 30 api
snapshot >> evidence/restart-scope.txt
+21
View File
@@ -0,0 +1,21 @@
"""Generate local credentials once; never print or rotate existing secrets."""
from pathlib import Path
import secrets
import shutil
root = Path(__file__).resolve().parents[1]
directory = root / 'secrets'
directory.mkdir(exist_ok=True)
password_file = directory / 'postgres_password'
url_file = directory / 'database_url'
if password_file.exists() != url_file.exists():
raise SystemExit('Incomplete secrets: restore the missing file; do not rotate an existing database password.')
if not password_file.exists():
password = secrets.token_hex(32)
password_file.write_text(password + '\n', encoding='utf-8')
url_file.write_text('postgresql://messenger:' + password + '@db:5432/messenger\n', encoding='utf-8')
password_file.chmod(0o600)
url_file.chmod(0o600)
if not (root / '.env').exists():
shutil.copyfile(root / '.env.example', root / '.env')
print('Local .env and secrets ready; values are not printed. Existing credentials preserved.')