-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathuploader.py
288 lines (253 loc) · 10.3 KB
/
uploader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright 2015-2016 Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import backoff
import boto3
import logging
from botocore.exceptions import ClientError
from os import path
LOG = logging.getLogger(__name__)
MAX_PACKAGE_SIZE = 50000000
class PackageUploader(object):
'''TODO: Should we decouple the config from the Object Init'''
def __init__(self, config, profile_name):
self._config = config
self._vpc_config = self._format_vpc_config()
self._aws_session = boto3.session.Session(region_name=config.region,
profile_name=profile_name)
self._lambda_client = self._aws_session.client('lambda')
self.version = None
'''
Calls the AWS methods to upload an existing package and update
the function configuration
returns the package version
'''
def upload_existing(self, pkg):
environment = {'Variables': self._config.variables}
if pkg:
self._validate_package_size(pkg.zip_file)
with open(pkg.zip_file, "rb") as fil:
zip_file = fil.read()
LOG.debug('running update_function_code')
conf_update_resp = None
if self._config.s3_bucket:
self._upload_s3(pkg.zip_file)
conf_update_resp = self._lambda_client.update_function_code(
FunctionName=self._config.name,
S3Bucket=self._config.s3_bucket,
S3Key=self._config.s3_package_name(),
Publish=False,
)
else:
conf_update_resp = self._lambda_client.update_function_code(
FunctionName=self._config.name,
ZipFile=zip_file,
Publish=False,
)
else:
conf_update_resp = self._lambda_client.update_function_code(
FunctionName=self._config.name,
ImageUri=self._config.image_uri,
Publish=False,
)
LOG.debug("AWS update_function_code response: %s"
% conf_update_resp)
waiter = self._lambda_client.get_waiter('function_updated')
LOG.debug("Waiting for lambda function to be updated")
waiter.wait(FunctionName=self._config.name)
@backoff.on_exception(backoff.expo, ClientError)
def update_config():
LOG.debug('running update_function_configuration')
if pkg:
response = self._lambda_client.update_function_configuration(
FunctionName=self._config.name,
Handler=self._config.handler,
Role=self._config.role,
Description=self._config.description,
Timeout=self._config.timeout,
MemorySize=self._config.memory,
VpcConfig=self._vpc_config,
Environment=environment,
TracingConfig=self._config.tracing,
Runtime=self._config.runtime,
)
else:
response = self._lambda_client.update_function_configuration(
FunctionName=self._config.name,
Role=self._config.role,
Description=self._config.description,
Timeout=self._config.timeout,
MemorySize=self._config.memory,
VpcConfig=self._vpc_config,
Environment=environment,
TracingConfig=self._config.tracing
)
LOG.debug("AWS update_function_configuration response: %s"
% response)
return response
version = update_config().get('Version')
@backoff.on_exception(backoff.expo, ClientError)
def publish():
# Publish the version after upload and config update if needed
waiter = self._lambda_client.get_waiter(
'function_updated')
LOG.debug("Waiting for lambda function to be updated")
waiter.wait(FunctionName=self._config.name)
resp = self._lambda_client.publish_version(
FunctionName=self._config.name,
)
LOG.debug("AWS publish_version response: %s" % resp)
return resp.get('Version')
if self._config.publish:
version = publish()
return version
'''
Creates and uploads a new lambda function
returns the package version
'''
def upload_new(self, pkg):
environment = {'Variables': self._config.variables}
code = {}
if pkg:
if self._config.s3_bucket:
code = {'S3Bucket': self._config.s3_bucket,
'S3Key': self._config.s3_package_name()}
self._upload_s3(pkg.zip_file)
else:
self._validate_package_size(pkg.zip_file)
with open(pkg.zip_file, "rb") as fil:
zip_file = fil.read()
code = {'ZipFile': zip_file}
else:
code = {'ImageUri': self._config.image_uri}
LOG.debug('running create_function_code')
if pkg:
response = self._lambda_client.create_function(
FunctionName=self._config.name,
Runtime=self._config.runtime,
Handler=self._config.handler,
Role=self._config.role,
Code=code,
Description=self._config.description,
Timeout=self._config.timeout,
MemorySize=self._config.memory,
Publish=self._config.publish,
VpcConfig=self._vpc_config,
Environment=environment,
TracingConfig=self._config.tracing,
)
else:
response = self._lambda_client.create_function(
FunctionName=self._config.name,
Role=self._config.role,
Code=code,
Description=self._config.description,
Timeout=self._config.timeout,
MemorySize=self._config.memory,
Publish=self._config.publish,
VpcConfig=self._vpc_config,
Environment=environment,
TracingConfig=self._config.tracing,
PackageType='Image'
)
LOG.debug("AWS create_function response: %s" % response)
return response.get('Version')
'''
Auto determines whether the function exists or not and calls
the appropriate method (upload_existing or upload_new).
'''
def upload(self, pkg):
existing_function = True
try:
get_resp = self._lambda_client.get_function_configuration(
FunctionName=self._config.name)
LOG.debug("AWS get_function_configuration response: %s" % get_resp)
except: # noqa: E722
existing_function = False
LOG.debug("function not found creating new function")
if existing_function:
self.version = self.upload_existing(pkg)
else:
self.version = self.upload_new(pkg)
'''
Create/update an alias to point to the package. Raises an
exception if the package has not been uploaded.
'''
def alias(self):
# if self.version is still None raise exception
if self.version is None:
raise Exception('Must upload package before applying alias')
if self._alias_exists():
self._update_alias()
else:
self._create_alias()
'''
Pulls down the current list of aliases and checks to see if
an alias exists.
'''
def _alias_exists(self):
resp = self._lambda_client.list_aliases(
FunctionName=self._config.name)
for alias in resp.get('Aliases'):
if alias.get('Name') == self._config.alias:
return True
return False
'''Creates alias'''
def _create_alias(self):
LOG.debug("Creating new alias %s" % self._config.alias)
resp = self._lambda_client.create_alias(
FunctionName=self._config.name,
Name=self._config.alias,
FunctionVersion=self.version,
Description=self._config.alias_description,
)
LOG.debug("AWS create_alias response: %s" % resp)
'''Update alias'''
def _update_alias(self):
LOG.debug("Updating alias %s" % self._config.alias)
resp = self._lambda_client.update_alias(
FunctionName=self._config.name,
Name=self._config.alias,
FunctionVersion=self.version,
Description=self._config.alias_description,
)
LOG.debug("AWS update_alias response: %s" % resp)
def _validate_package_size(self, pkg):
'''
Logs a warning if the package size is over the current max package size
'''
if path.getsize(pkg) > MAX_PACKAGE_SIZE:
LOG.warning("Size of your deployment package is larger than 50MB!")
def _format_vpc_config(self):
'''
Returns {} if the VPC config is set to None by Config,
returns the formatted config otherwise
'''
if self._config.raw['vpc']:
return {
'SubnetIds': self._config.raw['vpc']['subnets'],
'SecurityGroupIds': self._config.raw['vpc']['security_groups']
}
else:
return {
'SubnetIds': [],
'SecurityGroupIds': [],
}
def _upload_s3(self, zip_file):
'''
Uploads the lambda package to s3
'''
s3_client = self._aws_session.client('s3')
transfer = boto3.s3.transfer.S3Transfer(s3_client)
transfer.upload_file(zip_file, self._config.s3_bucket,
self._config.s3_package_name())