我们有一个模板container_instance_template.jinja
,它为我们的 GCP 部署管理器定义了我们的实例属性。
对于startup-script
元标记,我们有一个_startup-script.sh
我们想要加载的文件。但是我们不断收到模板加载错误。
我们的模板:
resources:
- name: {{ IT_NAME }}
type: compute.v1.instanceTemplate
properties:
properties:
metadata:
items:
- key: gce-container-declaration
value: |
{{ GenerateManifest(env['name'], properties['port'],properties['dockerImage'], properties['dockerEnv'])|indent(12) }}
- key: startup-script
value: ????????????
我们已经尝试了一切:
key: startup-script
value: |
{{ properties['startupScript'] }}
# This one does not work, because as it's just the reference to the file, GCP doesn't pick up the contents and doesn't execute the script. As far as we understood, it should though, anyways.
key: startup-script
value: |
{% include properties['startupScript'] %}
# This one does not work, because we get the error TemplateNotFound: ./_startup-script.sh
这是我们得到的错误:
- code: MANIFEST_EXPANSION_USER_ERROR
location: /deployments/app/manifests/manifest-14723924
message: |-
Manifest expansion encountered the following errors: Exception in container_instance_template.jinja
Traceback (most recent call last):
return template.render(resource)
return self.environment.handle_exception(exc_info, True)
reraise(exc_type, exc_value, tb)
File "<template>", line 22, in top-level template code
raise TemplateNotFound(template)
TemplateNotFound: ./_startup-script.sh
Resource: container_instance_template.jinja Resource: config
在我们最后一次尝试中,我们试图创建一个 Python 函数并将其导入到模板中,但没有成功:
# container_instance_template.jinja
imports:
- path: helpers.py
- path: properties['startupScript']
# And then using:
{{ include_file(properties['startupScript']) }}
# helpers.py
import jinja2
def include_file(name):
return jinja2.Markup(loader.get_source(env, name)[0])
loader = jinja2.PackageLoader(__name__, 'templates')
env = jinja2.Environment(loader=loader)
env.globals['include_file'] = include_file
我们找不到任何 GCP 示例、指南、文档或其他任何内容。如果我们内联 bash 脚本,它可以工作,但这是一个 hacky 解决方案。
我们尝试了对文件的所有类型的引用。它在那里,其他文件正常工作。