Class: EtcdDiscovery::Registrar

Inherits:
Object
  • Object
show all
Defined in:
lib/etcd-discovery/registrar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, host) ⇒ Registrar

Returns a new instance of Registrar.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/etcd-discovery/registrar.rb', line 24

def initialize(service, host)
  @logger = Logger.new($stdout)

  if host.is_a? Hash
    @host = Host.new host.merge('service_name' => service, 'uuid' => SecureRandom.uuid)
  elsif host.is_a? EtcdDiscovery::Host
    @host = host
  else
    raise TypeError, "host should be a Hash or a Etcd::Host, is a #{host.class}"
  end

  @service = EtcdDiscovery::Service.new service_params
  @state = :new
  @user     = @host.attributes['user']
  @password = @host.attributes['password']
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



19
20
21
# File 'lib/etcd-discovery/registrar.rb', line 19

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



22
23
24
# File 'lib/etcd-discovery/registrar.rb', line 22

def password
  @password
end

#serviceObject (readonly)

Returns the value of attribute service.



20
21
22
# File 'lib/etcd-discovery/registrar.rb', line 20

def service
  @service
end

#stateObject (readonly)

Returns the value of attribute state.



16
17
18
# File 'lib/etcd-discovery/registrar.rb', line 16

def state
  @state
end

#threadObject (readonly)

Returns the value of attribute thread.



17
18
19
# File 'lib/etcd-discovery/registrar.rb', line 17

def thread
  @thread
end

#userObject (readonly)

Returns the value of attribute user.



21
22
23
# File 'lib/etcd-discovery/registrar.rb', line 21

def user
  @user
end

#watcherObject (readonly)

Returns the value of attribute watcher.



18
19
20
# File 'lib/etcd-discovery/registrar.rb', line 18

def watcher
  @watcher
end

Instance Method Details

#clientObject



100
101
102
# File 'lib/etcd-discovery/registrar.rb', line 100

def client
  config.client
end

#configObject



104
105
106
# File 'lib/etcd-discovery/registrar.rb', line 104

def config
  EtcdDiscovery.config
end

#host_keyObject



108
109
110
# File 'lib/etcd-discovery/registrar.rb', line 108

def host_key
  "/services/#{@service.attributes['name']}/#{@host.attributes['uuid']}"
end

#registerObject



41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/etcd-discovery/registrar.rb', line 41

def register
  if @state == :started
    @logger.warn "#{@service} is already registered"
    return
  end

  @state = :started

  service_value = @service.to_json

  # Do not start credentials synchro if the service is not public or has no credentials
  if @service.attributes['public'] && (@service.attributes['user'].present? || @service.attributes['password'].present?)
    @watcher = Thread.new {
      @logger.warn "Watcher #{@service.attributes['name']} started"
      index = 0
      while @state == :started
        begin
          resp = client.watch service_key, { index: index }
        rescue => e
          @logger.warn "Fail to watch #{service_key}: #{e}, #{e.message}, #{e.class}"
          next
        end
        value = JSON.parse resp.node.value
        @user = value['user']
        @password = value['password']
        @host.set_credentials user, password
        @service.set_credentials user, password
        index = resp.etcd_index
      end
    }
  end

  client.set(service_key, value: service_value)
  @thread = Thread.new {
    @logger.warn "Register '#{@service}' started"
    while @state == :started
      value = @host.to_json
      begin
        client.set(host_key, value: value, ttl: config.register_ttl)
      rescue => e
        @logger.warn "Fail to set #{service_key}: #{e}, #{e.message}, #{e.class}"
      end
      sleep config.register_renew
    end
    @logger.warn "Register '#{@service}' stopped"
  }


  return self
end

#service_keyObject



112
113
114
# File 'lib/etcd-discovery/registrar.rb', line 112

def service_key
  "/services_infos/#{@service.attributes['name']}"
end

#service_paramsObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/etcd-discovery/registrar.rb', line 116

def service_params
  params = {
    'name' =>     host.attributes['service_name'],
    'critical' => host.attributes['critical'],
    'user' =>     host.attributes['user'],
    'password' => host.attributes['password'],
    'public' =>   host.attributes['public']
  }
  params['hostname'] = host.attributes['name']if params['public']
  params['ports'] = host.attributes['ports']  if params['public']
  return params
end

#stopObject

Raises:



92
93
94
95
96
97
98
# File 'lib/etcd-discovery/registrar.rb', line 92

def stop
  raise InvalidStateError.new(@state, :started) if @state != :started
  @logger.debug "Set state to :stopped"
  @state = :stopped
  @logger.debug "Delete #{key}"
  client.delete(key)
end