|
Exception and Report handlers are a feature of Chef that allow you to run code in response to a Chef run succeeding or failing.
|
| Name | Description |
|---|---|
| success?/failed? | Whether the chef run was successful |
| backtrace | The backtrace from exception, if any |
| exception | The raw exception, if any |
| formatted_exception | The exception as a formatted string, e.g. "ExceptionClass: message" |
| node | The node for the client run |
| all_resources | The list of all resources in the current run context's resource_collection |
| updated_resources | The list of all resources in the current run context's resource_collection that are marked as updated |
| elapsed_time | The time elapsed between the start and finish of the chef run |
| start_time/end_time | The time the chef run started or ended |
| run_context | An instance Chef::RunContext that keep track of the context of the chef run. It provides access several useful properties: cookbook_collection, resource_collection, definitions |
A handler may be assigned as a report or exception handler, there's no real difference in the code. Use success? or failed? if you need to know what situation caused it to run.
Opscode team member Joshua Timberman demonstrates a very simple report handler to make it clearer what resources were actually configured in the Chef run.
Writing a HandlerLet's start by writing a simple handler to send an email when a chef run fails. We'll keep things simple by using the pony library to send the email. For this example to work, you'll need to be able to send mail via /usr/sbin/sendmail or SMTP to localhost, but it should be pretty easy to extend the example if you need to. Installing and Configuring a HandlerYou can install and configure handlers either through use of the chef-handler LWRP, or manually. In a recipe with the chef_handler LWRPThe chef_handler LWRP that ships as part of the chef_handler cookbook provides an easy to use idempotent resource that can be included in your recipe code. Configuring the Handler with chef_handler LWRP To manually install and configure a handler, you'll need to use edit the Chef config file. In the Chef config (client.rb) fileThere is currently no default install location for handlers. The simplest way to distribute and install them is via rubygems, though any method of distributing the code (i.e., github, HTTP, etc.) will work. Once the handler is installed on the system, enable it in your client.rb file by requiring it. If you're using rubygems, you can require the handler by name: Loading a Handler Installed via Rubygems If you installed the handler by some other method, you will probably need to require it using the full path to the file: Loading a Handler Installed to an Arbitrary Location Once the handler is enabled, you may need to configure it. The way this is done will vary for each handler. If you have a simple handler that requires no additional configuration, you will most likely just need to create a new instance of the handler. For example, if you have a handler MyOrganization::EmailMe in which you hardcoded all the values it needs to send email, you would simply create a new instance of it: Create a New Instance of the MyOrganization::EmailMe Handler Finally, configure the handler to notify you for either failed chef runs or successful ones. Configuring Chef to Use Your Handler Putting it all together, your client.rb file would have a section like this: Configuring the Handler in Your client.rb Distributing Chef HandlersSee Distributing Chef Handlers for use of the Opscode provided cookbook to accomplish this task. Existing HandlersChef ships with a handler that will serialize the run status data to a JSON file. To use it, put the following in /etc/chef/client.rb or /etc/chef/solo.rb. /etc/chef/client.rb to use Chef's JsonFile handler LWRP entry to use Chef's JsonFile handler The data can be loaded and inspected via irb:
|
|
|



