Chef-如何编写包含DSL的自定义资源以“执行”程序? [英] Chef - how to write a custom resource containing DSL for "execute"

查看:58
本文介绍了Chef-如何编写包含DSL的自定义资源以“执行”程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个厨师定义发布到我们的聊天服务器。

I have written a chef definition that posts to our chat server.

由于不建议使用任何定义还有,我该如何将其重写为资源?我对如何使用事件方式触发代码特别感兴趣。

Since definitions are not recommended any more, how can I rewrite this as a resource? I'm particularly interested in how to use "event" ways to trigger the code.

文件聊天\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

现在,我已经阅读了厨师文档并尝试了各种方法(准确地说:a 模块资源),并阅读有关首席定制资源,但没有成功。

Now, I have read the chef documentation and tried various things (to be precise: a Module, a library and a resource) and read the documentation about chef custom resources, but without success.

有人可以指导我吗:如果这样做是正确的方法(12.6版以上),如何将这段代码转换为资源

Can someone please guide me: how to convert this code to a resource, if that is the proper way to do it (chef 12.6+) ?

我很想知道


  • 食谱中食谱资源的位置(聊天/食谱或其他地方?)

  • 代码的外观(从上面的定义转换)

  • 新代码如何(从另一个配方中)调用,我是否需要在其中包含

  • where in the cookbook does a recipe resource go (chat/recipes, or some place else?)
  • how the code should look (converting from my definition above)
  • how is the new code called (from another recipe) and do I need any includes there

推荐答案

custom_resource文档进行(未经测试) ):

From the custom_resource doc something like this should do (untested):

聊天/资源/消息.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资源只会在运行结束时触发。

By default notifications are delayed, so the chat_message resource will fire only at end of run.

必须在聊天食谱上视情况能够调用其custom_resource。

you deploy cookbook will have to depends on the chat cookbook to be able to call its custom_resource.

这篇关于Chef-如何编写包含DSL的自定义资源以“执行”程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆