|
| 1 | +import boto3 |
| 2 | +import os |
| 3 | +from argparse import ArgumentParser |
| 4 | + |
| 5 | +regions = [ |
| 6 | + "us-east-2", |
| 7 | + "us-east-1", |
| 8 | + "us-west-1", |
| 9 | + "us-west-2", |
| 10 | + "ap-south-1", |
| 11 | + "ap-northeast-2", |
| 12 | + "ap-southeast-1", |
| 13 | + "ap-southeast-2", |
| 14 | + "ap-northeast-1", |
| 15 | + "ca-central-1", |
| 16 | + # "cn-north-1", |
| 17 | + "eu-central-1", |
| 18 | + "eu-west-1", |
| 19 | + "eu-west-2", |
| 20 | + "eu-west-3", |
| 21 | + "eu-north-1", |
| 22 | + "sa-east-1" |
| 23 | + ] |
| 24 | + |
| 25 | + |
| 26 | +def get_bucket_name(bucket_prefix, region): |
| 27 | + if region == "eu-north-1": |
| 28 | + return '%s-%ss' % (bucket_prefix, region) |
| 29 | + return '%s-%s' % (bucket_prefix, region) |
| 30 | + |
| 31 | + |
| 32 | +def upload_code_in_multiple_regions(filepath, bucket_prefix): |
| 33 | + |
| 34 | + for region in regions: |
| 35 | + upload_code_in_S3(filepath, get_bucket_name(bucket_prefix, region), region) |
| 36 | + |
| 37 | + |
| 38 | +def create_buckets(bucket_prefix): |
| 39 | + for region in regions: |
| 40 | + s3 = boto3.client('s3', region) |
| 41 | + bucket_name = get_bucket_name(bucket_prefix, region) |
| 42 | + try: |
| 43 | + if region == "us-east-1": |
| 44 | + response = s3.create_bucket(Bucket=bucket_name) # the operation is idempotent |
| 45 | + else: |
| 46 | + response = s3.create_bucket(Bucket=bucket_name, |
| 47 | + CreateBucketConfiguration={ |
| 48 | + 'LocationConstraint': region |
| 49 | + }) |
| 50 | + print("Creating bucket", region, response) |
| 51 | + except: |
| 52 | + pass |
| 53 | + |
| 54 | + |
| 55 | +def upload_code_in_S3(filepath, bucket_name, region): |
| 56 | + print("Uploading zip file in S3", region) |
| 57 | + s3 = boto3.client('s3', region) |
| 58 | + filename = os.path.basename(filepath) |
| 59 | + s3.upload_file(filepath, bucket_name, filename, |
| 60 | + ExtraArgs={'ACL': 'public-read'}) |
| 61 | + |
| 62 | + |
| 63 | +def upload_cftemplate(templatepath, bucket_name, region='us-east-1'): |
| 64 | + print("Uploading template file in S3") |
| 65 | + s3 = boto3.client('s3', region) |
| 66 | + filename = os.path.basename(templatepath) |
| 67 | + s3.upload_file(templatepath, bucket_name, filename, |
| 68 | + ExtraArgs={'ACL': 'public-read'}) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + |
| 73 | + parser = ArgumentParser() |
| 74 | + parser.add_argument("-t", "--templatefile", dest="templatefile", |
| 75 | + help="CF template") |
| 76 | + |
| 77 | + parser.add_argument("-z", "--zipfile", dest="zipfile", |
| 78 | + help="deployment package") |
| 79 | + |
| 80 | + parser.add_argument("-d", "--deployment", dest="deployment", default="dev", |
| 81 | + help="aws account type") |
| 82 | + |
| 83 | + args = parser.parse_args() |
| 84 | + if args.deployment == "prod": |
| 85 | + zip_bucket_prefix = "appdevzipfiles" |
| 86 | + template_bucket = "appdev-cloudformation-templates" |
| 87 | + else: |
| 88 | + zip_bucket_prefix = "appdevstore" |
| 89 | + template_bucket = "cf-templates-5d0x5unchag-us-east-1" |
| 90 | + |
| 91 | + if not os.path.isfile(args.templatefile): |
| 92 | + raise Exception("templatefile does not exists") |
| 93 | + if not os.path.isfile(args.zipfile): |
| 94 | + raise Exception("zipfile does not exists") |
| 95 | + |
| 96 | + create_buckets(zip_bucket_prefix) |
| 97 | + upload_code_in_multiple_regions(args.zipfile, zip_bucket_prefix) |
| 98 | + upload_cftemplate(args.templatefile, template_bucket) |
| 99 | + print("Deployment Successfull: ALL files copied to %s" % args.deployment) |
0 commit comments