Class: Kontena::Machine::Aws::NodeDestroyer
- Inherits:
-
Object
- Object
- Kontena::Machine::Aws::NodeDestroyer
- Includes:
- Cli::ShellSpinner
- Defined in:
- lib/kontena/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 = 'eu-west-1') ⇒ NodeDestroyer
constructor
A new instance of NodeDestroyer.
- #run!(grid, name) ⇒ Object
Constructor Details
#initialize(api_client, access_key_id, secret_key, region = 'eu-west-1') ⇒ NodeDestroyer
Returns a new instance of NodeDestroyer.
14 15 16 17 18 19 20 |
# File 'lib/kontena/machine/aws/node_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_client ⇒ Object (readonly)
Returns the value of attribute api_client.
8 9 10 |
# File 'lib/kontena/machine/aws/node_destroyer.rb', line 8 def api_client @api_client end |
#ec2 ⇒ Object (readonly)
Returns the value of attribute ec2.
8 9 10 |
# File 'lib/kontena/machine/aws/node_destroyer.rb', line 8 def ec2 @ec2 end |
Instance Method Details
#run!(grid, name) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kontena/machine/aws/node_destroyer.rb', line 22 def run!(grid, 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 node = api_client.get("grids/#{grid['id']}/nodes")['nodes'].find{|n| n['name'] == name} if node spinner "Removing node #{name.colorize(:cyan)} from grid #{grid['name'].colorize(:cyan)} " do api_client.delete("nodes/#{grid['id']}/#{name}") end end end |