From 48c625c6783e07efd0b66ac8077f5200a41001c2 Mon Sep 17 00:00:00 2001 From: Stefan Reimer Date: Fri, 22 Mar 2019 10:58:13 +0000 Subject: [PATCH] Use yaml.safe_load everywhere, reuse code for sync, render and provision --- cloudbender/cli.py | 60 +++++++++++++++++++++----------------------- cloudbender/stack.py | 4 +-- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/cloudbender/cli.py b/cloudbender/cli.py index 36877c2..c93c1d2 100644 --- a/cloudbender/cli.py +++ b/cloudbender/cli.py @@ -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) diff --git a/cloudbender/stack.py b/cloudbender/stack.py index 45e0cc5..a12c6fb 100644 --- a/cloudbender/stack.py +++ b/cloudbender/stack.py @@ -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: