Class: Chef::Reporting::ResourceReporter

Inherits:
EventDispatch::Base
  • Object
show all
Defined in:
lib/chef_reporting/resource_reporter.rb

Defined Under Namespace

Classes: ResourceReport

Constant Summary collapse

PROTOCOL_VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rest_client) ⇒ ResourceReporter



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

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
  begin
    @run_id = uuid
  rescue LoadError
    Chef::Log.debug("Can't load SecureRandom - disabling resource_reporter")
    @reporting_enabled = false
  end
  @rest_client = rest_client
  @error_descriptions = {}
end

Instance Attribute Details

#error_descriptionsObject (readonly)

Returns the value of attribute error_descriptions.



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

def error_descriptions
  @error_descriptions
end

#exceptionObject (readonly)

Returns the value of attribute exception.



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

def exception
  @exception
end

#run_idObject (readonly)

Returns the value of attribute run_id.



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

def run_id
  @run_id
end

#statusObject (readonly)

Returns the value of attribute status.



82
83
84
# File 'lib/chef_reporting/resource_reporter.rb', line 82

def status
  @status
end

#updated_resourcesObject (readonly)

End class ResouceReport



81
82
83
# File 'lib/chef_reporting/resource_reporter.rb', line 81

def updated_resources
  @updated_resources
end

Instance Method Details

#cookbook_resolution_failed(expanded_run_list, exception) ⇒ Object



280
281
282
283
# File 'lib/chef_reporting/resource_reporter.rb', line 280

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



285
286
287
288
# File 'lib/chef_reporting/resource_reporter.rb', line 285

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

#end_timeObject



247
248
249
# File 'lib/chef_reporting/resource_reporter.rb', line 247

def end_time
  @run_status.end_time
end

#handle_error_starting_run(e, url) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef_reporting/resource_reporter.rb', line 124

def handle_error_starting_run(e, url)
  message = "Reporting error starting 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

#headers(additional_headers = {}) ⇒ Object



234
235
236
237
# File 'lib/chef_reporting/resource_reporter.rb', line 234

def headers(additional_headers = {})
  options = {'X-Ops-Reporting-Protocol-Version' => PROTOCOL_VERSION}
  options.merge(additional_headers)
end

#node_nameObject



239
240
241
# File 'lib/chef_reporting/resource_reporter.rb', line 239

def node_name
  @run_status.node.name
end

#post_reporting_dataObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/chef_reporting/resource_reporter.rb', line 209

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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/chef_reporting/resource_reporter.rb', line 251

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"] = @run_status.node.run_list.to_json
  run_data["total_res_count"] = @total_res_count.to_s
  run_data["data"] = {}
  run_data["start_time"] = start_time.to_s
  run_data["end_time"] = end_time.to_s

  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

#reporting_enabled?Boolean



290
291
292
# File 'lib/chef_reporting/resource_reporter.rb', line 290

def reporting_enabled?
  @reporting_enabled
end

#resource_completed(new_resource) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/chef_reporting/resource_reporter.rb', line 186

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



156
157
158
159
160
# File 'lib/chef_reporting/resource_reporter.rb', line 156

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



176
177
178
179
180
181
182
183
184
# File 'lib/chef_reporting/resource_reporter.rb', line 176

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



167
168
169
170
# File 'lib/chef_reporting/resource_reporter.rb', line 167

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



162
163
164
165
# File 'lib/chef_reporting/resource_reporter.rb', line 162

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



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

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

#run_completed(node) ⇒ Object



194
195
196
197
# File 'lib/chef_reporting/resource_reporter.rb', line 194

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

#run_failed(exception) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/chef_reporting/resource_reporter.rb', line 199

def run_failed(exception)
  @exception = exception
  @status = "failure"
  # If we failed before we received the run_started callback, there's not much we can do
  # in terms of reporting
  if @run_status
    post_reporting_data
  end
end

#run_list_expand_failed(node, exception) ⇒ Object



275
276
277
278
# File 'lib/chef_reporting/resource_reporter.rb', line 275

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

#run_started(run_status) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chef_reporting/resource_reporter.rb', line 110

def run_started(run_status)
  @run_status = run_status

  if reporting_enabled?
    begin
      resource_history_url = "reports/nodes/#{node_name}/runs"
      server_response = @rest_client.post_rest(resource_history_url, {:action => :start, :run_id => @run_id,
                                                                      :start_time => start_time.to_s}, headers)
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      handle_error_starting_run(e, resource_history_url)
    end
  end
end

#start_timeObject



243
244
245
# File 'lib/chef_reporting/resource_reporter.rb', line 243

def start_time
  @run_status.start_time
end