Last active
June 21, 2016 00:59
-
-
Save rayterrill/06278214783fdc799a5582d5e287dd74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#I spent many hours today trying to grok Handlers in Chef, and get a functional email handler in place. | |
#This is a brain dump of my configuration in the hopes that others find it useful (and to remind myself). | |
#cd to your repo's cookbooks directory, download the chef_handler cookbook, and un-tarzip | |
knife cookbook site download chef_handler | |
tar -xvzf chef_handler.tgz | |
#create a send_email.rb handler in your default chef_hander handler directory with the below results | |
#replace EMAILADDRESS, DOMAIN, EMAILSERVER with values for your environment | |
vi ~/repo/cookbooks/chef_handler/files/default/handlers/send_email.rb | |
############################################################################# | |
require 'net/smtp' | |
module MyEmail | |
class SendEmail < Chef::Handler | |
def report | |
message = "From: Chef <[email protected]>\n" | |
message << "To: Me <[email protected]>\n" | |
message << "Subject: Chef run failed\n" | |
message << "Date: #{Time.now.rfc2822}\n\n" | |
message << "Chef run failed on #{node.name}\n\n" | |
message << "------------------------------------------------------\n" | |
message << " EXCEPTION \n" | |
message << "------------------------------------------------------\n" | |
message << "#{run_status.formatted_exception}\n" | |
message << "------------------------------------------------------\n" | |
message << " STACK TRACE \n" | |
message << "------------------------------------------------------\n" | |
# Join the backtrace lines. Coerce to an array just in case. | |
message << Array(backtrace).join("\n") | |
Net::SMTP.start('EMAILSERVER.DOMAIN.com', 25) do |smtp| | |
smtp.send_message message, '[email protected]', '[email protected]' | |
end | |
end | |
end | |
end | |
############################################################################# | |
#in the cookbook in which you'd like to include notifications on failure, modify your default.rb recipe | |
#to include chef_handler, as well as a definition for your new notification using chef_handler | |
vi ~/repo/cookbooks/mycookbook/recipes/default.rb | |
############################################################################# | |
include_recipe 'chef_handler' | |
chef_handler "MyEmail::SendEmail" do | |
source "#{node['chef_handler']['handler_path']}\\send_email.rb" | |
action :nothing | |
supports :exception=>true | |
end.run_action(:enable) | |
############################################################################# | |
#upload your cookbooks to Chef server | |
knife cookbook upload chef_handler | |
knife cookbook upload mycookbook | |
#if you'd like to fail your cookbook push for testing purposes, you can use thus Ruby block from the Chef documentation to do that | |
vi ~/repo/cookbooks/mycookbook/recipes/default.rb | |
############################################################################# | |
ruby_block 'fail the run' do | |
block do | |
fail 'deliberately fail the run' | |
end | |
end | |
############################################################################# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment