-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcommand.py
More file actions
403 lines (383 loc) · 13.6 KB
/
Copy pathcommand.py
File metadata and controls
403 lines (383 loc) · 13.6 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""
CLI command for "deploy" command
"""
import logging
import os
import click
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import aws_creds_options, common_options, pass_context, print_cmdline_args
from samcli.commands._utils.cdk_support_decorators import unsupported_command_cdk
from samcli.commands._utils.click_mutex import ClickMutex
from samcli.commands._utils.command_exception_handler import command_exception_handler
from samcli.commands._utils.options import (
capabilities_option,
force_upload_option,
guided_deploy_stack_name,
image_repositories_option,
image_repository_option,
kms_key_id_option,
language_extensions_option,
metadata_option,
no_progressbar_option,
notification_arns_option,
parameter_override_option,
resolve_image_repos_option,
resolve_s3_option,
role_arn_option,
s3_bucket_option,
s3_prefix_option,
signing_profiles_option,
stack_name_option,
tags_option,
template_click_option,
use_json_option,
)
from samcli.commands.deploy.core.command import DeployCommand
from samcli.commands.deploy.utils import sanitize_parameter_overrides
from samcli.lib.bootstrap.bootstrap import manage_stack, print_managed_s3_bucket_info
from samcli.lib.bootstrap.companion_stack.companion_stack_manager import sync_ecr_stack
from samcli.lib.cfn_language_extensions.sam_integration import resolve_language_extensions_enabled
from samcli.lib.cli_validation.image_repository_validation import image_repository_validation
from samcli.lib.telemetry.metric import track_command
from samcli.lib.utils import osutils
from samcli.lib.utils.version_checker import check_newer_version
SHORT_HELP = "Deploy an AWS SAM application."
HELP_TEXT = """The sam deploy command creates a Cloudformation Stack and deploys your resources."""
DESCRIPTION = """
To turn on the guided interactive mode, specify the --guided option. This mode shows you the parameters
required for deployment, provides default options, and optionally saves these options in a configuration
file in your project directory. When you perform subsequent deployments of your application using sam deploy,
the AWS SAM CLI retrieves the required parameters from the configuration file.
Set SAM_CLI_POLL_DELAY Environment Variable with a value of seconds in your shell to configure
how often SAM CLI checks the Stack state, which is useful when seeing throttling from CloudFormation.
"""
CONFIG_SECTION = "parameters"
LOG = logging.getLogger(__name__)
@click.command(
"deploy",
short_help=SHORT_HELP,
context_settings={
"ignore_unknown_options": False,
"allow_interspersed_args": True,
"allow_extra_args": True,
"max_content_width": 120,
},
cls=DeployCommand,
help=HELP_TEXT,
description=DESCRIPTION,
requires_credentials=True,
)
@configuration_option(provider=ConfigProvider(section=CONFIG_SECTION))
@click.option(
"--guided",
"-g",
required=False,
is_flag=True,
is_eager=True,
help="Specify this flag to allow SAM CLI to guide you through the deployment using guided prompts.",
)
@template_click_option(include_build=True)
@click.option(
"--no-execute-changeset",
required=False,
is_flag=True,
help="Indicates whether to execute the change set. "
"Specify this flag to view stack changes before executing the change set.",
)
@click.option(
"--fail-on-empty-changeset/--no-fail-on-empty-changeset",
default=True,
required=False,
is_flag=True,
help="Specify whether AWS SAM CLI should return a non-zero exit code if there are no "
"changes to be made to the stack. Defaults to a non-zero exit code.",
)
@click.option(
"--confirm-changeset/--no-confirm-changeset",
default=False,
required=False,
is_flag=True,
help="Prompt to confirm if the computed changeset is to be deployed by SAM CLI.",
)
@click.option(
"--disable-rollback/--no-disable-rollback",
default=False,
required=False,
is_flag=True,
help="Preserves the state of previously provisioned resources when an operation fails.",
cls=ClickMutex,
incompatible_params=["on_failure"],
)
@click.option(
"--on-failure",
default="ROLLBACK",
type=click.Choice(["ROLLBACK", "DELETE", "DO_NOTHING"]),
required=False,
help="""
Provide an action to determine what will happen when a stack fails to create. Three actions are available:\n
- ROLLBACK: This will rollback a stack to a previous known good state.\n
- DELETE: The stack will rollback to a previous state if one exists, otherwise the stack will be deleted.\n
- DO_NOTHING: The stack will not rollback or delete, this is the same as disabling rollback.\n
Default behaviour is ROLLBACK.\n\n
This option is mutually exclusive with --disable-rollback/--no-disable-rollback. You can provide
--on-failure or --disable-rollback/--no-disable-rollback but not both at the same time.
""",
cls=ClickMutex,
incompatible_params=["disable_rollback", "no_disable_rollback"],
)
@click.option(
"--max-wait-duration",
default=60,
type=int,
help="Maximum duration in minutes to wait for the deployment to complete.",
)
@click.option(
"--express/--no-express",
default=False,
required=False,
is_flag=True,
help="Use CloudFormation Express mode to speed up deployments by completing once resource "
"configuration is applied, without waiting for full stabilization.",
)
@stack_name_option(callback=guided_deploy_stack_name) # pylint: disable=E1120
@s3_bucket_option(disable_callback=True) # pylint: disable=E1120
@image_repository_option
@image_repositories_option
@force_upload_option
@s3_prefix_option
@kms_key_id_option
@role_arn_option
@use_json_option
@resolve_s3_option(guided=True) # pylint: disable=E1120
@resolve_image_repos_option
@metadata_option
@notification_arns_option
@tags_option
@parameter_override_option
@signing_profiles_option
@no_progressbar_option
@capabilities_option
@language_extensions_option
@aws_creds_options
@common_options
@save_params_option
@image_repository_validation()
@pass_context
@track_command
@check_newer_version
@print_cmdline_args
@unsupported_command_cdk(alternative_command="cdk deploy")
@command_exception_handler
def cli(
ctx,
template_file,
stack_name,
s3_bucket,
image_repository,
image_repositories,
force_upload,
no_progressbar,
s3_prefix,
kms_key_id,
parameter_overrides,
capabilities,
no_execute_changeset,
role_arn,
notification_arns,
fail_on_empty_changeset,
use_json,
tags,
metadata,
guided,
confirm_changeset,
signing_profiles,
resolve_s3,
resolve_image_repos,
language_extensions,
save_params,
config_file,
config_env,
disable_rollback,
on_failure,
max_wait_duration,
express,
):
"""
`sam deploy` command entry point
"""
# All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing
do_cli(
template_file,
stack_name,
s3_bucket,
image_repository,
image_repositories,
force_upload,
no_progressbar,
s3_prefix,
kms_key_id,
parameter_overrides,
capabilities,
no_execute_changeset,
role_arn,
notification_arns,
fail_on_empty_changeset,
use_json,
tags,
metadata,
guided,
confirm_changeset,
ctx.region,
ctx.profile,
signing_profiles,
resolve_s3,
config_file,
config_env,
resolve_image_repos,
language_extensions,
disable_rollback,
on_failure,
max_wait_duration,
express,
) # pragma: no cover
def do_cli(
template_file,
stack_name,
s3_bucket,
image_repository,
image_repositories,
force_upload,
no_progressbar,
s3_prefix,
kms_key_id,
parameter_overrides,
capabilities,
no_execute_changeset,
role_arn,
notification_arns,
fail_on_empty_changeset,
use_json,
tags,
metadata,
guided,
confirm_changeset,
region,
profile,
signing_profiles,
resolve_s3,
config_file,
config_env,
resolve_image_repos,
language_extensions,
disable_rollback,
on_failure,
max_wait_duration,
express,
):
"""
Implementation of the ``cli`` method
"""
from samcli.commands.deploy.deploy_context import DeployContext
from samcli.commands.deploy.exceptions import DeployResolveS3AndS3SetError
from samcli.commands.deploy.guided_context import GuidedContext
from samcli.commands.package.package_context import PackageContext
language_extensions_enabled = resolve_language_extensions_enabled(language_extensions)
if guided:
# Allow for a guided deploy to prompt and save those details.
guided_context = GuidedContext(
template_file=template_file,
stack_name=stack_name,
s3_bucket=s3_bucket,
image_repository=image_repository,
image_repositories=image_repositories,
resolve_s3=resolve_s3,
resolve_image_repos=resolve_image_repos,
s3_prefix=s3_prefix,
region=region,
profile=profile,
confirm_changeset=confirm_changeset,
capabilities=capabilities,
signing_profiles=signing_profiles,
parameter_overrides=parameter_overrides,
config_section=CONFIG_SECTION,
config_env=config_env,
config_file=config_file,
disable_rollback=disable_rollback,
language_extensions_enabled=language_extensions_enabled,
)
guided_context.run()
else:
if resolve_s3:
if bool(s3_bucket):
raise DeployResolveS3AndS3SetError()
s3_bucket = manage_stack(profile=profile, region=region)
print_managed_s3_bucket_info(s3_bucket)
# TODO Refactor resolve-s3 and resolve-image-repos into one place
# after we figure out how to enable resolve-images-repos in package
if resolve_image_repos:
image_repositories = sync_ecr_stack(
template_file, stack_name, region, s3_bucket, s3_prefix, image_repositories
)
with osutils.tempfile_platform_independent() as output_template_file:
if guided:
context_param_overrides = sanitize_parameter_overrides(guided_context.guided_parameter_overrides)
else:
context_param_overrides = parameter_overrides
with PackageContext(
template_file=template_file,
s3_bucket=guided_context.guided_s3_bucket if guided else s3_bucket,
s3_prefix=guided_context.guided_s3_prefix if guided else s3_prefix,
image_repository=guided_context.guided_image_repository if guided else image_repository,
image_repositories=guided_context.guided_image_repositories if guided else image_repositories,
output_template_file=output_template_file.name,
kms_key_id=kms_key_id,
use_json=use_json,
force_upload=force_upload,
no_progressbar=no_progressbar,
metadata=metadata,
on_deploy=True,
region=guided_context.guided_region if guided else region,
profile=profile,
signing_profiles=guided_context.signing_profiles if guided else signing_profiles,
parameter_overrides=context_param_overrides,
language_extensions=language_extensions,
) as package_context:
package_context.run()
# 5s of sleep time between stack checks and describe stack events.
DEFAULT_POLL_DELAY = 5
try:
poll_delay = float(os.getenv("SAM_CLI_POLL_DELAY", str(DEFAULT_POLL_DELAY)))
except ValueError:
poll_delay = DEFAULT_POLL_DELAY
if poll_delay <= 0:
poll_delay = DEFAULT_POLL_DELAY
with DeployContext(
template_file=output_template_file.name,
stack_name=guided_context.guided_stack_name if guided else stack_name,
s3_bucket=guided_context.guided_s3_bucket if guided else s3_bucket,
image_repository=guided_context.guided_image_repository if guided else image_repository,
image_repositories=guided_context.guided_image_repositories if guided else image_repositories,
force_upload=force_upload,
no_progressbar=no_progressbar,
s3_prefix=guided_context.guided_s3_prefix if guided else s3_prefix,
kms_key_id=kms_key_id,
parameter_overrides=context_param_overrides,
capabilities=guided_context.guided_capabilities if guided else capabilities,
no_execute_changeset=no_execute_changeset,
role_arn=role_arn,
notification_arns=notification_arns,
fail_on_empty_changeset=fail_on_empty_changeset,
tags=tags,
region=guided_context.guided_region if guided else region,
profile=profile,
confirm_changeset=guided_context.confirm_changeset if guided else confirm_changeset,
signing_profiles=guided_context.signing_profiles if guided else signing_profiles,
use_changeset=True,
disable_rollback=guided_context.disable_rollback if guided else disable_rollback,
poll_delay=poll_delay,
on_failure=on_failure,
max_wait_duration=max_wait_duration,
language_extensions=language_extensions,
express=express,
) as deploy_context:
deploy_context.run()