在 Jinja 模板文件中,如何为实例的启动脚本加载 bash 脚本

In a Jinja template file, how to load a bash script for the startup-script of an instance

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

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

您是否尝试过将元数据标签 "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 以了解有关其他实现方式的更多详细信息。

如何将启动脚本添加到 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

来自 google 的 git 回购中有错误 here. This annoying thing cost me like two hours to figure out. The correct example can be found 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