Module: DiplomaticBag

Defined in:
lib/diplomatic_bag.rb,
lib/diplomatic_bag/info.rb,
lib/diplomatic_bag/nodes.rb,
lib/diplomatic_bag/service.rb,
lib/diplomatic_bag/services.rb,
lib/diplomatic_bag/datacenters.rb

Overview

Usefull usage of Diplomat lib: Datacenter functions

Class Method Summary collapse

Class Method Details

.compute_service_name(s) ⇒ string

Get sevice name or empty string

Parameters:

  • s

    service information

Returns:

  • (string)

    the name of service



8
9
10
11
12
# File 'lib/diplomatic_bag/services.rb', line 8

def self.compute_service_name(s)
  return '/node::health/' if s.nil? || s == ''

  s
end

.consul_info(options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/diplomatic_bag/info.rb', line 6

def self.consul_info(options = {})
  consul_self = Diplomat::Agent.self(options)
  puts 'Server: ' + consul_self['Config']['NodeName']
  puts 'Datacenter: ' + consul_self['Config']['Datacenter']
  puts 'Consul Version: ' + consul_self['Config']['Version']
  if consul_self['Stats']['consul']['leader_addr']
    puts 'Leader Address: ' + consul_self['Stats']['consul']['leader_addr']
    puts 'applied_index: ' + consul_self['Stats']['raft']['applied_index']
    puts 'commit_index: ' + consul_self['Stats']['raft']['commit_index']
  end
  members = Diplomat::Members.get(options)
  servers = members.select { |member| member['Tags']['role'] == 'consul' }.sort_by { |n| n['Name'] }
  nodes = members.select { |member| member['Tags']['role'] == 'node' }
  leader = Diplomat::Status.leader(options).split(':')[0]
  puts 'Servers Count: ' + servers.count.to_s
  puts 'Nodes Count: ' + nodes.count.to_s
  puts 'Servers:'
  servers.map do |s|
    if s['Tags']['role'] == 'consul'
      if s['Addr'] == leader
        puts '  ' + s['Name'] + ' ' + s['Addr'] + ' *Leader*'
      else
        puts '  ' + s['Name'] + ' ' + s['Addr']
      end
    end
  end
end

.get_all_services_status(options = {}) ⇒ Object

Get the full list of services with their status

Parameters:

  • options (defaults to: {})

    to query list



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/diplomatic_bag/services.rb', line 17

def self.get_all_services_status(options = {})
  result = {}
  services = Diplomat::Health.state('any', options)
  grouped_by_service = services.group_by { |h| compute_service_name(h['ServiceName']) }.values
  grouped_by_service.each do |s|
    grouped_by_status = s.group_by { |h| h['Status'] }.values
    status = {}
    grouped_by_status.each do |state|
      status[state[0]['Status']] = state.count
    end
    service_name = compute_service_name(s[0]['ServiceName'])
    result[service_name] = status
  end
  result
end

.get_datacenters_list(dc, options = {}) ⇒ Array[string]

Return the list of datacenters matching given regexp

Returns:

  • (Array[string])

    an array of DC matching given regexp



7
8
9
10
11
12
13
14
# File 'lib/diplomatic_bag/datacenters.rb', line 7

def self.get_datacenters_list(dc, options = {})
  dcs = []
  datacenters = Diplomat::Datacenter.get(nil, options)
  dc.each do |c|
    dcs.concat(datacenters.select { |d| d[/#{c}/] })
  end
  dcs.uniq
end

.get_duplicate_node_id(options = {}) ⇒ Array[Object]

Get the list of nodes having duplicate node ids

Parameters:

  • options (defaults to: {})

    options to query with

Returns:

  • (Array[Object])

    an array of objects with node_name, status: status



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/diplomatic_bag/nodes.rb', line 8

def self.get_duplicate_node_id(options = {})
  status = {
    1 => 'Alive',
    2 => '?',
    3 => 'Left',
    4 => 'Failed'
  }
  result = []
  members = Diplomat::Members.get(options)
  grouped = members.group_by do |row|
    [row['Tags']['id']]
  end
  filtered = grouped.values.select { |a| a.size > 1 }
  filtered.each do |dup|
    instance = {}
    instance[:node_id] = dup[0]['Tags']['id']
    nodes = []
    dup.each do |inst|
      nodes << { name: inst['Name'], status: status[inst['Status']] }
    end
    instance[:nodes] = nodes
    result << instance
  end
  result
end

.get_service_info(service, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/diplomatic_bag/service.rb', line 5

def self.get_service_info(service, options = {})
  result = {}
  health = Diplomat::Health.service(service, options)
  result[service] = {}
  health.each do |h|
    result[service][h['Node']['Node']] = {
      'Address': h['Node']['Address'],
      'Port': h['Service']['Port']
    }
    checks = {}
    h['Checks'].each do |c|
      checks[c['Name']] = { 'status': c['Status'], 'output': c['Output'] }
    end
    result[service][h['Node']['Node']]['Checks'] = checks
  end
  result
end

.get_services_info(service, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/diplomatic_bag/services.rb', line 38

def self.get_services_info(service, options = {})
  result = []
  services = get_services_list(service, options)
  services.each do |s|
    result << get_service_info(s.to_s, options)
  end
  result
end

.get_services_list(service, options = {}) ⇒ Object



33
34
35
36
# File 'lib/diplomatic_bag/services.rb', line 33

def self.get_services_list(service, options = {})
  services = Diplomat::Service.get_all(options)
  services.to_h.keys.grep(/#{service}/)
end