【问题标题】:Chef - how to write a custom resource containing DSL for "execute"Chef - 如何为“执行”编写包含 DSL 的自定义资源
【发布时间】:2016-10-12 19:30:51
【问题描述】:

我写了一个厨师 definition 来发帖到我们的聊天服务器。

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

文件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 文档并尝试了各种方法(准确地说:Modulelibraryresource)并阅读了有关 chef custom resources 的文档,但没有成功。

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

很高兴知道

  • 食谱资源在食谱中的哪个位置(chat/recipes,或其他地方?)
  • 代码的外观(从我上面的定义转换而来)
  • 新代码是如何调用的(来自另一个配方),我需要在那里包含任何内容

【问题讨论】:

    标签: chef-infra chef-recipe chatops


    【解决方案1】:

    来自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 资源只会在运行结束时触发。

    您部署说明书必须在 chat 说明书上使用 depends 才能调用其 custom_resource。

    【讨论】:

    • 太好了,谢谢! name_property 的东西非常整洁。有什么办法可以给你买啤酒吗?这对我帮助很大。
    • 谢谢,很高兴它有帮助;)
    • 顺便说一句,如果资源只有一个动作,似乎你不需要在notifies中指定:send
    • 是的,但我将始终指定操作作为良好做法。如果您向资源添加第二个操作,这可以避免破坏所有内容(例如 send_SMS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2015-07-20
    • 1970-01-01
    相关资源
    最近更新 更多