0

我们在节点上安排了 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工作正常。

4

1 回答 1

1

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

于 2016-10-14T09:28:12.410 回答