summaryrefslogtreecommitdiff
path: root/lib/chef/resource_reporter.rb
diff options
context:
space:
mode:
authormmzyk <mmzyk@opscode.com>2013-04-02 14:09:37 -0400
committerBryan McLellan <btm@opscode.com>2013-06-18 08:58:18 -0700
commit05e528135bef550b4d9b2dc2f82bf849dbecf821 (patch)
tree9886d67ae8b620f9d3b2c16aee17748b68a3a987 /lib/chef/resource_reporter.rb
parent155085fd687de2ca5e3e00172b9a4c089f933e97 (diff)
downloadchef-05e528135bef550b4d9b2dc2f82bf849dbecf821.tar.gz
[OC-6569] Rework generation of run_id to be in client
* Generate run_id on client and send to server on run begin * Use SecureRandom for ID generation. This will not work on Ruby < 1.8.7. On those platforms we disable reporting * Use generated run_id in specs, checking it's passed through correctly
Diffstat (limited to 'lib/chef/resource_reporter.rb')
-rw-r--r--lib/chef/resource_reporter.rb38
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