Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. import boto3, sys, os, time
  5. from botocore.exceptions import ClientError
  6.  
  7. def fail(msg):
  8.     if os.name != "nt":
  9.         print("\033[91m* " + msg + "\033[0m")
  10.     else:
  11.         print("[ERROR] " + msg)
  12.  
  13. def success(msg):
  14.     if os.name != "nt":
  15.         print("\033[92m* " + msg + "\033[0m")
  16.     else:
  17.         print("[SUCCESS] " + msg)
  18.  
  19. def info(msg):
  20.     if os.name != "nt":
  21.         print("\033[94m* " + msg + "\033[0m")
  22.     else:
  23.         print("[INFO] " + msg)
  24.  
  25. class CloudFront:
  26.     def __init__(self):
  27.         ''' CloudFront Constructor '''
  28.  
  29.     def update_distribution(self, distribution_id):
  30.         print("Updating distribution %s..." % distribution_id)
  31.         try:
  32.             cf = boto3.client('cloudfront')
  33.             response = cf.get_distribution(Id=distribution_id)
  34.  
  35.             name = 'test-svc'
  36.             is_compressed = True
  37.             caller_id = "test"
  38.  
  39.             CF_CONFIG = dict(
  40.                 CallerReference = caller_id,
  41.                 Comment = "testing",
  42.                 Enabled = True,
  43.                 Aliases = dict(Quantity = 0),
  44.                 Logging = dict(
  45.                     Enabled = False,
  46.                     IncludeCookies = False,
  47.                     Bucket = '',
  48.                     Prefix = ''
  49.                 ),
  50.                 PriceClass = 'PriceClass_All',
  51.                 DefaultRootObject = '',
  52.                 WebACLId = '',
  53.                 HttpVersion = 'http2',
  54.                 Origins = dict(
  55.                   Quantity = 3,
  56.                     Items = [
  57.                         dict(
  58.                             Id = 'S3-%s.sw.stg'% name,
  59.                             DomainName = '%s.s3.amazonaws.com'% name, #
  60.                             S3OriginConfig = dict(OriginAccessIdentity = ''),
  61.                             OriginCustomHeaders = ''
  62.                         ),
  63.                         dict(
  64.                             Id = 'Custom-cdashboard-%s-int.linkplatforms.com'% name,
  65.                             DomainName = 'cdashboard-%s-int.linkplatforms.com'% name,
  66.                             CustomOriginConfig = dict(
  67.                                 OriginProtocolPolicy = "https-only",
  68.                                 HTTPPort = 80,
  69.                                 HTTPSPort = 443
  70.                             )
  71.                         ),
  72.                         dict(
  73.                             Id = 'Custom-c-%s-int.linkplatforms.com'% name,
  74.                             DomainName = 'c-%s-int.linkplatforms.com'% name,
  75.                             CustomOriginConfig = dict(
  76.                                 OriginProtocolPolicy = "https-only",
  77.                                 HTTPPort = 80,
  78.                                 HTTPSPort = 443
  79.                             )
  80.                         )
  81.                     ]
  82.                 ),
  83.                 DefaultCacheBehavior = dict(
  84.                     TargetOriginId = 'S3-%s.sw.stg'% name,
  85.                     ViewerProtocolPolicy = 'redirect-to-https', # 'redirect-to-https' | 'https-only' | 'allow-all'
  86.                     TrustedSigners = dict(Quantity=0, Enabled=False),
  87.                     ForwardedValues = dict(
  88.                         Cookies = {'Forward':'all'},
  89.                         Headers = dict(
  90.                             Quantity=3,
  91.                             Items = [
  92.                                 "Access-Control-Request-Headers",
  93.                                 "Access-Control-Request-Method",
  94.                                 "Origin"
  95.                             ]
  96.                         ),
  97.                         QueryString = False,
  98.                         QueryStringCacheKeys = dict(Quantity=0)
  99.                     ),
  100.                     MinTTL = 0,
  101.                     Compress = is_compressed,
  102.                     AllowedMethods = dict(
  103.                         Quantity=3,
  104.                         Items = ['GET', 'HEAD', 'OPTIONS'] # temp fixed
  105.                     )
  106.                 ),
  107.                 CacheBehaviors = dict(
  108.                     Quantity = 2,
  109.                     Items = [
  110.                         dict(
  111.                             TargetOriginId = 'Custom-cdashboard-%s-int.linkplatforms.com'% name,
  112.                             PathPattern = 'assets/downloads/*',
  113.                             ViewerProtocolPolicy = 'redirect-to-https',
  114.                             TrustedSigners = dict(Quantity=0, Enabled=False),
  115.                             ForwardedValues = dict(
  116.                                 Cookies = {'Forward':'all'},
  117.                                 Headers = dict(
  118.                                     Quantity=3,
  119.                                     Items = [
  120.                                         "Access-Control-Request-Headers",
  121.                                         "Access-Control-Request-Method",
  122.                                         "Origin"
  123.                                     ]
  124.                                 ),
  125.                                 QueryString = False,
  126.                                 QueryStringCacheKeys = dict(Quantity=0)
  127.                             ),
  128.                             MinTTL = 0,
  129.                             Compress = is_compressed,
  130.                             AllowedMethods = dict(
  131.                                 Quantity=3,
  132.                                 Items = ['GET', 'HEAD', 'OPTIONS'] # temp fixed
  133.                             )
  134.                         ),
  135.                         dict(
  136.                             TargetOriginId = 'Custom-c-%s-int.linkplatforms.com'% name,
  137.                             PathPattern = 'web/assets/*',
  138.                             ViewerProtocolPolicy = 'redirect-to-https',
  139.                             TrustedSigners = dict(Quantity=0, Enabled=False),
  140.                             ForwardedValues = dict(
  141.                                 Cookies = {'Forward':'all'},
  142.                                 Headers = dict(
  143.                                     Quantity=3,
  144.                                     Items = [
  145.                                         "Access-Control-Request-Headers",
  146.                                         "Access-Control-Request-Method",
  147.                                         "Origin"
  148.                                     ]
  149.                                 ),
  150.                                 QueryString = False,
  151.                                 QueryStringCacheKeys = dict(Quantity=0)
  152.                             ),
  153.                             MinTTL = 0,
  154.                             Compress = is_compressed,
  155.                             AllowedMethods = dict(
  156.                                 Quantity=3,
  157.                                 Items = ['GET', 'HEAD', 'OPTIONS'] # temp fixed
  158.                             )
  159.                         )
  160.                     ]
  161.                 )
  162.             )
  163.             cf.update_distribution(DistributionConfig=CF_CONFIG, Id=response['Distribution']['Id'], IfMatch=response['ETag'])
  164.  
  165.             distribution_id = response['Distribution']['Id']
  166.             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] ")
  167.             if choice.lower() == 'yes' or choice.lower() == 'y':
  168.                 waiter = cf.get_waiter('distribution_deployed')
  169.                 waiter.wait(Id=distribution_id)
  170.             print("=> Disitribution %s was updated successfully !" % distribution_id)
  171.             return True
  172.         except:
  173.             a, b, c = sys.exc_info()
  174.             fail("Could not update distribution: " + str(b))
  175.             return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement