diff --git a/lib/docker.rb b/lib/docker.rb index b87657961..97c6e2c3e 100644 --- a/lib/docker.rb +++ b/lib/docker.rb @@ -35,11 +35,24 @@ def default_socket_url end def env_url - ENV['DOCKER_URL'] + ENV['DOCKER_URL'] || ENV['DOCKER_HOST'] + end + + def env_options + if cert_path = ENV['DOCKER_CERT_PATH'] + { + client_cert: File.join(cert_path, 'cert.pem'), + client_key: File.join(cert_path, 'key.pem'), + ssl_ca_file: File.join(cert_path, 'ca.pem'), + scheme: 'https' + } + else + {} + end end def url - @url ||= ENV['DOCKER_URL'] || ENV['DOCKER_HOST'] || default_socket_url + @url ||= env_url || default_socket_url # docker uses a default notation tcp:// which means tcp://localhost:2375 if @url == 'tcp://' @url = 'tcp://localhost:2375' @@ -48,7 +61,7 @@ def url end def options - @options ||= {} + @options ||= env_options end def url=(new_url) @@ -57,7 +70,7 @@ def url=(new_url) end def options=(new_options) - @options = new_options + @options = env_options.merge(new_options || {}) reset_connection! end @@ -98,8 +111,8 @@ def validate_version! raise Docker::Error::VersionError, "Expected API Version: #{API_VERSION}" end - module_function :default_socket_url, :env_url, :url, :url=, :options, - :options=, :creds, :creds=, :logger, :logger=, + module_function :default_socket_url, :env_url, :url, :url=, :env_options, + :options, :options=, :creds, :creds=, :logger, :logger=, :connection, :reset_connection!, :version, :info, :authenticate!, :validate_version! end diff --git a/spec/docker_spec.rb b/spec/docker_spec.rb index f0e571aa6..129bf9cad 100644 --- a/spec/docker_spec.rb +++ b/spec/docker_spec.rb @@ -6,6 +6,7 @@ before do ENV['DOCKER_URL'] = nil ENV['DOCKER_HOST'] = nil + ENV['DOCKER_CERT_PATH'] = nil end it { should be_a Module } @@ -17,7 +18,7 @@ end context "when the DOCKER_* ENV variables aren't set" do - its(:options) { {} } + its(:options) { should == {} } its(:url) { should == 'unix:///var/run/docker.sock' } its(:connection) { should be_a Docker::Connection } end @@ -25,9 +26,10 @@ context "when the DOCKER_* ENV variables are set" do before do ENV['DOCKER_URL'] = 'unixs:///var/run/not-docker.sock' + Docker.options = nil end - its(:options) { {} } + its(:options) { should == {} } its(:url) { should == 'unixs:///var/run/not-docker.sock' } its(:connection) { should be_a Docker::Connection } end @@ -35,19 +37,21 @@ context "when the DOCKER_HOST is set and uses default tcp://" do before do ENV['DOCKER_HOST'] = 'tcp://' + Docker.options = nil end - its(:options) { {} } + its(:options) { should == {} } its(:url) { should == 'tcp://localhost:2375' } its(:connection) { should be_a Docker::Connection } end - context "when the DOCKER_HOST ENV variables is set" do + context "when the DOCKER_HOST ENV variable is set" do before do ENV['DOCKER_HOST'] = 'tcp://someserver:8103' + Docker.options = nil end - its(:options) { {} } + its(:options) { should == {} } its(:url) { should == 'tcp://someserver:8103' } its(:connection) { should be_a Docker::Connection } end @@ -56,13 +60,32 @@ before do ENV['DOCKER_HOST'] = 'tcp://someserver:8103' ENV['DOCKER_URL'] = 'tcp://someotherserver:8103' - + Docker.options = nil end - its(:options) { {} } + its(:options) { should == {} } its(:url) { should == 'tcp://someotherserver:8103' } its(:connection) { should be_a Docker::Connection } end + + context "when the DOCKER_CERT_PATH and DOCKER_HOST ENV variables are set" do + before do + ENV['DOCKER_HOST'] = 'tcp://someserver:8103' + ENV['DOCKER_CERT_PATH'] = '/boot2dockert/cert/path' + Docker.options = nil + end + + its(:options) { + should == { + client_cert: '/boot2dockert/cert/path/cert.pem', + client_key: '/boot2dockert/cert/path/key.pem', + ssl_ca_file: '/boot2dockert/cert/path/ca.pem', + scheme: 'https' + } + } + its(:url) { should == 'tcp://someserver:8103' } + its(:connection) { should be_a Docker::Connection } + end end describe '#reset_connection!' do