-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathbuild.py
More file actions
398 lines (319 loc) · 13.8 KB
/
build.py
File metadata and controls
398 lines (319 loc) · 13.8 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
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import logging
from .base import BaseAction, build_walker
from .base import STACK_POLL_TIME
from ..providers.base import Template
from ..exceptions import (
MissingParameterException,
StackDidNotChange,
StackDoesNotExist,
CancelExecution,
)
from ..status import (
NotSubmittedStatus,
NotUpdatedStatus,
DidNotChangeStatus,
SubmittedStatus,
CompleteStatus,
FailedStatus,
SkippedStatus,
PENDING,
WAITING,
SUBMITTED,
INTERRUPTED
)
logger = logging.getLogger(__name__)
def build_stack_tags(stack):
"""Builds a common set of tags to attach to a stack"""
return [{'Key': t[0], 'Value': t[1]} for t in stack.tags.items()]
def should_update(stack):
"""Tests whether a stack should be submitted for updates to CF.
Args:
stack (:class:`stacker.stack.Stack`): The stack object to check.
Returns:
bool: If the stack should be updated, return True.
"""
if stack.locked:
if not stack.force:
logger.debug("Stack %s locked and not in --force list. "
"Refusing to update.", stack.name)
return False
else:
logger.debug("Stack %s locked, but is in --force "
"list.", stack.name)
return True
def should_submit(stack):
"""Tests whether a stack should be submitted to CF for update/create
Args:
stack (:class:`stacker.stack.Stack`): The stack object to check.
Returns:
bool: If the stack should be submitted, return True.
"""
if stack.enabled:
return True
logger.debug("Stack %s is not enabled. Skipping.", stack.name)
return False
def should_ensure_cfn_bucket(outline, dump):
"""Test whether access to the cloudformation template bucket is required
Args:
outline (bool): The outline action.
dump (bool): The dump action.
Returns:
bool: If access to CF bucket is needed, return True.
"""
return not outline and not dump
def _resolve_parameters(parameters, blueprint):
"""Resolves CloudFormation Parameters for a given blueprint.
Given a list of parameters, handles:
- discard any parameters that the blueprint does not use
- discard any empty values
- convert booleans to strings suitable for CloudFormation
Args:
parameters (dict): A dictionary of parameters provided by the
stack definition
blueprint (:class:`stacker.blueprint.base.Blueprint`): A Blueprint
object that is having the parameters applied to it.
Returns:
dict: The resolved parameters.
"""
params = {}
param_defs = blueprint.get_parameter_definitions()
for key, value in parameters.items():
if key not in param_defs:
logger.debug("Blueprint %s does not use parameter %s.",
blueprint.name, key)
continue
if value is None:
logger.debug("Got None value for parameter %s, not submitting it "
"to cloudformation, default value should be used.",
key)
continue
if isinstance(value, bool):
logger.debug("Converting parameter %s boolean \"%s\" to string.",
key, value)
value = str(value).lower()
params[key] = value
return params
class UsePreviousParameterValue(object):
""" A simple class used to indicate a Parameter should use it's existng
value.
"""
pass
def _handle_missing_parameters(parameter_values, all_params, required_params,
existing_stack=None):
"""Handles any missing parameters.
If an existing_stack is provided, look up missing parameters there.
Args:
parameter_values (dict): key/value dictionary of stack definition
parameters
all_params (list): A list of all the parameters used by the
template/blueprint.
required_params (list): A list of all the parameters required by the
template/blueprint.
existing_stack (dict): A dict representation of the stack. If
provided, will be searched for any missing parameters.
Returns:
list of tuples: The final list of key/value pairs returned as a
list of tuples.
Raises:
MissingParameterException: Raised if a required parameter is
still missing.
"""
missing_params = list(set(all_params) - set(parameter_values.keys()))
if existing_stack and 'Parameters' in existing_stack:
stack_parameters = [
p["ParameterKey"] for p in existing_stack["Parameters"]
]
for p in missing_params:
if p in stack_parameters:
logger.debug(
"Using previous value for parameter %s from existing "
"stack",
p
)
parameter_values[p] = UsePreviousParameterValue
final_missing = list(set(required_params) - set(parameter_values.keys()))
if final_missing:
raise MissingParameterException(final_missing)
return list(parameter_values.items())
class Action(BaseAction):
"""Responsible for building & coordinating CloudFormation stacks.
Generates the build plan based on stack dependencies (these dependencies
are determined automatically based on output lookups from other stacks).
The plan can then either be printed out as an outline or executed. If
executed, each stack will get launched in order which entails:
- Pushing the generated CloudFormation template to S3 if it has changed
- Submitting either a build or update of the given stack to the
:class:`stacker.provider.base.Provider`.
"""
def build_parameters(self, stack, provider_stack=None):
"""Builds the CloudFormation Parameters for our stack.
Args:
stack (:class:`stacker.stack.Stack`): A stacker stack
provider_stack (dict): An optional Stacker provider object
Returns:
dict: The parameters for the given stack
"""
resolved = _resolve_parameters(stack.parameter_values, stack.blueprint)
required_parameters = list(stack.required_parameter_definitions)
all_parameters = list(stack.all_parameter_definitions)
parameters = _handle_missing_parameters(resolved, all_parameters,
required_parameters,
provider_stack)
param_list = []
for key, value in parameters:
param_dict = {"ParameterKey": key}
if value is UsePreviousParameterValue:
param_dict["UsePreviousValue"] = True
else:
param_dict["ParameterValue"] = str(value)
param_list.append(param_dict)
return param_list
def _launch_stack(self, stack, **kwargs):
"""Handles the creating or updating of a stack in CloudFormation.
Also makes sure that we don't try to create or update a stack while
it is already updating or creating.
"""
old_status = kwargs.get("status")
wait_time = 0 if old_status is PENDING else STACK_POLL_TIME
if self.cancel.wait(wait_time):
return INTERRUPTED
if not should_submit(stack):
return NotSubmittedStatus()
provider = self.build_provider(stack)
try:
provider_stack = provider.get_stack(stack.fqn)
except StackDoesNotExist:
provider_stack = None
if provider_stack and not should_update(stack):
return NotUpdatedStatus()
recreate = False
if provider_stack and old_status == SUBMITTED:
logger.debug(
"Stack %s provider status: %s",
stack.fqn,
provider.get_stack_status(provider_stack),
)
if provider.is_stack_rolling_back(provider_stack):
if 'rolling back' in old_status.reason:
return old_status
logger.debug("Stack %s entered a roll back", stack.fqn)
if 'updating' in old_status.reason:
reason = 'rolling back update'
else:
reason = 'rolling back new stack'
return SubmittedStatus(reason)
elif provider.is_stack_in_progress(provider_stack):
logger.debug("Stack %s in progress.", stack.fqn)
return old_status
elif provider.is_stack_destroyed(provider_stack):
logger.debug("Stack %s finished deleting", stack.fqn)
recreate = True
# Continue with creation afterwards
# Failure must be checked *before* completion, as both will be true
# when completing a rollback, and we don't want to consider it as
# a successful update.
elif provider.is_stack_failed(provider_stack):
reason = old_status.reason
if 'rolling' in reason:
reason = reason.replace('rolling', 'rolled')
status_reason = provider.get_rollback_status_reason(stack.fqn)
logger.info(
"%s Stack Roll Back Reason: " + status_reason, stack.fqn)
return FailedStatus(reason)
elif provider.is_stack_completed(provider_stack):
return CompleteStatus(old_status.reason)
else:
return old_status
logger.debug("Resolving stack %s", stack.fqn)
stack.resolve(self.context, self.provider)
logger.debug("Launching stack %s now.", stack.fqn)
template = self._template(stack.blueprint)
stack_policy = self._stack_policy(stack)
tags = build_stack_tags(stack)
parameters = self.build_parameters(stack, provider_stack)
force_change_set = stack.blueprint.requires_change_set
if recreate:
logger.debug("Re-creating stack: %s", stack.fqn)
provider.create_stack(stack.fqn, template, parameters,
tags, stack_policy=stack_policy)
return SubmittedStatus("re-creating stack")
elif not provider_stack:
logger.debug("Creating new stack: %s", stack.fqn)
provider.create_stack(stack.fqn, template, parameters, tags,
force_change_set,
stack_policy=stack_policy)
return SubmittedStatus("creating new stack")
try:
wait = stack.in_progress_behavior == "wait"
if wait and provider.is_stack_in_progress(provider_stack):
return WAITING
if provider.prepare_stack_for_update(provider_stack, tags):
existing_params = provider_stack.get('Parameters', [])
provider.update_stack(
stack.fqn,
template,
existing_params,
parameters,
tags,
force_interactive=stack.protected,
force_change_set=force_change_set,
stack_policy=stack_policy,
)
logger.debug("Updating existing stack: %s", stack.fqn)
return SubmittedStatus("updating existing stack")
else:
return SubmittedStatus("destroying stack for re-creation")
except CancelExecution:
return SkippedStatus(reason="canceled execution")
except StackDidNotChange:
return DidNotChangeStatus()
def _template(self, blueprint):
"""Generates a suitable template based on whether or not an S3 bucket
is set.
If an S3 bucket is set, then the template will be uploaded to S3 first,
and CreateStack/UpdateStack operations will use the uploaded template.
If not bucket is set, then the template will be inlined.
"""
if self.bucket_name:
return Template(url=self.s3_stack_push(blueprint))
else:
return Template(body=blueprint.rendered)
def _stack_policy(self, stack):
"""Returns a Template object for the stacks stack policy, or None if
the stack doesn't have a stack policy."""
if stack.stack_policy:
return Template(body=stack.stack_policy)
def _generate_plan(self, tail=False, outline=False, dump=False):
return self.plan(
description="Create/Update stacks",
action_name="build",
action=self._launch_stack,
tail=self._tail_stack if tail else None,
context=self.context,
run_hooks=not outline and not dump)
def pre_run(self, outline=False, dump=False, *args, **kwargs):
"""Any steps that need to be taken prior to running the action."""
if should_ensure_cfn_bucket(outline, dump):
self.ensure_cfn_bucket()
def run(self, concurrency=0, outline=False,
tail=False, dump=False, *args, **kwargs):
"""Kicks off the build/update of the stacks in the stack_definitions.
This is the main entry point for the Builder.
"""
plan = self._generate_plan(tail=tail, outline=outline, dump=dump)
if not plan.keys():
logger.warn('WARNING: No stacks detected (error in config?)')
if not outline and not dump:
plan.outline(logging.DEBUG)
logger.debug("Launching stacks: %s", ", ".join(plan.keys()))
walker = build_walker(concurrency)
plan.execute(walker)
else:
if outline:
plan.outline()
if dump:
plan.dump(directory=dump, context=self.context,
provider=self.provider)