【问题标题】:GCP VM Deployment: How to update yaml config properties dynamically while creating a VMGCP VM 部署:如何在创建 VM 时动态更新 yaml 配置属性
【发布时间】:2018-11-20 10:40:51
【问题描述】:

我们有一个 nodejs 应用程序,它使用配置文件 (.yaml) 和模板在 GCP 上创建 VM。现在,我想在创建 VM 时根据来自 UI 的用户输入更新 yaml/模板中的一些属性。我们如何动态更新配置属性?提前感谢您的任何建议。

【问题讨论】:

    标签: google-api google-cloud-platform gcloud google-api-nodejs-client google-deployment-manager


    【解决方案1】:

    看来你有两个选择:

    1) jinja 模板方式

    您定义一个 jinja 模板,而不是配置文件: 资源:

    # my-template.jinja
    resources:
    - name: my-resource
      type: some-type
      properties:
        prop1: {{ properties['foo'] }}
        prop2: {{ properties['bar'] }}
    

    然后,您可以像这样调用它,变量 foo 和 bar 将被映射到提供的属性:

    gcloud deployment-manager deployments create <my-deployment> \
      --template my-template.jinja \
      --properties foo:user-custom-value,bar:another-value
    

    2) 老式的模板方式

    我们正在替换文本本身中的自定义值,而不是使用渲染引擎(就像 jinja2 一样)

    # my-template.yaml
    resources:
    - name: my-resource
      type: some-type
      properties:
        prop1: REPLACE-PROP-1
        prop2: REPLACE-PROP-2
    

    尽可能替换文本,如果您正在运行 shell 脚本,则可以使用 sed,或者从 node/javascript 本身使用

    const replaces = [
      {name: 'REPLACE-PROP-1', value: 'user-custom-value'},
      {name: 'REPLACE-PROP-2', value: 'another-custom-value'},
    ];
    const templateYaml = fs.readFileSync('my-template.yaml','utf-8');
    const customYaml = replaces
      .map(r => templateYaml.replace(RegExp(r.name,'g'), r.value);
    

    或者使用 sed

    sed -ie 's/REPLACE-PROP-1/user-custom-value/g' my-template.yaml
    sed -ie 's/REPLACE-PROP-2/another-cst-value/g' my-template.yaml
    

    最后部署配置

    gcloud deployment-manager deployments create <my-deployment> \
      --config my-template.yaml
    

    【讨论】:

      【解决方案2】:

      GCP 部署管理器无法动态执行此操作。您必须添加一个附加层(例如单击部署市场),它允许用户在应用配置文件之前选择变量。 DM 没有这样做的东西。

      【讨论】:

        猜你喜欢
        • 2018-06-25
        • 1970-01-01
        • 2019-05-06
        • 2018-12-23
        • 1970-01-01
        • 1970-01-01
        • 2019-12-13
        • 1970-01-01
        • 2018-12-25
        相关资源
        最近更新 更多