From e2a09752b5a521c7c10700bddd30f0d9d009fc3d Mon Sep 17 00:00:00 2001 From: Cameron Davison Date: Wed, 28 Dec 2016 12:18:38 -0600 Subject: [PATCH 1/3] add force_pull to docker driver --- client/driver/docker.go | 10 ++++++-- client/driver/docker_test.go | 30 ++++++++++++++++++++++ website/source/docs/drivers/docker.html.md | 4 +++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index 7ac703d46fbf..19475f802cfd 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -136,6 +136,7 @@ type DockerDriverConfig struct { WorkDir string `mapstructure:"work_dir"` // Working directory inside the container Logging []DockerLoggingOpts `mapstructure:"logging"` // Logging options for syslog server Volumes []string `mapstructure:"volumes"` // Host-Volumes to mount in, syntax: /path/to/host/directory:/destination/path/in/container + ForcePull bool `mapstructure:"force_pull"` // Always force pull before running image, usefull if your tags are mutable } // Validate validates a docker driver config @@ -331,6 +332,9 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error { "volumes": &fields.FieldSchema{ Type: fields.TypeArray, }, + "force_pull": &fields.FieldSchema{ + Type: fields.TypeBool, + }, }, } @@ -890,9 +894,11 @@ func (d *DockerDriver) createImage(driverConfig *DockerDriverConfig, client *doc var dockerImage *docker.Image var err error // We're going to check whether the image is already downloaded. If the tag - // is "latest" we have to check for a new version every time so we don't + // is "latest", or ForcePull is set, we have to check for a new version every time so we don't // bother to check and cache the id here. We'll download first, then cache. - if tag != "latest" { + if driverConfig.ForcePull { + d.logger.Printf("[DEBUG] driver.docker: force pull image '%s:%s' instead of inspecting local", repo, tag) + } else if tag != "latest" { dockerImage, err = client.InspectImage(image) } diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 580ed3dfe1f5..9ac374b5a05d 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -699,6 +699,36 @@ func TestDockerDriver_Labels(t *testing.T) { } } +func TestDockerDriver_ForcePull_IsInvalidConfig(t *testing.T) { + task, _, _ := dockerTask() + task.Config["force_pull"] = "nothing" + + driverCtx, execCtx := testDriverContexts(task) + driverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"} + driver := NewDockerDriver(driverCtx) + if err := driver.Prestart(execCtx, task); err == nil { + execCtx.AllocDir.Destroy() + t.Fatalf("error expected in prestart") + } else { + execCtx.AllocDir.Destroy() + } +} + +func TestDockerDriver_ForcePull(t *testing.T) { + task, _, _ := dockerTask() + task.Config["force_pull"] = "true" + + client, handle, cleanup := dockerSetup(t, task) + defer cleanup() + + waitForExist(t, client, handle.(*DockerHandle)) + + _, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) + if err != nil { + t.Fatalf("err: %v", err) + } +} + func TestDockerDriver_DNS(t *testing.T) { task, _, _ := dockerTask() task.Config["dns_servers"] = []string{"8.8.8.8", "8.8.4.4"} diff --git a/website/source/docs/drivers/docker.html.md b/website/source/docs/drivers/docker.html.md index 08ca130ad02b..0955670e71cd 100644 --- a/website/source/docs/drivers/docker.html.md +++ b/website/source/docs/drivers/docker.html.md @@ -164,6 +164,10 @@ The `docker` driver supports the following configuration in the job spec: * `interactive` - (Optional) `true` or `false` (default). Keep STDIN open on the container. +* `force_pull` - (Optional) `true` or `false` (default). Always pull latest image + instead of using existing local image. Should be set to `true` if repository tags + are mutable. + * `shm_size` - (Optional) The size (bytes) of /dev/shm for the container. * `logging` - (Optional) A key-value map of Docker logging options. The default From 2d6e7e7b57d32d273a0f7fec4af0c1ba7b5f7eb8 Mon Sep 17 00:00:00 2001 From: Cameron Davison Date: Mon, 9 Jan 2017 15:55:01 -0600 Subject: [PATCH 2/3] fixing typo in comment --- client/driver/docker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index 19475f802cfd..a740fc51f157 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -136,7 +136,7 @@ type DockerDriverConfig struct { WorkDir string `mapstructure:"work_dir"` // Working directory inside the container Logging []DockerLoggingOpts `mapstructure:"logging"` // Logging options for syslog server Volumes []string `mapstructure:"volumes"` // Host-Volumes to mount in, syntax: /path/to/host/directory:/destination/path/in/container - ForcePull bool `mapstructure:"force_pull"` // Always force pull before running image, usefull if your tags are mutable + ForcePull bool `mapstructure:"force_pull"` // Always force pull before running image, useful if your tags are mutable } // Validate validates a docker driver config From d4b8fc953aa50e4834eedc35d51bdca665011cdb Mon Sep 17 00:00:00 2001 From: Cameron Davison Date: Mon, 9 Jan 2017 15:55:50 -0600 Subject: [PATCH 3/3] using new ctx instead of getting both params back --- client/driver/docker_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 9ac374b5a05d..9b1e129d5032 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -703,14 +703,13 @@ func TestDockerDriver_ForcePull_IsInvalidConfig(t *testing.T) { task, _, _ := dockerTask() task.Config["force_pull"] = "nothing" - driverCtx, execCtx := testDriverContexts(task) - driverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"} - driver := NewDockerDriver(driverCtx) - if err := driver.Prestart(execCtx, task); err == nil { - execCtx.AllocDir.Destroy() + ctx := testDriverContexts(t, task) + defer ctx.AllocDir.Destroy() + ctx.DriverCtx.config.Options = map[string]string{"docker.cleanup.image": "false"} + driver := NewDockerDriver(ctx.DriverCtx) + + if err := driver.Prestart(ctx.ExecCtx, task); err == nil { t.Fatalf("error expected in prestart") - } else { - execCtx.AllocDir.Destroy() } }