lib/rudy/routines/handlers/keypair.rb in rudy-0.9.1 vs lib/rudy/routines/handlers/keypair.rb in rudy-0.9.2

- old
+ new

@@ -21,21 +21,37 @@ end def create(name=:root) keyname = user_keypairname name kp_file = pkey name - kp = nil - if registered? keyname - raise PrivateKeyNotFound, keyname if kp_file.nil? + + if registered? name && !@@global.force raise PrivateKeyNotFound, kp_file if !File.exists?(kp_file) - else - raise PrivateKeyFileExists, kp_file if File.exists?(kp_file) - li "Creating keypair: #{keyname}" - kp = Rudy::AWS::EC2::Keypairs.create(keyname) - li "Saving #{kp_file}" - Rudy::Utils.write_to_file(kp_file, kp.private_key, 'w', 0600) end + + if Rudy::AWS::EC2::Keypairs.exists? keyname + if @@global.force + li "Destroying existing keypair: #{keyname}" + Rudy::AWS::EC2::Keypairs.destroy keyname + else + raise Rudy::AWS::EC2::KeypairAlreadyDefined, keyname + end + end + + if File.exists?(kp_file) + if @@global.force + delete_pkey name + else + raise PrivateKeyFileExists, kp_file + end + end + + li "Creating keypair: #{keyname}" + kp = Rudy::AWS::EC2::Keypairs.create(keyname) + li "Saving #{kp_file}" + Rudy::Utils.write_to_file(kp_file, kp.private_key, 'w', 0600) + kp end def unregister(name=:root) keyname = user_keypairname name @@ -53,10 +69,10 @@ registered?(name) && pkey?(name) end def registered?(name=:root) keyname = user_keypairname name - Rudy::AWS::EC2::Keypairs.exists?( keyname) rescue false + Rudy::AWS::EC2::Keypairs.exists?(keyname) end def pkey(name=:root) user_keypairpath name end \ No newline at end of file