We have chef-client scheduled on our nodes and I'll like to set up exception handling so the error is reported in our chat.
Previously I got help here to write a LWRP which posts to chat
chat_message do
message "Hello, World"
channel "deployments"
end
Now, in my recipe deploy_stuff
, I would like a simple way to report exceptions with the LWRP chat_message
, and I tried this:
# 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
However it didn't work:
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)
What does undefined method chat_message
mean - can't I use a LWRP here?
I am sure the chat
cookbook is included because other code in my recipe calling chat_message
works fine.
No, the event handler just runs plain old Ruby code, not the recipe DSL. You could put the important logic in a library method and call it from both the resource and the event handler.