lib/chef/resource_reporter.rb in chef-10.14.0.beta.1 vs lib/chef/resource_reporter.rb in chef-10.14.0.beta.2

- old
+ new

@@ -1,8 +1,9 @@ # # Author:: Daniel DeLeo (<dan@opscode.com>) # Author:: Prajakta Purohit (prajakta@opscode.com>) +# Auther:: Tyler Cloke (<tyler@opscode.com>) # # Copyright:: Copyright (c) 2012 Opscode, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,10 +23,12 @@ require 'chef/event_dispatch/base' class Chef class ResourceReporter < EventDispatch::Base + + class ResourceReport < Struct.new(:new_resource, :current_resource, :action, :exception, :elapsed_time) @@ -49,11 +52,11 @@ as_hash = {} as_hash["type"] = new_resource.class.dsl_name as_hash["name"] = new_resource.name as_hash["id"] = new_resource.identity as_hash["after"] = new_resource.state - as_hash["before"] = current_resource.state if current_resource + as_hash["before"] = current_resource ? current_resource.state : {} as_hash["duration"] = (elapsed_time * 1000).to_i.to_s # TODO: include diffs, etc. here: as_hash["delta"] = "" # TODO: rename as "action" as_hash["result"] = action.to_s @@ -78,10 +81,11 @@ attr_reader :updated_resources attr_reader :status attr_reader :exception attr_reader :run_id + attr_reader :error_descriptions def initialize(rest_client) @reporting_enabled = true @updated_resources = [] @total_res_count = 0 @@ -89,18 +93,20 @@ @status = "success" @exception = nil @run_id = nil @rest_client = rest_client @node = nil + @error_descriptions = nil end def node_load_completed(node, expanded_run_list_with_versions, config) @node = node resource_history_url = "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}") rescue Net::HTTPServerException => e raise unless e.response.code.to_s == "404" Chef::Log.debug("Received 404 attempting to generate run history id (URL Path: #{resource_history_url}), assuming feature is not supported.") @reporting_enabled = false end @@ -116,26 +122,35 @@ @pending_update = nil unless nested_resource?(new_resource) end def resource_skipped(resource, action, conditional) @total_res_count += 1 + @pending_update = nil unless nested_resource?(resource) end def resource_updated(new_resource, action) @total_res_count += 1 - resource_completed unless nested_resource?(new_resource) end 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 - resource_completed end + description = Formatters::ErrorMapper.resource_failed_helper(new_resource, action, exception) + @error_descriptions = description.for_json end + def resource_completed(new_resource) + if @pending_update && !nested_resource?(new_resource) + @pending_update.finish + @updated_resources << @pending_update + @pending_update = nil + end + end + def run_completed(node) if reporting_enabled? resource_history_url = "nodes/#{@node.name}/runs/#{run_id}" run_data = report(node) run_data["action"] = "end" @@ -147,32 +162,47 @@ end end def run_failed(exception) @exception = exception - @status = "failed" + @status = "failure" end def report(node) run_data = {} 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 - run_data["exception"] = {} - run_data["exception"]["class"] = exception.inspect - run_data["exception"]["message"] = exception.message - run_data["exception"]["backtrace"] = exception.backtrace - run_data["exception"]["description"] = "FIXME: error inspection stuff should go here" + run_data["data"]["exception"] = {} + run_data["data"]["exception"]["class"] = exception.inspect + run_data["data"]["exception"]["message"] = exception.message + run_data["data"]["exception"]["backtrace"] = exception.backtrace + run_data["data"]["exception"]["description"] = @error_descriptions end - run_data["data"] = {} run_data end + def run_list_expand_failed(node, exception) + description = Formatters::ErrorMapper.run_list_expand_failed_helper(node, exception) + @error_descriptions = description.for_json + end + + def cookbook_resolution_failed(expanded_run_list, exception) + description = Formatters::ErrorMapper.cookbook_resolution_failed_helper(expanded_run_list, exception) + @error_descriptions = description.for_json + end + + def cookbook_sync_failed(cookbooks, exception) + description = Formatters::ErrorMapper.cookbook_sync_failed_helper(cookbooks, exception) + @error_descriptions = description.for_json + end + def reporting_enabled? @reporting_enabled end private @@ -181,15 +211,9 @@ # another resource's update, we assume that the nested resource is just the # implementation of a provider, and we want to hide it from the reporting # output. def nested_resource?(new_resource) @pending_update && @pending_update.new_resource != new_resource - end - - def resource_completed - @pending_update.finish - @updated_resources << @pending_update - @pending_update = nil end end end