Class: Kontena::Plugin::Cloud::Node::TerminateCommand

Inherits:
Command
  • Object
show all
Includes:
Cli::Common, Common, PlatformOption, Platform::Common
Defined in:
lib/kontena/plugin/cloud/node/terminate_command.rb

Constant Summary

Constants included from CloudCommand

CloudCommand::PLATFORM_NOT_SELECTED_ERROR

Instance Method Summary collapse

Methods included from PlatformOption

included

Methods included from Common

#cached_platforms_by_id, #compute_client, #compute_url, #config, #get_platform

Methods included from Platform::Common

#cached_platforms, #current_organization, #current_platform, #fetch_platforms, #fetch_platforms_for_org, #find_platform_by_name, #login_to_platform, #parse_platform_name, #platform_config_exists?, #prompt_platform, #require_platform

Methods included from CloudCommand

#verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kontena/plugin/cloud/node/terminate_command.rb', line 16

def execute
  org, platform = parse_platform_name(current_master.name)
  unless self.name
    name = prompt_name(platform, org)
  else
    exit_with_error "Invalid name" if org.nil? || platform.nil?
    name = self.name
  end

  confirm_command(name) unless forced?

  nodes = compute_client.get("/organizations/#{org}/nodes")['data'].map { |n|
    Kontena::Cli::Models::Node.new(n)
  }
  node = nodes.find { |n| n.name == name }
  exit_with_error "Node not found" unless node

  spinner "Terminating node #{pastel.cyan(node.name)} from platform #{pastel.cyan("#{org}/#{platform}")}" do
    compute_client.delete("/organizations/#{org}/nodes/#{node.id}")
  end
  platform = get_platform(org, node.platform_id)
  if platform
    client.delete("nodes/#{current_grid}/#{name}")
  end
end

#prompt_name(platform, org) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kontena/plugin/cloud/node/terminate_command.rb', line 42

def prompt_name(platform, org)
  platform = find_platform_by_name(platform, org)
  exit_with_error "Platform not selected" unless platform

  nodes = compute_client.get("/organizations/#{org}/nodes")['data'].map { |n|
    Kontena::Cli::Models::Node.new(n)
  }
  nodes.delete_if { |n| n.platform_id != platform.id || n.state == 'terminated' }
  exit_with_error "No nodes" if nodes.size == 0
  grid_nodes = client.get("grids/#{current_grid}/nodes")['nodes']
  prompt.select("Choose node") do |menu|
    nodes.each do |node|
      grid_node = grid_nodes.find { |n| n['name'] == node.name }
      if grid_node
        menu.choice "#{node.name} #{grid_node['initial_member'] ? '(initial)' : ''}", node.name
      else
        menu.choice "#{node.name} (orphan)", node.name
      end
    end
  end
end