From 5cb2ce04871cdb46582fc66a7ccb0eb17818b7e3 Mon Sep 17 00:00:00 2001 From: FENG Zhichao Date: Sat, 15 Aug 2015 14:31:41 +1000 Subject: [PATCH] Add a cli option to specify backend services host if they are not running locally. * e.g. Services running in Docker containers on Mac OS X need to be accessed via Docker VM IP address instead of OS X's localhost. --- lib/invoker.rb | 1 + lib/invoker/cli.rb | 5 +++++ lib/invoker/power/balancer.rb | 6 +++++- spec/invoker/power/balancer_spec.rb | 12 ++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/invoker.rb b/lib/invoker.rb index a45769e..553ac9f 100644 --- a/lib/invoker.rb +++ b/lib/invoker.rb @@ -37,6 +37,7 @@ module Invoker class << self attr_accessor :config, :tail_watchers, :commander attr_accessor :dns_cache, :daemonize + attr_accessor :backend_host alias_method :daemonize?, :daemonize diff --git a/lib/invoker/cli.rb b/lib/invoker/cli.rb index 044685a..6ba607a 100644 --- a/lib/invoker/cli.rb +++ b/lib/invoker/cli.rb @@ -34,10 +34,15 @@ def uninstall type: :boolean, banner: "Daemonize the server into the background", aliases: [:d] + option :backend_host, + type: :string, + banner: "Host to be used for backend services if they are not running locally. e.g. docker container services in Mac OS X", + aliases: [:h] def start(file) Invoker.setup_config_location port = options[:port] || 9000 Invoker.daemonize = options[:daemon] + Invoker.backend_host = options[:backend_host] Invoker.load_invoker_config(file, port) warn_about_terminal_notifier warn_about_old_configuration diff --git a/lib/invoker/power/balancer.rb b/lib/invoker/power/balancer.rb index 58bd8fd..79ad2c9 100644 --- a/lib/invoker/power/balancer.rb +++ b/lib/invoker/power/balancer.rb @@ -71,7 +71,7 @@ def headers_received(headers) dns_check_response = UrlRewriter.new.select_backend_config(headers['Host']) if dns_check_response && dns_check_response.port - connection.server(session, host: '0.0.0.0', port: dns_check_response.port) + connection.server(session, host: backend_host, port: dns_check_response.port) else return_error_page(404) http_parser.reset @@ -105,6 +105,10 @@ def frontend_disconnect(backend, name) connection.close_connection_after_writing if backend == session end + def backend_host + Invoker.backend_host || '0.0.0.0' + end + private def return_error_page(status) diff --git a/spec/invoker/power/balancer_spec.rb b/spec/invoker/power/balancer_spec.rb index 37e9f08..3062581 100644 --- a/spec/invoker/power/balancer_spec.rb +++ b/spec/invoker/power/balancer_spec.rb @@ -19,4 +19,16 @@ @balancer.headers_received(headers) end end + + describe "#backend_host" do + it "should return localhost as default when backend host is not configured" do + Invoker.backend_host = nil + expect(@balancer.backend_host).to eql("0.0.0.0") + end + + it "should return as per configured backend host" do + Invoker.backend_host = "192.168.59.103" + expect(@balancer.backend_host).to eql("192.168.59.103") + end + end end