Class: Chef::ResourceReporter

Inherits:
EventDispatch::Base show all
Defined in:
lib/chef/resource_reporter.rb

Defined Under Namespace

Classes: ResourceReport

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from EventDispatch::Base

#attribute_file_load_failed, #attribute_file_loaded, #attribute_load_complete, #attribute_load_start, #converge_complete, #converge_start, #cookbook_clean_complete, #cookbook_clean_start, #cookbook_resolution_complete, #cookbook_resolution_start, #cookbook_sync_complete, #cookbook_sync_start, #definition_file_load_failed, #definition_file_loaded, #definition_load_complete, #definition_load_start, #handler_executed, #handlers_completed, #handlers_start, #library_file_load_failed, #library_file_loaded, #library_load_complete, #library_load_start, #lwrp_file_load_failed, #lwrp_file_loaded, #lwrp_load_complete, #lwrp_load_start, #msg, #node_load_failed, #node_load_start, #ohai_completed, #provider_requirement_failed, #recipe_file_load_failed, #recipe_file_loaded, #recipe_load_complete, #recipe_load_start, #recipe_not_found, #registration_completed, #registration_failed, #registration_start, #removed_cookbook_file, #resource_action_start, #resource_bypassed, #resource_current_state_load_bypassed, #resource_failed_retriable, #resource_update_applied, #run_start, #skipping_registration, #synchronized_cookbook, #updated_cookbook_file, #whyrun_assumption

Constructor Details

#initialize(rest_client) ⇒ ResourceReporter

Returns a new instance of ResourceReporter.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/resource_reporter.rb', line 90

def initialize(rest_client)
  if Chef::Config[:enable_reporting] && !Chef::Config[:why_run]
    @reporting_enabled = true
  else
    @reporting_enabled = false
  end
  @updated_resources = []
  @total_res_count = 0
  @pending_update  = nil
  @status = "success"
  @exception = nil
  @run_id = nil
  @rest_client = rest_client
  @node = nil
  @error_descriptions = {}
  @summary_only = true
end

Instance Attribute Details

#error_descriptionsObject (readonly)

Returns the value of attribute error_descriptions.



87
88
89
# File 'lib/chef/resource_reporter.rb', line 87

def error_descriptions
  @error_descriptions
end

#exceptionObject (readonly)

Returns the value of attribute exception.



85
86
87
# File 'lib/chef/resource_reporter.rb', line 85

def exception
  @exception
end

#run_idObject (readonly)

Returns the value of attribute run_id.



86
87
88
# File 'lib/chef/resource_reporter.rb', line 86

def run_id
  @run_id
end

#statusObject (readonly)

Returns the value of attribute status.



84
85
86
# File 'lib/chef/resource_reporter.rb', line 84

def status
  @status
end

#summary_onlyObject (readonly)

Returns the value of attribute summary_only.



88
89
90
# File 'lib/chef/resource_reporter.rb', line 88

def summary_only
  @summary_only
end

#updated_resourcesObject (readonly)

Returns the value of attribute updated_resources.



83
84
85
# File 'lib/chef/resource_reporter.rb', line 83

def updated_resources
  @updated_resources
end

Instance Method Details

#cookbook_resolution_failed(expanded_run_list, exception) ⇒ Object



250
251
252
253
# File 'lib/chef/resource_reporter.rb', line 250

def cookbook_resolution_failed(expanded_run_list, exception)
  description = Formatters::ErrorMapper.cookbook_resolution_failed(expanded_run_list, exception)
  @error_descriptions = description.for_json
end

#cookbook_sync_failed(cookbooks, exception) ⇒ Object



255
256
257
258
# File 'lib/chef/resource_reporter.rb', line 255

def cookbook_sync_failed(cookbooks, exception)
  description = Formatters::ErrorMapper.cookbook_sync_failed(cookbooks, exception)
  @error_descriptions = description.for_json
end

#node_load_completed(node, expanded_run_list_with_versions, config) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chef/resource_reporter.rb', line 108

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_uri = URI.parse(server_response["uri"])
      @run_id = ::File.basename(run_uri.path)
      Chef::Log.info("Chef server generated run history id: #{@run_id}")
      @summary_only = server_response["summary_only"]
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      if !e.response || e.response.code.to_s != "404"
        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.")
          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.")
        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.")
      end
      @reporting_enabled = false
    end
  end
end

#post_reporting_dataObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/chef/resource_reporter.rb', line 183

def post_reporting_data
  if reporting_enabled?
    run_data = prepare_run_data
    resource_history_url = "reports/nodes/#{@node.name}/runs/#{@run_id}"
    Chef::Log.info("Sending resource update report (run-id: #{@run_id})")
    Chef::Log.debug run_data.inspect
    compressed_data = encode_gzip(run_data.to_json)
    begin
      #if summary only is enabled send the uncompressed run_data excluding the run_data["resources"] and some additional metrics.
      if @summary_only
        run_data = report_summary(run_data, compressed_data)
        Chef::Log.info("run_data_summary: #{run_data}")
        @rest_client.post_rest(resource_history_url, run_data)
      else
        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)
      end
    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)
        Chef::Log.error("Failed to post reporting data to server (HTTP 400), saving to #{Chef::FileCache.load("failed-reporting-data.json", false)}")
      else
        Chef::Log.error("Failed to post reporting data to server (HTTP #{e.response.code.to_s})")
      end
    end
  else
    Chef::Log.debug("Server doesn't support resource history, skipping resource report.")
  end
end

#prepare_run_dataObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/chef/resource_reporter.rb', line 216

def prepare_run_data
  run_data = {}
  run_data["action"] = "end"
  run_data["resources"] = updated_resources.map do |resource_record|
    resource_record.for_json
  end
  run_data["status"] = @status
  run_data["run_list"] = @node.run_list.to_json
  run_data["total_res_count"] = @total_res_count.to_s
  run_data["data"] = {}

  if exception
    exception_data = {}
    exception_data["class"] = exception.inspect
    exception_data["message"] = exception.message
    exception_data["backtrace"] = exception.backtrace.to_json
    exception_data["description"] =  @error_descriptions
    run_data["data"]["exception"] = exception_data
  end
  run_data
end

#report_summary(run_data, compressed_data) ⇒ Object



238
239
240
241
242
243
# File 'lib/chef/resource_reporter.rb', line 238

def report_summary(run_data, compressed_data)
  run_data["updated_res_count"] = updated_resources.count.to_s
  run_data["post_size"] = compressed_data.bytesize.to_s
  run_data["resources"] = []
  run_data
end

#reporting_enabled?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/chef/resource_reporter.rb', line 260

def reporting_enabled?
  @reporting_enabled
end

#resource_completed(new_resource) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/chef/resource_reporter.rb', line 164

def resource_completed(new_resource)
  if @pending_update && !nested_resource?(new_resource)
    @pending_update.finish
    @updated_resources << @pending_update
    @pending_update = nil
  end
end

#resource_current_state_loaded(new_resource, action, current_resource) ⇒ Object



134
135
136
137
138
# File 'lib/chef/resource_reporter.rb', line 134

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)
  end
end

#resource_failed(new_resource, action, exception) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/chef/resource_reporter.rb', line 154

def resource_failed(new_resource, action, exception)
  @total_res_count += 1
  unless nested_resource?(new_resource)
    @pending_update ||= ResourceReport.new_for_exception(new_resource, action)
    @pending_update.exception = exception
  end
  description = Formatters::ErrorMapper.resource_failed(new_resource, action, exception)
  @error_descriptions = description.for_json
end

#resource_skipped(resource, action, conditional) ⇒ Object



145
146
147
148
# File 'lib/chef/resource_reporter.rb', line 145

def resource_skipped(resource, action, conditional)
  @total_res_count += 1
  @pending_update = nil unless nested_resource?(resource)
end

#resource_up_to_date(new_resource, action) ⇒ Object



140
141
142
143
# File 'lib/chef/resource_reporter.rb', line 140

def resource_up_to_date(new_resource, action)
  @total_res_count += 1
  @pending_update = nil unless nested_resource?(new_resource)
end

#resource_updated(new_resource, action) ⇒ Object



150
151
152
# File 'lib/chef/resource_reporter.rb', line 150

def resource_updated(new_resource, action)
  @total_res_count += 1
end

#run_completed(node) ⇒ Object



172
173
174
175
# File 'lib/chef/resource_reporter.rb', line 172

def run_completed(node)
  @status = "success"
  post_reporting_data
end

#run_failed(exception) ⇒ Object



177
178
179
180
181
# File 'lib/chef/resource_reporter.rb', line 177

def run_failed(exception)
  @exception = exception
  @status = "failure"
  post_reporting_data
end

#run_list_expand_failed(node, exception) ⇒ Object



245
246
247
248
# File 'lib/chef/resource_reporter.rb', line 245

def run_list_expand_failed(node, exception)
  description = Formatters::ErrorMapper.run_list_expand_failed(node, exception)
  @error_descriptions = description.for_json
end