Chef - 如何为 "execute" 编写包含 DSL 的自定义资源
Chef - how to write a custom resource containing DSL for "execute"
我写了一个厨师 definition
发布到我们的聊天服务器。
由于不再推荐定义,我如何将其重写为资源?我对如何使用 "event" 方式触发代码特别感兴趣。
文件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 文档并尝试了各种方法(准确地说:一个 Module
、一个 library
和一个 resource
)并阅读了关于 chef custom resources,但没有成功。
有人可以指导我:如何将此代码转换为 resource
,如果这是正确的方法(chef 12.6+)?
我很想知道
- 菜谱资源在食谱中的什么位置(
chat/recipes
,还是其他地方?)
- 代码的外观(从我上面的定义转换而来)
- 新代码是如何调用的(来自另一个配方),我需要任何包含吗
来自 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
现在在另一本食谱中:
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
在 chat
说明书上才能调用其 custom_resource。
我写了一个厨师 definition
发布到我们的聊天服务器。
由于不再推荐定义,我如何将其重写为资源?我对如何使用 "event" 方式触发代码特别感兴趣。
文件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 文档并尝试了各种方法(准确地说:一个 Module
、一个 library
和一个 resource
)并阅读了关于 chef custom resources,但没有成功。
有人可以指导我:如何将此代码转换为 resource
,如果这是正确的方法(chef 12.6+)?
我很想知道
- 菜谱资源在食谱中的什么位置(
chat/recipes
,还是其他地方?) - 代码的外观(从我上面的定义转换而来)
- 新代码是如何调用的(来自另一个配方),我需要任何包含吗
来自 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
现在在另一本食谱中:
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
在 chat
说明书上才能调用其 custom_resource。