Manages the process of creating or updating a Chef::ApiClient on the server and writing the resulting private key to disk. Registration uses the validator credentials for its API calls. This allows it to bootstrap a new client/node identity by borrowing the validator client identity when creating a new client.
# File lib/chef/api_client/registration.rb, line 69 def assert_destination_writable! if (File.exists?(destination) && !File.writable?(destination)) or !File.writable?(File.dirname(destination)) raise Chef::Exceptions::CannotWritePrivateKey, "I cannot write your private key to #{destination} - check permissions?" end end
# File lib/chef/api_client/registration.rb, line 92 def create response = http_api.post("clients", :name => name, :admin => false) @private_key = response["private_key"] response end
# File lib/chef/api_client/registration.rb, line 83 def create_or_update create rescue Net::HTTPServerException => e # If create fails because the client exists, attempt to update. This # requires admin privileges. raise unless e.response.code == "409" update end
# File lib/chef/api_client/registration.rb, line 116 def file_flags base_flags = File::CREAT|File::TRUNC|File::RDWR # Windows doesn't have symlinks, so it doesn't have NOFOLLOW base_flags |= File::NOFOLLOW if defined?(File::NOFOLLOW) base_flags end
# File lib/chef/api_client/registration.rb, line 110 def http_api @http_api_as_validator ||= Chef::REST.new(Chef::Config[:chef_server_url], Chef::Config[:validation_client_name], Chef::Config[:validation_key]) end
Runs the client registration process, including creating the client on the chef-server and writing its private key to disk.
# File lib/chef/api_client/registration.rb, line 53 def run assert_destination_writable! retries = Config[:client_registration_retries] || 5 begin create_or_update rescue Net::HTTPFatalError => e # HTTPFatalError implies 5xx. raise if retries <= 0 retries -= 1 Chef::Log.warn("Failed to register new client, #{retries} tries remaining") Chef::Log.warn("Response: HTTP #{e.response.code} - #{e}") retry end write_key end
# File lib/chef/api_client/registration.rb, line 98 def update response = http_api.put("clients/#{name}", :name => name, :admin => false, :private_key => true) if response.respond_to?(:private_key) # Chef 11 @private_key = response.private_key else # Chef 10 @private_key = response["private_key"] end response end
Generated with the Darkfish Rdoc Generator 2.