chef-infra - Chef - 如何编写包含 DSL 的自定义资源 "execute"

标签 chef-infra chef-recipe chatops

我编写了一个 Chef 定义,并将其发布到我们的聊天服务器。

既然不再推荐定义,我该如何将其重写为资源?我对如何使用“事件”方式触发代码特别感兴趣。

文件chat\definitions\post.rb:

define :chat_post do

  chat_url = 'https://chat.our.company/hooks/abcdef1234567890'
  message = params[:name]

  execute "echo" do
    command "curl -m 5 -i -X POST -d \"payload={"text": "#{message}"\" #{chat_url}"
    ignore_failure true
  end
end

调用配方中的代码:

artifacts.each do |artifactItem|
  # deploy stuff
  # ...

  chat_post "#{node['hostname']}: Deployed #{artifact_name}-#{version}"
end

现在,我已经阅读了 Chef 文档并尝试了各种东西(准确地说:一个模块,一个和一个资源)并阅读有关 chef custom resources 的文档,但没有成功。

有人可以指导我:如何将此代码转换为资源,如果这是正确的方法(chef 12.6+)?

我很高兴知道

  • 菜谱资源位于菜谱中的哪个位置(聊天/菜谱,还是其他地方?)
  • 代码应该是什么样子(从我上面的定义转换而来)
  • 如何调用新代码(来自另一个配方)以及我是否需要其中包含任何内容

最佳答案

the custom_resource doc 应该做这样的事情(未经测试):

chat/resources/message.rb中:

property :chat_url, String, default: 'https://chat.our.company/hooks/abcdef1234567890'
property :message, String, name_property: true

action :send
  execute "echo #{message}" do
    command "curl -m 5 -i -X POST -d \"payload={"text": "#{message}"\" #{chat_url}"
    ignore_failure true
  end
end

现在在另一本 Recipe 中:

artifacts.each do |artifactItem|
  # prepare the message:

  chat_message "#{node['hostname']}: Deployed #{artifact_name}-#{version}" do
    action :nothing
  end

  # deploy stuff
  # dummy code follow
  deploy artifactItem['artifact_name'] do
    source "whatever_url/#{artifactItem}
    notifies :send,"chat_message[#{node['hostname']}: Deployed #{artifactItem["artifact_name"]}-#{artifactItem['artifact_name']}]"
  end
end

默认情况下,通知会延迟,因此 chat_message 资源只会在运行结束时触发。

您部署的说明书必须取决于聊天说明书才能调用其custom_resource。

关于chef-infra - Chef - 如何编写包含 DSL 的自定义资源 "execute",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39488843/

相关文章:

rvm - 我如何与 Chef 一起使用 `bundle install`?

hadoop - 如何在Test Kitchen Chef中使用群集节点我需要与 Chef 一起在测试厨房中安装hadoop群集进行测试

ruby - 如何删除字符串的一部分?

java - 在 Chef 中禁用 docker 容器日志配置

docker - 如何启用docker镜像ssh

chef-infra - Chef中的执行顺序

ruby - 在 Chef Recipe 中部署 Java 应用程序

jenkins - 如何通过 HipChat 中的命令触发 jenkins 构建

Heroku ChatOps (Slack Integration) - 仅路由生产事件