lib/chef-irc-snitch.rb in chef-irc-snitch-0.0.7 vs lib/chef-irc-snitch.rb in chef-irc-snitch-0.0.8

- old
+ new

@@ -11,51 +11,51 @@ def initialize(irc_uri, github_user, github_token, ssl = false) @irc_uri = irc_uri @github_user = github_user @github_token = github_token @ssl = ssl + @timestamp = Time.now.getutc end def report - Chef::Log.error("Chef run failed @ #{Time.now.getutc}, snitchin' to chefs via IRC") + message = "#{run_status.formatted_exception}\n" + message << Array(backtrace).join("\n") - gist = "#{run_status.formatted_exception}\n" - gist << Array(backtrace).join("\n") + if STDOUT.tty? + Chef::Log.error("Chef run failed @ #{@timestamp}") + puts message + else + Chef::Log.error("Chef run failed @ #{@timestamp}, snitchin' to chefs via IRC") - max_attempts = 2 - gist_id = nil + gist_id = nil - begin - timeout(8) do - res = Net::HTTP.post_form(URI.parse("http://gist.github.com/api/v1/json/new"), { - "files[#{node.name}-#{Time.now.to_i.to_s}]" => gist, - "login" => @github_user, - "token" => @github_token, - "description" => "Chef run failed on #{node.name} @ #{Time.now.getutc}", - "public" => false - }) - gist_id = JSON.parse(res.body)["gists"].first["repo"] - Chef::Log.info("Created a GitHub Gist @ https://gist.github.com/#{gist_id}") + begin + timeout(10) do + res = Net::HTTP.post_form(URI.parse("http://gist.github.com/api/v1/json/new"), { + "files[#{node.name}-#{@timestamp.to_i.to_s}]" => message, + "login" => @github_user, + "token" => @github_token, + "description" => "Chef run failed on #{node.name} @ #{@timestamp}", + "public" => false + }) + gist_id = JSON.parse(res.body)["gists"].first["repo"] + Chef::Log.info("Created a GitHub Gist @ https://gist.github.com/#{gist_id}") + end + rescue Timeout::Error + Chef::Log.error("Timed out while attempting to create a GitHub Gist") end - rescue Timeout::Error - Chef::Log.error("Timed out while attempting to create a GitHub Gist, retrying ...") - max_attempts -= 1 - retry if max_attempts > 0 - end - max_attempts = 2 - ip_address = (node.has_key? :ec2) ? node.ec2.public_ipv4 : node.ipaddress - message = "Chef run failed on #{node.name} : #{ip_address} : #{node.roles.join(", ")} : https://gist.github.com/#{gist_id}" + ip_address = (node.has_key? :ec2) ? node.ec2.public_ipv4 : node.ipaddress + message = "Chef run failed on #{node.name} : #{ip_address} : #{node.roles.join(", ")} : https://gist.github.com/#{gist_id}" - begin - timeout(8) do - CarrierPigeon.send(:uri => @irc_uri, :message => message, :ssl => @ssl) - Chef::Log.info("Informed chefs via IRC : #{message}") + begin + timeout(10) do + CarrierPigeon.send(:uri => @irc_uri, :message => message, :ssl => @ssl) + Chef::Log.info("Informed chefs via IRC '#{message}'") + end + rescue Timeout::Error + Chef::Log.error("Timed out while attempting to message Chefs via IRC") end - rescue Timeout::Error - Chef::Log.error("Timed out while attempting to message Chefs via IRC, retrying ...") - max_attempts -= 1 - retry if max_attempts > 0 end end end