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

问题描述 投票:0回答: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解决方案。

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

deployment google-cloud-platform jinja2 google-deployment-manager
1个回答
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,了解有关实现此目的的其他方法的更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.