Skip to content

Instantly share code, notes, and snippets.

@mago0
Created June 12, 2012 19:14
Show Gist options
  • Save mago0/2919532 to your computer and use it in GitHub Desktop.
Save mago0/2919532 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'chef'
require 'chef/handler'
require 'erubis'
require 'pony'
require 'pp'
class MailHandler < Chef::Handler
attr_reader :options
def initialize(opts = {})
@options = {
:to_address => "root",
:template_path => File.join(File.dirname(__FILE__), "mail.erb")
}
@options.merge! opts
end
def report
notify = false
run_status.updated_resources.each { |r|
if r.resource_name.to_s == "git"
notify = true
end
}
if notify
status = success? ? "Successful" : "Failed"
subject = "#{status} Chef run with git deploy 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 => "chef-client@#{node.fqdn}",
:subject => subject,
:body => body
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment