This commit is contained in:
2026-11-19 19:45:26 +03:00
parent 276dbdad8a
commit ffa3d99557
34 changed files with 3277 additions and 41 deletions
+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
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""Replay one failed attachment through a new pipeline version."""
import argparse
from datetime import datetime, timezone
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from db import ApiError, Store
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("attachment_id")
parser.add_argument("--reason", default="operator_replay")
args = parser.parse_args()
store = Store.connect(run_migrations=False)
try:
job_id, version = store.replay_attachment(
args.attachment_id,
args.reason,
datetime.now(timezone.utc),
)
print("replayed attachment=%s job=%s pipeline_version=%d" %
(args.attachment_id, job_id, version))
finally:
store.close()
if __name__ == "__main__":
try:
main()
except ApiError as error:
print("Replay failed: " + error.message, file=sys.stderr)
raise SystemExit(1) from None
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart api postgres redis minio nginx
+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