Skip to content
Browse files

Register / unregister file server with the router.

Change-Id: I50d2a96795440ab2ab61af4b56e64b7c61918dab
  • Loading branch information...
1 parent ce55eb2 commit f6af6f35d7a0d59899a45049ebd2ea9100babb6c @kowshik kowshik committed
View
2 config/dea.yml
@@ -17,6 +17,8 @@ local_route:
#where the server that provides access to application home directories (log files, etc.) lives.
file_viewer_port: 12345
+domain: vcap.me
+
logging:
level: debug
View
23 lib/dea/bootstrap.rb
@@ -1,9 +1,11 @@
# coding: UTF-8
require "set"
+
require "steno"
require "steno/config"
require "steno/core_ext"
+
require "vcap/common"
require "vcap/component"
@@ -58,11 +60,11 @@ def setup
setup_droplet_registry
setup_resource_manager
setup_instance_registry
+ setup_directory_server
setup_signal_handlers
setup_directories
setup_pid_file
setup_sweepers
- setup_directory_server
setup_nats
setup_router_client
end
@@ -253,6 +255,8 @@ def setup_directory_server
def start_directory_server
@directory_server.start
+
+ register_directory_server
end
def setup_nats
@@ -283,6 +287,19 @@ def start_finish
send_advertise
end
+ def register_directory_server
+ uri = "#{@directory_server.uuid}.#{config["domain"]}"
+ @router_client.register_directory_server(local_ip,
+ config["directory_server_port"],
+ uri)
+ end
+
+ def unregister_directory_server
+ port = config["directory_server_port"]
+ uri = "#{@directory_server.uuid}.#{config["domain"]}"
+ @router_client.unregister_directory_server(local_ip, port, uri)
+ end
+
def start
start_component
start_nats
@@ -456,6 +473,8 @@ def handle_router_start(message)
next if !instance.running? || instance.application_uris.empty?
router_client.register_instance(instance)
end
+
+ register_directory_server
end
def handle_dea_status(message)
@@ -614,6 +633,8 @@ def shutdown
nats.stop
+ unregister_directory_server
+
pending_stops = Set.new([])
on_pending_empty = proc do
logger.info("All instances stopped, exiting.")
View
3 lib/dea/directory_server.rb
@@ -1,6 +1,7 @@
# coding: UTF-8
require "thin"
+require "securerandom"
require "vcap/common"
require "dea/directory"
@@ -11,6 +12,7 @@ class DirectoryServer
attr_reader :credentials
attr_reader :port
attr_reader :uri
+ attr_reader :uuid
def initialize(host, port, instance_registry)
@host = host
@@ -18,6 +20,7 @@ def initialize(host, port, instance_registry)
@uri = "http://#{host}:#{port}/instances"
@credentials = [VCAP.secure_uuid, VCAP.secure_uuid]
@instance_registry = instance_registry
+ @uuid = SecureRandom.uuid
@server = create_server(host, port, @credentials, instance_registry)
end
View
24 lib/dea/router_client.rb
@@ -9,20 +9,30 @@ def initialize(bootstrap)
@bootstrap = bootstrap
end
+ def register_directory_server(host, port, uri)
+ req = generate_directory_server_request(host, port, uri)
+ bootstrap.nats.publish("router.register", req)
+ end
+
+ def unregister_directory_server(host, port, uri)
+ req = generate_directory_server_request(host, port, uri)
+ bootstrap.nats.publish("router.unregister", req)
+ end
+
def register_instance(instance, opts = {})
- req = generate_request(instance, opts)
+ req = generate_instance_request(instance, opts)
bootstrap.nats.publish("router.register", req)
end
def unregister_instance(instance, opts = {})
- req = generate_request(instance, opts)
+ req = generate_instance_request(instance, opts)
bootstrap.nats.publish("router.unregister", req)
end
private
# Same format is used for both registration and unregistration
- def generate_request(instance, opts = {})
+ def generate_instance_request(instance, opts = {})
{ "dea" => bootstrap.uuid,
"app" => instance.application_id,
"uris" => opts[:uris] || instance.application_uris,
@@ -34,5 +44,13 @@ def generate_request(instance, opts = {})
}
}
end
+
+ # Same format is used for both registration and unregistration
+ def generate_directory_server_request(host, port, uri)
+ { "host" => host,
+ "port" => port,
+ "uris" => [uri],
+ }
+ end
end
end
View
3 lib/vcap/dea/config.rb
@@ -13,6 +13,7 @@ class VCAP::Dea::Config < VCAP::Config
:reset_at_startup => VCAP::JsonSchema::BoolSchema.new, #blow away saved state at startup.
#XXX make this optionally nil, but still in schema:local_route => String,
:file_viewer_port => Integer,
+ :domain => String,
:resources => {
:node_limits => {
:max_memory => Integer,
@@ -25,7 +26,7 @@ class VCAP::Dea::Config < VCAP::Config
},
},
:nats_uri => String, # where nats lives.
- :logging => {
+ :logging => {
:level => String, # debug, info, etc.
optional(:file) => String, # Log file to use
optional(:syslog) => String, # Name to associate with syslog messages (should start with 'vcap.')
View
14 spec/dea/bootstrap/router_registration_spec.rb
@@ -8,9 +8,10 @@
before do
bootstrap.unstub(:setup_router_client)
+ bootstrap.unstub(:setup_directory_server)
end
- it "should publish a message on 'router.register' upon receipt of a message on 'router.start'" do
+ it "should publish two messages on 'router.register' upon receipt of a message on 'router.start'" do
instances = []
responses = []
@@ -39,7 +40,7 @@
nats_mock.publish("router.start")
end
- expected = {
+ expected_0 = {
"dea" => bootstrap.uuid,
"app" => instances[0].application_id,
"uris" => instances[0].application_uris,
@@ -51,8 +52,13 @@
}
}
- responses.size.should == 1
- responses[0].should == expected
+ expected_1 = {
+ "host" => bootstrap.local_ip,
+ "port" => bootstrap.config["directory_server_port"],
+ "uris" => ["#{bootstrap.directory_server.uuid}.#{bootstrap.config["domain"]}"]
+ }
+
+ responses.should =~ [expected_0, expected_1]
end
describe "upon receipt of a message on 'dea.update'" do
View
16 spec/dea/bootstrap_spec.rb
@@ -9,7 +9,9 @@
before do
@config = {
- "base_dir" => tmpdir
+ "base_dir" => tmpdir,
+ "directory_server_port" => 12345,
+ "domain" => "default",
}
end
@@ -144,6 +146,8 @@ def test_signal(signal)
it "should stop and flush nats" do
bootstrap.setup_signal_handlers
bootstrap.setup_instance_registry
+ bootstrap.setup_router_client
+ bootstrap.setup_directory_server
nats_client_mock = mock("nats_client")
nats_client_mock.should_receive(:flush)
@@ -151,6 +155,16 @@ def test_signal(signal)
nats_mock = mock("nats")
nats_mock.should_receive(:stop)
nats_mock.should_receive(:client).and_return(nats_client_mock)
+ nats_mock.should_receive(:publish) do |subject, message|
+ subject.should == "router.unregister"
+
+ message.should be_an_instance_of Hash
+ message["host"].should == bootstrap.local_ip
+ message["port"].should == bootstrap.config["directory_server_port"]
+ message["uris"].size.should == 1
+ message["uris"][0].should match /.*\.#{bootstrap.config["domain"]}$/
+ end
+
bootstrap.stub(:nats).and_return(nats_mock)
# Don't want to exit tests :)
View
1 spec/support/bootstrap_setup.rb
@@ -21,6 +21,7 @@
"test2" => {},
},
"directory_server_port" => 12345,
+ "domain" => "default",
}
mock_runtime = mock("Dea::Runtime")

0 comments on commit f6af6f3

Please sign in to comment.
Something went wrong with that request. Please try again.