class EC2::KeyPair

Attributes

key_pair_name[R]
pem_file[R]

Public Class Methods

new(ec2) click to toggle source
# File lib/etude_for_aws/ec2/key_pair.rb, line 5
def initialize(ec2)
  @config = ec2.config
  @gateway = ec2.gateway
  @key_pair_name = @config.key_pair_name
  path = Dir.pwd + @config.key_pair_path
  @pem_file = path + "#{@key_pair_name}.pem"
end

Public Instance Methods

create() click to toggle source
# File lib/etude_for_aws/ec2/key_pair.rb, line 13
def create
  key_pairs_result = @gateway.select_key_pairs
  key_pairs = []
  if key_pairs_result.key_pairs.count > 0
    key_pairs_result.key_pairs.each do |key_pair|
      key_pairs << key_pair.key_name
    end
  end

  unless key_pairs.include?(@key_pair_name)
    begin
      key_pair = @gateway.create_key_pairs(@key_pair_name)
      puts "Created key pair '#{key_pair.key_name}'."
      puts "\nSHA-1 digest of the DER encoded private key:"
      puts "#{key_pair.key_fingerprint}"
      puts "\nUnencrypted PEM encoded RSA private key:"
      puts "#{key_pair.key_material}"
    rescue Aws::EC2::Errors::InvalidKeyPairDuplicate
      puts "A key pair named '#{@key_pair_name}' already exists."
    end


    File.open(@pem_file, "w") do |file|
      file.puts(key_pair.key_material)
    end
  end
end
delete() click to toggle source
# File lib/etude_for_aws/ec2/key_pair.rb, line 41
def delete
  @gateway.delete_key_pairs(@key_pair_name)
  FileUtils.rm(@pem_file)
end