Class: Kontena::Machine::Aws::MasterDestroyer

Inherits:
Object
  • Object
show all
Includes:
Cli::ShellSpinner
Defined in:
lib/kontena/machine/aws/master_destroyer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client, access_key_id, secret_key, region = 'eu-west-1') ⇒ MasterDestroyer

Returns a new instance of MasterDestroyer.

Parameters:

  • api_client (Kontena::Client)

    Kontena api client

  • access_key_id (String)

    aws_access_key_id

  • secret_key (String)

    aws_secret_access_key

  • region (String) (defaults to: 'eu-west-1')


14
15
16
17
18
19
20
# File 'lib/kontena/machine/aws/master_destroyer.rb', line 14

def initialize(api_client, access_key_id, secret_key, region = 'eu-west-1')
  @api_client = api_client
  @ec2 = ::Aws::EC2::Resource.new(
    region: region,
    credentials: ::Aws::Credentials.new(access_key_id, secret_key)
  )
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



8
9
10
# File 'lib/kontena/machine/aws/master_destroyer.rb', line 8

def api_client
  @api_client
end

#ec2Object (readonly)

Returns the value of attribute ec2.



8
9
10
# File 'lib/kontena/machine/aws/master_destroyer.rb', line 8

def ec2
  @ec2
end

Instance Method Details

#run!(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kontena/machine/aws/master_destroyer.rb', line 22

def run!(name)
  instances = ec2.instances({
    filters: [
      {name: 'tag:Name', values: [name]}
    ]
  })
  abort("Cannot find AWS instance #{name}") if instances.to_a.size == 0
  abort("There are multiple instances with name #{name}") if instances.to_a.size > 1
  instance = instances.first
  if instance
    spinner "Terminating AWS instance #{name.colorize(:cyan)} " do
      instance.terminate
      until instance.reload.state.name.to_s == 'terminated'
        sleep 1
      end
    end
  else
    abort "Cannot find instance #{name.colorize(:cyan)} in AWS"
  end
end