перенос 4 лабы и реализация 5
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""Идемпотентное создание приватного bucket отдельной административной учёткой."""
|
||||
import os
|
||||
import time
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from minio import Minio
|
||||
from minio.error import MinioException, S3Error
|
||||
import urllib3
|
||||
|
||||
from src.config import secret
|
||||
|
||||
|
||||
def bootstrap():
|
||||
endpoint = urlsplit(os.environ.get("S3_ENDPOINT", "http://s3:9000"))
|
||||
client = Minio(endpoint.netloc, access_key=secret("S3_ROOT_USER"), secret_key=secret("S3_ROOT_PASSWORD"),
|
||||
secure=endpoint.scheme == "https", region=os.environ.get("S3_REGION", "us-east-1"),
|
||||
http_client=urllib3.PoolManager(timeout=urllib3.Timeout(connect=1, read=3), retries=False))
|
||||
bucket = os.environ.get("S3_BUCKET", "messenger-attachments")
|
||||
deadline = time.monotonic() + 60
|
||||
while True:
|
||||
try:
|
||||
if not client.bucket_exists(bucket):
|
||||
client.make_bucket(bucket)
|
||||
print("Private bucket is ready; static IAM permits only authenticated accounts", flush=True)
|
||||
return
|
||||
except S3Error as error:
|
||||
if error.code in {"BucketAlreadyOwnedByYou", "BucketAlreadyExists"}:
|
||||
return
|
||||
if time.monotonic() >= deadline:
|
||||
raise RuntimeError("S3 bootstrap failed") from None
|
||||
except (MinioException, urllib3.exceptions.HTTPError, OSError):
|
||||
if time.monotonic() >= deadline:
|
||||
raise RuntimeError("S3 bootstrap timed out") from None
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bootstrap()
|
||||
Reference in New Issue
Block a user