diplomatic_bag 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/diplomat.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'json'
2
+ require 'base64'
3
+ require 'faraday'
4
+
5
+ # Top level namespace ensures all required libraries are included and initializes the gem configration.
6
+ module Diplomat
7
+ class << self
8
+ attr_accessor :root_path
9
+ attr_accessor :lib_path
10
+ attr_accessor :configuration
11
+
12
+ # Internal: Requires internal Faraday libraries.
13
+ # @param *libs One or more relative String names to Faraday classes.
14
+ # @return [nil]
15
+ def require_libs(*libs)
16
+ libs.each do |lib|
17
+ require "#{lib_path}/#{lib}"
18
+ end
19
+ end
20
+
21
+ alias require_lib require_libs
22
+ end
23
+
24
+ raise 'Diplomat only supports ruby >= 2.0.0' unless RUBY_VERSION.to_f >= 2.0
25
+
26
+ self.root_path = File.expand_path __dir__
27
+ self.lib_path = File.expand_path 'diplomat', __dir__
28
+
29
+ require_libs 'configuration', 'rest_client', 'kv', 'datacenter', 'service',
30
+ 'members', 'node', 'nodes', 'check', 'health', 'session', 'lock',
31
+ 'error', 'event', 'acl', 'maintenance', 'query', 'agent', 'status'
32
+ self.configuration ||= Diplomat::Configuration.new
33
+
34
+ class << self
35
+ # Build optional configuration by yielding a block to configure
36
+ # @yield [Diplomat::Configuration]
37
+ def configure
38
+ self.configuration ||= Diplomat::Configuration.new
39
+ yield(configuration)
40
+ end
41
+
42
+ private
43
+
44
+ # Send all other unknown commands to Diplomat::Kv
45
+ # @deprecated Please use Diplomat::Kv instead.
46
+ # @param name [Symbol] Method to send to Kv
47
+ # @param *args List of arguments to send to Kv
48
+ # @param &block block to send to Kv
49
+ # @return [Object]
50
+ def method_missing(name, *args, &block)
51
+ Diplomat::Kv.new.send(name, *args, &block) || super
52
+ end
53
+
54
+ # Make `respond_to_missing?` fall back to super
55
+ #
56
+ # @param meth_id [Symbol] the tested method
57
+ # @oaram with_private if private methods should be tested too
58
+ def respond_to_missing?(meth_id, with_private = false)
59
+ access_method?(meth_id) || super
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ # Usefull usage of Diplomat lib: Datacenter functions
2
+ module DiplomaticBag
3
+ def self.get_datacenters_list(dc, options = {})
4
+ dcs = []
5
+ datacenters = Diplomat::Datacenter.get(nil, options)
6
+ dc.each do |c|
7
+ dcs.concat(datacenters.select { |d| d[/#{c}/] })
8
+ end
9
+ dcs.uniq
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ # Usefull usage of Diplomat lib: Info functions
2
+ module DiplomaticBag
3
+ # rubocop:disable AbcSize
4
+ def self.consul_info(options = {})
5
+ consul_self = Diplomat::Agent.self(options)
6
+ puts 'Server: ' + consul_self['Config']['NodeName']
7
+ puts 'Datacenter: ' + consul_self['Config']['Datacenter']
8
+ puts 'Consul Version: ' + consul_self['Config']['Version']
9
+ if consul_self['Stats']['consul']['leader_addr']
10
+ puts 'Leader Address: ' + consul_self['Stats']['consul']['leader_addr']
11
+ puts 'applied_index: ' + consul_self['Stats']['raft']['applied_index']
12
+ puts 'commit_index: ' + consul_self['Stats']['raft']['commit_index']
13
+ end
14
+ members = Diplomat::Members.get(options)
15
+ servers = members.select { |member| member['Tags']['role'] == 'consul' }.sort_by { |n| n['Name'] }
16
+ nodes = members.select { |member| member['Tags']['role'] == 'node' }
17
+ leader = Diplomat::Status.leader(options).split(':')[0]
18
+ puts 'Servers Count: ' + servers.count.to_s
19
+ puts 'Nodes Count: ' + nodes.count.to_s
20
+ puts 'Servers:'
21
+ servers.map do |s|
22
+ if s['Tags']['role'] == 'consul'
23
+ if s['Addr'] == leader
24
+ puts ' ' + s['Name'] + ' ' + s['Addr'] + ' *Leader*'
25
+ else
26
+ puts ' ' + s['Name'] + ' ' + s['Addr']
27
+ end
28
+ end
29
+ end
30
+ end
31
+ # rubocop:enable AbcSize
32
+ end
@@ -0,0 +1,28 @@
1
+ # Usefull usage of Diplomat lib: Nodes functions
2
+ module DiplomaticBag
3
+ def self.get_duplicate_node_id(options = {})
4
+ status = {
5
+ 1 => 'Alive',
6
+ 2 => '?',
7
+ 3 => 'Left',
8
+ 4 => 'Failed'
9
+ }
10
+ result = []
11
+ members = Diplomat::Members.get(options)
12
+ grouped = members.group_by do |row|
13
+ [row['Tags']['id']]
14
+ end
15
+ filtered = grouped.values.select { |a| a.size > 1 }
16
+ filtered.each do |dup|
17
+ instance = {}
18
+ instance[:node_id] = dup[0]['Tags']['id']
19
+ nodes = []
20
+ dup.each do |inst|
21
+ nodes << { name: inst['Name'], status: status[inst['Status']] }
22
+ end
23
+ instance[:nodes] = nodes
24
+ result << instance
25
+ end
26
+ result
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ # Usefull usage of Diplomat lib: Service functions
2
+ module DiplomaticBag
3
+ def self.get_service_info(service, options = {})
4
+ result = {}
5
+ health = Diplomat::Health.service(service, options)
6
+ result[service] = {}
7
+ health.each do |h|
8
+ result[service][h['Node']['Node']] = {
9
+ 'Address': h['Node']['Address'],
10
+ 'Port': h['Service']['Port']
11
+ }
12
+ checks = {}
13
+ h['Checks'].each do |c|
14
+ checks[c['Name']] = { 'status': c['Status'], 'output': c['Output'] }
15
+ end
16
+ result[service][h['Node']['Node']]['Checks'] = checks
17
+ end
18
+ result
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ # Usefull usage of Diplomat lib: Services functions
2
+ module DiplomaticBag
3
+ def self.compute_service_name(s)
4
+ return '/node::health/' if s.nil? || s == ''
5
+
6
+ s
7
+ end
8
+
9
+ def self.get_all_services_status(options = {})
10
+ result = {}
11
+ services = Diplomat::Health.state('any', options)
12
+ grouped_by_service = services.group_by { |h| compute_service_name(h['ServiceName']) }.values
13
+ grouped_by_service.each do |s|
14
+ grouped_by_status = s.group_by { |h| h['Status'] }.values
15
+ status = {}
16
+ grouped_by_status.each do |state|
17
+ status[state[0]['Status']] = state.count
18
+ end
19
+ service_name = compute_service_name(s[0]['ServiceName'])
20
+ result[service_name] = status
21
+ end
22
+ result
23
+ end
24
+
25
+ def self.get_services_list(service, options = {})
26
+ services = Diplomat::Service.get_all(options)
27
+ services.to_h.keys.grep(/#{service}/)
28
+ end
29
+
30
+ def self.get_services_info(service, options = {})
31
+ result = []
32
+ services = get_services_list(service, options)
33
+ services.each do |s|
34
+ result << get_service_info(s.to_s, options)
35
+ end
36
+ result
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ require 'diplomat'
2
+
3
+ # Usefull usage of Diplomat lib.
4
+ module DiplomaticBag
5
+ # Load all module files
6
+ Dir[File.join(__dir__, 'diplomatic_bag', '*.rb')].each { |file| require file }
7
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diplomatic_bag
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Benoit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: diplomat
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Toolbox for Consul
28
+ email:
29
+ - n.benoit@criteo.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - features/configuration.feature
37
+ - features/step_definitions/setup_diplomat.rb
38
+ - features/step_definitions/test_key_value.rb
39
+ - lib/diplomat.rb
40
+ - lib/diplomat/acl.rb
41
+ - lib/diplomat/agent.rb
42
+ - lib/diplomat/check.rb
43
+ - lib/diplomat/configuration.rb
44
+ - lib/diplomat/datacenter.rb
45
+ - lib/diplomat/error.rb
46
+ - lib/diplomat/event.rb
47
+ - lib/diplomat/health.rb
48
+ - lib/diplomat/kv.rb
49
+ - lib/diplomat/lock.rb
50
+ - lib/diplomat/maintenance.rb
51
+ - lib/diplomat/members.rb
52
+ - lib/diplomat/node.rb
53
+ - lib/diplomat/nodes.rb
54
+ - lib/diplomat/query.rb
55
+ - lib/diplomat/rest_client.rb
56
+ - lib/diplomat/service.rb
57
+ - lib/diplomat/session.rb
58
+ - lib/diplomat/status.rb
59
+ - lib/diplomat/version.rb
60
+ - lib/diplomatic_bag.rb
61
+ - lib/diplomatic_bag/datacenters.rb
62
+ - lib/diplomatic_bag/info.rb
63
+ - lib/diplomatic_bag/nodes.rb
64
+ - lib/diplomatic_bag/service.rb
65
+ - lib/diplomatic_bag/services.rb
66
+ homepage: https://github.com/WeAreFarmGeek/diplomat
67
+ licenses:
68
+ - BSD-3-Clause
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Toolbox for Consul
89
+ test_files: []