是否可以在YAML文件中使用抽象?

时间:2019-04-25 05:10:16

标签: yaml abstraction

是否可以使用argumentsabstraction 最小化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并做相同类型的事情,是否需要重写所有这些代码?还是有办法具有“功能”或抽象YAML的重复部分

更新 我使用call来抽象对slack的调用,但是现在我想知道是否可以对slack进行通用调用并动态更新消息-因为现在我要重复传递给的参数我定义为call编辑的代码块

示例

  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}

1 个答案:

答案 0 :(得分:1)

YAML规范中唯一允许 尽量减少重复使用 anchor在一个节点上 使用别名引用该节点。这适用于两个叶节点 (即任何类型的标量值)和集合节点 (映射,序列)。锚定集合的别名 “替换”集合下面的整个子树。

除了映射中还有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是别名 使用此映射,每次使用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}