Commit 601b4817 authored by eric pellegrini's avatar eric pellegrini

better code for checking missing jinja 2 templates

parent e3daea1f
......@@ -272,20 +272,23 @@ class PackerTemplate:
node["provisioners"] = self._provisioners
node["post-processors"] = self._postprocessors
env = jinja2.Environment(undefined=jinja2.DebugUndefined)
template = env.from_string(repr(node))
rendered = template.render(**self._environment)
stringified_node = repr(node)
# Create a jinja evironment for parsing the manifest
env = jinja2.Environment()
# Parse the string with jinja templates in unrendered state in order to get te set of all jinja templates
ast = env.parse(stringified_node)
jinja_templates = jinja2.meta.find_undeclared_variables(ast)
# Retrieve those templates which are not declared in the environment. These are the undefined templates.
undefined = jinja_templates.difference(self._environment.keys())
# Check if rendering was done correctly
ast = env.parse(rendered)
undefined = jinja2.meta.find_undeclared_variables(ast)
# If there are some undefined templates, stop here.
if undefined:
print(f'The following variables are undefined: {undefined!r}')
sys.exit(1)
# Replace back %< .* %> to {{ .* }}
rendered = rendered.replace("%<","{{")
rendered = rendered.replace(">%","}}")
template = env.from_string(stringified_node)
rendered = template.render(**self._environment)
# Dump to the output file
with open(output_file, "w") as fout:
......
......@@ -24,7 +24,7 @@ packer:
boot_wait: 5s
boot_command:
- "<tab> "
- "preseed/url=http://%< .HTTPIP >%:%< .HTTPPort >%/{{ preseed_file_name }} "
- "preseed/url=http://{% raw %}{{ .HTTPIP }}:{{ .HTTPPort }}{% endraw %}/{{ preseed_file_name }} "
- "auto-install/enable=true "
- "net.ifnames=0 "
- "netcfg/get_hostname={{ vm_name }} "
......@@ -38,7 +38,7 @@ packer:
- "passwd/user-password-again= {{ user_password }} "
- "passwd/root-password={{ root_password }} "
- "passwd/root-password-again={{ root_password }} "
- "no_proxy=%< .HTTPIP >%,{{ no_proxy }} "
- "no_proxy={% raw %}{{ .HTTPIP }}{% endraw %},{{ no_proxy }} "
- "<enter>"
disk_size: "{{ disk_size }}"
http_directory: "http"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment