Class: Cf::Registrar

Inherits:
Object
  • Object
show all
Defined in:
lib/cf/registrar.rb

Constant Summary collapse

DISCOVER_TOPIC =
"vcap.component.discover"
ANNOUNCE_TOPIC =
"vcap.component.announce"
ROUTER_START_TOPIC =
"router.start"
ROUTER_GREET_TOPIC =
"router.greet"
ROUTER_REGISTER_TOPIC =
"router.register"
ROUTER_UNREGISTER_TOPIC =
"router.unregister"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Registrar

Returns a new instance of Registrar.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cf/registrar.rb', line 18

def initialize(config)
  @logger = Steno.logger("cf.registrar")

  config = symbolize_keys(config)

  @message_bus_servers = config[:message_bus_servers]
  @host = config[:host]
  @port = config[:port]
  @uri = config[:uri]
  @tags = config[:tags]
  @index = config[:index] || 0
  @private_instance_id = config[:private_instance_id]

  if config[:varz]
    @type = config[:varz][:type]
    @username = config[:varz][:username]
    @password = config[:varz][:password]
    @uuid = config[:varz][:uuid] || SecureRandom.uuid
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def host
  @host
end

#indexObject (readonly)

Returns the value of attribute index.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def index
  @index
end

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def logger
  @logger
end

#message_bus_serversObject (readonly)

Returns the value of attribute message_bus_servers.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def message_bus_servers
  @message_bus_servers
end

#passwordObject (readonly)

Returns the value of attribute password.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def port
  @port
end

#private_instance_idObject (readonly)

Returns the value of attribute private_instance_id.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def private_instance_id
  @private_instance_id
end

#tagsObject (readonly)

Returns the value of attribute tags.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def tags
  @tags
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def type
  @type
end

#uriObject (readonly)

Returns the value of attribute uri.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def uri
  @uri
end

#usernameObject (readonly)

Returns the value of attribute username.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def username
  @username
end

#uuidObject (readonly)

Returns the value of attribute uuid.



15
16
17
# File 'lib/cf/registrar.rb', line 15

def uuid
  @uuid
end

Instance Method Details

#register_varz_credentialsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cf/registrar.rb', line 39

def register_varz_credentials
  discover_msg = {
    :type => type,
    :host => "#{host}:#{port}",
    :index => index,
    :uuid => "#{index}-#{uuid}",
    :credentials => [username, password]
  }

  if username.nil? || password.nil?
    logger.error("Could not register nil varz credentials")
  else
    logger.info("Connected to NATS - varz registration")

    message_bus.subscribe(DISCOVER_TOPIC) do |_, reply|
      logger.debug("Received #{DISCOVER_TOPIC} publishing #{reply.inspect} #{discover_msg.inspect}")
      message_bus.publish(reply, discover_msg)
    end

    logger.info("Announcing start up #{ANNOUNCE_TOPIC}")
    message_bus.publish(ANNOUNCE_TOPIC, discover_msg)
  end
end

#register_with_routerObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cf/registrar.rb', line 63

def register_with_router
  logger.info("Connected to NATS - router registration")

  @router_start_sid = message_bus.subscribe(ROUTER_START_TOPIC) do |message|
    handle_router_greeting(message)
  end

  message_bus.request(ROUTER_GREET_TOPIC) do |message|
    handle_router_greeting(message)
  end

  send_registration_message
end

#shutdown(&block) ⇒ Object



77
78
79
80
81
# File 'lib/cf/registrar.rb', line 77

def shutdown(&block)
  EM.cancel_timer(@registration_timer) if @registration_timer
  unsubscribe_from_router_start
  send_unregistration_message(&block)
end