Class: Kontena::Machine::Aws::KeypairProvisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/machine/aws/keypair_provisioner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key_id, secret_key, region) ⇒ KeypairProvisioner

Returns a new instance of KeypairProvisioner.

Parameters:

  • access_key_id (String)

    aws_access_key_id

  • secret_key (String)

    aws_secret_access_key

  • region (String)


11
12
13
14
15
# File 'lib/kontena/machine/aws/keypair_provisioner.rb', line 11

def initialize(access_key_id, secret_key, region)
  @ec2 = ::Aws::EC2::Resource.new(
    region: region, credentials: ::Aws::Credentials.new(access_key_id, secret_key)
  )
end

Instance Attribute Details

#ec2Object (readonly)

Returns the value of attribute ec2.



6
7
8
# File 'lib/kontena/machine/aws/keypair_provisioner.rb', line 6

def ec2
  @ec2
end

#keypair_nameObject (readonly)

Returns the value of attribute keypair_name.



6
7
8
# File 'lib/kontena/machine/aws/keypair_provisioner.rb', line 6

def keypair_name
  @keypair_name
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



6
7
8
# File 'lib/kontena/machine/aws/keypair_provisioner.rb', line 6

def public_key
  @public_key
end

#regionObject (readonly)

Returns the value of attribute region.



6
7
8
# File 'lib/kontena/machine/aws/keypair_provisioner.rb', line 6

def region
  @region
end

Instance Method Details

#run!(opts) ⇒ Object

Parameters:

  • opts (Hash)


28
29
30
31
32
33
34
35
# File 'lib/kontena/machine/aws/keypair_provisioner.rb', line 28

def run!(opts)
  validate_opts!(opts)
  ec2.import_key_pair(
    key_name: keypair_name,
    public_key_material: public_key,
    dry_run: false
  )
end

#validate_opts!(opts) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/kontena/machine/aws/keypair_provisioner.rb', line 17

def validate_opts!(opts)
  if opts[:public_key]
    @public_key = opts[:public_key]
  else
    raise "Missing public key"
  end

  @keypair_name = opts[:keypair_name] || "kontena-#{SecureRandom.hex(4)}-#{Time.now.strftime '%Y-%m-%d'}"
end