【问题标题】:chef exception handling -> simplest way to use chat_message (LWRP) on :run_failed厨师异常处理->在 :run_failed 上使用 chat_message (LWRP) 的最简单方法
【发布时间】:2016-10-14 17:28:12
【问题描述】:

我们在节点上安排了 chef-client,我想设置异常处理,以便在我们的聊天中报告错误。

以前我在这里得到了帮助,写了一个 LWRP,可以发帖聊天

chat_message do
    message "Hello, World"
    channel "deployments"
end

现在,在我的配方deploy_stuff 中,我想要一种简单 的方法来报告 LWRP chat_message 的异常,我尝试了这个:

# set up error handler
Chef.event_handler do
  on :run_failed do |exception|
    chat_message ":exclamation: chef run_failed on #{Chef.run_context.node.name}: #{exception.message}"
  end
end

但是没有用:

Running handlers:
[2016-10-14T10:13:48+02:00] ERROR: Running exception handlers
Running handlers complete
[2016-10-14T10:13:48+02:00] ERROR: Exception handlers complete
Chef Client failed. 10 resources updated in 20 seconds
[2016-10-14T10:13:48+02:00] ERROR: undefined method `chat_message' for #<#<Class:0x00000005e1ef60>:0x00000005e1ee70>
[2016-10-14T10:13:48+02:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

undefined method chat_message 是什么意思 - 我不能在这里使用 LWRP 吗?

我确信 chat 食谱已包含在内,因为我的食谱中调用 chat_message 的其他代码可以正常工作。

【问题讨论】:

    标签: exception-handling chef-infra chef-recipe lwrp


    【解决方案1】:

    不,事件处理程序只运行普通的旧 Ruby 代码,而不是配方 DSL。您可以将重要的逻辑放在库方法中,并从资源和事件处理程序中调用它。

    【讨论】:

    • 库方法可以使用DSL吗?因为chat_message又使用execute do \n command "curl ...." \n end
    • 不,您可以从 DSL 调用纯 Ruby,但反之则不行。
    • 您需要使用 chef::http 客户端 API。
    • 嗯,好的。构建异常处理机制的另一种方法怎么样......可以使用 DSL 的方法?到目前为止谢谢... :)
    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2016-08-21
    • 1970-01-01
    相关资源
    最近更新 更多