"""Idempotent private bucket bootstrap; executed with separate admin credentials.""" import time from uuid import uuid4 from botocore.exceptions import BotoCoreError, ClientError from .storage.s3 import S3Settings, S3Store def main(): store = S3Store(S3Settings.load()) try: for attempt in range(60): try: store.client.list_buckets() break except (BotoCoreError, ClientError): if attempt == 59: raise RuntimeError("S3 startup timed out") from None time.sleep(1) bucket = store.settings.bucket try: store.client.head_bucket(Bucket=bucket) except ClientError as error: if str(error.response["Error"]["Code"]) not in {"404", "NoSuchBucket", "NotFound"}: raise store.client.create_bucket(Bucket=bucket) # No anonymous identity; revoke any previously configured public grants/policy. store.client.put_bucket_acl(Bucket=bucket, ACL="private") try: store.client.delete_bucket_policy(Bucket=bucket) except ClientError as error: if error.response["Error"]["Code"] != "NoSuchBucketPolicy": raise # The master health endpoint can become available before a volume accepts # writes after a restart. Verify the actual data path before APIs start. probe_key = f"_bootstrap/write-probe-{uuid4()}" for attempt in range(30): try: store.client.put_object(Bucket=bucket, Key=probe_key, Body=b"ready") store.client.delete_object(Bucket=bucket, Key=probe_key) break except (BotoCoreError, ClientError): if attempt == 29: raise RuntimeError("S3 write path startup timed out") from None time.sleep(1) print("PASS: private S3 bucket ready") finally: store.client.close() if __name__ == "__main__": main()