# File lib/capazon/capistrano_plugin.rb, line 18
  def setup_keypair(auth,keypair_name,private_key_path=nil)
    amazon = connect(auth)
              
    #verify keypair doesn't already exist
    raise Exception, "Keypair #{keypair_name} already exists." unless amazon.describe_keypairs(keypair_name).parse.empty?
    
    #create keypair
    private_key = amazon.create_keypair(keypair_name)
    raise Exception, "Private Key not correctly generated" unless private_key.parse[0][0] == "KEYPAIR"
    puts "Keypair \"#{keypair_name}\" generated"
    
    unless private_key_path == nil
      #write private key to file
      text_private_key = private_key.parse.inject("") { |text_private_key, a| a.join("\t") + "\n" }
      File.open(private_key_path, 'w') do |file|
        file.write(text_private_key)
      end
      system "chmod 600 #{private_key_path}"
      puts "Written to ./#{private_key_path}"
    end
  end