dryad-consul 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -3
- data/lib/dryad/consul.rb +47 -3
- data/lib/dryad/consul/health_check.rb +19 -12
- data/lib/dryad/consul/version.rb +1 -1
- metadata +4 -5
- data/lib/dryad/consul/railtie.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81a3b8b9de62325983f37abf4941b498727632bce970e46272eb0a1214d97b29
|
4
|
+
data.tar.gz: 0c960ad4cfaa9bf517d28058c381cef0b28e71d4e25b3b739fc460864743a438
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a9daa27bf4aa030d0c507305a2c8cbf07bf964840301067348c46556e00880dc65a8df88cfa59df9989ade601e781d2ee84c9aac096193cd59981cb288e5da3
|
7
|
+
data.tar.gz: fbdc715e93107f64bfc681c177bb3ffb41b1d22344d8cca3e262e9af0728fd76f216bf5ce716ed76ad9a086c878bfe72c8935a655a78993ae7b62780f322490e
|
data/Gemfile.lock
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
dryad-consul (1.0.
|
4
|
+
dryad-consul (1.0.1)
|
5
5
|
concurrent-ruby (~> 1.1, >= 1.1.5)
|
6
6
|
diplomat (~> 2.2, >= 2.2.4)
|
7
|
-
dryad-core (= 1.0.
|
7
|
+
dryad-core (= 1.0.1)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
@@ -15,7 +15,7 @@ GEM
|
|
15
15
|
diplomat (2.2.5)
|
16
16
|
deep_merge (~> 1.0, >= 1.0.1)
|
17
17
|
faraday (~> 0.9)
|
18
|
-
dryad-core (1.0.
|
18
|
+
dryad-core (1.0.1)
|
19
19
|
faraday (0.15.4)
|
20
20
|
multipart-post (>= 1.2, < 3)
|
21
21
|
multipart-post (2.1.1)
|
data/lib/dryad/consul.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
require "diplomat"
|
2
2
|
require "dryad/core"
|
3
3
|
require "dryad/consul/config_observer"
|
4
|
-
require "dryad/consul/railtie" if defined?(Rails)
|
5
4
|
require "dryad/consul/version"
|
6
5
|
require "dryad/consul/service_registry"
|
7
6
|
require "dryad/consul/service"
|
8
7
|
require "dryad/consul/key_value_client"
|
9
8
|
require "dryad/consul/config_provider"
|
10
9
|
require "dryad/consul/service_observer"
|
11
|
-
|
12
|
-
require "erb"
|
10
|
+
require "dryad/consul/health_check"
|
13
11
|
|
14
12
|
module Dryad
|
15
13
|
module Consul
|
@@ -20,12 +18,58 @@ module Dryad
|
|
20
18
|
if consul[:username].nil? || consul[:password].nil?
|
21
19
|
url = "http://#{consul[:host]}:#{consul[:port]}"
|
22
20
|
else
|
21
|
+
require "erb"
|
23
22
|
url = "http://#{consul[:username]}:#{ERB::Util.url_encode(consul[:password])}@#{consul[:host]}:#{consul[:port]}"
|
24
23
|
end
|
25
24
|
::Diplomat.configure do |config|
|
26
25
|
config.url = url
|
27
26
|
end
|
28
27
|
end
|
28
|
+
|
29
|
+
def build_check(check_config, name, address, port)
|
30
|
+
if check_config
|
31
|
+
interval = check_config[:interval] || 10
|
32
|
+
if !check_config[:url].nil?
|
33
|
+
http = check_config[:url]
|
34
|
+
http = "#{k.to_s}://#{address}:#{port}#{url}" if http.start_with?("/")
|
35
|
+
timeout = check_config[:timeout] || 5
|
36
|
+
Dryad::Consul::HTTPHealthCheck.new(http, interval, timeout, interval * 10)
|
37
|
+
elsif !check_config[:grpc_use_tls].nil?
|
38
|
+
grpc = "#{address}:#{port}/#{name}"
|
39
|
+
Dryad::Consul::GRPCHealthCheck.new(grpc, interval, check_config[:grpc_use_tls], interval * 10)
|
40
|
+
else
|
41
|
+
Dryad::Consul::TTLHealthCheck.new(interval, interval * 10)
|
42
|
+
end
|
43
|
+
else
|
44
|
+
Dryad::Consul::TTLHealthCheck.new(10, 100)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_service(name, group, service_config)
|
49
|
+
address = if service_config[:address]
|
50
|
+
service_config[:address]
|
51
|
+
else
|
52
|
+
require 'socket'
|
53
|
+
Socket.ip_address_list.detect{|ai| ai.ipv4_private?}&.ip_address
|
54
|
+
end
|
55
|
+
schemas = Dryad::Core::Schema.constants.map{|s| Dryad::Core::Schema.const_get(s).to_sym}
|
56
|
+
portals = service_config.slice(*schemas).map do |k, v|
|
57
|
+
params = v.slice(:port, :non_certifications).merge({
|
58
|
+
schema: k.to_s,
|
59
|
+
pattern: v[:pattern] || "/.*",
|
60
|
+
check: build_check(v[:check], name, address, v[:port])
|
61
|
+
})
|
62
|
+
Dryad::Core::Portal.new(params)
|
63
|
+
end
|
64
|
+
Dryad::Consul::Service.new({
|
65
|
+
name: name,
|
66
|
+
group: group,
|
67
|
+
address: address,
|
68
|
+
portals: portals,
|
69
|
+
priority: service_config[:priority] || 0,
|
70
|
+
load_balancing: service_config[:load_balancing] || []
|
71
|
+
})
|
72
|
+
end
|
29
73
|
end
|
30
74
|
end
|
31
75
|
end
|
@@ -1,40 +1,47 @@
|
|
1
1
|
module Dryad
|
2
2
|
module Consul
|
3
3
|
class HealthCheck
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :deregister_critical_service_after
|
5
5
|
|
6
6
|
def initialize(duration)
|
7
|
-
@
|
7
|
+
@deregister_critical_service_after = "#{duration}s"
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes
|
11
|
+
self.instance_variables.map do |attribute|
|
12
|
+
key = attribute.to_s.gsub('@', '')
|
13
|
+
[key, self.instance_variable_get(attribute)]
|
14
|
+
end.to_h
|
8
15
|
end
|
9
16
|
end
|
10
17
|
|
11
18
|
class TTLHealthCheck < HealthCheck
|
12
19
|
attr_accessor :ttl
|
13
20
|
|
14
|
-
def initialize(ttl,
|
15
|
-
super(
|
16
|
-
@ttl = ttl
|
21
|
+
def initialize(ttl, deregister_critical_service_after)
|
22
|
+
super(deregister_critical_service_after)
|
23
|
+
@ttl = "#{ttl}s"
|
17
24
|
end
|
18
25
|
end
|
19
26
|
|
20
27
|
class HTTPHealthCheck < HealthCheck
|
21
28
|
attr_accessor :http, :interval, :timeout
|
22
29
|
|
23
|
-
def initialize(http, interval, timeout,
|
24
|
-
super(
|
30
|
+
def initialize(http, interval, timeout, deregister_critical_service_after)
|
31
|
+
super(deregister_critical_service_after)
|
25
32
|
@http = http
|
26
|
-
@interval = interval
|
27
|
-
@timeout = timeout
|
33
|
+
@interval = "#{interval}s"
|
34
|
+
@timeout = "#{timeout}s"
|
28
35
|
end
|
29
36
|
end
|
30
37
|
|
31
38
|
class GRPCHealthCheck < HealthCheck
|
32
39
|
attr_accessor :grpc, :interval, :grpc_use_tls
|
33
40
|
|
34
|
-
def initialize(grpc, interval, grpc_use_tls,
|
35
|
-
super(
|
41
|
+
def initialize(grpc, interval, grpc_use_tls, deregister_critical_service_after)
|
42
|
+
super(deregister_critical_service_after)
|
36
43
|
@grpc = grpc
|
37
|
-
@interval = interval
|
44
|
+
@interval = "#{interval}s"
|
38
45
|
@grpc_use_tls = grpc_use_tls
|
39
46
|
end
|
40
47
|
end
|
data/lib/dryad/consul/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dryad-consul
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pan Jie
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dryad-core
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.0.
|
19
|
+
version: 1.0.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.0.
|
26
|
+
version: 1.0.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: diplomat
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,7 +128,6 @@ files:
|
|
128
128
|
- lib/dryad/consul/config_provider.rb
|
129
129
|
- lib/dryad/consul/health_check.rb
|
130
130
|
- lib/dryad/consul/key_value_client.rb
|
131
|
-
- lib/dryad/consul/railtie.rb
|
132
131
|
- lib/dryad/consul/service.rb
|
133
132
|
- lib/dryad/consul/service_observer.rb
|
134
133
|
- lib/dryad/consul/service_registry.rb
|
data/lib/dryad/consul/railtie.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require "rails"
|
2
|
-
require "dryad"
|
3
|
-
|
4
|
-
module Dryad
|
5
|
-
module Consul
|
6
|
-
class DBConfigObserver < ConfigObserver
|
7
|
-
def update_self(config_desc)
|
8
|
-
ActiveRecord::Base.configurations = YAML.load(ERB.new(config_desc.payload).result)
|
9
|
-
ActiveRecord::Base.establish_connection(Rails.env.to_sym)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Railtie < ::Rails::Railtie
|
14
|
-
initializer "dryad_consul.set_consul" do
|
15
|
-
ActiveSupport.on_load(:dryad_consul) do
|
16
|
-
Dryad::Consul.configure_consul(Dryad.configuration)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
initializer "dryad_consul.update_active_record_connection" do
|
21
|
-
config.after_initialize do
|
22
|
-
db_path = "#{Dryad.configuration.namespace}/#{Dryad.configuration.group}/database.yml"
|
23
|
-
observer = ::Dryad::Consul::DBConfigObserver.new
|
24
|
-
begin
|
25
|
-
db_config = Dryad::Consul::ConfigProvider.instance.load(db_path, observer)
|
26
|
-
raise Dryad::Core::ConfigurationNotFound, db_path if db_config.nil?
|
27
|
-
ActiveRecord::Base.configurations = YAML.load(ERB.new(db_config.payload).result)
|
28
|
-
ActiveRecord::Base.establish_connection(Rails.env.to_sym)
|
29
|
-
rescue Dryad::Core::ConfigurationNotFound => e
|
30
|
-
raise e
|
31
|
-
rescue Exception => e
|
32
|
-
Rails.logger.warn e.message
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
ActiveSupport.run_load_hooks(:dryad_consul, Consul)
|
39
|
-
end
|