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

Deployment 在Jinja模板文件中,如何为实例的启动脚本加载bash脚本,deployment,google-cloud-platform,jinja2,google-deployment-manager,Deployment,Google Cloud Platform,Jinja2,Google Deployment Manager,我们有一个模板container\u instance\u template.jinja,它为我们的GCP部署管理器定义了我们的实例属性 对于启动脚本元标记,我们有一个\u startup-script.sh要加载的文件。但我们不断得到模板加载错误 我们的模板: resources: - name: {{ IT_NAME }} type: compute.v1.instanceTemplate properties: properties: metadata:

我们有一个模板
container\u instance\u template.jinja
,它为我们的GCP部署管理器定义了我们的实例属性

对于
启动脚本
元标记,我们有一个
\u 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.
这是我们得到的错误:

- 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
我们找不到任何GCP示例、指南、文档或其他内容。如果我们内联bash脚本,它可以工作,但它是一个黑客解决方案


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

您是否尝试过将元数据标记“启动脚本”更改为“启动脚本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云存储桶的适当权限


查看有关实现此功能的其他方法的更多详细信息。

如何将启动脚本添加到yaml文件中,而不是像下面这样: 进口:

-路径:instance.jinja -路径:../startup-script.sh 名称:startup-script.sh 资源: -姓名:我的实例 类型:instance.jinja 特性: 文件中的元数据: 启动脚本:startup-script.sh 区域:区域到运行
谷歌发现的git回购协议中有一个错误。这件烦人的事花了我两个小时才弄明白。可以找到正确的示例:

请确保将以下内容放在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
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
- 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
metadata:
  items:
  {% for key, value in properties['metadata-from-file'].iteritems() %}
  - key: {{ key }}
    value: |
      {{ imports[value]|indent(10) }}
{% endfor %}
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