Class Chef::Handler
In: lib/chef/handler/error_report.rb
lib/chef/handler/json_file.rb
lib/chef/handler.rb
Parent: Object

Chef::Handler

The base class for an Exception or Notification Handler. Create your own handler by subclassing Chef::Handler. When a Chef run fails with an uncaught Exception, Chef will set the run_status on your handler and call report

Example:

  require 'net/smtp'

  module MyOrg
    class OhNoes < Chef::Handler

      def report
        # Create the email message
        message  = "From: Your Name <your@mail.address>\n"
        message << "To: Destination Address <someone@example.com>\n"
        message << "Subject: Chef Run Failure\n"
        message << "Date: #{Time.now.rfc2822}\n\n"

        # The Node is available as +node+
        message << "Chef run failed on #{node.name}\n"
        # +run_status+ is a value object with all of the run status data
        message << "#{run_status.formatted_exception}\n"
        # Join the backtrace lines. Coerce to an array just in case.
        message << Array(backtrace).join("\n")

        # Send the email
        Net::SMTP.start('your.smtp.server', 25) do |smtp|
          smtp.send_message message, 'from@address', 'to@address'
        end
      end

    end
  end

Methods

Classes and Modules

Class Chef::Handler::ErrorReport
Class Chef::Handler::JsonFile

Attributes

run_status  [R]  The Chef::RunStatus object containing data about the Chef run.

Public Class methods

The list of currently configured exception handlers

The list of currently configured report handlers

Run the exception handlers. Usually will be called by a notification from Chef::Client when the run fails.

Run the report handlers. This will usually be called by a notification from Chef::Client

Public Instance methods

Return the Hash representation of the run_status

The main entry point for report handling. Subclasses should override this method with their own report handling logic.

Runs the report handler, rescuing and logging any errors it may cause. This ensures that all handlers get a chance to run even if one fails. This method should not be overridden by subclasses unless you know what you‘re doing.

Runs the report handler without any error handling. This method should not be used directly except in testing.

[Validate]