Chef infra Chef-如何为“编写包含DSL的自定义资源”;执行;

Chef infra Chef-如何为“编写包含DSL的自定义资源”;执行;,chef-infra,chef-recipe,chatops,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

我已经写了一个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 12.6+),如何将此代码转换为
资源

我很想知道

  • 食谱资源在食谱中的哪个位置(
    chat/recipes
    ,或者其他地方?)
  • 代码的外观(从上面的定义转换)
  • 新代码是如何调用的(来自另一个配方),我是否需要任何包含在那里的代码
从类似这样的事情中应该做到(未经测试):

在聊天室/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
现在在另一本食谱中:

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
默认情况下,通知会延迟,因此聊天室消息资源将仅在运行结束时触发


您部署的cookbook必须
依赖于
聊天
cookbook才能调用其自定义资源。

太好了,谢谢!房地产的名字很整洁。我能给你买杯啤酒吗?这对我帮助很大。谢谢,很高兴它帮了我;)顺便说一句,如果资源只有一个操作,那么您似乎不需要在
通知中指定
:send
,是的,但我认为最好总是指定操作。如果向资源添加第二个操作(例如发送短信),这可以避免破坏一切