-
Notifications
You must be signed in to change notification settings - Fork 311
/
update.py
83 lines (73 loc) · 2.85 KB
/
update.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
from uuid import uuid1
import click
from sceptre.context import SceptreContext
from sceptre.cli.helpers import catch_exceptions, confirmation
from sceptre.cli.helpers import write, stack_status_exit_code
from sceptre.cli.helpers import simplify_change_set_description
from sceptre.stack_status import StackChangeSetStatus
from sceptre.plan.plan import SceptrePlan
@click.command(name="update", short_help="Update a stack.")
@click.argument("path")
@click.option(
"-c", "--change-set", is_flag=True,
help="Create a change set before updating."
)
@click.option(
"-v", "--verbose", is_flag=True, help="Display verbose output."
)
@click.option(
"-y", "--yes", is_flag=True, help="Assume yes to all questions."
)
@click.pass_context
@catch_exceptions
def update_command(ctx, path, change_set, verbose, yes):
"""
Updates a stack for a given config PATH. Or perform an update via
change-set when the change-set flag is set.
\f
:param path: Path to execute the command on.
:type path: str
:param change_set: Whether a change set should be created.
:type change_set: bool
:param verbose: A flag to print a verbose output.
:type verbose: bool
:param yes: A flag to answer 'yes' to all CLI questions.
:type yes: bool
"""
context = SceptreContext(
command_path=path,
project_path=ctx.obj.get("project_path"),
user_variables=ctx.obj.get("user_variables"),
options=ctx.obj.get("options"),
output_format=ctx.obj.get("output_format"),
ignore_dependencies=ctx.obj.get("ignore_dependencies")
)
plan = SceptrePlan(context)
if change_set:
change_set_name = "-".join(["change-set", uuid1().hex])
plan.create_change_set(change_set_name)
try:
# Wait for change set to be created
statuses = plan.wait_for_cs_completion(change_set_name)
# Exit if change set fails to create
for status in list(statuses.values()):
if status != StackChangeSetStatus.READY:
exit(1)
# Describe changes
descriptions = plan.describe_change_set(change_set_name)
for description in list(descriptions.values()):
if not verbose:
description = simplify_change_set_description(description)
write(description, context.output_format)
# Execute change set if happy with changes
if yes or click.confirm("Proceed with stack update?"):
plan.execute_change_set(change_set_name)
except Exception as e:
raise e
finally:
# Clean up by deleting change set
plan.delete_change_set(change_set_name)
else:
confirmation("update", yes, command_path=path)
responses = plan.update()
exit(stack_status_exit_code(responses.values()))