@@ -66,26 +66,29 @@ def load_signing_key(key)
6666
6767# Register the client
6868def register ( name = Chef ::Config [ :node_name ] , destination = Chef ::Config [ :client_key ] )
69-
70- if File . exists? ( destination )
71- raise Chef ::Exceptions ::CannotWritePrivateKey , "I cannot write your private key to #{ destination } - check permissions?" unless File . writable? ( destination )
72- end
69+ raise Chef ::Exceptions ::CannotWritePrivateKey , "I cannot write your private key to #{ destination } - check permissions?" if ( File . exists? ( destination ) && !File . writable? ( destination ) )
7370
7471nc = Chef ::ApiClient . new
7572nc . name ( name )
76- response = nc . save ( true , true )
77-
78- Chef ::Log . debug ( "Registration response: #{ response . inspect } " )
79-
80- raise Chef ::Exceptions ::CannotWritePrivateKey , "The response from the server did not include a private key!" unless response . has_key? ( "private_key" )
8173
82- begin
83- # Write out the private key
84- file = File . open ( destination , File ::WRONLY |File ::EXCL |File ::CREAT , 0600 )
85- file . print ( response [ "private_key" ] )
86- file . close
87- rescue
88- raise Chef ::Exceptions ::CannotWritePrivateKey , "I cannot write your private key to #{ destination } "
74+ catch ( :done ) do
75+ retries = Chef ::Config [ :client_registration_retries ] || 5
76+ retries . downto ( 0 ) do
77+ begin
78+ response = nc . save ( true , true )
79+ Chef ::Log . debug ( "Registration response: #{ response . inspect } " )
80+ raise Chef ::Exceptions ::CannotWritePrivateKey , "The response from the server did not include a private key!" unless response . has_key? ( "private_key" )
81+ # Write out the private key
82+ file = File . open ( destination , "w" )
83+ file . print ( response [ "private_key" ] )
84+ file . close
85+ throw :done
86+ rescue IOError
87+ raise Chef ::Exceptions ::CannotWritePrivateKey , "I cannot write your private key to #{ destination } "
88+ rescue Net ::HTTPFatalError => e
89+ raise unless e . response . code == "500"
90+ end
91+ end
8992end
9093
9194true
0 commit comments