Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support the ENV variables used by boot2docker.
Browse files Browse the repository at this point in the history
pwnall committed Nov 7, 2014
1 parent 004f4ab commit 2be6915
Showing 2 changed files with 40 additions and 8 deletions.
26 changes: 20 additions & 6 deletions lib/docker.rb
Original file line number Diff line number Diff line change
@@ -35,11 +35,25 @@ def default_socket_url
end

def env_url
ENV['DOCKER_URL']
ENV['DOCKER_URL'] || ENV['DOCKER_HOST']
end

def env_options
if ENV['DOCKER_CERT_PATH']
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 +62,7 @@ def url
end

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

def url=(new_url)
@@ -57,7 +71,7 @@ def url=(new_url)
end

def options=(new_options)
@options = new_options
@options = env_options.merge(new_options || {})
reset_connection!
end

@@ -98,8 +112,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
22 changes: 20 additions & 2 deletions spec/docker_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
@@ -42,7 +43,7 @@
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'
end
@@ -56,13 +57,30 @@
before do
ENV['DOCKER_HOST'] = 'tcp://someserver:8103'
ENV['DOCKER_URL'] = 'tcp://someotherserver:8103'

end

its(:options) { {} }
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'
end

its(:options) {
{
client_cert: '/boot2dockert/cert/path/cert.pem',
client_key: '/boot2dockert/cert/path/key.pem',
ssl_ca_file: '/boot2dockert/cert/path/ca.pem',
scheme: '/boot2dockert/cert/path/https'
}
}
its(:url) { should == 'tcp://someserver:8103' }
its(:connection) { should be_a Docker::Connection }
end
end

describe '#reset_connection!' do

0 comments on commit 2be6915

Please sign in to comment.