Sha256: 8a62ac1f8c6b2fd19cdfba323a9aac6eb558915e0bece068048a60b5f0d147f7

Contents?: true

Size: 888 Bytes

Versions: 7

Compression:

Stored size: 888 Bytes

Contents

require 'aws-sdk-ec2'
require 'logger'
require 'securerandom'
require 'tempfile'
require 'pathname'

module AmiSpec
  class AwsKeyPair

    def self.create(**args)
      new(**args).tap(&:create)
    end

    def initialize(ec2: Aws::EC2::Resource.new, key_name_prefix: 'ami-spec-', logger: Logger.new(STDOUT))
      @ec2 = ec2
      @key_name = "#{key_name_prefix}#{SecureRandom.uuid}"
      @logger = logger
    end

    attr_reader :key_name, :key_file

    def create
      @logger.info "Creating temporary AWS key pair: #{@key_name}"
      @key_pair = @ec2.create_key_pair(key_name: @key_name)
      @temp_file = Tempfile.new('key')
      @temp_file.write(@key_pair.key_material)
      @temp_file.close
      @key_file = Pathname.new(@temp_file.path)
    end

    def delete
      @logger.info "Deleting temporary AWS key pair: #{@key_name}"
      @key_pair.delete
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ami_spec-1.8.2 lib/ami_spec/aws_key_pair.rb
ami_spec-1.8.1 lib/ami_spec/aws_key_pair.rb
ami_spec-1.8.0 lib/ami_spec/aws_key_pair.rb
ami_spec-1.7.0 lib/ami_spec/aws_key_pair.rb
ami_spec-1.6.0 lib/ami_spec/aws_key_pair.rb
ami_spec-1.5.0 lib/ami_spec/aws_key_pair.rb
ami_spec-1.4.0 lib/ami_spec/aws_key_pair.rb