Merge pull request #617 from floatdrop/docker-section-in-yaml

Add docker section in .drone.yml with net option
This commit is contained in:
Brad Rydzewski 2014-10-23 21:05:19 -07:00
commit 38fd589083
5 changed files with 73 additions and 0 deletions

View file

@ -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)

View file

@ -19,6 +19,7 @@ type KeyValuePair struct {
type HostConfig struct {
Binds []string
ContainerIDFile string
NetworkMode string
LxcConf []KeyValuePair
Privileged bool
PortBindings map[Port][]PortBinding

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -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