class Gaptool::TerminateCommand

Public Instance Methods

execute() click to toggle source
# File lib/gaptool_client/commands.rb, line 48
def execute
  node = Gaptool::API.client.getonenode(instance)
  nodes = [node]
  Gaptool::Helpers.info(nodes, false, false)
  zone = node['zone'][0..-2]
  if node['environment'] != environment || node['role'] != role
    Gaptool::Helpers.error("'#{node['role']}-#{node['environment']}' do not match provided value (#{role}-#{environment})")
  end
  if node['terminable'] == false
    puts Rainbow('"terminate" command is disabled for this instance').yellow
    puts 'To terminate the instance, set it as terminable first running:'
    puts Rainbow("gt set -k terminable -v true -i #{instance}").cyan
    puts
    Gaptool::Helpers.error("Cannot terminate instance #{instance}",
                           code: 2)
  end
  print Rainbow('Terminate instance? [type yes to confirm]: ').green
  res = $stdin.gets.chomp
  return 0 unless res.downcase == 'yes'
  puts "Terminating instance #{node['role']}:#{node['environment']}:#{node['instance']} in region #{zone}"
  begin
    res = Gaptool::API.client.terminatenode(instance, zone)
  rescue => e
    Gaptool::Helpers.error("Cannot terminate instance: #{e}")
  end
  Gaptool::Helpers.error("Cannot terminate instance: #{res['message']}") \
    if res['result'] == 'error'
  puts Rainbow("Successfully terminated instance #{instance}").green
end