From df7623d9d8c1530f0083c755690d6e81f53c2e5b Mon Sep 17 00:00:00 2001 From: Donald Guy Date: Fri, 20 Mar 2015 12:04:38 -0400 Subject: [PATCH 1/2] builder/docker: Run scripts /w `exec` if -v > 1.4 --- builder/docker/communicator.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/builder/docker/communicator.go b/builder/docker/communicator.go index bad2c1ff6..c6e3829f0 100644 --- a/builder/docker/communicator.go +++ b/builder/docker/communicator.go @@ -9,12 +9,14 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strconv" "sync" "syscall" "time" "github.com/ActiveState/tail" + "github.com/hashicorp/go-version" "github.com/mitchellh/packer/packer" ) @@ -26,6 +28,27 @@ type Communicator struct { lock sync.Mutex } +var dockerVersion version.Version +var useDockerExec bool + +func init() { + execConstraint, _ := version.NewConstraint(">= 1.4.0") + + versionExtractor := regexp.MustCompile(version.VersionRegexpRaw) + dockerVersionOutput, _ := exec.Command("docker", "-v").Output() + dockerVersionString := string(versionExtractor.FindSubmatch(dockerVersionOutput)[0]) + + dockerVersion, err := version.NewVersion(dockerVersionString) + if err != nil { + log.Printf("Docker returned malformed version string: %e", err) + log.Printf("Assuming no `exec` capability, using `attatch`") + useDockerExec = false + } else { + log.Printf("Docker version detected as %s", dockerVersion) + useDockerExec = execConstraint.Check(dockerVersion) + } +} + func (c *Communicator) Start(remote *packer.RemoteCmd) error { // Create a temporary file to store the output. Because of a bug in // Docker, sometimes all the output doesn't properly show up. This @@ -41,7 +64,13 @@ func (c *Communicator) Start(remote *packer.RemoteCmd) error { // This file will store the exit code of the command once it is complete. exitCodePath := outputFile.Name() + "-exit" - cmd := exec.Command("docker", "attach", c.ContainerId) + var cmd *exec.Cmd + if useDockerExec { + cmd = exec.Command("docker", "exec", "-i", c.ContainerId, "/bin/sh") + } else { + cmd = exec.Command("docker", "attach", c.ContainerId) + } + stdin_w, err := cmd.StdinPipe() if err != nil { // We have to do some cleanup since run was never called From a7206aebd79738993ad1105faf6bc6be6161c90f Mon Sep 17 00:00:00 2001 From: Donald Guy Date: Fri, 20 Mar 2015 12:50:03 -0400 Subject: [PATCH 2/2] builder/docker: attempt to satisfy travis for #1993 --- builder/docker/communicator.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/builder/docker/communicator.go b/builder/docker/communicator.go index c6e3829f0..bd1c87240 100644 --- a/builder/docker/communicator.go +++ b/builder/docker/communicator.go @@ -28,19 +28,23 @@ type Communicator struct { lock sync.Mutex } -var dockerVersion version.Version +var dockerVersion *version.Version var useDockerExec bool func init() { execConstraint, _ := version.NewConstraint(">= 1.4.0") versionExtractor := regexp.MustCompile(version.VersionRegexpRaw) - dockerVersionOutput, _ := exec.Command("docker", "-v").Output() - dockerVersionString := string(versionExtractor.FindSubmatch(dockerVersionOutput)[0]) + dockerVersionOutput, err := exec.Command("docker", "-v").Output() + extractedVersion := versionExtractor.FindSubmatch(dockerVersionOutput) - dockerVersion, err := version.NewVersion(dockerVersionString) - if err != nil { - log.Printf("Docker returned malformed version string: %e", err) + if extractedVersion != nil { + dockerVersionString := string(extractedVersion[0]) + dockerVersion, err = version.NewVersion(dockerVersionString) + } + + if dockerVersion == nil { + log.Printf("Could not determine docker version: %v", err) log.Printf("Assuming no `exec` capability, using `attatch`") useDockerExec = false } else {