Skip to content
Snippets Groups Projects
Commit 601b4817 authored by eric pellegrini's avatar eric pellegrini
Browse files

better code for checking missing jinja 2 templates

parent e3daea1f
No related branches found
No related tags found
No related merge requests found
...@@ -272,20 +272,23 @@ class PackerTemplate: ...@@ -272,20 +272,23 @@ class PackerTemplate:
node["provisioners"] = self._provisioners node["provisioners"] = self._provisioners
node["post-processors"] = self._postprocessors node["post-processors"] = self._postprocessors
env = jinja2.Environment(undefined=jinja2.DebugUndefined) stringified_node = repr(node)
template = env.from_string(repr(node))
rendered = template.render(**self._environment) # 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 # If there are some undefined templates, stop here.
ast = env.parse(rendered)
undefined = jinja2.meta.find_undeclared_variables(ast)
if undefined: if undefined:
print(f'The following variables are undefined: {undefined!r}') print(f'The following variables are undefined: {undefined!r}')
sys.exit(1) sys.exit(1)
# Replace back %< .* %> to {{ .* }} template = env.from_string(stringified_node)
rendered = rendered.replace("%<","{{") rendered = template.render(**self._environment)
rendered = rendered.replace(">%","}}")
# Dump to the output file # Dump to the output file
with open(output_file, "w") as fout: with open(output_file, "w") as fout:
......
...@@ -24,7 +24,7 @@ packer: ...@@ -24,7 +24,7 @@ packer:
boot_wait: 5s boot_wait: 5s
boot_command: boot_command:
- "<tab> " - "<tab> "
- "preseed/url=http://%< .HTTPIP >%:%< .HTTPPort >%/{{ preseed_file_name }} " - "preseed/url=http://{% raw %}{{ .HTTPIP }}:{{ .HTTPPort }}{% endraw %}/{{ preseed_file_name }} "
- "auto-install/enable=true " - "auto-install/enable=true "
- "net.ifnames=0 " - "net.ifnames=0 "
- "netcfg/get_hostname={{ vm_name }} " - "netcfg/get_hostname={{ vm_name }} "
...@@ -38,7 +38,7 @@ packer: ...@@ -38,7 +38,7 @@ packer:
- "passwd/user-password-again= {{ user_password }} " - "passwd/user-password-again= {{ user_password }} "
- "passwd/root-password={{ root_password }} " - "passwd/root-password={{ root_password }} "
- "passwd/root-password-again={{ root_password }} " - "passwd/root-password-again={{ root_password }} "
- "no_proxy=%< .HTTPIP >%,{{ no_proxy }} " - "no_proxy={% raw %}{{ .HTTPIP }}{% endraw %},{{ no_proxy }} "
- "<enter>" - "<enter>"
disk_size: "{{ disk_size }}" disk_size: "{{ disk_size }}"
http_directory: "http" http_directory: "http"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment