diff options
author | mmzyk <mmzyk@opscode.com> | 2013-04-17 16:40:36 -0400 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2013-06-18 08:58:18 -0700 |
commit | 41187a430ee3702af913413d0a7554faba362870 (patch) | |
tree | 15ae7cc8eb7da63d3adc343578d1616dc84cb49a /lib/chef/resource_reporter.rb | |
parent | 05e528135bef550b4d9b2dc2f82bf849dbecf821 (diff) | |
download | chef-41187a430ee3702af913413d0a7554faba362870.tar.gz |
[OC-7356] Protocol Version for Reporting
- Add protocol version header
- Handle reporting protocl version mismatch error
- Clarify message when client protocol version is wrong for reporting
- Modify tests to have extra header passed into mocks
Diffstat (limited to 'lib/chef/resource_reporter.rb')
-rw-r--r-- | lib/chef/resource_reporter.rb | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index a3112116a5..ca7718b661 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -25,8 +25,6 @@ require 'chef/event_dispatch/base' class Chef class ResourceReporter < EventDispatch::Base - - class ResourceReport < Struct.new(:new_resource, :current_resource, :action, @@ -68,7 +66,6 @@ class Chef as_hash["cookbook_name"] = new_resource.cookbook_name as_hash["cookbook_version"] = new_resource.cookbook_version.version as_hash - end def finish @@ -78,7 +75,8 @@ class Chef def success? !self.exception end - end + + end # End class ResouceReport attr_reader :updated_resources attr_reader :status @@ -86,6 +84,8 @@ class Chef attr_reader :run_id attr_reader :error_descriptions + PROTOCOL_VERSION = '0.1.0' + def initialize(rest_client) if Chef::Config[:enable_reporting] && !Chef::Config[:why_run] @reporting_enabled = true @@ -108,39 +108,55 @@ class Chef @error_descriptions = {} end + def headers(additional_headers = {}) + options = {'X-Ops-Reporting-Protocol-Version' => PROTOCOL_VERSION} + options.merge(additional_headers) + end + def node_load_completed(node, expanded_run_list_with_versions, config) @node = node if reporting_enabled? begin resource_history_url = "reports/nodes/#{node.name}/runs" - server_response = @rest_client.post_rest(resource_history_url, {:action => :begin, :run_id => @run_id}) + server_response = @rest_client.post_rest(resource_history_url, {:action => :begin, :run_id => @run_id}, headers) 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] - reporting_status = "Reporting fatals enabled. Aborting run. " - Chef::Log.error(message + exception + reporting_status) - raise - else - reporting_status = "Disabling reporting for run." - Chef::Log.info(message + exception + reporting_status) - end - else - reason = "Received 404. " - reporting_status = "Disabling reporting for run." - Chef::Log.debug(message + reason + reporting_status) - end - @reporting_enabled = false + handle_error_beginning_run(e, resource_history_url) end end end + def handle_error_beginning_run(e, url) + message = "Reporting error beginning run. URL: #{url} " + code = if e.response.code + e.response.code.to_s + else + "Exception Code Empty" + end + + if !e.response || (code != "404" && code != "406") + exception = "Exception: #{code} " + if Chef::Config[:enable_reporting_url_fatals] + reporting_status = "Reporting fatals enabled. Aborting run. " + Chef::Log.error(message + exception + reporting_status) + raise + else + reporting_status = "Disabling reporting for run." + Chef::Log.info(message + exception + reporting_status) + end + else + reason = "Received #{code}. " + if code == "406" + reporting_status = "Client version not supported. Please update the client. Disabling reporting for run." + Chef::Log.info(message + reason + reporting_status) + else + reporting_status = "Disabling reporting for run." + Chef::Log.debug(message + reason + reporting_status) + end + end + + @reporting_enabled = false + end + def resource_current_state_loaded(new_resource, action, current_resource) unless nested_resource?(new_resource) @pending_update = ResourceReport.new_with_current_state(new_resource, action, current_resource) @@ -201,7 +217,7 @@ class Chef Chef::Log.debug("Sending compressed run data...") # Since we're posting compressed data we can not directly call post_rest which expects JSON reporting_url = @rest_client.create_url(resource_history_url) - @rest_client.raw_http_request(:POST, reporting_url, {'Content-Encoding' => 'gzip'}, compressed_data) + @rest_client.raw_http_request(:POST, reporting_url, headers({'Content-Encoding' => 'gzip'}), compressed_data) rescue Net::HTTPServerException => e if e.response.code.to_s == "400" Chef::FileCache.store("failed-reporting-data.json", Chef::JSONCompat.to_json_pretty(run_data), 0640) |