Class: Central::Machine::Aws::NodeDestroyer
- Inherits:
-
Object
- Object
- Central::Machine::Aws::NodeDestroyer
- Defined in:
- lib/central/machine/aws/node_destroyer.rb
Instance Attribute Summary collapse
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
-
#ec2 ⇒ Object
readonly
Returns the value of attribute ec2.
Instance Method Summary collapse
-
#initialize(api_client, access_key_id, secret_key, region = 'us-west-2') ⇒ NodeDestroyer
constructor
A new instance of NodeDestroyer.
- #run!(stack, name) ⇒ Object
Constructor Details
#initialize(api_client, access_key_id, secret_key, region = 'us-west-2') ⇒ NodeDestroyer
Returns a new instance of NodeDestroyer.
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_client ⇒ Object (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 |
#ec2 ⇒ Object (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 |