Class: Kontena::Cli::Services::ListCommand

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

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #health_status, #health_status_icon, #int_to_filesize, #parse_build_args, #parse_container_name, #parse_deploy_opts, #parse_health_check, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #render_service_deploy_instances, #restart_service, #scale_service, #show_service, #show_service_containers, #show_service_instances, #start_service, #stop_service, #update_service, #wait_for_deploy_to_finish

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 included from TableGenerator::Helper

#generate_table, included, #print_table, #table_generator

Methods included from GridOptions

included

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kontena/cli/services/list_command.rb', line 50

def execute
  print_table(services) do |row|
    row['name'] = quiet? ? service_name(row) :  health_status_icon(health_status(row)) + " " + service_name(row)
    next if quiet?
    row['stateful'] = row['stateful'] ? pastel.green('yes') : 'no'
    row['ports'] = row['ports'].map(&method(:service_port)).join(',')
    row['state'] = pastel.send(state_color(row['state']), row['state'])

    instances = [row['instance_counts']['running'], row['instances']]
    if instances.first < instances.last
      instances[0] = pastel.cyan(instances[0].to_s)
    end
    row['instances'] = instances.join(' / ')
  end
end

#fieldsObject



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

def fields
  quiet? ? ['name'] : {name: 'name', instances: 'instances', stateful: 'stateful', state: 'state', "exposed ports" => 'ports' }
end

#service_name(service) ⇒ Object



35
36
37
38
39
# File 'lib/kontena/cli/services/list_command.rb', line 35

def service_name(service)
  stack_id = stack_id(service)
  return service['name'] if stack_id == 'null'
  [ stack_id(service), service['name'] ].compact.join('/')
end

#service_port(port) ⇒ Object



23
24
25
# File 'lib/kontena/cli/services/list_command.rb', line 23

def service_port(port)
  "#{port['ip']}:#{port['node_port']}->#{port['container_port']}/#{port['protocol']}"
end

#servicesObject



15
16
17
# File 'lib/kontena/cli/services/list_command.rb', line 15

def services
  client.get("grids/#{current_grid}/services#{"?stack=#{stack}" if stack}")['services'].sort_by{|s| s['updated_at'] }.reverse
end

#stack_id(service) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/kontena/cli/services/list_command.rb', line 27

def stack_id(service)
  if quiet?
    service.fetch('stack', {}).fetch('id', 'null')
  else
    service.fetch('stack', {}).fetch('name', nil)
  end
end

#state_color(state) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/kontena/cli/services/list_command.rb', line 41

def state_color(state)
  case state
  when 'running' then :green
  when 'initialized' then :cyan
  when 'stopped' then :red
  else :blue
  end
end