def register(name=Chef::Config[:node_name], destination=Chef::Config[:client_key])
if (File.exists?(destination) && !File.writable?(destination))
raise Chef::Exceptions::CannotWritePrivateKey, "I cannot write your private key to #{destination} - check permissions?"
end
nc = Chef::ApiClient.new
nc.name(name)
catch(:done) do
retries = config[:client_registration_retries] || 5
0.upto(retries) do |n|
begin
response = nc.save(true, true)
Chef::Log.debug("Registration response: #{response.inspect}")
raise Chef::Exceptions::CannotWritePrivateKey, "The response from the server did not include a private key!" unless response.has_key?("private_key")
file = ::File.open(destination, File::WRONLY|File::EXCL|File::CREAT, 0600)
file.print(response["private_key"])
file.close
throw :done
rescue IOError
raise Chef::Exceptions::CannotWritePrivateKey, "I cannot write your private key to #{destination}"
rescue Net::HTTPFatalError => e
Chef::Log.warn("Failed attempt #{n} of #{retries+1} on client creation")
raise unless e.response.code == "500"
end
end
end
true
end