-
Notifications
You must be signed in to change notification settings - Fork 61
/
default.py
1395 lines (1176 loc) · 54 KB
/
default.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Default AWS Provider."""
# pylint: disable=too-many-lines,too-many-public-methods
import json
import logging
import sys
import time
# thread safe, memoize, provider builder.
from threading import Lock
import botocore.exceptions
import yaml
from botocore.config import Config
from six.moves import urllib
from ... import exceptions
from ...actions.diff import DictValue, diff_parameters
from ...actions.diff import format_params_diff as format_diff
from ...session_cache import get_session
from ...ui import ui
from ...util import parse_cloudformation_template
from ..base import BaseProvider
LOGGER = logging.getLogger(__name__)
# This value controls the maximum number of times a CloudFormation API call
# will be attempted, after being throttled. This value is used in an
# exponential backoff algorithm to determine how long the client should wait
# until attempting a retry:
#
# base * growth_factor ^ (attempts - 1)
#
# A value of 10 here would cause the worst case wait time for the last retry to
# be ~8 mins:
#
# 1 * 2 ^ (10 - 1) = 512 seconds
#
# References:
# https://github.com/boto/botocore/blob/1.6.1/botocore/retryhandler.py#L39-L58
# https://github.com/boto/botocore/blob/1.6.1/botocore/data/_retry.json#L97-L121
MAX_ATTEMPTS = 10
# Updated this to 15 retries with a 1 second sleep between retries. This is
# only used when a call to `get_events` fails due to the stack not being
# found. This is often the case because Cloudformation is taking too long
# to create the stack. 15 seconds should, hopefully, be plenty of time for
# the stack to start showing up in the API.
MAX_TAIL_RETRIES = 15
TAIL_RETRY_SLEEP = 1
GET_EVENTS_SLEEP = 1
DEFAULT_CAPABILITIES = ["CAPABILITY_NAMED_IAM",
"CAPABILITY_AUTO_EXPAND"]
def get_cloudformation_client(session):
"""Get CloudFormaiton boto3 client."""
config = Config(
retries=dict(
max_attempts=MAX_ATTEMPTS
)
)
return session.client('cloudformation', config=config)
def get_output_dict(stack):
"""Return a dict of key/values for the outputs for a given CF stack.
Args:
stack (Dict[str, Any]): The stack object to get
outputs from.
Returns:
Dict[str, Any]: A dictionary with key/values for each output on the
stack.
"""
outputs = {}
if not stack.get('Outputs'):
return outputs
for output in stack['Outputs']:
LOGGER.debug(" %s %s: %s", stack['StackName'], output['OutputKey'],
output['OutputValue'])
outputs[output['OutputKey']] = output['OutputValue']
return outputs
def s3_fallback(fqn, template, parameters, tags, method,
change_set_name=None, service_role=None):
"""Falling back to legacy stacker S3 bucket region for templates."""
LOGGER.warning("DEPRECATION WARNING: Falling back to legacy "
"stacker S3 bucket region for templates. See "
"https://docs.onica.com/projects/runway/en/release/"
"cfngin/config.html#s3-bucket"
" for more information.")
# extra line break on purpose to avoid status updates removing URL
# from view
LOGGER.warning("\n")
LOGGER.debug("Modifying the S3 TemplateURL to point to "
"us-east-1 endpoint")
template_url = template.url
template_url_parsed = urllib.parse.urlparse(template_url)
template_url_parsed = template_url_parsed._replace(
netloc="s3.amazonaws.com")
template_url = urllib.parse.urlunparse(template_url_parsed)
LOGGER.debug("Using template_url: %s", template_url)
args = generate_cloudformation_args(
fqn, parameters, tags, template,
service_role=service_role,
change_set_name=change_set_name
)
response = method(**args)
return response
def get_change_set_name():
"""Return a valid Change Set Name.
The name has to satisfy the following regex::
[a-zA-Z][-a-zA-Z0-9]*
And must be unique across all change sets.
"""
return 'change-set-{}'.format(int(time.time()))
def requires_replacement(changeset):
"""Return the changes within the changeset that require replacement.
Args:
changeset (list): List of changes
Returns:
list: A list of changes that require replacement, if any.
"""
return [r for r in changeset if
r["ResourceChange"].get("Replacement", False) == "True"]
def output_full_changeset(full_changeset=None, params_diff=None,
answer=None, fqn=None):
"""Optionally output full changeset.
Args:
full_changeset (Optional[List[Dict[str, Any]]]): A list of the full
changeset that will be output if the user specifies verbose.
params_diff (Optional[List[:class:`runway.cfngin.actions.diff.DictValue`):
A list of DictValue detailing the differences between two
parameters returned by
:func:`runway.cfngin.actions.diff.diff_dictionaries`.
answer (Optional[str]): predetermined answer to the prompt if it has
already been answered or inferred.
fqn (Optional[str]): fully qualified name of the stack.
"""
if not answer:
answer = ui.ask('Show full change set? [y/n] ').lower()
if answer == 'n':
return
if answer in ['y', 'v']:
if fqn:
msg = '%s full changeset' % (fqn)
else:
msg = 'Full changeset'
if params_diff:
LOGGER.info(
"%s:\n\n%s\n%s",
msg,
format_params_diff(params_diff),
yaml.safe_dump(full_changeset),
)
else:
LOGGER.info(
"%s:\n%s",
msg,
yaml.safe_dump(full_changeset),
)
return
raise exceptions.CancelExecution
def ask_for_approval(full_changeset=None, params_diff=None,
include_verbose=False, fqn=None):
"""Prompt the user for approval to execute a change set.
Args:
full_changeset (Optional[List[Dict[str, Any]]]): A list of the full
changeset that will be output if the user specifies verbose.
params_diff (Optional[List[:class:`runway.cfngin.actions.diff`]]):
A list of DictValue detailing the differences between two
parameters returned by
:func:`runway.cfngin.actions.diff.diff_dictionaries`
include_verbose (bool): Boolean for whether or not to include
the verbose option.
fqn (str): fully qualified name of the stack.
Raises:
CancelExecution: If approval no given.
"""
approval_options = ['y', 'n']
if include_verbose:
approval_options.append('v')
approve = ui.ask("Execute the above changes? [{}] ".format(
'/'.join(approval_options))).lower()
if include_verbose and approve == "v":
output_full_changeset(full_changeset=full_changeset,
params_diff=params_diff, answer=approve, fqn=fqn)
return ask_for_approval(fqn=fqn)
if approve == "y":
return None
raise exceptions.CancelExecution
def output_summary(fqn, action, changeset, params_diff,
replacements_only=False):
"""Log a summary of the changeset.
Args:
fqn (string): fully qualified name of the stack
action (string): action to include in the log message
changeset (list): AWS changeset
params_diff (list): A list of dictionaries detailing the differences
between two parameters returned by
:func:`runway.cfngin.actions.diff.diff_dictionaries`
replacements_only (bool, optional): boolean for whether or not we only
want to list replacements
"""
replacements = []
changes = []
for change in changeset:
resource = change['ResourceChange']
replacement = resource.get('Replacement') == 'True'
summary = '- %s %s (%s)' % (
resource['Action'],
resource['LogicalResourceId'],
resource['ResourceType'],
)
if replacement:
replacements.append(summary)
else:
changes.append(summary)
summary = ''
if params_diff:
summary += summarize_params_diff(params_diff)
if replacements:
if not replacements_only:
summary += 'Replacements:\n'
summary += '\n'.join(replacements)
if changes:
if summary:
summary += '\n'
summary += 'Changes:\n%s' % ('\n'.join(changes))
LOGGER.info('%s %s:\n%s', fqn, action, summary)
def format_params_diff(params_diff):
"""Wrap :func:`runway.cfngin.actions.diff.format_params_diff` for testing."""
return format_diff(params_diff)
def summarize_params_diff(params_diff):
"""Summarize parameter diff."""
summary = ''
added_summary = [v.key for v in params_diff
if v.status() is DictValue.ADDED]
if added_summary:
summary += 'Parameters Added: %s\n' % ', '.join(added_summary)
removed_summary = [v.key for v in params_diff
if v.status() is DictValue.REMOVED]
if removed_summary:
summary += 'Parameters Removed: %s\n' % ', '.join(removed_summary)
modified_summary = [v.key for v in params_diff
if v.status() is DictValue.MODIFIED]
if modified_summary:
summary += 'Parameters Modified: %s\n' % ', '.join(modified_summary)
return summary
def wait_till_change_set_complete(cfn_client, change_set_id, try_count=25,
sleep_time=.5, max_sleep=3):
"""Check state of a changeset, returning when it is in a complete state.
Since changesets can take a little bit of time to get into a complete
state, we need to poll it until it does so. This will try to get the
state ``try_count`` times, waiting ``sleep_time`` * 2 seconds between each
try up to the ``max_sleep`` number of seconds. If, after that time, the
changeset is not in a complete state it fails. These default settings will
wait a little over one minute.
Args:
cfn_client (:class:`botocore.client.Client`): Used to query
CloudFormation.
change_set_id (str): The unique changeset id to wait for.
try_count (int): Number of times to try the call.
sleep_time (int): Time to sleep between attempts.
max_sleep (int): Max time to sleep during backoff
Return:
Dict[str, Any]: The response from CloudFormation for the
``describe_change_set`` call.
"""
complete = False
response = None
for _ in range(try_count):
response = cfn_client.describe_change_set(
ChangeSetName=change_set_id,
)
complete = response["Status"] in ("FAILED", "CREATE_COMPLETE")
if complete:
break
if sleep_time == max_sleep:
LOGGER.debug(
"Still waiting on changeset for another %s seconds",
sleep_time
)
time.sleep(sleep_time)
# exponential backoff with max
sleep_time = min(sleep_time * 2, max_sleep)
if not complete:
raise exceptions.ChangesetDidNotStabilize(change_set_id)
return response
def create_change_set(cfn_client, fqn, template, parameters, tags,
change_set_type='UPDATE', service_role=None):
"""Create CloudFormation change set."""
LOGGER.debug("Attempting to create change set of type %s for stack: %s.",
change_set_type,
fqn)
args = generate_cloudformation_args(
fqn, parameters, tags, template,
change_set_type=change_set_type,
service_role=service_role,
change_set_name=get_change_set_name()
)
try:
response = cfn_client.create_change_set(**args)
except botocore.exceptions.ClientError as err:
if err.response['Error']['Message'] == ('TemplateURL must reference '
'a valid S3 object to which '
'you have access.'):
response = s3_fallback(fqn, template, parameters,
tags, cfn_client.create_change_set,
get_change_set_name(),
service_role)
else:
raise
change_set_id = response["Id"]
response = wait_till_change_set_complete(
cfn_client, change_set_id
)
status = response["Status"]
if status == "FAILED":
status_reason = response["StatusReason"]
if ("didn't contain changes" in response["StatusReason"] or
"No updates are to be performed" in response["StatusReason"]):
LOGGER.debug(
"Stack %s did not change, not updating and removing "
"changeset.",
fqn,
)
cfn_client.delete_change_set(ChangeSetName=change_set_id)
raise exceptions.StackDidNotChange()
LOGGER.warning(
"Got strange status, '%s' for changeset '%s'. Not deleting for "
"further investigation - you will need to delete the changeset "
"manually.",
status, change_set_id
)
raise exceptions.UnhandledChangeSetStatus(
fqn, change_set_id, status, status_reason
)
execution_status = response["ExecutionStatus"]
if execution_status != "AVAILABLE":
raise exceptions.UnableToExecuteChangeSet(fqn,
change_set_id,
execution_status)
changes = response["Changes"]
return changes, change_set_id
def check_tags_contain(actual, expected):
"""Check if a set of AWS resource tags is contained in another.
Every tag key in ``expected`` must be present in ``actual``, and have the
same value. Extra keys in `actual` but not in ``expected`` are ignored.
Args:
actual (List[Dict[str, str]]): Set of tags to be verified, usually
from the description of a resource. Each item must be a
``dict`` containing ``Key`` and ``Value`` items.
expected (List[Dict[str, str]]): Set of tags that must be present in
``actual`` (in the same format).
"""
actual_set = set((item["Key"], item["Value"]) for item in actual)
expected_set = set((item["Key"], item["Value"]) for item in expected)
return actual_set >= expected_set
def generate_cloudformation_args(stack_name, parameters, tags, template,
capabilities=None,
change_set_type=None,
service_role=None,
stack_policy=None,
change_set_name=None):
"""Generate the args for common CloudFormation API interactions.
This is used for ``create_stack``/``update_stack``/``create_change_set``
calls in CloudFormation.
Args:
stack_name (str): The fully qualified stack name in Cloudformation.
parameters (List[Dict[str, Any]]): A list of dictionaries that defines
the parameter list to be applied to the Cloudformation stack.
tags (List[Dict[str, str]]): A list of dictionaries that defines the
tags that should be applied to the Cloudformation stack.
template (:class:`runway.cfngin.provider.base.Template`): The template
object.
capabilities (Optional[List[str]]): A list of capabilities to use when
updating Cloudformation.
change_set_type (Optional[str]): An optional change set type to use
with create_change_set.
service_role (Optional[str]): An optional service role to use when
interacting with Cloudformation.
stack_policy (:class:`runway.cfngin.providers.base.Template`):
A template object representing a stack policy.
change_set_name (Optional[str]): An optional change set name to use
with create_change_set.
Returns:
Dict[str, Any]: A dictionary of arguments to be used in the
Cloudformation API call.
"""
args = {
"StackName": stack_name,
"Parameters": parameters,
"Tags": tags,
"Capabilities": capabilities or DEFAULT_CAPABILITIES,
}
if service_role:
args["RoleARN"] = service_role
if change_set_name:
args["ChangeSetName"] = change_set_name
if change_set_type:
args["ChangeSetType"] = change_set_type
if template.url:
args["TemplateURL"] = template.url
else:
args["TemplateBody"] = template.body
# When creating args for CreateChangeSet, don't include the stack policy,
# since ChangeSets don't support it.
if not change_set_name:
args.update(generate_stack_policy_args(stack_policy))
return args
def generate_stack_policy_args(stack_policy=None):
"""Convert a stack policy object into keyword args.
Args:
stack_policy (:class:`runway.cfngin.providers.base.Template`):
A template object representing a stack policy.
Returns:
dict: A dictionary of keyword arguments to be used elsewhere.
"""
args = {}
if stack_policy:
LOGGER.debug("Stack has a stack policy")
if stack_policy.url:
# CFNgin currently does not support uploading stack policies to
# S3, so this will never get hit (unless your implementing S3
# uploads, and then you're probably reading this comment about why
# the exception below was raised :))
#
# args["StackPolicyURL"] = stack_policy.url
raise NotImplementedError
args["StackPolicyBody"] = stack_policy.body
return args
class ProviderBuilder(object): # pylint: disable=too-few-public-methods
"""Implements a Memorized ProviderBuilder for the AWS provider."""
def __init__(self, region=None, **kwargs):
"""Instantiate class."""
self.region = region
self.kwargs = kwargs
self.providers = {}
self.lock = Lock()
def build(self, region=None, profile=None):
"""Get or create the provider for the given region and profile."""
with self.lock:
# memorization lookup key derived from region + profile.
key = "{}-{}".format(profile, region)
try:
# assume provider is in provider dictionary.
provider = self.providers[key]
except KeyError:
LOGGER.debug('Missed memorized lookup (%s), creating new AWS '
'Provider.', key)
if not region:
region = self.region
# memoize the result for later.
self.providers[key] = Provider(
get_session(region=region, profile=profile),
region=region,
**self.kwargs
)
provider = self.providers[key]
return provider
class Provider(BaseProvider):
"""AWS CloudFormation Provider."""
DELETING_STATUS = "DELETE_IN_PROGRESS"
DELETED_STATUS = "DELETE_COMPLETE"
IN_PROGRESS_STATUSES = (
"CREATE_IN_PROGRESS",
"IMPORT_IN_PROGRESS",
"UPDATE_IN_PROGRESS",
"DELETE_IN_PROGRESS",
"UPDATE_COMPLETE_CLEANUP_IN_PROGRESS",
)
ROLLING_BACK_STATUSES = (
"ROLLBACK_IN_PROGRESS",
"IMPORT_ROLLBACK_IN_PROGRESS",
"UPDATE_ROLLBACK_IN_PROGRESS"
)
FAILED_STATUSES = (
"CREATE_FAILED",
"ROLLBACK_FAILED",
"ROLLBACK_COMPLETE",
"DELETE_FAILED",
"IMPORT_ROLLBACK_FAILED",
"UPDATE_ROLLBACK_FAILED",
# Note: UPDATE_ROLLBACK_COMPLETE is in both the FAILED and COMPLETE
# sets, because we need to wait for it when a rollback is triggered,
# but still mark the stack as failed.
"UPDATE_ROLLBACK_COMPLETE",
)
COMPLETE_STATUSES = (
"CREATE_COMPLETE",
"DELETE_COMPLETE",
"IMPORT_COMPLETE",
"UPDATE_COMPLETE",
"IMPORT_ROLLBACK_COMPLETE",
"UPDATE_ROLLBACK_COMPLETE",
)
RECREATION_STATUSES = (
"CREATE_FAILED",
"ROLLBACK_FAILED",
"ROLLBACK_COMPLETE"
)
REVIEW_STATUS = "REVIEW_IN_PROGRESS"
def __init__(self, session, region=None, interactive=False,
replacements_only=False, recreate_failed=False,
service_role=None):
"""Instantiate class."""
self._outputs = {}
self.region = region
self.cloudformation = get_cloudformation_client(session)
self.interactive = interactive
# replacements only is only used in interactive mode
self.replacements_only = interactive and replacements_only
self.recreate_failed = interactive or recreate_failed
self.service_role = service_role
def get_stack(self, stack_name, *args, **kwargs): # pylint: disable=unused-argument
"""Get stack."""
try:
return self.cloudformation.describe_stacks(
StackName=stack_name)['Stacks'][0]
except botocore.exceptions.ClientError as err:
if "does not exist" not in str(err):
raise
raise exceptions.StackDoesNotExist(stack_name)
def get_stack_status(self, stack, *args, **kwargs): # pylint: disable=unused-argument
"""Get stack status."""
return stack['StackStatus']
def is_stack_being_destroyed(self, stack, **kwargs): # pylint: disable=unused-argument
"""Whether the status of the stack indicates it is 'being destroyed'."""
return self.get_stack_status(stack) == self.DELETING_STATUS
def is_stack_completed(self, stack):
"""Whether the status of the stack indicates it is 'complete'."""
return self.get_stack_status(stack) in self.COMPLETE_STATUSES
def is_stack_in_progress(self, stack):
"""Whether the status of the stack indicates it is 'in progress'."""
return self.get_stack_status(stack) in self.IN_PROGRESS_STATUSES
def is_stack_destroyed(self, stack):
"""Whether the status of the stack indicates it is 'deleted'."""
return self.get_stack_status(stack) == self.DELETED_STATUS
def is_stack_recreatable(self, stack):
"""Whether the status of the stack indicates it is 'recreating'."""
return self.get_stack_status(stack) in self.RECREATION_STATUSES
def is_stack_rolling_back(self, stack):
"""Whether the status of the stack indicates it is 'rolling back'."""
return self.get_stack_status(stack) in self.ROLLING_BACK_STATUSES
def is_stack_failed(self, stack):
"""Whether the status of the stack indicates it is 'failed'."""
return self.get_stack_status(stack) in self.FAILED_STATUSES
def is_stack_in_review(self, stack):
"""Whether the status of the stack indicates if 'review in progress'."""
return self.get_stack_status(stack) == self.REVIEW_STATUS
def tail_stack(self, stack, cancel, log_func=None):
"""Tail the events of a stack."""
def _log_func(event):
event_args = [event['ResourceStatus'], event['ResourceType'],
event.get('ResourceStatusReason', None)]
# filter out any values that are empty
event_args = [arg for arg in event_args if arg]
template = " ".join(["[%s]"] + ["%s" for _ in event_args])
LOGGER.info(template, *([stack.fqn] + event_args))
log_func = log_func or _log_func
LOGGER.info("Tailing stack: %s", stack.fqn)
attempts = 0
while True:
attempts += 1
try:
self.tail(stack.fqn, cancel=cancel, log_func=log_func,
include_initial=False)
break
except botocore.exceptions.ClientError as err:
if "does not exist" in str(err) and attempts < MAX_TAIL_RETRIES:
# stack might be in the process of launching, wait for a
# second and try again
if cancel.wait(TAIL_RETRY_SLEEP):
return
continue
raise
@staticmethod
def _tail_print(event):
print("%s %s %s" % (event['ResourceStatus'],
event['ResourceType'],
event['EventId']))
def get_events(self, stack_name, chronological=True):
"""Get the events in batches and return in chronological order."""
next_token = None
event_list = []
while True:
if next_token is not None:
events = self.cloudformation.describe_stack_events(
StackName=stack_name, NextToken=next_token
)
else:
events = self.cloudformation.describe_stack_events(
StackName=stack_name
)
event_list.append(events['StackEvents'])
next_token = events.get('NextToken', None)
if next_token is None:
break
time.sleep(GET_EVENTS_SLEEP)
if chronological:
return reversed(sum(event_list, []))
return sum(event_list, [])
def get_rollback_status_reason(self, stack_name):
"""Process events and returns latest roll back reason."""
# flake8/pylint fight over the indent here on python2
# TODO remove "disable=bad-continuation" when dropping python2
event = next((item for item in self.get_events(stack_name, False)
if item["ResourceStatus"] == "UPDATE_ROLLBACK_IN_PROGRESS"
), None) # pylint: disable=bad-continuation
if event:
reason = event["ResourceStatusReason"]
return reason
event = next((item for item in self.get_events(stack_name)
if item["ResourceStatus"] ==
"ROLLBACK_IN_PROGRESS"), None)
reason = event["ResourceStatusReason"]
return reason
def tail(self, stack_name, cancel, log_func=_tail_print, sleep_time=5,
include_initial=True):
"""Show and then tail the event log."""
# First dump the full list of events in chronological order and keep
# track of the events we've seen already
seen = set()
initial_events = self.get_events(stack_name)
for event in initial_events:
if include_initial:
log_func(event)
seen.add(event['EventId'])
# Now keep looping through and dump the new events
while True:
events = self.get_events(stack_name)
for event in events:
if event['EventId'] not in seen:
log_func(event)
seen.add(event['EventId'])
if cancel.wait(sleep_time):
return
def destroy_stack(self, stack, *args, **kwargs): # pylint: disable=unused-argument
"""Destroy a CloudFormation Stack.
Args:
stack (:class:`stacker.stack.Stack`): Stack to be destroyed.
Keyword Args:
action (str): Name of the action being executed. This impacts
the log message used.
approval (Optional[str]): Response to approval prompt.
force_interactive (bool): Always ask for approval.
"""
action = kwargs.pop('action', 'destroy')
approval = kwargs.pop('approval', None)
force_interactive = kwargs.pop('force_interactive', False)
fqn = self.get_stack_name(stack)
LOGGER.debug("Attempting to delete stack %s", fqn)
if action == 'build':
LOGGER.info('%s was removed from the Stacker config file '
'so it is being destroyed.', fqn)
destroy_method = self.select_destroy_method(force_interactive)
return destroy_method(fqn=fqn, action=action,
approval=approval, **kwargs)
def create_stack(self, fqn, # pylint: disable=arguments-differ
template, parameters, tags, force_change_set=False,
stack_policy=None, termination_protection=False, **kwargs):
"""Create a new Cloudformation stack.
Args:
fqn (str): The fully qualified name of the Cloudformation stack.
template (:class:`runway.cfngin.providers.base.Template`):
A Template object to use when creating the stack.
parameters (list): A list of dictionaries that defines the
parameter list to be applied to the Cloudformation stack.
tags (list): A list of dictionaries that defines the tags
that should be applied to the Cloudformation stack.
force_change_set (bool): Whether or not to force change set use.
stack_policy (:class:`runway.cfngin.providers.base.Template`):
A template object representing a stack policy.
termination_protection (bool): End state of the stack's termination
protection.
"""
LOGGER.debug("Attempting to create stack %s:.", fqn)
LOGGER.debug(" parameters: %s", parameters)
LOGGER.debug(" tags: %s", tags)
if template.url:
LOGGER.debug(" template_url: %s", template.url)
else:
LOGGER.debug(" no template url, uploading template "
"directly.")
if force_change_set:
LOGGER.debug("force_change_set set to True, creating stack with "
"changeset.")
_changes, change_set_id = create_change_set(
self.cloudformation, fqn, template, parameters, tags,
'CREATE', service_role=self.service_role, **kwargs
)
self.cloudformation.execute_change_set(
ChangeSetName=change_set_id,
)
self.update_termination_protection(fqn, termination_protection)
else:
args = generate_cloudformation_args(
fqn, parameters, tags, template,
service_role=self.service_role,
stack_policy=stack_policy,
)
# this arg is only valid for stack creation so its not part of
# generate_cloudformation_args.
args['EnableTerminationProtection'] = termination_protection
try:
self.cloudformation.create_stack(**args)
except botocore.exceptions.ClientError as err:
if err.response['Error']['Message'] == ('TemplateURL must '
'reference a valid S3 '
'object to which you '
'have access.'):
s3_fallback(fqn, template, parameters, tags,
self.cloudformation.create_stack,
self.service_role)
else:
raise
def select_update_method(self, force_interactive, force_change_set):
"""Select the correct update method when updating a stack.
Args:
force_interactive (str): Whether or not to force interactive mode
no matter what mode the provider is in.
force_change_set (bool): Whether or not to force change set use.
Returns:
function: The correct object method to use when updating.
"""
if self.interactive or force_interactive:
return self.interactive_update_stack
if force_change_set:
return self.noninteractive_changeset_update
return self.default_update_stack
def prepare_stack_for_update(self, stack, tags):
"""Prepare a stack for updating.
It may involve deleting the stack if is has failed it's initial
creation. The deletion is only allowed if:
- The stack contains all the tags configured in the current context;
- The stack is in one of the statuses considered safe to re-create
- ``recreate_failed`` is enabled, due to either being explicitly
enabled by the user, or because interactive mode is on.
Args:
stack (dict): a stack object returned from get_stack
tags (list): list of expected tags that must be present in the
stack if it must be re-created
Returns:
bool: True if the stack can be updated, False if it must be
re-created
"""
if self.is_stack_destroyed(stack):
return False
if self.is_stack_completed(stack):
return True
stack_name = self.get_stack_name(stack)
stack_status = self.get_stack_status(stack)
if self.is_stack_in_progress(stack):
raise exceptions.StackUpdateBadStatus(
stack_name, stack_status,
'Update already in-progress')
if not self.is_stack_recreatable(stack):
raise exceptions.StackUpdateBadStatus(
stack_name, stack_status,
'Unsupported state for re-creation')
if not self.recreate_failed:
raise exceptions.StackUpdateBadStatus(
stack_name, stack_status,
'Stack re-creation is disabled. Run CFNgin again with the '
'--recreate-failed option to force it to be deleted and '
'created from scratch.')
stack_tags = self.get_stack_tags(stack)
if not check_tags_contain(stack_tags, tags):
raise exceptions.StackUpdateBadStatus(
stack_name, stack_status,
'Tags differ from current configuration, possibly not created '
'with CFNgin')
if self.interactive:
sys.stdout.write(
'The \"%s\" stack is in a failed state (%s).\n'
'It cannot be updated, but it can be deleted and re-created.\n'
'All its current resources will IRREVERSIBLY DESTROYED.\n'
'Proceed carefully!\n\n' % (stack_name, stack_status))
sys.stdout.flush()
ask_for_approval(include_verbose=False, fqn=stack_name)
LOGGER.warning('Destroying stack \"%s\" for re-creation', stack_name)
self.destroy_stack(stack, approval='y')
return False
def update_stack(self, fqn, template, # pylint: disable=arguments-differ
old_parameters, parameters, tags, force_interactive=False,
force_change_set=False, stack_policy=None,
termination_protection=False, **kwargs):
"""Update a Cloudformation stack.
Args:
fqn (str): The fully qualified name of the Cloudformation stack.
template (:class:`runway.cfngin.providers.base.Template`):
A Template object to use when updating the stack.
old_parameters (List[Dict[str, Any]]): A list of dictionaries that
defines the parameter list on the existing Cloudformation
stack.
parameters (List[Dict[str, Any]]): A list of dictionaries that
defines the parameter list to be applied to the Cloudformation
stack.
tags (List[Dict[str, str]]): A list of dictionaries that defines
the tags that should be applied to the Cloudformation stack.
force_interactive (bool): A flag that indicates whether the update
should be interactive. If set to True, interactive mode will
be used no matter if the provider is in interactive mode or
not. False will follow the behavior of the provider.
force_change_set (bool): A flag that indicates whether the update
must be executed with a change set.
stack_policy (:class:`runway.cfngin.providers.base.Template`):
A template object representing a stack policy.
termination_protection (bool): End state of the stack's termination
protection.
"""
LOGGER.debug("Attempting to update stack %s:", fqn)
LOGGER.debug(" parameters: %s", parameters)
LOGGER.debug(" tags: %s", tags)
if template.url:
LOGGER.debug(" template_url: %s", template.url)
else:
LOGGER.debug(" no template url, uploading template directly.")
update_method = self.select_update_method(force_interactive,
force_change_set)
self.update_termination_protection(fqn, termination_protection)
return update_method(fqn, template, old_parameters, parameters,
stack_policy=stack_policy, tags=tags, **kwargs)
def update_termination_protection(self, fqn, termination_protection):
"""Update a Stack's termination protection if needed.
Runs before the normal stack update process.
Args:
fqn (str): The fully qualified name of the Cloudformation stack.
termination_protection (bool): End state of the stack's termination
protection.
"""
stack = self.get_stack(fqn)
if stack['EnableTerminationProtection'] != termination_protection:
LOGGER.debug(
'Updating termination protection of stack "%s" to "%s"',
fqn,
termination_protection
)
self.cloudformation.update_termination_protection(
EnableTerminationProtection=termination_protection,
StackName=fqn
)
def deal_with_changeset_stack_policy(self, fqn, stack_policy):
"""Set a stack policy when using changesets.
ChangeSets don't allow you to set stack policies in the same call to
update them. This sets it before executing the changeset if the
stack policy is passed in.
Args:
stack_policy (:class:`runway.cfngin.providers.base.Template`):