def parse_mako(stack_name, template_body, parameters): """ Parses Mako templates """ # The default for strict_undefined is False. Change to True to # troubleshoot pesky templates mako_template = mako.template.Template( template_body, strict_undefined=False ) parameters["get_stack_output"] = get_stack_output parameters["get_stack_resource"] = get_stack_resource parameters["call_aws"] = call_aws try: template = yaml.load(mako_template.render(**parameters)) except Exception: raise SystemExit( mako.exceptions.text_error_template().render() ) # Automatically adds and merges outputs for every resource in the # template - outputs are automatically exported. # An existing output in the template will not be overriden by an # automatic output. outputs = { k: { "Value": {"Ref": k}, "Export": {"Name": "{}-{}".format(stack_name, k)} } for k in template.get("Resources", {}).keys() } outputs.update(template.get("Outputs", {})) template["Outputs"] = outputs return template
def parse_jinja(stack_name, template_body, parameters): """ Parses Jinja templates """ jinja_template = jinja2.Template(template_body) parameters["utils"] = gpwm.utils # parameters["get_stack_output"] = get_stack_output # parameters["get_stack_resource"] = get_stack_resource # parameters["call_aws"] = call_aws try: template = yaml.load(jinja_template.render(**parameters)) # Ignoring yaml tags unknown to this script, because one might want to use # the providers tags like !Ref, !Sub, etc in their templates except yaml.constructor.ConstructorError as exc: if "could not determine a constructor for the tag" not in exc.problem: raise exc # Automatically adds and merges outputs for every resource in the # template - outputs are automatically exported. # An existing output in the template will not be overriden by an # automatic output. outputs = { k: { "Value": {"Ref": k}, "Export": {"Name": "{}-{}".format(stack_name, k)}} for k in template.get("Resources", {}).keys() } outputs.update(template.get("Outputs", {})) template["Outputs"] = outputs return template
def parse_mako(stack_name, template_body, parameters): """ Parses Mako templates """ # The default for strict_undefined is False. Change to True to # troubleshoot pesky templates mako_template = mako.template.Template( template_body, strict_undefined=False ) parameters["utils"] = gpwm.utils # parameters["get_stack_output"] = get_stack_output # parameters["get_stack_resource"] = get_stack_resource # parameters["call_aws"] = call_aws try: rendered_mako_template = mako_template.render(**parameters) # Weird Mako exception handling: # http://docs.makotemplates.org/en/latest/usage.html#handling-exceptions except Exception: raise SystemExit( mako.exceptions.text_error_template().render() ) # Ignoring yaml tags unknown to this script, because one might want to use # the providers tags like !Ref, !Sub, etc in their templates try: template = yaml.load(rendered_mako_template) except yaml.constructor.ConstructorError as exc: if "could not determine a constructor for the tag" not in exc.problem: raise exc # Automatically adds and merges outputs for every resource in the # template - outputs are automatically exported. # An existing output in the template will not be overriden by an # automatic output. outputs = { k: { "Value": {"Ref": k}, "Export": {"Name": "{}-{}".format(stack_name, k)} } for k in template.get("Resources", {}).keys() } outputs.update(template.get("Outputs", {})) if outputs: template["Outputs"] = outputs return template
def _render_template_image(self, template): if self._prerender: self._requested_images.append(template) return None else: pool_name = template.get("pool", "default") image = self._chosen_images[pool_name].images.pop(0) template_vars = { "snaptime": image.snaptime, "agediff": DateTools.age_diff, "datetime": datetime, } caption_template = mako.template.Template(template.get( "caption", ""), strict_undefined=True) caption = caption_template.render(**template_vars) return { "filename": image.filename, "caption": caption, }
def parse_jinja(stack_name, template_body, parameters): """ Parses Jinja templates """ jinja_template = jinja2.Template(template_body) parameters["get_stack_output"] = get_stack_output parameters["get_stack_resource"] = get_stack_resource parameters["call_aws"] = call_aws template = yaml.load(jinja_template.render(**parameters)) # Automatically adds and merges outputs for every resource in the # template - outputs are automatically exported. # An existing output in the template will not be overriden by an # automatic output. outputs = { k: { "Value": {"Ref": k}, "Export": {"Name": "{}-{}".format(stack_name, k)}} for k in template.get("Resources", {}).keys() } outputs.update(template.get("Outputs", {})) template["Outputs"] = outputs return template