我们有一个模板container_instance_template.jinja
,用于为GCP的Deployment Manager定义实例属性。
对于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脚本,它可以工作,但是那是一个棘手的解决方案。
我们尝试了对该文件的所有类型的引用。在那里,其他文件正常工作。
答案 0 :(得分:0)
您是否尝试过将元数据标签“ startup-script”更改为“ startup-script-url”并链接到Google云存储位置?像这样:
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-url
value: gs:project/bucket/yourscript.sh
确保您已授予VM相应的权限以访问Google云存储桶。
请查看this google article,以获取其他实现此方法的详细信息。
答案 1 :(得分:0)
如何将启动脚本添加到yaml文件而不是jinja中,如下所示: 进口:
- path: instance.jinja - path: ../startup-script.sh name: startup-script.sh resources: - name: my-instance type: instance.jinja properties: metadata-from-file: startup-script: startup-script.sh zone: ZONE_TO_RUN
答案 2 :(得分:0)
来自Google的here的git回购中有一个错误。这个烦人的事情花了我两个小时才能弄清楚。可以找到正确的示例here:
metadata:
items:
{% for key, value in properties['metadata-from-file'].iteritems() %}
- key: {{ key }}
value: |
{{ imports[value]|indent(10) }}
{% endfor %}
请确保将以下内容放入您的config.yaml文件中:
imports:
- path: instance.jinja
- path: ../startup-script.sh
name: startup-script.sh
resources:
- name: my-instance
type: instance.jinja
properties:
metadata-from-file:
startup-script: startup-script.sh
zone: ZONE_TO_RUN