是否可以在 YAML 文件中使用抽象?
Is it possible to use abstraction in a YAML file?
是否可以使用 arguments
或 abstraction
来最大限度地减少 YAML 文件中的重复代码?
我正在编写一个触发部署的 YAML 文件,在部署之前和之后我想调用一个松弛通道来指示部署正在开始和完成,以及如果它失败了。
这是我写的但是感觉太冗长了:
example_deploy:
- call: notify
in:
msgText: "Deployment starting for environment *${environment}*"
- try:
- ${oneops.environmentCommitAndDeploy(environment = 'production', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: notify
in:
msgText: " :fire: Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
- exit
- call: notify
in:
msgText: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
notify:
- task: slack
in:
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiLooper}
text: "${msgText}"
现在,如果我想要 example_deploy_2
并做同样类型的事情,我是否必须重写所有代码?或者有没有办法让 "function" 或 抽象 YAML 的重复部分?
更新
我已经使用 call
来抽象对 slack 的调用,但现在我想知道我是否可以对 slack 进行通用调用并动态更新消息 - 因为现在我正在重复传递给的参数我定义为 call
ed
的代码块
例子
example_deploy:
- call: slack_start_deploy
- try:
- ${transitionVariableUpdate(platform = '${platform}', environment = '${environment}', component = '${component_ear}' variables = { appVersion = '${BRANCH_NAME}-${BUILD_NUMBER}' })}
- ${environmentCommitAndDeploy(environment = 'qa', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: slack_deploy_error
- exit
- call: slack_deploy_success
slack_start_deploy:
- slack.postMessage:
text: "${entryPoint} Deployment starting for environment *${environment}*"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_error:
- slack.postMessage:
text: " :fire: ${entryPoint} Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_success:
- slack.postMessage:
text: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
YAML 规范中唯一允许
最小化重复是使用
anchor 在节点上和
使用别名引用该节点。这适用于两个叶节点
(即任何类型的标量值)和收集节点
(映射、序列)。本质上是锚定集合的别名
"replace" 集合下的整个子树。
除此之外还有 merge
映射中的 key <<
是
由大多数 YAML 加载器实现(通常在构造阶段),
你可以让一个或多个映射为
具有合并键的映射中未指定的键
(直接或通过早期处理的合并)。
最重要的是,任何使用 YAML 加载程序的程序都可以扩展加载程序
(通常是它的构造机制,但这可以更早完成
在加载过程中),因为他们认为合适,但这种机制是
不被视为 YAML 的一部分。
合并机制可以部署在你的YAML上,减少行数。如果你的例子
更改为 example.yaml
:
example_deploy:
- call: slack_start_deploy
- try:
- ${transitionVariableUpdate(platform = '${platform}', environment = '${environment}', component = '${component_ear}' variables = { appVersion = '${BRANCH_NAME}-${BUILD_NUMBER}' })}
- ${environmentCommitAndDeploy(environment = 'qa', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: slack_deploy_error
- exit
- call: slack_deploy_success
slack_start_deploy:
- slack.postMessage: &pm
text: "${entryPoint} Deployment starting for environment *${environment}*"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_error:
- slack.postMessage:
text: " :fire: ${entryPoint} Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
<<: *pm
slack_deploy_success:
- slack.postMessage:
text: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
<<: *pm
(请注意,我更改了 error:
和 - call: ...
行的缩进,如您所呈现的那样
文件无效 YAML)
在上面,&pm
是具有四个键的映射节点的锚点。 *pm
s 是别名
使用此映射,每次使用 text
.
的原始值
下面的Python程序通过加载,然后转储显示合并键是如何扩展到
加载过程中您的原件。
import sys
from pathlib import Path
import ruamel.yaml
example = Path('example.yaml')
yaml = ruamel.yaml.YAML(typ='safe')
yaml.default_flow_style = False
data = yaml.load(example)
yaml.dump(data, sys.stdout)
给出:
error:
- log: 'Error trying to deploy: ${lastError.cause}'
- call: slack_deploy_error
- exit
- call: slack_deploy_success
example_deploy:
- call: slack_start_deploy
- try:
- ${transitionVariableUpdate(platform = '${platform}', environment = '${environment}',
component = '${component_ear}' variables = { appVersion = '${BRANCH_NAME}-${BUILD_NUMBER}'
})}
- ${environmentCommitAndDeploy(environment = 'qa', platform = '${platform}', deployAllPlatforms
= false )}
slack_deploy_error:
- slack.postMessage:
channelId: ${alerts}
iconEmoji: ${slackEmojiConcord}
text: ' :fire: ${entryPoint} Deployment failed for environment *${environment}*
http://concord.com/#/process/${txId}/log'
username: ${slackname}
slack_deploy_success:
- slack.postMessage:
channelId: ${alerts}
iconEmoji: ${slackEmojiConcord}
text: ' :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log'
username: ${slackname}
slack_start_deploy:
- slack.postMessage:
channelId: ${alerts}
iconEmoji: ${slackEmojiConcord}
text: ${entryPoint} Deployment starting for environment *${environment}*
username: ${slackname}
是否可以使用 arguments
或 abstraction
来最大限度地减少 YAML 文件中的重复代码?
我正在编写一个触发部署的 YAML 文件,在部署之前和之后我想调用一个松弛通道来指示部署正在开始和完成,以及如果它失败了。
这是我写的但是感觉太冗长了:
example_deploy:
- call: notify
in:
msgText: "Deployment starting for environment *${environment}*"
- try:
- ${oneops.environmentCommitAndDeploy(environment = 'production', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: notify
in:
msgText: " :fire: Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
- exit
- call: notify
in:
msgText: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
notify:
- task: slack
in:
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiLooper}
text: "${msgText}"
现在,如果我想要 example_deploy_2
并做同样类型的事情,我是否必须重写所有代码?或者有没有办法让 "function" 或 抽象 YAML 的重复部分?
更新
我已经使用 call
来抽象对 slack 的调用,但现在我想知道我是否可以对 slack 进行通用调用并动态更新消息 - 因为现在我正在重复传递给的参数我定义为 call
ed
例子
example_deploy:
- call: slack_start_deploy
- try:
- ${transitionVariableUpdate(platform = '${platform}', environment = '${environment}', component = '${component_ear}' variables = { appVersion = '${BRANCH_NAME}-${BUILD_NUMBER}' })}
- ${environmentCommitAndDeploy(environment = 'qa', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: slack_deploy_error
- exit
- call: slack_deploy_success
slack_start_deploy:
- slack.postMessage:
text: "${entryPoint} Deployment starting for environment *${environment}*"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_error:
- slack.postMessage:
text: " :fire: ${entryPoint} Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_success:
- slack.postMessage:
text: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
YAML 规范中唯一允许 最小化重复是使用 anchor 在节点上和 使用别名引用该节点。这适用于两个叶节点 (即任何类型的标量值)和收集节点 (映射、序列)。本质上是锚定集合的别名 "replace" 集合下的整个子树。
除此之外还有 merge
映射中的 key <<
是
由大多数 YAML 加载器实现(通常在构造阶段),
你可以让一个或多个映射为
具有合并键的映射中未指定的键
(直接或通过早期处理的合并)。
最重要的是,任何使用 YAML 加载程序的程序都可以扩展加载程序 (通常是它的构造机制,但这可以更早完成 在加载过程中),因为他们认为合适,但这种机制是 不被视为 YAML 的一部分。
合并机制可以部署在你的YAML上,减少行数。如果你的例子
更改为 example.yaml
:
example_deploy:
- call: slack_start_deploy
- try:
- ${transitionVariableUpdate(platform = '${platform}', environment = '${environment}', component = '${component_ear}' variables = { appVersion = '${BRANCH_NAME}-${BUILD_NUMBER}' })}
- ${environmentCommitAndDeploy(environment = 'qa', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: slack_deploy_error
- exit
- call: slack_deploy_success
slack_start_deploy:
- slack.postMessage: &pm
text: "${entryPoint} Deployment starting for environment *${environment}*"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_error:
- slack.postMessage:
text: " :fire: ${entryPoint} Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
<<: *pm
slack_deploy_success:
- slack.postMessage:
text: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
<<: *pm
(请注意,我更改了 error:
和 - call: ...
行的缩进,如您所呈现的那样
文件无效 YAML)
在上面,&pm
是具有四个键的映射节点的锚点。 *pm
s 是别名
使用此映射,每次使用 text
.
下面的Python程序通过加载,然后转储显示合并键是如何扩展到 加载过程中您的原件。
import sys
from pathlib import Path
import ruamel.yaml
example = Path('example.yaml')
yaml = ruamel.yaml.YAML(typ='safe')
yaml.default_flow_style = False
data = yaml.load(example)
yaml.dump(data, sys.stdout)
给出:
error:
- log: 'Error trying to deploy: ${lastError.cause}'
- call: slack_deploy_error
- exit
- call: slack_deploy_success
example_deploy:
- call: slack_start_deploy
- try:
- ${transitionVariableUpdate(platform = '${platform}', environment = '${environment}',
component = '${component_ear}' variables = { appVersion = '${BRANCH_NAME}-${BUILD_NUMBER}'
})}
- ${environmentCommitAndDeploy(environment = 'qa', platform = '${platform}', deployAllPlatforms
= false )}
slack_deploy_error:
- slack.postMessage:
channelId: ${alerts}
iconEmoji: ${slackEmojiConcord}
text: ' :fire: ${entryPoint} Deployment failed for environment *${environment}*
http://concord.com/#/process/${txId}/log'
username: ${slackname}
slack_deploy_success:
- slack.postMessage:
channelId: ${alerts}
iconEmoji: ${slackEmojiConcord}
text: ' :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log'
username: ${slackname}
slack_start_deploy:
- slack.postMessage:
channelId: ${alerts}
iconEmoji: ${slackEmojiConcord}
text: ${entryPoint} Deployment starting for environment *${environment}*
username: ${slackname}