Class: Gaptool::TerminateCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/gaptool_client/commands.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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