def prepare_packer_template(config, template_name):
"""
Prepares a packer template JSON file according to configuration and writes
it into a temporary location where packer later expects it.
Uses jinja2 template syntax to generate the resulting JSON file.
Templates are in templates/ and snippets in templates/snippets/.
"""
try:
template_fd = resource_stream(__name__,
'templates/{}.json'.format(template_name))
except FileNotFoundError:
print("Template doesn't exist: {}".format(template_name))
sys.exit(2)
filepath = resource_filename(__name__, 'templates/')
env = Environment(loader=FileSystemLoader(filepath), autoescape=False,
trim_blocks=True, lstrip_blocks=True)
template = env.get_template("{}.json".format(template_name))
# write to temporary file
f = create_cachefd('{}.json'.format(template_name))
f.write(template.render(config)) # pylint: disable=no-member
f.close()
return f.name
评论列表
文章目录