Class: Central::Machine::Aws::NodeDestroyer

Inherits:
Object
  • Object
show all
Defined in:
lib/central/machine/aws/node_destroyer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client, access_key_id, secret_key, region = 'us-west-2') ⇒ NodeDestroyer

Returns a new instance of NodeDestroyer.

Parameters:

  • api_client (Central::Client)

    Central api client

  • access_key_id (String)

    aws_access_key_id

  • secret_key (String)

    aws_secret_access_key

  • region (String) (defaults to: 'us-west-2')


13
14
15
16
17
18
19
# File 'lib/central/machine/aws/node_destroyer.rb', line 13

def initialize(api_client, access_key_id, secret_key, region = 'us-west-2')
  @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.



7
8
9
# File 'lib/central/machine/aws/node_destroyer.rb', line 7

def api_client
  @api_client
end

#ec2Object (readonly)

Returns the value of attribute ec2.



7
8
9
# File 'lib/central/machine/aws/node_destroyer.rb', line 7

def ec2
  @ec2
end

Instance Method Details

#run!(stack, name) ⇒ Object



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

def run!(stack, 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
    ShellSpinner "Terminating AWS instance #{name.colorize(:cyan)} " do
      instance.terminate
      sleep 2 until instance.reload.state.name.to_s == 'terminated'
    end
  else
    abort "Cannot find instance #{name.colorize(:cyan)} in AWS"
  end
  node = api_client.get("stacks/#{stack['id']}/nodes")['nodes'].find { |n| n['name'] == name }
  if node
    ShellSpinner "Removing node #{name.colorize(:cyan)} from stack #{stack['name'].colorize(:cyan)} " do
      api_client.delete("stacks/#{stack['id']}/nodes/#{name}")
    end
  end
end