Class: PortAuthority::Tools::ServiceList

Inherits:
PortAuthority::Tool show all
Defined in:
lib/port-authority/tools/service_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PortAuthority::Tool

#initialize

Constructor Details

This class inherits a constructor from PortAuthority::Tool

Instance Attribute Details

#etcdObject (readonly)

Returns the value of attribute etcd.



13
14
15
# File 'lib/port-authority/tools/service_list.rb', line 13

def etcd
  @etcd
end

Instance Method Details

#optparseObject



15
16
17
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
53
54
55
56
57
58
59
60
61
# File 'lib/port-authority/tools/service_list.rb', line 15

def optparse
  @options = Hash.new
  @options[:url] = ENV['ETCDCTL_ENDPOINT']
  @options[:url] ||= "http://127.0.0.1:2379"
  @options[:output_yaml] = false
  @options[:output_json] = false
  @options[:ip_only] = false
  @options[:ports] = false
  @options[:service_filter] = '.*'

  OptionParser.new do |opts|
    opts.banner = "Lists\n\nUsage: #{$0} [OPTIONS] NETWORK_NAME"
    opts.separator ""
    opts.separator "Connection options:"
    opts.on("-u", "--url URL", "URL endpoint of the ETCD service (ETCDCTL_ENDPOINT envvar also applies) [DEFAULT: http://127.0.0.1:2379]") do |param|
      @options[:url] = param
    end

    opts.separator ""
    opts.separator "Filter options:"
    opts.on("-s SERVICE_NAME", "--service-filter SERVICE_NAME", String, "List only services by name") do |param|
      @options[:service_filter] = param
    end

    opts.separator ""
    opts.separator "Output options:"
    opts.on("-i", "--ips", "List only IPs (text mode)") do
      @options[:ip_only] = true
    end
    opts.on("-p", "--ports", "List exposed ports (text mode)") do
      @options[:ports] = true
    end
    opts.on("-y", "--yaml", "Output the data as YAML") do
      @options[:output_yaml] = true
    end
    opts.on("-j", "--json", "Output the data as JSON") do
      @options[:output_json] = true
    end

    opts.separator ""
    opts.separator "Common options:"
    opts.on_tail("-h", "--help", "show usage") do |param|
      puts opts
      exit! 0
    end
  end.parse!
end

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/port-authority/tools/service_list.rb', line 63

def run
  unless @ARGS[0]
    $stderr.puts 'Missing NETWORK_NAME!'
    exit 1
  end

  @etcd = PortAuthority::Etcd.shell_cluster_connect(@options[:url])

  services = @etcd.swarm_list_services(@ARGS[0], @options[:service_filter])

  if @options[:output_yaml]
    puts services.to_yaml
  elsif @options[:output_json]
    puts services.to_json
  else
    if @options[:ip_only]
      services.each { |_, params| puts params['ip'] }
    elsif @options[:ports]
      services.each do |name, params|
        params['ports'].each do |port|
          puts "#{name} #{params['ip']}:#{port}"
        end
      end
    else
      services.each { |name, params| puts "#{name} #{params['ip']}" }
    end
  end
end