Class: MailHandler

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef/handler/mail.rb,
lib/chef/handler/mail/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MailHandler

Returns a new instance of MailHandler.


23
24
25
26
27
28
29
# File 'lib/chef/handler/mail.rb', line 23

def initialize(opts = {})
  @options = {
    :to_address => "root",
    :template_path => File.join(File.dirname(__FILE__), "mail.erb")
  }
  @options.merge! opts
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


22
23
24
# File 'lib/chef/handler/mail.rb', line 22

def options
  @options
end

Instance Method Details

#reportObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/handler/mail.rb', line 31

def report
  status = success? ? "Successful" : "Failed"
  subject = "#{status} Chef run on node #{node.fqdn}"

  Chef::Log.debug("mail handler template path: #{options[:template_path]}")
  if File.exists? options[:template_path]
    template = IO.read(options[:template_path]).chomp
  else
    Chef::Log.error("mail handler template not found: #{options[:template_path]}")
    raise Errno::ENOENT
  end

  context = {
    :status => status,
    :run_status => run_status
  }

  body = Erubis::Eruby.new(template).evaluate(context)

  Pony.mail(
    :to => options[:to_address],
    :from => "[email protected]#{node.fqdn}",
    :subject => subject,
    :body => body
  )
end