Sha256: 4aacba2147d3aa662789f881c2142c0e6cd954ba8b286291027ca75d8de5086c

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require_relative 'services_helper'

module Kontena::Cli::Services
  class ListCommand < Kontena::Command
    include Kontena::Cli::Common
    include Kontena::Cli::GridOptions
    include Kontena::Cli::TableGenerator::Helper
    include ServicesHelper

    option '--stack', 'STACK', 'Stack name'

    requires_current_master
    requires_current_master_token

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

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

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

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

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

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

    def execute
      print_table(services) do |row|
        row['name'] = service_name(row)
        next if quiet?
        row['health_icon'] = health_status_icon(health_status(row))
        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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kontena-cli-1.3.0.rc1 lib/kontena/cli/services/list_command.rb
kontena-cli-1.3.0.pre2 lib/kontena/cli/services/list_command.rb