Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # coding: utf-8
- import boto3, sys, os, time
- from botocore.exceptions import ClientError
- def fail(msg):
- if os.name != "nt":
- print("\033[91m* " + msg + "\033[0m")
- else:
- print("[ERROR] " + msg)
- def success(msg):
- if os.name != "nt":
- print("\033[92m* " + msg + "\033[0m")
- else:
- print("[SUCCESS] " + msg)
- def info(msg):
- if os.name != "nt":
- print("\033[94m* " + msg + "\033[0m")
- else:
- print("[INFO] " + msg)
- class CloudFront:
- def __init__(self):
- ''' CloudFront Constructor '''
- def update_distribution(self, distribution_id):
- print("Updating distribution %s..." % distribution_id)
- try:
- cf = boto3.client('cloudfront')
- response = cf.get_distribution(Id=distribution_id)
- name = 'test-svc'
- is_compressed = True
- caller_id = "test"
- CF_CONFIG = dict(
- CallerReference = caller_id,
- Comment = "testing",
- Enabled = True,
- Aliases = dict(Quantity = 0),
- Logging = dict(
- Enabled = False,
- IncludeCookies = False,
- Bucket = '',
- Prefix = ''
- ),
- PriceClass = 'PriceClass_All',
- DefaultRootObject = '',
- WebACLId = '',
- HttpVersion = 'http2',
- Origins = dict(
- Quantity = 3,
- Items = [
- dict(
- Id = 'S3-%s.sw.stg'% name,
- DomainName = '%s.s3.amazonaws.com'% name, #
- S3OriginConfig = dict(OriginAccessIdentity = ''),
- OriginCustomHeaders = ''
- ),
- dict(
- Id = 'Custom-cdashboard-%s-int.linkplatforms.com'% name,
- DomainName = 'cdashboard-%s-int.linkplatforms.com'% name,
- CustomOriginConfig = dict(
- OriginProtocolPolicy = "https-only",
- HTTPPort = 80,
- HTTPSPort = 443
- )
- ),
- dict(
- Id = 'Custom-c-%s-int.linkplatforms.com'% name,
- DomainName = 'c-%s-int.linkplatforms.com'% name,
- CustomOriginConfig = dict(
- OriginProtocolPolicy = "https-only",
- HTTPPort = 80,
- HTTPSPort = 443
- )
- )
- ]
- ),
- DefaultCacheBehavior = dict(
- TargetOriginId = 'S3-%s.sw.stg'% name,
- ViewerProtocolPolicy = 'redirect-to-https', # 'redirect-to-https' | 'https-only' | 'allow-all'
- TrustedSigners = dict(Quantity=0, Enabled=False),
- ForwardedValues = dict(
- Cookies = {'Forward':'all'},
- Headers = dict(
- Quantity=3,
- Items = [
- "Access-Control-Request-Headers",
- "Access-Control-Request-Method",
- "Origin"
- ]
- ),
- QueryString = False,
- QueryStringCacheKeys = dict(Quantity=0)
- ),
- MinTTL = 0,
- Compress = is_compressed,
- AllowedMethods = dict(
- Quantity=3,
- Items = ['GET', 'HEAD', 'OPTIONS'] # temp fixed
- )
- ),
- CacheBehaviors = dict(
- Quantity = 2,
- Items = [
- dict(
- TargetOriginId = 'Custom-cdashboard-%s-int.linkplatforms.com'% name,
- PathPattern = 'assets/downloads/*',
- ViewerProtocolPolicy = 'redirect-to-https',
- TrustedSigners = dict(Quantity=0, Enabled=False),
- ForwardedValues = dict(
- Cookies = {'Forward':'all'},
- Headers = dict(
- Quantity=3,
- Items = [
- "Access-Control-Request-Headers",
- "Access-Control-Request-Method",
- "Origin"
- ]
- ),
- QueryString = False,
- QueryStringCacheKeys = dict(Quantity=0)
- ),
- MinTTL = 0,
- Compress = is_compressed,
- AllowedMethods = dict(
- Quantity=3,
- Items = ['GET', 'HEAD', 'OPTIONS'] # temp fixed
- )
- ),
- dict(
- TargetOriginId = 'Custom-c-%s-int.linkplatforms.com'% name,
- PathPattern = 'web/assets/*',
- ViewerProtocolPolicy = 'redirect-to-https',
- TrustedSigners = dict(Quantity=0, Enabled=False),
- ForwardedValues = dict(
- Cookies = {'Forward':'all'},
- Headers = dict(
- Quantity=3,
- Items = [
- "Access-Control-Request-Headers",
- "Access-Control-Request-Method",
- "Origin"
- ]
- ),
- QueryString = False,
- QueryStringCacheKeys = dict(Quantity=0)
- ),
- MinTTL = 0,
- Compress = is_compressed,
- AllowedMethods = dict(
- Quantity=3,
- Items = ['GET', 'HEAD', 'OPTIONS'] # temp fixed
- )
- )
- ]
- )
- )
- cf.update_distribution(DistributionConfig=CF_CONFIG, Id=response['Distribution']['Id'], IfMatch=response['ETag'])
- distribution_id = response['Distribution']['Id']
- choice = raw_input("The process of creating new distribution will take a long time. Do you want to wait util the new disitribution is ready (status = deployed) ? [y/n] ")
- if choice.lower() == 'yes' or choice.lower() == 'y':
- waiter = cf.get_waiter('distribution_deployed')
- waiter.wait(Id=distribution_id)
- print("=> Disitribution %s was updated successfully !" % distribution_id)
- return True
- except:
- a, b, c = sys.exc_info()
- fail("Could not update distribution: " + str(b))
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement