diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/resource_reporter.rb | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index 0916313b73..a3112116a5 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -97,7 +97,12 @@ class Chef @pending_update = nil @status = "success" @exception = nil - @run_id = nil + begin + @run_id = uuid + rescue LoadError + Chef::Log.debug("Can't load SecureRandom - disabling resource_reporter") + @reporting_enabled = false + end @rest_client = rest_client @node = nil @error_descriptions = {} @@ -108,21 +113,28 @@ class Chef if reporting_enabled? begin resource_history_url = "reports/nodes/#{node.name}/runs" - server_response = @rest_client.post_rest(resource_history_url, {:action => :begin}) - #run_uri = URI.parse(server_response["uri"]) - #@run_id = ::File.basename(run_uri.path) - #Chef::Log.info("Chef server generated run history id: #{@run_id}") - @run_id = UUIDTools::UUID.random_create + server_response = @rest_client.post_rest(resource_history_url, {:action => :begin, :run_id => @run_id}) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e + message = "Reporting error beginning run. URL: #{resource_history_url} " if !e.response || e.response.code.to_s != "404" + code = if e.response.code + e.response.code + else + "Exception Code Empty" + end + exception = "Exception: #{code} " if Chef::Config[:enable_reporting_url_fatals] - Chef::Log.error("Received exception #{"(" + e.response.code + ") " if e.response.code}attempting to generate run history id (URL Path: #{resource_history_url}), and enable_reporting_url_fatals is set, aborting run.") + reporting_status = "Reporting fatals enabled. Aborting run. " + Chef::Log.error(message + exception + reporting_status) raise else - Chef::Log.info("Received exception #{"(" + e.response.code + ") " if e.response.code}attempting to generate run history id (URL Path: #{resource_history_url}), disabling reporting for this run.") + reporting_status = "Disabling reporting for run." + Chef::Log.info(message + exception + reporting_status) end else - Chef::Log.debug("Received 404 attempting to generate run history id (URL Path: #{resource_history_url}), assuming feature is not supported on server.") + reason = "Received 404. " + reporting_status = "Disabling reporting for run." + Chef::Log.debug(message + reason + reporting_status) end @reporting_enabled = false end @@ -260,5 +272,13 @@ class Chef end end + def uuid + require 'securerandom' + # Ruby 1.8.7 does not have SecureRandom::uuid, so copy the 1.9.2 impl here + ary = SecureRandom.random_bytes(16).unpack("NnnnnN") + ary[2] = (ary[2] & 0x0fff) | 0x4000 + ary[3] = (ary[3] & 0x3fff) | 0x8000 + "%08x-%04x-%04x-%04x-%04x%08x" % ary + end end end |