sst 2.0.13 → 2.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,130 +0,0 @@
1
- import subprocess
2
- import os
3
- import tempfile
4
- import json
5
- import glob
6
- import traceback
7
- import logging
8
- import shutil
9
- import boto3
10
- import contextlib
11
- import asyncio
12
- import functools
13
- from datetime import datetime
14
- from uuid import uuid4
15
-
16
- from urllib.request import Request, urlopen
17
- from zipfile import ZipFile
18
-
19
- logger = logging.getLogger()
20
- logger.setLevel(logging.INFO)
21
-
22
- s3 = boto3.resource('s3')
23
- awslambda = boto3.client('lambda')
24
- cloudfront = boto3.client('cloudfront')
25
-
26
- CFN_SUCCESS = "SUCCESS"
27
- CFN_FAILED = "FAILED"
28
-
29
- def handler(event, context):
30
-
31
- def cfn_error(message=None):
32
- logger.error("| cfn_error: %s" % message)
33
- cfn_send(event, context, CFN_FAILED, reason=message)
34
-
35
- try:
36
- logger.info(event)
37
-
38
- # cloudformation request type (create/update/delete)
39
- request_type = event['RequestType']
40
-
41
- # extract resource properties
42
- props = event['ResourceProperties']
43
- old_props = event.get('OldResourceProperties', {})
44
- physical_id = event.get('PhysicalResourceId', None)
45
-
46
- try:
47
- distribution_id = props.get('DistributionId', '')
48
- distribution_paths = props.get('DistributionPaths', ['/*'])
49
- wait_for_invalidation = props.get('WaitForInvalidation', 'true')
50
- except KeyError as e:
51
- cfn_error("missing request resource property %s. props: %s" % (str(e), props))
52
- return
53
-
54
- # if we are creating a new resource, allocate a physical id for it
55
- # otherwise, we expect physical id to be relayed by cloudformation
56
- if request_type == "Create":
57
- physical_id = "aws.cdk.s3deployment.%s" % str(uuid4())
58
- else:
59
- if not physical_id:
60
- cfn_error("invalid request: request type is '%s' but 'PhysicalResourceId' is not defined" % request_type)
61
- return
62
-
63
- if request_type == "Update" or request_type == "Create":
64
- if distribution_id:
65
- cloudfront_invalidate(distribution_id, distribution_paths, wait_for_invalidation)
66
-
67
- cfn_send(event, context, CFN_SUCCESS, physicalResourceId=physical_id)
68
- except KeyError as e:
69
- cfn_error("invalid request. Missing key %s" % str(e))
70
- except Exception as e:
71
- logger.exception(e)
72
- cfn_error(str(e))
73
-
74
- #---------------------------------------------------------------------------------------------------
75
- # invalidate files in the CloudFront distribution edge caches
76
- def cloudfront_invalidate(distribution_id, distribution_paths, wait_for_invalidation):
77
- invalidation_resp = cloudfront.create_invalidation(
78
- DistributionId=distribution_id,
79
- InvalidationBatch={
80
- 'Paths': {
81
- 'Quantity': len(distribution_paths),
82
- 'Items': distribution_paths
83
- },
84
- 'CallerReference': str(uuid4()),
85
- })
86
- # by default, will wait up to 10 minutes
87
- if wait_for_invalidation == "true":
88
- cloudfront.get_waiter('invalidation_completed').wait(
89
- DistributionId=distribution_id,
90
- Id=invalidation_resp['Invalidation']['Id'])
91
-
92
- #---------------------------------------------------------------------------------------------------
93
- # executes an "aws" cli command
94
- def aws_command(*args):
95
- aws="/opt/awscli/aws" # from AwsCliLayer
96
- logger.info("| aws %s" % ' '.join(args))
97
- subprocess.check_call([aws] + list(args))
98
-
99
- #---------------------------------------------------------------------------------------------------
100
- # sends a response to cloudformation
101
- def cfn_send(event, context, responseStatus, responseData={}, physicalResourceId=None, noEcho=False, reason=None):
102
-
103
- responseUrl = event['ResponseURL']
104
- logger.info(responseUrl)
105
-
106
- responseBody = {}
107
- responseBody['Status'] = responseStatus
108
- responseBody['Reason'] = reason or ('See the details in CloudWatch Log Stream: ' + context.log_stream_name)
109
- responseBody['PhysicalResourceId'] = physicalResourceId or context.log_stream_name
110
- responseBody['StackId'] = event['StackId']
111
- responseBody['RequestId'] = event['RequestId']
112
- responseBody['LogicalResourceId'] = event['LogicalResourceId']
113
- responseBody['NoEcho'] = noEcho
114
- responseBody['Data'] = responseData
115
-
116
- body = json.dumps(responseBody)
117
- logger.info("| response body:\n" + body)
118
-
119
- headers = {
120
- 'content-type' : '',
121
- 'content-length' : str(len(body))
122
- }
123
-
124
- try:
125
- request = Request(responseUrl, method='PUT', data=bytes(body.encode('utf-8')), headers=headers)
126
- with contextlib.closing(urlopen(request)) as response:
127
- logger.info("| status code: " + response.reason)
128
- except Exception as e:
129
- logger.error("| unable to send response to CloudFormation")
130
- logger.exception(e)