Class: Chef::Handler

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/chef/handler.rb,
lib/chef/handler/json_file.rb,
lib/chef/handler/error_report.rb

Overview

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 <[email protected]>\n"
      message << "To: Destination Address <[email protected]>\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

Direct Known Subclasses

ErrorReport, JsonFile

Defined Under Namespace

Classes: ErrorReport, JsonFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#run_statusObject (readonly)

The Chef::RunStatus object containing data about the Chef run.



133
134
135
# File 'lib/chef/handler.rb', line 133

def run_status
  @run_status
end

Class Method Details

.exception_handlersObject

The list of currently configured exception handlers



107
108
109
# File 'lib/chef/handler.rb', line 107

def self.exception_handlers
  Array(Chef::Config[:exception_handlers])
end

.report_handlersObject

The list of currently configured report handlers



82
83
84
# File 'lib/chef/handler.rb', line 82

def self.report_handlers
  Array(Chef::Config[:report_handlers])
end

.run_exception_handlers(run_status) ⇒ Object

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



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/handler.rb', line 113

def self.run_exception_handlers(run_status)
  events = run_status.events
  events.handlers_start(exception_handlers.size)
  Chef::Log.error("Running exception handlers")
  exception_handlers.each do |handler|
    handler.run_report_safely(run_status)
    events.handler_executed(handler)
  end
  events.handlers_completed
  Chef::Log.error("Exception handlers complete")
end

.run_report_handlers(run_status) ⇒ Object

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



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef/handler.rb', line 88

def self.run_report_handlers(run_status)
  events = run_status.events
  events.handlers_start(report_handlers.size)
  Chef::Log.info("Running report handlers") unless Chef::Config[:quiet_logging]
  report_handlers.each do |handler|
    handler.run_report_safely(run_status)
    events.handler_executed(handler)
  end
  events.handlers_completed
  Chef::Log.info("Report handlers complete") unless Chef::Config[:quiet_logging]
end

.run_start_handlers(run_status) ⇒ Object

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



67
68
69
70
71
72
73
# File 'lib/chef/handler.rb', line 67

def self.run_start_handlers(run_status)
  Chef::Log.info("Running start handlers") unless Chef::Config[:quiet_logging]
  start_handlers.each do |handler|
    handler.run_report_safely(run_status)
  end
  Chef::Log.info("Start handlers complete.") unless Chef::Config[:quiet_logging]
end

.start_handlersObject

The list of currently configured start handlers



61
62
63
# File 'lib/chef/handler.rb', line 61

def self.start_handlers
  Array(Chef::Config[:start_handlers])
end

Instance Method Details

#dataObject

Return the Hash representation of the run_status



230
231
232
# File 'lib/chef/handler.rb', line 230

def data
  @run_status.to_hash
end

#reportObject

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



206
207
# File 'lib/chef/handler.rb', line 206

def report
end

#run_report_safely(run_status) ⇒ Object

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.



213
214
215
216
217
218
219
220
# File 'lib/chef/handler.rb', line 213

def run_report_safely(run_status)
  run_report_unsafe(run_status)
rescue Exception => e
  Chef::Log.error("Report handler #{self.class.name} raised #{e.inspect}")
  Array(e.backtrace).each { |line| Chef::Log.error(line) }
ensure
  @run_status = nil
end

#run_report_unsafe(run_status) ⇒ Object

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



224
225
226
227
# File 'lib/chef/handler.rb', line 224

def run_report_unsafe(run_status)
  @run_status = run_status
  report
end