This commit is contained in:
2026-11-05 18:08:31 +03:00
parent ec540a1a72
commit d6e976e4c0
33 changed files with 2789 additions and 52 deletions
+6 -1
View File
@@ -40,7 +40,12 @@ def main():
assert 'X-Instance-Id' in response['headers']
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
python_checks = (
list((ROOT/'tests').glob('*.py'))
+ list((ROOT/'scripts').glob('*.py'))
+ list((ROOT/'student-tests').glob('*.py'))
)
for file in python_checks:
ast.parse(file.read_text(),filename=str(file))
for file in ROOT.rglob('*.json'):
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
+60
View File
@@ -0,0 +1,60 @@
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot
$secrets = Join-Path $root 'secrets'
$names = @(
'postgres_password',
'database_url',
'minio_root_user',
'minio_root_password',
's3_access_key_id',
's3_secret_access_key'
)
if (-not (Test-Path (Join-Path $root '.env'))) {
Copy-Item (Join-Path $root '.env.example') (Join-Path $root '.env')
}
$existing = @($names | Where-Object { Test-Path (Join-Path $secrets $_) })
if ($existing.Count -gt 0) {
if ($existing.Count -eq $names.Count) {
Write-Output 'Local secrets already exist; leaving them unchanged.'
exit 0
}
throw 'The local secret set is incomplete. Restore it as a set; do not rotate credentials against existing volumes.'
}
New-Item -ItemType Directory -Force $secrets | Out-Null
function New-HexSecret([int]$byteCount) {
$bytes = New-Object byte[] $byteCount
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$rng.GetBytes($bytes)
}
finally {
$rng.Dispose()
}
$builder = New-Object System.Text.StringBuilder
foreach ($byte in $bytes) {
[void]$builder.Append($byte.ToString('x2'))
}
return $builder.ToString()
}
$databasePassword = New-HexSecret 32
$values = @{
'postgres_password' = $databasePassword
'database_url' = "postgresql://messenger:$databasePassword@postgres:5432/messenger"
'minio_root_user' = 'root-' + (New-HexSecret 6)
'minio_root_password' = New-HexSecret 32
's3_access_key_id' = New-HexSecret 10
's3_secret_access_key' = New-HexSecret 32
}
foreach ($name in $names) {
$path = Join-Path $secrets $name
[System.IO.File]::WriteAllText(
$path,
$values[$name],
[System.Text.Encoding]::ASCII
)
}
Write-Output 'Created local Compose secrets. Their values were not printed.'
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env python3
"""Apply database migrations once before API replicas start."""
import logging
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from db import Store
def main():
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
store = Store.connect()
try:
logging.info('database migrations are current')
finally:
store.close()
if __name__ == '__main__':
main()
+91
View File
@@ -0,0 +1,91 @@
#!/usr/bin/env python3
"""Dry-run first; remove only old, unreferenced objects or stale upload attempts."""
import argparse
from datetime import datetime, timedelta, timezone
import json
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from db import ApiError, Store
from s3store import ObjectStore, S3Unavailable
def parse_timestamp(value):
return datetime.fromisoformat(value.replace('Z', '+00:00'))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--grace-seconds', type=int, default=900)
parser.add_argument('--apply', action='store_true')
args = parser.parse_args()
if args.grace_seconds < 900:
parser.error('--grace-seconds must be at least 900')
store = Store.connect(run_migrations=False)
objects = ObjectStore.from_env()
try:
cutoff = datetime.now(timezone.utc) - timedelta(seconds=args.grace_seconds)
records = store.list_attachments_for_reconciliation()
by_key = {record['object_key']: record for record in records}
summary = {
'mode': 'apply' if args.apply else 'dry-run',
'grace_seconds': args.grace_seconds,
'stale_uploads': 0,
'old_failed_objects': 0,
'orphan_objects': 0,
'missing_ready_objects': 0,
'actions_applied': 0,
}
for record in records:
key = record['object_key']
if (record['status'] == 'processing'
and parse_timestamp(record['created_at']) < cutoff):
summary['stale_uploads'] += 1
exists = objects.object_exists(key)
if args.apply:
if exists:
objects.delete_object(key)
store.mark_attachment_failed(
record['id'], 'upload_interrupted', datetime.now(timezone.utc)
)
summary['actions_applied'] += 1
elif (record['status'] == 'failed'
and parse_timestamp(record['updated_at']) < cutoff):
if objects.object_exists(key):
summary['old_failed_objects'] += 1
if args.apply:
objects.delete_object(key)
summary['actions_applied'] += 1
elif record['status'] == 'ready' and not objects.object_exists(key):
summary['missing_ready_objects'] += 1
if args.apply:
store.mark_reconciled_missing(
record['id'], datetime.now(timezone.utc)
)
summary['actions_applied'] += 1
for item in objects.list_objects():
key = item['Key']
if key in by_key or item['LastModified'] >= cutoff:
continue
summary['orphan_objects'] += 1
if args.apply:
objects.delete_object(key)
summary['actions_applied'] += 1
print(json.dumps(summary, sort_keys=True))
finally:
store.close()
objects.client.close()
if __name__ == '__main__':
try:
main()
except (ApiError, S3Unavailable) as error:
print('Reconciliation failed: ' + error.message if isinstance(error, ApiError) else
'Reconciliation failed: S3 is unavailable', file=sys.stderr)
raise SystemExit(1) from None
+5
View File
@@ -0,0 +1,5 @@
@echo off
setlocal
cd /d "%~dp0.."
docker compose restart api postgres redis minio nginx
exit /b %errorlevel%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart api postgres redis minio nginx
+21
View File
@@ -0,0 +1,21 @@
@echo off
setlocal
cd /d "%~dp0.."
if "%TARGET_INSTANCE_ID%"=="" (
echo TARGET_INSTANCE_ID is required 1>&2
exit /b 2
)
set TARGET_STOPPED=
for /f "delims=" %%i in ('docker compose ps -q api') do if not defined TARGET_STOPPED call :stop_if_target %%i
if defined TARGET_STOPPED exit /b 0
echo No running API replica matches TARGET_INSTANCE_ID 1>&2
exit /b 1
:stop_if_target
for /f "delims=" %%h in ('docker inspect --format "{{.Config.Hostname}}" %1') do if "%%h"=="%TARGET_INSTANCE_ID%" (
docker stop --time 2 %1 >nul
if errorlevel 1 exit /b 1
set TARGET_STOPPED=1
exit /b 0
)
exit /b 1
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
cd "$(dirname "$0")/.."
for container_id in $(docker compose ps -q api); do
instance_id=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
if [ "$instance_id" = "$TARGET_INSTANCE_ID" ]; then
docker stop --time 2 "$container_id" >/dev/null
exit 0
fi
done
printf '%s\n' 'No running API replica matches TARGET_INSTANCE_ID' >&2
exit 1