Use yaml.safe_load everywhere, reuse code for sync, render and provision

This commit is contained in:
Stefan Reimer 2019-03-22 10:58:13 +00:00
parent 429c13f9ba
commit 48c625c678
2 changed files with 30 additions and 34 deletions

View File

@ -36,9 +36,7 @@ def render(cb, stack_names, multi):
""" Renders template and its parameters """
stacks = _find_stacks(cb, stack_names, multi)
for s in stacks:
s.render()
s.write_template_file()
_render(stacks)
@click.command()
@ -49,22 +47,9 @@ def sync(cb, stack_names, multi):
""" Renders template and provisions it right away """
stacks = _find_stacks(cb, stack_names, multi)
for step in sort_stacks(cb, stacks):
if step:
with ThreadPoolExecutor(max_workers=len(step)) as group:
futures = []
for stack in step:
stack.render()
stack.write_template_file()
status = stack.get_status()
if not status:
futures.append(group.submit(stack.create))
else:
futures.append(group.submit(stack.update))
for future in as_completed(futures):
future.result()
_render(stacks)
_provision(cb, stacks)
@click.command()
@ -99,20 +84,7 @@ def provision(cb, stack_names, multi):
""" Creates or updates stacks or stack groups """
stacks = _find_stacks(cb, stack_names, multi)
for step in sort_stacks(cb, stacks):
if step:
with ThreadPoolExecutor(max_workers=len(step)) as group:
futures = []
for stack in step:
status = stack.get_status()
if not status:
futures.append(group.submit(stack.create))
else:
futures.append(group.submit(stack.update))
for future in as_completed(futures):
future.result()
_provision(cb, stacks)
@click.command()
@ -206,6 +178,30 @@ def _find_stacks(cb, stack_names, multi=False):
return stacks
def _render(stacks):
""" Utility function to reuse code between tasks """
for s in stacks:
s.render()
s.write_template_file()
def _provision(cb, stacks):
""" Utility function to reuse code between tasks """
for step in sort_stacks(cb, stacks):
if step:
with ThreadPoolExecutor(max_workers=len(step)) as group:
futures = []
for stack in step:
status = stack.get_status()
if not status:
futures.append(group.submit(stack.create))
else:
futures.append(group.submit(stack.update))
for future in as_completed(futures):
future.result()
cli.add_command(render)
cli.add_command(sync)
cli.add_command(validate)

View File

@ -122,7 +122,7 @@ class Stack(object):
rendered = template.render(_config)
try:
self.data = yaml.load(rendered)
self.data = yaml.safe_load(rendered)
except Exception as e:
# In case we rendered invalid yaml this helps to debug
logger.error(rendered)
@ -178,7 +178,7 @@ class Stack(object):
self.cfn_template = yaml_contents.read()
logger.debug('Read cfn template %s.', yaml_file)
self.data = yaml.load(self.cfn_template)
self.data = yaml.safe_load(self.cfn_template)
self._parse_metadata()
else: