Module: Dryad::Consul

Defined in:
lib/dryad/consul.rb,
lib/dryad/consul/service.rb,
lib/dryad/consul/version.rb,
lib/dryad/consul/health_check.rb,
lib/dryad/consul/config_observer.rb,
lib/dryad/consul/config_provider.rb,
lib/dryad/consul/key_value_client.rb,
lib/dryad/consul/service_observer.rb,
lib/dryad/consul/service_registry.rb

Defined Under Namespace

Classes: ConfigObserver, ConfigProvider, Error, GRPCHealthCheck, HTTPHealthCheck, HealthCheck, KeyValueClient, Service, ServiceObserver, ServiceRegistry, TTLHealthCheck

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

.build_check(check_config, name, address, port) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dryad/consul.rb', line 29

def build_check(check_config, name, address, port)
  if check_config
    interval = check_config[:interval] || 10
    if !check_config[:url].nil?
      http = check_config[:url]
      http = "#{k.to_s}://#{address}:#{port}#{url}" if http.start_with?("/")
      timeout = check_config[:timeout] || 5
      Dryad::Consul::HTTPHealthCheck.new(http, interval, timeout, interval * 10)
    elsif !check_config[:grpc_use_tls].nil?
      grpc = "#{address}:#{port}/#{name}"
      Dryad::Consul::GRPCHealthCheck.new(grpc, interval, check_config[:grpc_use_tls], interval * 10)
    else
      Dryad::Consul::TTLHealthCheck.new(interval, interval * 10)
    end
  else
    Dryad::Consul::TTLHealthCheck.new(10, 100)
  end
end

.build_service(name, group, service_config) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dryad/consul.rb', line 48

def build_service(name, group, service_config)
  address = if service_config[:address]
    service_config[:address]
  else
    require 'socket'
    Socket.ip_address_list.detect{|ai| ai.ipv4_private?}&.ip_address
  end
  schemas = Dryad::Core::Schema.constants.map{|s| Dryad::Core::Schema.const_get(s).to_sym}
  portals = service_config.slice(*schemas).map do |k, v|
    params = v.slice(:port, :non_certifications).merge({
      schema: k.to_s,
      pattern: v[:pattern] || "/.*",
      check: build_check(v[:check], name, address, v[:port])
    })
    Dryad::Core::Portal.new(params)
  end
  Dryad::Consul::Service.new({
    name: name,
    group: group,
    address: address,
    portals: portals,
    priority: service_config[:priority] || 0,
    load_balancing: service_config[:load_balancing] || []
  })
end

.configure_consul(configuration) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dryad/consul.rb', line 16

def configure_consul(configuration)
  consul = configuration.consul
  if consul[:username].nil? || consul[:password].nil?
    url = "http://#{consul[:host]}:#{consul[:port]}"
  else
    require "erb"
    url = "http://#{consul[:username]}:#{ERB::Util.url_encode(consul[:password])}@#{consul[:host]}:#{consul[:port]}"
  end
  ::Diplomat.configure do |config|
    config.url = url
  end
end