Add sync command, make *cb* boolean available within Jinja context

This commit is contained in:
Stefan Reimer 2019-03-20 12:51:17 +00:00
parent 68f9ca68d1
commit cf7a9df9ae
3 changed files with 37 additions and 4 deletions

View File

@ -27,5 +27,6 @@ Commands:
delete Deletes stacks or stack groups delete Deletes stacks or stack groups
provision Creates or updates stacks or stack groups provision Creates or updates stacks or stack groups
render Renders template and its parameters render Renders template and its parameters
sync Renders template and provisions it right away
validate Validates already rendered templates using cfn-lint validate Validates already rendered templates using cfn-lint
``` ```

View File

@ -36,12 +36,37 @@ def render(cb, stack_names, multi):
""" Renders template and its parameters """ """ Renders template and its parameters """
stacks = _find_stacks(cb, stack_names, multi) stacks = _find_stacks(cb, stack_names, multi)
for s in stacks: for s in stacks:
s.render() s.render()
s.write_template_file() s.write_template_file()
@click.command()
@click.argument("stack_names", nargs=-1)
@click.option("--multi", is_flag=True, help="Allow more than one stack to match")
@click.pass_obj
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()
@click.command() @click.command()
@click.argument("stack_names", nargs=-1) @click.argument("stack_names", nargs=-1)
@click.option("--multi", is_flag=True, help="Allow more than one stack to match") @click.option("--multi", is_flag=True, help="Allow more than one stack to match")
@ -182,6 +207,7 @@ def _find_stacks(cb, stack_names, multi=False):
cli.add_command(render) cli.add_command(render)
cli.add_command(sync)
cli.add_command(validate) cli.add_command(validate)
cli.add_command(provision) cli.add_command(provision)
cli.add_command(delete) cli.add_command(delete)

View File

@ -93,10 +93,16 @@ class Stack(object):
'CloudBender.Version': __version__ 'CloudBender.Version': __version__
} }
jenv.globals['_config'] = {'cfn': self.template_vars, 'Metadata': template_metadata} cb = False
if self.template_vars['Mode'] == "CloudBender":
cb = True
_config = {'cb': cb, 'cfn': self.template_vars, 'Metadata': template_metadata}
jenv.globals['_config'] = _config
# First render pass to calculate a md5 checksum # First render pass to calculate a md5 checksum
template_metadata['Template.Hash'] = hashlib.md5(template.render({'cfn': self.template_vars, 'Metadata': template_metadata}).encode('utf-8')).hexdigest() template_metadata['Template.Hash'] = hashlib.md5(template.render(_config).encode('utf-8')).hexdigest()
# Reset and set Metadata for final render pass # Reset and set Metadata for final render pass
jenv.globals['get_custom_att'](context={'_config': self.template_vars}, reset=True) jenv.globals['get_custom_att'](context={'_config': self.template_vars}, reset=True)
@ -113,7 +119,7 @@ class Stack(object):
pass pass
logger.info('Rendering %s', template.filename) logger.info('Rendering %s', template.filename)
rendered = template.render({'cfn': self.template_vars, 'Metadata': template_metadata}) rendered = template.render(_config)
try: try:
self.data = yaml.load(rendered) self.data = yaml.load(rendered)