Commit df7623d9 authored by Donald Guy's avatar Donald Guy

builder/docker: Run scripts /w `exec` if -v > 1.4

parent b49d74d9
...@@ -9,12 +9,14 @@ import ( ...@@ -9,12 +9,14 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"sync" "sync"
"syscall" "syscall"
"time" "time"
"github.com/ActiveState/tail" "github.com/ActiveState/tail"
"github.com/hashicorp/go-version"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
...@@ -26,6 +28,27 @@ type Communicator struct { ...@@ -26,6 +28,27 @@ type Communicator struct {
lock sync.Mutex 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 { func (c *Communicator) Start(remote *packer.RemoteCmd) error {
// Create a temporary file to store the output. Because of a bug in // Create a temporary file to store the output. Because of a bug in
// Docker, sometimes all the output doesn't properly show up. This // Docker, sometimes all the output doesn't properly show up. This
...@@ -41,7 +64,13 @@ func (c *Communicator) Start(remote *packer.RemoteCmd) error { ...@@ -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. // This file will store the exit code of the command once it is complete.
exitCodePath := outputFile.Name() + "-exit" 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() stdin_w, err := cmd.StdinPipe()
if err != nil { if err != nil {
// We have to do some cleanup since run was never called // We have to do some cleanup since run was never called
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment