-
Notifications
You must be signed in to change notification settings - Fork 166
Use more condensed output #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cac3cd
Change the default behavior of the interactive provider
mwildehahn 4f61a81
Merge branch 'change-sets' into change-sets-strict
mwildehahn b724e2e
Merge branch 'change-sets' into change-sets-strict
mwildehahn f716d4f
Output a more condensed summary
mwildehahn a4cbe15
Use --replacements-only intead of --strict
mwildehahn 1d9e19d
Use lowercase
mwildehahn d1199ae
Add docstrings
mwildehahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,92 @@ def get_change_set_name(): | |
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"]["Replacement"] == | ||
'True'] | ||
|
||
|
||
def ask_for_approval(full_changeset=None, include_verbose=False): | ||
"""Prompt the user for approval to execute a change set. | ||
|
||
Args: | ||
full_changeset (Optional[list]): A list of the full changeset that will | ||
be output if the user specifies verbose. | ||
include_verbose (Optional[bool]): Boolean for whether or not to include | ||
the verbose option | ||
|
||
""" | ||
approval_options = ['y', 'n'] | ||
if include_verbose: | ||
approval_options.append('v') | ||
|
||
approve = raw_input("Execute the above changes? [{}] ".format( | ||
'/'.join(approval_options))) | ||
|
||
if include_verbose and approve == "v": | ||
logger.info( | ||
"Full changeset:\n%s", | ||
yaml.safe_dump(full_changeset), | ||
) | ||
return ask_for_approval() | ||
elif approve != "y": | ||
raise exceptions.CancelExecution | ||
|
||
|
||
def output_summary(fqn, action, changeset, replacements_only=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docstring |
||
"""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 | ||
replacements_only (Optional[bool]): boolean for whether or not we only | ||
want to list replacements | ||
|
||
""" | ||
replacements = [] | ||
changes = [] | ||
for change in changeset: | ||
resource = change['ResourceChange'] | ||
replacement = resource['Replacement'] == 'True' | ||
summary = '- %s %s (%s)' % ( | ||
resource['Action'], | ||
resource['LogicalResourceId'], | ||
resource['ResourceType'], | ||
) | ||
if replacement: | ||
replacements.append(summary) | ||
else: | ||
changes.append(summary) | ||
|
||
summary = '' | ||
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) | ||
|
||
|
||
class Provider(AWSProvider): | ||
"""AWS Cloudformation Change Set Provider""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.replacements_only = kwargs.pop('replacements_only', False) | ||
super(Provider, self).__init__(*args, **kwargs) | ||
|
||
def _wait_till_change_set_complete(self, change_set_id): | ||
complete = False | ||
response = None | ||
|
@@ -71,14 +154,18 @@ def update_stack(self, fqn, template_url, parameters, tags, **kwargs): | |
if response["ExecutionStatus"] != "AVAILABLE": | ||
raise Exception("Unable to execute change set: {}".format(response)) | ||
|
||
message = ( | ||
"Cloudformation wants to make the following changes to stack: " | ||
"%s\n%s" | ||
) | ||
logger.info(message, fqn, yaml.safe_dump(response["Changes"])) | ||
approve = raw_input("Execute the above changes? [y/n] ") | ||
if approve != "y": | ||
raise exceptions.CancelExecution | ||
action = "replacements" if self.replacements_only else "changes" | ||
changeset = response["Changes"] | ||
if self.replacements_only: | ||
changeset = requires_replacement(changeset) | ||
|
||
if len(changeset): | ||
output_summary(fqn, action, changeset, | ||
replacements_only=self.replacements_only) | ||
ask_for_approval( | ||
full_changeset=response["Changes"], | ||
include_verbose=True, | ||
) | ||
|
||
retry_on_throttling( | ||
self.cloudformation.execute_change_set, | ||
|
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import unittest | ||
from ....providers.aws.interactive import requires_replacement | ||
|
||
|
||
def generate_resource_change(replacement=True): | ||
resource_change = { | ||
"Action": "Modify", | ||
"Details": [], | ||
"LogicalResourceId": "Fake", | ||
"PhysicalResourceId": "arn:aws:fake", | ||
"Replacement": "True" if replacement else "False", | ||
"ResourceType": "AWS::Fake", | ||
"Scope": ["Properties"], | ||
} | ||
return { | ||
"ResourceChange": resource_change, | ||
"Type": "Resource", | ||
} | ||
|
||
|
||
class TestInteractiveProvider(unittest.TestCase): | ||
|
||
def test_requires_replacement(self): | ||
changeset = [ | ||
48E3 | generate_resource_change(), | |
generate_resource_change(replacement=False), | ||
generate_resource_change(), | ||
] | ||
replacement = requires_replacement(changeset) | ||
self.assertEqual(len(replacement), 2) | ||
for resource in replacement: | ||
self.assertEqual(resource["ResourceChange"]["Replacement"], "True") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docstring