From ceb801ff008b6125d3a359635295f5091088f520 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 25 Mar 2015 08:33:18 +0000 Subject: [PATCH] Only load pony when we need it This way we can avoid mucking around with rescue to avoid exceptions if it isn't installed yet. --- .../chef/templates/default/client.rb.erb | 22 ++++++------------ .../chef/templates/default/report.rb.erb | 23 ++++++++++--------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/cookbooks/chef/templates/default/client.rb.erb b/cookbooks/chef/templates/default/client.rb.erb index 52cbbdf01..dff9d19ed 100644 --- a/cookbooks/chef/templates/default/client.rb.erb +++ b/cookbooks/chef/templates/default/client.rb.erb @@ -29,23 +29,15 @@ chef_server_url "https://chef.openstreetmap.org" Ohai::Config[:plugin_path] << "<%= node[:ohai][:plugin_dir] %>" -begin +# Load supporting code for report handlers - # Load supporting code for report handlers +require "/etc/chef/report" - require "/etc/chef/report" +# Create report handler - # Create report handler +email_handler = Chef::Handler::Email.new(:to => "tom@compton.nu") - email_handler = Chef::Handler::Email.new(:to => "tom@compton.nu") +# Configure report handlers - # Configure report handlers - - exception_handlers << email_handler - report_handlers << email_handler - -rescue LoadError - - # Ignore errors in case required gems not installed yet - -end +exception_handlers << email_handler +report_handlers << email_handler diff --git a/cookbooks/chef/templates/default/report.rb.erb b/cookbooks/chef/templates/default/report.rb.erb index 5e7886029..0317f9286 100644 --- a/cookbooks/chef/templates/default/report.rb.erb +++ b/cookbooks/chef/templates/default/report.rb.erb @@ -1,34 +1,35 @@ # DO NOT EDIT - This file is being maintained by Chef require "rubygems" -require "pony" - + class Chef class Handler class Email < Chef::Handler attr_reader :config - def initialize(config={}) + def initialize(config = {}) @config = config @config[:from] ||= "root@openstreetmap.org" @config end - + def report - if failed? and not exception.is_a? SystemExit + if failed? && !exception.is_a?(SystemExit) subject = "Chef run failed on #{node.name}" message = "#{run_status.formatted_exception}\n" elsif elapsed_time > 300 subject = "Chef run took #{elapsed_time} on #{node.name}" message = "" end - - if subject - message << Array(backtrace).join("\n") - Pony.mail(:to => @config[:to], :from => @config[:from], - :subject => subject, :body => message, :via => :smtp) - end + return unless subject + + message << Array(backtrace).join("\n") + + require "pony" + + Pony.mail(:to => @config[:to], :from => @config[:from], + :subject => subject, :body => message, :via => :smtp) end end end -- 2.43.2