1

我们有一个模板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 解决方案。

我们尝试了对文件的所有类型的引用。它在那里,其他文件正常工作。

4

3 回答 3

1

如何将启动脚本添加到 yaml 文件而不是 jinja 中,如下所示:

- 路径:instance.jinja
- 路径:../startup-script.sh
  名称:启动脚本.sh

资源:
- 名称:我的实例
  类型:instance.jinja
  特性:
    来自文件的元数据:
      启动脚本:启动脚本.sh
    区域:ZONE_TO_RUN
于 2019-08-13T20:23:38.300 回答
0

您是否尝试过将元数据标签“startup-script”更改为“startup-script-url”并链接到谷歌云存储位置?就像是:

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

确保您授予虚拟机访问谷歌云存储桶的适当权限。

查看这篇谷歌文章,了解更多关于其他实现方式的详细信息。

于 2019-04-22T14:16:54.073 回答
0

在这里找到的 google 的 git repo 中有一个错误。这件烦人的事情花了我两个小时才弄清楚。正确的例子可以在这里找到:

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
于 2019-09-26T19:16:31.200 回答