google-api - GCP VM 部署:如何在创建 VM 时动态更新 yaml 配置属性
问题描述
我们有一个 nodejs 应用程序,它使用配置文件 (.yaml) 和模板在 GCP 上创建 VM。现在我想在创建 VM 时根据来自 UI 的用户输入更新 yaml/模板中的一些属性。我们如何动态更新配置属性?在此先感谢您的任何建议。
解决方案
似乎您有两个选择:
1)神社模板方式
您可以定义一个 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
sed
尽可能替换文本,如果您正在运行 shell 脚本,或者从 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
推荐阅读
- mysql - 当不是正在搜索的每一行都包含 JSON 时,如何使用 SQL 查询在 JSON 字符串中搜索?
- java - TestNG 如何在测试失败时执行 beforeclass() 方法
- javascript - 如何在悬停时显示工具提示?
- python - 无法在类方法中访问类变量
- java - Discord JDA:带有回调的单元测试 queue() 方法
- c - 为什么这个用于复制文件的 c 程序不能正常工作?
- wordpress - 在 WooCommerce 中取消订阅后,是否有办法取消学生在 LifterLMS 中的注册?
- mongodb - 不同集合上的 Mongo db 事务
- android - ExoPlayer 同时播放多个视频
- c# - 派生类认为我的基类被删除了