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

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

parent 10f8e674
import collections import collections
import glob import glob
import jinja2 import jinja2
import jinja2.meta
import json import json
import os import os
import pprint import pprint
import sys
import yaml import yaml
from typing import Dict, List from typing import Dict, List
...@@ -270,12 +272,24 @@ class PackerTemplate: ...@@ -270,12 +272,24 @@ class PackerTemplate:
node["provisioners"] = self._provisioners node["provisioners"] = self._provisioners
node["post-processors"] = self._postprocessors node["post-processors"] = self._postprocessors
# Render the jinja2 templates with the parameters dictionary provided in the template file and the available environment variables env = jinja2.Environment(undefined=jinja2.DebugUndefined)
jinja_template = jinja2.Template(repr(node)) template = env.from_string(repr(node))
s = jinja_template.render(environment=self._environment) 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 # Dump to the output file
with open(output_file, "w") as fout: 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: def __str__(self) -> str:
"""Returns the string representation for this :class:`PackerTemplate`. """Returns the string representation for this :class:`PackerTemplate`.
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