From 54a6e56611832db9015784bf12e9acf44de0738b Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Wed, 15 Oct 2014 15:24:20 +0600 Subject: [PATCH] Add docker section in .drone.yml with net option --- shared/build/build.go | 4 +++ shared/build/docker/structs.go | 1 + shared/build/script/docker.go | 24 ++++++++++++++++++ shared/build/script/docker_test.go | 40 ++++++++++++++++++++++++++++++ shared/build/script/script.go | 4 +++ 5 files changed, 73 insertions(+) create mode 100644 shared/build/script/docker.go create mode 100644 shared/build/script/docker_test.go diff --git a/shared/build/build.go b/shared/build/build.go index 986805851..be94904b5 100644 --- a/shared/build/build.go +++ b/shared/build/build.go @@ -330,6 +330,10 @@ func (b *Builder) run() error { Privileged: (b.Privileged && len(b.Repo.PR) == 0), } + if host.Privileged { + host.NetworkMode = script.DockerNetworkMode(b.Build.Docker) + } + // debugging log.Noticef("starting build %s", b.Build.Name) diff --git a/shared/build/docker/structs.go b/shared/build/docker/structs.go index be0aeaa03..d30282f9c 100644 --- a/shared/build/docker/structs.go +++ b/shared/build/docker/structs.go @@ -19,6 +19,7 @@ type KeyValuePair struct { type HostConfig struct { Binds []string ContainerIDFile string + NetworkMode string LxcConf []KeyValuePair Privileged bool PortBindings map[Port][]PortBinding diff --git a/shared/build/script/docker.go b/shared/build/script/docker.go new file mode 100644 index 000000000..6686045f1 --- /dev/null +++ b/shared/build/script/docker.go @@ -0,0 +1,24 @@ +package script + +const ( + DefaultDockerNetworkMode = "bridge" +) + +// Docker stores the configuration details for +// configuring docker container. +type Docker struct { + // NetworkMode (also known as `--net` option) + // Could be set only if Docker is running in privileged mode + NetworkMode *string `yaml:"net,omitempty"` +} + +// DockerNetworkMode returns DefaultNetworkMode +// when Docker.NetworkMode is empty. +// DockerNetworkMode returns Docker.NetworkMode +// when it is not empty. +func DockerNetworkMode(d *Docker) string { + if d == nil || d.NetworkMode == nil { + return DefaultDockerNetworkMode + } + return *d.NetworkMode +} diff --git a/shared/build/script/docker_test.go b/shared/build/script/docker_test.go new file mode 100644 index 000000000..5b6c1e94e --- /dev/null +++ b/shared/build/script/docker_test.go @@ -0,0 +1,40 @@ +package script + +import ( + "testing" +) + +func TestDockerNetworkMode(t *testing.T) { + var d *Docker + var expected string + + expected = DefaultDockerNetworkMode + d = nil + if actual := DockerNetworkMode(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } + + expected = DefaultDockerNetworkMode + d = &Docker{} + if actual := DockerNetworkMode(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } + + expected = DefaultDockerNetworkMode + d = &Docker{NetworkMode: nil} + if actual := DockerNetworkMode(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } + + expected = "bridge" + d = &Docker{NetworkMode: &expected} + if actual := DockerNetworkMode(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } + + expected = "host" + d = &Docker{NetworkMode: &expected} + if actual := DockerNetworkMode(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } +} diff --git a/shared/build/script/script.go b/shared/build/script/script.go index cbedd9984..372e1dc20 100644 --- a/shared/build/script/script.go +++ b/shared/build/script/script.go @@ -71,6 +71,10 @@ type Build struct { // Git specified git-specific parameters, such as // the clone depth and path Git *git.Git `yaml:"git,omitempty"` + + // Docker container parameters, such as + // NetworkMode and UserName + Docker *Docker `yaml:"docker,omitempty"` } // Write adds all the steps to the build script, including