From cfa9082761abe7b997e462be9406cf8313e6a384 Mon Sep 17 00:00:00 2001 From: Keith Thornhill Date: Thu, 13 Feb 2014 15:20:43 -0800 Subject: [PATCH] enable use of docker-standard DOCKER_HOST environment variable to allow drone to run builds on remote docker hosts. --- pkg/build/docker/client.go | 29 ++++++++++---- pkg/build/docker/client_test.go | 67 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 pkg/build/docker/client_test.go diff --git a/pkg/build/docker/client.go b/pkg/build/docker/client.go index 1bfcbd660..bb33a28fb 100644 --- a/pkg/build/docker/client.go +++ b/pkg/build/docker/client.go @@ -32,15 +32,8 @@ var Logging = true // New creates an instance of the Docker Client func New() *Client { c := &Client{} - c.proto = DEFAULTPROTOCOL - c.addr = DEFAULTUNIXSOCKET - // if the default socket doesn't exist then - // we'll try to connect to the default tcp address - if _, err := os.Stat(DEFAULTUNIXSOCKET); err != nil { - c.proto = "tcp" - c.addr = "0.0.0.0:4243" - } + c.setHost(DEFAULTUNIXSOCKET) c.Images = &ImageService{c} c.Containers = &ContainerService{c} @@ -76,6 +69,26 @@ var ( ErrBadRequest = errors.New("Bad Request") ) +func (c *Client) setHost(defaultUnixSocket string) { + c.proto = DEFAULTPROTOCOL + c.addr = defaultUnixSocket + + if os.Getenv("DOCKER_HOST") != "" { + pieces := strings.Split(os.Getenv("DOCKER_HOST"), "://") + if len(pieces) == 2 { + c.proto = pieces[0] + c.addr = pieces[1] + } + } else { + // if the default socket doesn't exist then + // we'll try to connect to the default tcp address + if _, err := os.Stat(defaultUnixSocket); err != nil { + c.proto = "tcp" + c.addr = "0.0.0.0:4243" + } + } +} + // helper function used to make HTTP requests to the Docker daemon. func (c *Client) do(method, path string, in, out interface{}) error { // if data input is provided, serialize to JSON diff --git a/pkg/build/docker/client_test.go b/pkg/build/docker/client_test.go new file mode 100644 index 000000000..6869fddcc --- /dev/null +++ b/pkg/build/docker/client_test.go @@ -0,0 +1,67 @@ +package docker + +import ( + "io/ioutil" + "os" + "testing" +) + +func TestHostFromEnv(t *testing.T) { + os.Setenv("DOCKER_HOST", "tcp://1.1.1.1:4243") + defer os.Setenv("DOCKER_HOST", "") + + client := New() + + if client.proto != "tcp" { + t.Fail() + } + + if client.addr != "1.1.1.1:4243" { + t.Fail() + } +} + +func TestInvalidHostFromEnv(t *testing.T) { + os.Setenv("DOCKER_HOST", "tcp:1.1.1.1:4243") // missing tcp:// prefix + defer os.Setenv("DOCKER_HOST", "") + + client := New() + + if client.addr == "1.1.1.1:4243" { + t.Fail() + } +} + +func TestSocketHost(t *testing.T) { + // create temporary file to represent the docker socket + file, err := ioutil.TempFile("", "TestDefaultUnixHost") + if err != nil { + t.Fail() + } + file.Close() + defer os.Remove(file.Name()) + + client := &Client{} + client.setHost(file.Name()) + + if client.proto != "unix" { + t.Fail() + } + + if client.addr != file.Name() { + t.Fail() + } +} + +func TestDefaultTcpHost(t *testing.T) { + client := &Client{} + client.setHost("/tmp/missing_socket") + + if client.proto != "tcp" { + t.Fail() + } + + if client.addr != "0.0.0.0:4243" { + t.Fail() + } +}