我写了一个厨师definition
发布到我们的聊天服务器。
由于不再推荐定义,我如何将其重写为资源?我对如何使用“事件”方式触发代码特别感兴趣。
File 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
现在,我已经阅读了厨师文档并尝试了各种方法(准确地说:Module
, a library
and a resource
)并阅读有关的文档厨师定制资源 https://docs.chef.io/custom_resources.html,但没有成功。
有人可以指导我:如何将此代码转换为resource
,如果这是正确的方法(厨师 12.6+)?
我很高兴知道
- 食谱资源在食谱中的位置(
chat/recipes
,或者其他地方?)
- 代码应该是什么样子(从我上面的定义转换)
- 新代码是如何调用的(来自另一个食谱),我需要其中包含任何内容吗
From 自定义资源文档 https://docs.chef.io/custom_resources.html像这样的事情应该做(未经测试):
in 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
现在在另一本食谱中:
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 资源只会在运行结束时触发。
你部署食谱将必须depends
on the chat
食谱能够调用其custom_resource。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)