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

Inherits:
Kontena::Command show all
Includes:
Common, GridOptions, Helpers::HealthHelper, Helpers::TimeHelper, 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::TimeHelper

#time_since

Methods included from Helpers::HealthHelper

#grid_health, #health_icon, #node_etcd_health, #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=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #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, #instance, 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

#executeObject



93
94
95
# File 'lib/kontena/cli/nodes/list_command.rb', line 93

def execute
  print_table(node_data)
end

#fieldsObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/kontena/cli/nodes/list_command.rb', line 49

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

#grid_nodes(grid_name) ⇒ Object



64
65
66
# File 'lib/kontena/cli/nodes/list_command.rb', line 64

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

#gridsObject



60
61
62
# File 'lib/kontena/cli/nodes/list_command.rb', line 60

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

#node_dataObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kontena/cli/nodes/list_command.rb', line 68

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['agent_version'] ||= '-'
      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'] || node['node_id'])
      end
    end

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

#node_initial(node, grid) ⇒ Object



40
41
42
43
# File 'lib/kontena/cli/nodes/list_command.rb', line 40

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

#node_labels(node) ⇒ Object



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

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

#node_name(node, grid) ⇒ Object



18
19
20
21
# File 'lib/kontena/cli/nodes/list_command.rb', line 18

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

#node_status(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kontena/cli/nodes/list_command.rb', line 23

def node_status(node)
  case node_status = node['status']
  when 'created'
    "#{pastel.dark('created')} #{time_since(node['created_at'], terse: true)}"
  when 'connecting'
    "#{pastel.cyan('connecting')} #{time_since(node['connected_at'], terse: true)}"
  when 'online'
    "#{pastel.green('online')} #{time_since(node['connected_at'], terse: true)}"
  when 'drain'
    "#{pastel.yellow('drain')}"
  when 'offline'
    "#{pastel.red('offline')} #{time_since(node['disconnected_at'], terse: true)}"
  else
    pastel.white(node_status.to_s)
  end
end