diff options
author | Daniel DeLeo <dan@opscode.com> | 2012-05-17 14:25:41 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2012-05-17 14:28:39 -0700 |
commit | e3102983d4e3ec776560eff269fefe0cda148d09 (patch) | |
tree | 600c1f7be3f19f21f39bb8f5e8415e2bba9a6de0 /chef/lib/chef/formatters | |
parent | 65b55369ecd27d0094913623d1b1459e3f609408 (diff) | |
download | chef-e3102983d4e3ec776560eff269fefe0cda148d09.tar.gz |
Add an error wrapper for client registration errors
Diffstat (limited to 'chef/lib/chef/formatters')
-rw-r--r-- | chef/lib/chef/formatters/doc.rb | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/chef/lib/chef/formatters/doc.rb b/chef/lib/chef/formatters/doc.rb index d3294c8189..922c719ca2 100644 --- a/chef/lib/chef/formatters/doc.rb +++ b/chef/lib/chef/formatters/doc.rb @@ -3,6 +3,9 @@ require 'chef/formatters/base' class Chef module Formatters + # == CompileErrorInspector + # Wraps exceptions that occur during the compile phase of a Chef run and + # tries to find the code responsible for the error. class CompileErrorInspector attr_reader :path @@ -52,6 +55,106 @@ class Chef end + # == RegistrationErrorInspector + # Wraps exceptions that occur during the client registration process and + # suggests possible causes. + class RegistrationErrorInspector + attr_reader :exception + attr_reader :node_name + attr_reader :config + + def initialize(node_name, exception, config) + @node_name = node_name + @exception = exception + @config = config + end + + def suspected_cause + case exception + when Net::HTTPServerException, Net::HTTPFatalError + humanize_http_exception + when Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT, SocketError + m=<<-E +There was a network error connecting to the Chef Server: +#{exception.message} + +Your chef_server_url may be misconfigured, or the network could be down. + chef_server_url "#{server_url}" +E + when Chef::Exceptions::PrivateKeyMissing + "Your validator private key could not be loaded from #{api_key}\n" + + "Check your configuration file and ensure that your validator key is readable" + else + ui.error "#{e.class.name}: #{e.message}" + end + end + + def humanize_http_exception + response = exception.response + explanation = case response + when Net::HTTPUnauthorized + # TODO: generate a different message for time skew error + m=<<-E +Failed to authenticate to the Chef Server (HTTP 401). + +One of these configuration options may be incorrect: + chef_server_url "#{server_url}" + validation_client_name "#{username}" + validation_key "#{api_key}" + +If these settings are correct, your validation_key may be invalid. +E + when Net::HTTPForbidden + m=<<-E +Your validation client is not authorized to create the client for this node (HTTP 403). + +Possible causes: +* There may already be a client named "#{config[:node_name]}" +* Your validation client (#{username}) may have misconfigured authorization permissions. +E + when Net::HTTPBadRequest + "The data in your request was invalid" + when Net::HTTPNotFound + m=<<-E +The server returned a HTTP 404. This usually indicates that your chef_server_url is incorrect. + chef_server_url "#{server_url}" +E + when Net::HTTPInternalServerError + "Chef Server had a fatal error attempting to create the client." + when Net::HTTPBadGateway, Net::HTTPServiceUnavailable + "The Chef Server is temporarily unavailable" + else + response.message + end + explanation + end + + def username + #config[:node_name] + config[:validation_client_name] + end + + def api_key + config[:validation_key] + #config[:client_key] + end + + def server_url + config[:chef_server_url] + end + + # Parses JSON from the error response sent by Chef Server and returns the + # error message + #-- + # TODO: this code belongs in Chef::REST + def format_rest_error + Array(Chef::JSONCompat.from_json(exception.response.body)["error"]).join('; ') + rescue Exception + response.body + end + + end + #-- # TODO: not sold on the name, but the output is similar to what rspec calls # "specdoc" @@ -83,6 +186,7 @@ class Chef # About to attempt to register as +node_name+ def registration_start(node_name, config) + puts "Creating a new client identity for #{node_name} using the validator key." end def registration_completed @@ -90,6 +194,13 @@ class Chef # Failed to register this client with the server. def registration_failed(node_name, exception, config) + error_inspector = RegistrationErrorInspector.new(node_name, exception, config) + puts "\n" + puts "-" * 80 + puts "Chef encountered an error attempting to create the client \"#{node_name}\"" + puts "\n" + puts error_inspector.suspected_cause + puts "-" * 80 end def node_load_start(node_name, config) |