Skip to content

Commit

Permalink
Support the ENV variables used by boot2docker.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed Nov 13, 2014
1 parent 004f4ab commit 57c6b73
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
25 changes: 19 additions & 6 deletions lib/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -48,7 +61,7 @@ def url
end

def options
@options ||= {}
@options ||= env_options
end

def url=(new_url)
Expand All @@ -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

Expand Down Expand Up @@ -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
37 changes: 30 additions & 7 deletions spec/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -17,37 +18,40 @@
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

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

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
Expand All @@ -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
Expand Down

0 comments on commit 57c6b73

Please sign in to comment.