Class: Kontena::Cli::Stacks::ListCommand

Inherits:
Kontena::Command show all
Includes:
Common, GridOptions, Common
Defined in:
lib/kontena/cli/stacks/list_command.rb

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from Common

#abort_on_validation_errors, #current_dir, #display_notifications, #generate_services, #hint_on_validation_notifications, #require_config_file, #set_env_variables, #stack_from_yaml, #stack_name, #stacks_client

Methods included from Kontena::Cli::Services::ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #health_status, #health_status_icon, #int_to_filesize, #parse_build_args, #parse_container_name, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #restart_service, #scale_service, #show_service, #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=, #api_url_version, #ask, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_account, #current_grid, #current_grid=, #current_master, #current_master=, #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_current_account, #require_current_grid, #require_current_master, #require_token, #reset_client, #reset_cloud_client, #running_silent?, #running_verbose?, #settings, #settings_filename, #spinner, #sprint, #sputs, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning, #yes?

Methods included from GridOptions

included

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, 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



14
15
16
# File 'lib/kontena/cli/stacks/list_command.rb', line 14

def execute
  list_stacks
end

#list_stacksObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kontena/cli/stacks/list_command.rb', line 18

def list_stacks
  response = client.get("grids/#{current_grid}/stacks")

  titles = ['NAME', 'VERSION', 'SERVICES', 'STATE', 'EXPOSED PORTS']
  puts "%-60s %-10s %-10s %-10s %-50s" % titles

  response['stacks'].each do |stack|
    ports = stack_ports(stack)
    health = stack_health(stack)
    if health == :unhealthy
      icon = ''.freeze
      color = :red
    elsif health == :partial
      icon = ''.freeze
      color = :yellow
    elsif health == :healthy
      icon = ''.freeze
      color = :green
    else
      icon = ''.freeze
      color = :dim
    end

    vars = [
      icon.colorize(color),
      "#{stack['name']}",
      "#{stack['version']}",
      stack['services'].size,
      stack['state'],
      ports.join(",")
    ]

    puts "%s %-58s %-10s %-10s %-10s %-50s" % vars
  end
end

#stack_health(stack) ⇒ Symbol

Parameters:

  • stack (Hash)

Returns:

  • (Symbol)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kontena/cli/stacks/list_command.rb', line 70

def stack_health(stack)
  services_count = stack['services'].size
  return :unknown if services_count == 0

  fully_healthy_count = 0
  partial_healthy_count = 0
  unhealthy_count = 0
  unknown_count = 0
  stack['services'].each { |s|
    total = s.dig('health_status', 'total').to_i
    healthy = s.dig('health_status', 'healthy').to_i
    if total > 0 && healthy == total
      fully_healthy_count += 1
    elsif healthy < total && healthy > 0
      partial_healthy_count += 1
    elsif healthy == 0 && total > 0
      unhealthy_count += 1
    else
      unknown_count += 1
    end
  }
  return :partial if partial_healthy_count > 0
  return :partial if unhealthy_count > 0 && fully_healthy_count > 0
  return :unhealthy if unhealthy_count == services_count
  return :healthy if fully_healthy_count == services_count
  return :healthy if fully_healthy_count > 0 && unknown_count > 0

  :unknown
end

#stack_ports(stack) ⇒ Array<String>

Parameters:

  • stack (Hash)

Returns:



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kontena/cli/stacks/list_command.rb', line 56

def stack_ports(stack)
  ports = []
  stack['services'].each{|s|
    service_ports = s['ports'].map{|p|
      p['ip'] = '*' if p['ip'] == '0.0.0.0'
      "#{p['ip']}:#{p['node_port']}->#{p['container_port']}/#{p['protocol']}"
    }
    ports = ports + service_ports unless service_ports.empty?
  }
  ports
end