-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
command.py
581 lines (533 loc) · 20.8 KB
/
command.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"""CLI command for "sync" command."""
import logging
import os
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple
import click
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.context import Context
from samcli.cli.main import aws_creds_options, pass_context, print_cmdline_args
from samcli.cli.main import common_options as cli_framework_options
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.constants import (
DEFAULT_BUILD_DIR,
DEFAULT_BUILD_DIR_WITH_AUTO_DEPENDENCY_LAYER,
DEFAULT_CACHE_DIR,
)
from samcli.commands._utils.custom_options.replace_help_option import ReplaceHelpSummaryOption
from samcli.commands._utils.option_value_processor import process_image_options
from samcli.commands._utils.options import (
base_dir_option,
build_image_option,
build_in_source_option,
capabilities_option,
image_repositories_option,
image_repository_option,
kms_key_id_option,
metadata_option,
notification_arns_option,
parameter_override_option,
role_arn_option,
s3_bucket_option,
s3_prefix_option,
stack_name_option,
tags_option,
template_option_without_build,
use_container_build_option,
watch_exclude_option,
)
from samcli.commands.build.click_container import ContainerOptions
from samcli.commands.build.command import _get_mode_value_from_envvar
from samcli.commands.sync.core.command import SyncCommand
from samcli.commands.sync.sync_context import SyncContext
from samcli.lib.bootstrap.bootstrap import manage_stack
from samcli.lib.build.bundler import EsbuildBundlerManager
from samcli.lib.cli_validation.image_repository_validation import image_repository_validation
from samcli.lib.providers.provider import (
ResourceIdentifier,
get_all_resource_ids,
get_unique_resource_ids,
)
from samcli.lib.providers.sam_stack_provider import SamLocalStackProvider
from samcli.lib.sync.infra_sync_executor import InfraSyncExecutor, InfraSyncResult
from samcli.lib.sync.sync_flow_executor import SyncFlowExecutor
from samcli.lib.sync.sync_flow_factory import SyncCodeResources, SyncFlowFactory
from samcli.lib.sync.watch_manager import WatchManager
from samcli.lib.telemetry.event import EventTracker, track_long_event
from samcli.lib.telemetry.metric import track_command, track_template_warnings
from samcli.lib.utils.colors import Colored
from samcli.lib.utils.version_checker import check_newer_version
from samcli.lib.warnings.sam_cli_warning import CodeDeployConditionWarning, CodeDeployWarning
if TYPE_CHECKING: # pragma: no cover
from samcli.commands.build.build_context import BuildContext
from samcli.commands.deploy.deploy_context import DeployContext
from samcli.commands.package.package_context import PackageContext
LOG = logging.getLogger(__name__)
HELP_TEXT = """
NEW! Sync an AWS SAM Project to AWS.
"""
DESCRIPTION = """
By default, `$sam sync` runs a full AWS Cloudformation stack update.
Running `sam sync --watch` with `--code` will provide a way to run just code
synchronization, speeding up start time skipping template changes.
Remember to update the deployed stack by running
without --code for infrastructure changes.
`$sam sync` also supports nested stacks and nested stack resources.
"""
SYNC_INFO_TEXT = """
The SAM CLI will use the AWS Lambda, Amazon API Gateway, and AWS StepFunctions APIs to upload your code without
performing a CloudFormation deployment. This will cause drift in your CloudFormation stack.
**The sync command should only be used against a development stack**.
"""
SYNC_CONFIRMATION_TEXT = """
Confirm that you are synchronizing a development stack.
Enter Y to proceed with the command, or enter N to cancel:
"""
SHORT_HELP = "Sync an AWS SAM project to AWS."
DEFAULT_TEMPLATE_NAME = "template.yaml"
DEFAULT_CAPABILITIES = ("CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND")
# TODO(sriram-mv): Move context settings to be global such as width.
@click.command(
"sync",
cls=SyncCommand,
help=HELP_TEXT,
short_help=SHORT_HELP,
description=DESCRIPTION,
requires_credentials=True,
context_settings={"max_content_width": 120},
)
@configuration_option(provider=ConfigProvider(section="parameters"))
@template_option_without_build
@click.option(
"--code",
is_flag=True,
help="Sync ONLY code resources. This includes Lambda Functions, API Gateway, and Step Functions.",
cls=ClickMutex,
)
@click.option(
"--watch/--no-watch",
is_flag=True,
help="Watch local files and automatically sync with cloud.",
cls=ClickMutex,
)
@click.option(
"--resource-id",
multiple=True,
help="Sync code for all the resources with the ID. To sync a resource within a nested stack, "
"use the following pattern {ChildStack}/{logicalId}.",
)
@click.option(
"--resource",
multiple=True,
cls=ReplaceHelpSummaryOption,
type=click.Choice(SyncCodeResources.values(), case_sensitive=True),
replace_help_option="--resource RESOURCE",
help=f"Sync code for all resources of the given resource type. Accepted values are {SyncCodeResources.values()}",
)
@click.option(
"--dependency-layer/--no-dependency-layer",
default=True,
is_flag=True,
help="Separate dependencies of individual function into a Lambda layer for improved performance.",
)
@click.option(
"--skip-deploy-sync/--no-skip-deploy-sync",
default=True,
is_flag=True,
help="This option will skip the initial infrastructure deployment if it is not required"
" by comparing the local template with the template deployed in cloud.",
)
@watch_exclude_option
@stack_name_option(required=True) # pylint: disable=E1120
@base_dir_option
@use_container_build_option
@build_in_source_option
@build_image_option(cls=ContainerOptions)
@image_repository_option
@image_repositories_option
@s3_bucket_option(disable_callback=True) # pylint: disable=E1120
@s3_prefix_option
@kms_key_id_option
@role_arn_option
@parameter_override_option
@cli_framework_options
@aws_creds_options
@metadata_option
@notification_arns_option
@tags_option
@capabilities_option(default=DEFAULT_CAPABILITIES) # pylint: disable=E1120
@save_params_option
@pass_context
@track_command
@track_long_event("SyncUsed", "Start", "SyncUsed", "End")
@image_repository_validation(support_resolve_image_repos=False)
@track_template_warnings([CodeDeployWarning.__name__, CodeDeployConditionWarning.__name__])
@check_newer_version
@print_cmdline_args
@unsupported_command_cdk()
@command_exception_handler
def cli(
ctx: Context,
template_file: str,
code: bool,
watch: bool,
resource_id: Optional[List[str]],
resource: Optional[List[str]],
dependency_layer: bool,
skip_deploy_sync: bool,
stack_name: str,
base_dir: Optional[str],
parameter_overrides: dict,
image_repository: str,
image_repositories: Optional[List[str]],
s3_bucket: str,
s3_prefix: str,
kms_key_id: str,
capabilities: Optional[List[str]],
role_arn: Optional[str],
notification_arns: Optional[List[str]],
tags: dict,
metadata: dict,
use_container: bool,
save_params: bool,
config_file: str,
config_env: str,
build_image: Optional[Tuple[str]],
build_in_source: Optional[bool],
watch_exclude: Optional[Dict[str, List[str]]],
) -> None:
"""
`sam sync` command entry point
"""
mode = _get_mode_value_from_envvar("SAM_BUILD_MODE", choices=["debug"])
# All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing
do_cli(
template_file,
code,
watch,
resource_id,
resource,
dependency_layer,
skip_deploy_sync,
stack_name,
ctx.region,
ctx.profile,
base_dir,
parameter_overrides,
mode,
image_repository,
image_repositories,
s3_bucket,
s3_prefix,
kms_key_id,
capabilities,
role_arn,
notification_arns,
tags,
metadata,
use_container,
build_image,
config_file,
config_env,
build_in_source,
watch_exclude,
) # pragma: no cover
def do_cli(
template_file: str,
code: bool,
watch: bool,
resource_id: Optional[List[str]],
resource: Optional[List[str]],
dependency_layer: bool,
skip_deploy_sync: bool,
stack_name: str,
region: str,
profile: str,
base_dir: Optional[str],
parameter_overrides: dict,
mode: Optional[str],
image_repository: str,
image_repositories: Optional[List[str]],
s3_bucket: str,
s3_prefix: str,
kms_key_id: str,
capabilities: Optional[List[str]],
role_arn: Optional[str],
notification_arns: Optional[List[str]],
tags: dict,
metadata: dict,
use_container: bool,
build_image: Optional[Tuple[str]],
config_file: str,
config_env: str,
build_in_source: Optional[bool],
watch_exclude: Optional[Dict[str, List[str]]],
) -> None:
"""
Implementation of the ``cli`` method
"""
from samcli.cli.global_config import GlobalConfig
from samcli.commands.build.build_context import BuildContext
from samcli.commands.deploy.deploy_context import DeployContext
from samcli.commands.package.package_context import PackageContext
from samcli.lib.utils import osutils
global_config = GlobalConfig()
if not global_config.is_accelerate_opt_in_stack(template_file, stack_name):
if not click.confirm(Colored().yellow(SYNC_INFO_TEXT + SYNC_CONFIRMATION_TEXT), default=True):
return
global_config.set_accelerate_opt_in_stack(template_file, stack_name)
else:
LOG.info(Colored().color_log(msg=SYNC_INFO_TEXT, color="yellow"), extra=dict(markup=True))
s3_bucket_name = s3_bucket or manage_stack(profile=profile, region=region)
if dependency_layer is True:
dependency_layer = check_enable_dependency_layer(template_file)
# Note: ADL with use-container is not supported yet. Remove this logic once its supported.
if use_container and dependency_layer:
LOG.info(
"Note: Automatic Dependency Layer is not yet supported with use-container. \
sam sync will be run without Automatic Dependency Layer."
)
dependency_layer = False
build_dir = DEFAULT_BUILD_DIR_WITH_AUTO_DEPENDENCY_LAYER if dependency_layer else DEFAULT_BUILD_DIR
LOG.debug("Using build directory as %s", build_dir)
EventTracker.track_event("UsedFeature", "Accelerate")
processed_build_images = process_image_options(build_image)
with BuildContext(
resource_identifier=None,
template_file=template_file,
base_dir=base_dir,
build_dir=build_dir,
cache_dir=DEFAULT_CACHE_DIR,
clean=True,
use_container=use_container,
cached=True,
parallel=True,
parameter_overrides=parameter_overrides,
mode=mode,
create_auto_dependency_layer=dependency_layer,
stack_name=stack_name,
print_success_message=False,
locate_layer_nested=True,
build_in_source=build_in_source,
build_images=processed_build_images,
) as build_context:
built_template = os.path.join(build_dir, DEFAULT_TEMPLATE_NAME)
with osutils.tempfile_platform_independent() as output_template_file:
with PackageContext(
template_file=built_template,
s3_bucket=s3_bucket_name,
image_repository=image_repository,
image_repositories=image_repositories,
s3_prefix=s3_prefix,
kms_key_id=kms_key_id,
output_template_file=output_template_file.name,
no_progressbar=True,
metadata=metadata,
region=region,
profile=profile,
use_json=False,
force_upload=True,
) as package_context:
# 500ms of sleep time between stack checks and describe stack events.
DEFAULT_POLL_DELAY = 0.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=stack_name,
s3_bucket=s3_bucket_name,
image_repository=image_repository,
image_repositories=image_repositories,
no_progressbar=True,
s3_prefix=s3_prefix,
kms_key_id=kms_key_id,
parameter_overrides=parameter_overrides,
capabilities=capabilities,
role_arn=role_arn,
notification_arns=notification_arns,
tags=tags,
region=region,
profile=profile,
no_execute_changeset=True,
fail_on_empty_changeset=True,
confirm_changeset=False,
use_changeset=False,
force_upload=True,
signing_profiles=None,
disable_rollback=False,
poll_delay=poll_delay,
on_failure=None,
) as deploy_context:
with SyncContext(
dependency_layer, build_context.build_dir, build_context.cache_dir, skip_deploy_sync
) as sync_context:
if watch:
watch_excludes_filter = watch_exclude or {}
execute_watch(
template=template_file,
build_context=build_context,
package_context=package_context,
deploy_context=deploy_context,
sync_context=sync_context,
auto_dependency_layer=dependency_layer,
disable_infra_syncs=code,
watch_exclude=watch_excludes_filter,
)
elif code:
execute_code_sync(
template=template_file,
build_context=build_context,
deploy_context=deploy_context,
sync_context=sync_context,
resource_ids=resource_id,
resource_types=resource,
auto_dependency_layer=dependency_layer,
)
else:
infra_sync_result = execute_infra_contexts(
build_context, package_context, deploy_context, sync_context
)
code_sync_resources = infra_sync_result.code_sync_resources
if not infra_sync_result.infra_sync_executed and code_sync_resources:
resource_ids = [str(resource) for resource in code_sync_resources]
LOG.info("Queuing up code sync for the resources that require an update")
LOG.debug("The following resources will be code synced for an update: %s", resource_ids)
execute_code_sync(
template=template_file,
build_context=build_context,
deploy_context=deploy_context,
sync_context=sync_context,
resource_ids=resource_ids,
resource_types=None,
auto_dependency_layer=dependency_layer,
use_built_resources=True,
)
def execute_infra_contexts(
build_context: "BuildContext",
package_context: "PackageContext",
deploy_context: "DeployContext",
sync_context: "SyncContext",
) -> InfraSyncResult:
"""Executes the sync for infra.
Parameters
----------
build_context : BuildContext
package_context : PackageContext
deploy_context : DeployContext
sync_context : SyncContext
Returns
-------
InfraSyncResult
Data class that contains infra sync execution result
"""
infra_sync_executor = InfraSyncExecutor(build_context, package_context, deploy_context, sync_context)
return infra_sync_executor.execute_infra_sync(first_sync=True)
def execute_code_sync(
template: str,
build_context: "BuildContext",
deploy_context: "DeployContext",
sync_context: "SyncContext",
resource_ids: Optional[List[str]],
resource_types: Optional[List[str]],
auto_dependency_layer: bool,
use_built_resources: bool = False,
) -> None:
"""Executes the sync flow for code.
Parameters
----------
template : str
Template file name
build_context : BuildContext
BuildContext
deploy_context : DeployContext
DeployContext
sync_context: SyncContext
SyncContext object that obtains sync information.
resource_ids : List[str]
List of resource IDs to be synced.
resource_types : List[str]
List of resource types to be synced.
auto_dependency_layer: bool
Boolean flag to whether enable certain sync flows for auto dependency layer feature
use_built_resources: bool
Boolean flag to whether to use pre-build resources from BuildContext or build resources from scratch
"""
stacks = SamLocalStackProvider.get_stacks(template)[0]
factory = SyncFlowFactory(build_context, deploy_context, sync_context, stacks, auto_dependency_layer)
factory.load_physical_id_mapping()
executor = SyncFlowExecutor()
sync_flow_resource_ids: Set[ResourceIdentifier] = (
get_unique_resource_ids(stacks, resource_ids, resource_types)
if resource_ids or resource_types
else set(get_all_resource_ids(stacks))
)
for resource_id in sync_flow_resource_ids:
built_result = build_context.build_result if use_built_resources else None
sync_flow = factory.create_sync_flow(resource_id, built_result)
if sync_flow:
executor.add_sync_flow(sync_flow)
else:
LOG.warning("Cannot create SyncFlow for %s. Skipping.", resource_id)
executor.execute()
def execute_watch(
template: str,
build_context: "BuildContext",
package_context: "PackageContext",
deploy_context: "DeployContext",
sync_context: "SyncContext",
auto_dependency_layer: bool,
disable_infra_syncs: bool,
watch_exclude: Dict[str, List[str]],
):
"""Start sync watch execution
Parameters
----------
template : str
Template file path
build_context : BuildContext
BuildContext
package_context : PackageContext
PackageContext
deploy_context : DeployContext
DeployContext
sync_context: SyncContext
SyncContext object that obtains sync information.
auto_dependency_layer: bool
Boolean flag to whether enable certain sync flows for auto dependency layer feature.
disable_infra_syncs: bool
Boolean flag to determine if sam sync only executes code syncs.
"""
# Note: disable_infra_syncs is different from skip_deploy_sync,
# disable_infra_syncs completely disables infra syncs and
# skip_deploy_sync skips the initial infra sync if it's not required.
watch_manager = WatchManager(
template,
build_context,
package_context,
deploy_context,
sync_context,
auto_dependency_layer,
disable_infra_syncs,
watch_exclude,
)
watch_manager.start()
def check_enable_dependency_layer(template_file: str):
"""
Check if auto dependency layer should be enabled
:param template_file: template file string
:return: True if ADL should be enabled, False otherwise
"""
stacks, _ = SamLocalStackProvider.get_stacks(template_file)
for stack in stacks:
esbuild = EsbuildBundlerManager(stack)
if esbuild.esbuild_configured():
# Disable ADL if esbuild is configured. esbuild already makes the package size
# small enough to ensure that ADL isn't needed to improve performance
click.secho("esbuild is configured, disabling auto dependency layer.", fg="yellow")
return False
return True