Commit 30c6e192 authored by eric pellegrini's avatar eric pellegrini

added functionality for printing missing templates when rendering manifest.yml files

parent 10f8e674
import collections
import glob
import jinja2
import jinja2.meta
import json
import os
import pprint
import sys
import yaml
from typing import Dict, List
......@@ -270,12 +272,24 @@ class PackerTemplate:
node["provisioners"] = self._provisioners
node["post-processors"] = self._postprocessors
# Render the jinja2 templates with the parameters dictionary provided in the template file and the available environment variables
jinja_template = jinja2.Template(repr(node))
s = jinja_template.render(environment=self._environment)
env = jinja2.Environment(undefined=jinja2.DebugUndefined)
template = env.from_string(repr(node))
rendered = template.render(**self._environment)
# Check if rendering was done correctly
ast = env.parse(rendered)
undefined = jinja2.meta.find_undeclared_variables(ast)
if undefined:
print(f'The following variables are undefined: {undefined!r}')
# Replace back %< .* %> to {{ .* }}
rendered = rendered.replace("%<","{{")
rendered = rendered.replace(">%","}}")
# Dump to the output file
with open(output_file, "w") as fout:
json.dump(yaml.safe_load(s), fout, **kwargs)
json.dump(yaml.safe_load(rendered), fout, **kwargs)
def __str__(self) -> str:
"""Returns the string representation for this :class:`PackerTemplate`.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment