-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathpush.py
286 lines (268 loc) · 10.4 KB
/
push.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
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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 os
import sys
import zipfile
import tempfile
import contextlib
from datetime import datetime
from botocore.exceptions import ClientError
from awscli.customizations.codedeploy.utils import validate_s3_location
from awscli.customizations.commands import BasicCommand
from awscli.compat import BytesIO, ZIP_COMPRESSION_MODE
ONE_MB = 1 << 20
MULTIPART_LIMIT = 6 * ONE_MB
class Push(BasicCommand):
NAME = 'push'
DESCRIPTION = (
'Bundles and uploads to Amazon Simple Storage Service (Amazon S3) an '
'application revision, which is a zip archive file that contains '
'deployable content and an accompanying Application Specification '
'file (AppSpec file). If the upload is successful, a message is '
'returned that describes how to call the create-deployment command to '
'deploy the application revision from Amazon S3 to target Amazon '
'Elastic Compute Cloud (Amazon EC2) instances.'
)
ARG_TABLE = [
{
'name': 'application-name',
'synopsis': '--application-name <app-name>',
'required': True,
'help_text': (
'Required. The name of the AWS CodeDeploy application to be '
'associated with the application revision.'
)
},
{
'name': 's3-location',
'synopsis': '--s3-location s3://<bucket>/<key>',
'required': True,
'help_text': (
'Required. Information about the location of the application '
'revision to be uploaded to Amazon S3. You must specify both '
'a bucket and a key that represent the Amazon S3 bucket name '
'and the object key name. Content will be zipped before '
'uploading. Use the format s3://<bucket>/<key>'
),
},
{
'name': 'ignore-hidden-files',
'action': 'store_true',
'default': False,
'group_name': 'ignore-hidden-files',
'help_text': (
'Optional. Set the --ignore-hidden-files flag to not bundle '
'and upload hidden files to Amazon S3; otherwise, set the '
'--no-ignore-hidden-files flag (the default) to bundle and '
'upload hidden files to Amazon S3.'
)
},
{
'name': 'no-ignore-hidden-files',
'action': 'store_true',
'default': False,
'group_name': 'ignore-hidden-files'
},
{
'name': 'source',
'synopsis': '--source <path>',
'default': '.',
'help_text': (
'Optional. The location of the deployable content and the '
'accompanying AppSpec file on the development machine to be '
'zipped and uploaded to Amazon S3. If not specified, the '
'current directory is used.'
)
},
{
'name': 'description',
'synopsis': '--description <description>',
'help_text': (
'Optional. A comment that summarizes the application '
'revision. If not specified, the default string "Uploaded by '
'AWS CLI \'time\' UTC" is used, where \'time\' is the current '
'system time in Coordinated Universal Time (UTC).'
)
}
]
def _run_main(self, parsed_args, parsed_globals):
self._validate_args(parsed_args)
self.codedeploy = self._session.create_client(
'codedeploy',
region_name=parsed_globals.region,
endpoint_url=parsed_globals.endpoint_url,
verify=parsed_globals.verify_ssl
)
self.s3 = self._session.create_client(
's3',
region_name=parsed_globals.region
)
self._push(parsed_args)
def _validate_args(self, parsed_args):
validate_s3_location(parsed_args, 's3_location')
if parsed_args.ignore_hidden_files \
and parsed_args.no_ignore_hidden_files:
raise RuntimeError(
'You cannot specify both --ignore-hidden-files and '
'--no-ignore-hidden-files.'
)
if not parsed_args.description:
parsed_args.description = (
'Uploaded by AWS CLI {0} UTC'.format(
datetime.utcnow().isoformat()
)
)
def _push(self, params):
with self._compress(
params.source,
params.ignore_hidden_files
) as bundle:
try:
upload_response = self._upload_to_s3(params, bundle)
params.eTag = upload_response['ETag'].replace('"', "")
if 'VersionId' in upload_response:
params.version = upload_response['VersionId']
except Exception as e:
raise RuntimeError(
'Failed to upload \'%s\' to \'%s\': %s' %
(params.source,
params.s3_location,
str(e))
)
self._register_revision(params)
if 'version' in params:
version_string = ',version={0}'.format(params.version)
else:
version_string = ''
s3location_string = (
'--s3-location bucket={0},key={1},'
'bundleType=zip,eTag={2}{3}'.format(
params.bucket,
params.key,
params.eTag,
version_string
)
)
sys.stdout.write(
'To deploy with this revision, run:\n'
'aws deploy create-deployment '
'--application-name {0} {1} '
'--deployment-group-name <deployment-group-name> '
'--deployment-config-name <deployment-config-name> '
'--description <description>\n'.format(
params.application_name,
s3location_string
)
)
@contextlib.contextmanager
def _compress(self, source, ignore_hidden_files=False):
source_path = os.path.abspath(source)
appspec_path = os.path.sep.join([source_path, 'appspec.yml'])
with tempfile.TemporaryFile('w+b') as tf:
zf = zipfile.ZipFile(tf, 'w', allowZip64=True)
# Using 'try'/'finally' instead of 'with' statement since ZipFile
# does not have support context manager in Python 2.6.
try:
contains_appspec = False
for root, dirs, files in os.walk(source, topdown=True):
if ignore_hidden_files:
files = [fn for fn in files if not fn.startswith('.')]
dirs[:] = [dn for dn in dirs if not dn.startswith('.')]
for fn in files:
filename = os.path.join(root, fn)
filename = os.path.abspath(filename)
arcname = filename[len(source_path) + 1:]
if filename == appspec_path:
contains_appspec = True
zf.write(filename, arcname, ZIP_COMPRESSION_MODE)
if not contains_appspec:
raise RuntimeError(
'{0} was not found'.format(appspec_path)
)
finally:
zf.close()
yield tf
def _upload_to_s3(self, params, bundle):
size_remaining = self._bundle_size(bundle)
if size_remaining < MULTIPART_LIMIT:
return self.s3.put_object(
Bucket=params.bucket,
Key=params.key,
Body=bundle
)
else:
return self._multipart_upload_to_s3(
params,
bundle,
size_remaining
)
def _bundle_size(self, bundle):
bundle.seek(0, 2)
size = bundle.tell()
bundle.seek(0)
return size
def _multipart_upload_to_s3(self, params, bundle, size_remaining):
create_response = self.s3.create_multipart_upload(
Bucket=params.bucket,
Key=params.key
)
upload_id = create_response['UploadId']
try:
part_num = 1
multipart_list = []
bundle.seek(0)
while size_remaining > 0:
data = bundle.read(MULTIPART_LIMIT)
upload_response = self.s3.upload_part(
Bucket=params.bucket,
Key=params.key,
UploadId=upload_id,
PartNumber=part_num,
Body=BytesIO(data)
)
multipart_list.append({
'PartNumber': part_num,
'ETag': upload_response['ETag']
})
part_num += 1
size_remaining -= len(data)
return self.s3.complete_multipart_upload(
Bucket=params.bucket,
Key=params.key,
UploadId=upload_id,
MultipartUpload={'Parts': multipart_list}
)
except ClientError as e:
self.s3.abort_multipart_upload(
Bucket=params.bucket,
Key=params.key,
UploadId=upload_id
)
raise e
def _register_revision(self, params):
revision = {
'revisionType': 'S3',
's3Location': {
'bucket': params.bucket,
'key': params.key,
'bundleType': 'zip',
'eTag': params.eTag
}
}
if 'version' in params:
revision['s3Location']['version'] = params.version
self.codedeploy.register_application_revision(
applicationName=params.application_name,
revision=revision,
description=params.description
)