Files
mtusi2026lab4/app/s3_bootstrap.py
T
Чиняева f39ae06ef3 Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
2026-11-07 01:37:21 +03:00

40 lines
1.2 KiB
Python

"""One-shot, idempotent private-bucket bootstrap.
Runs in the dedicated ``s3-init`` Compose service with the administrative S3
identity. The API replicas start only after this service exits successfully,
and they use a different, restricted identity.
"""
from __future__ import annotations
import logging
import sys
import time
from . import s3
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
log = logging.getLogger("messenger.s3-bootstrap")
def main() -> int:
deadline = time.monotonic() + 90.0
backoff = 0.5
while True:
try:
s3.ensure_bucket()
if not s3.check_ready():
raise RuntimeError("restricted identity cannot see the bucket")
log.info("private bucket is present and visible to the API identity")
return 0
except Exception as error:
if time.monotonic() >= deadline:
log.error("bucket bootstrap failed: %s", type(error).__name__)
return 1
log.warning("bucket bootstrap is not ready yet: %s", type(error).__name__)
time.sleep(backoff)
backoff = min(backoff * 2, 5.0)
if __name__ == "__main__":
sys.exit(main())