Class: Kontena::Cli::Nodes::ListCommand

Inherits:
Kontena::Command show all
Includes:
Common, GridOptions, Helpers::HealthHelper, TableGenerator::Helper
Defined in:
lib/kontena/cli/nodes/list_command.rb

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from TableGenerator::Helper

#generate_table, included, #print_table, #table_generator

Methods included from Helpers::HealthHelper

#grid_health, #health_icon, #node_health

Methods included from GridOptions

included

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#execute ⇒ Object



77
78
79
# File 'lib/kontena/cli/nodes/list_command.rb', line 77

def execute
  print_table(node_data)
end

#fields ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/kontena/cli/nodes/list_command.rb', line 34

def fields
  return ['name'] if quiet?
  {
    name:    'name',
    version: 'agent_version',
    status:  'status',
    initial: 'initial',
    labels:  'labels'
  }
end

#grid_nodes(grid_name) ⇒ Object



49
50
51
# File 'lib/kontena/cli/nodes/list_command.rb', line 49

def grid_nodes(grid_name)
  client.get("grids/#{grid_name}/nodes")['nodes']
end

#grids ⇒ Object



45
46
47
# File 'lib/kontena/cli/nodes/list_command.rb', line 45

def grids
  all? ? client.get("grids")['grids'] : [client.get("grids/#{current_grid}")]
end

#node_data ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kontena/cli/nodes/list_command.rb', line 53

def node_data
  grids.flat_map do |grid|
    grid_nodes = []

    grid_nodes(grid['id']).each do |node|
      node['name'] = node_name(node, grid)
      grid_nodes << node
      next if quiet?
      node['initial'] = node_initial(node, grid)
      node['status'] = node_status(node)
      node['labels'] = node_labels(node)
    end

    unless quiet?
      grid_health = grid_health(grid, grid_nodes)
      grid_nodes.each do |node|
        node['name'] = health_icon(node_health(node, grid_health)) + " " + (node['name'] || '(initializing)')
      end
    end

    grid_nodes.sort_by { |n| n['node_number'] }
  end
end

#node_initial(node, grid) ⇒ Object



25
26
27
28
# File 'lib/kontena/cli/nodes/list_command.rb', line 25

def node_initial(node, grid)
  return '-' unless node['initial_member']
  "#{node['node_number']} / #{grid['initial_size']}"
end

#node_labels(node) ⇒ Object



30
31
32
# File 'lib/kontena/cli/nodes/list_command.rb', line 30

def node_labels(node)
  (node['labels'] || ['-']).join(',')
end

#node_name(node, grid) ⇒ Object



16
17
18
19
# File 'lib/kontena/cli/nodes/list_command.rb', line 16

def node_name(node, grid)
  return node['name'] unless all?
  "#{grid['name']}/#{node['name']}"
end

#node_status(node) ⇒ Object



21
22
23
# File 'lib/kontena/cli/nodes/list_command.rb', line 21

def node_status(node)
  node['connected'] ? pastel.green('online') : pastel.red('offline')
end