Commit 9587a926 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Merge branch 'docker-exec-by-version' of https://github.com/donaldguy/packer...

Merge branch 'docker-exec-by-version' of https://github.com/donaldguy/packer into donaldguy-docker-exec-by-version
parents 7bbb9408 a7206aeb
......@@ -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,31 @@ 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, err := exec.Command("docker", "-v").Output()
extractedVersion := versionExtractor.FindSubmatch(dockerVersionOutput)
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 {
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 +68,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
......
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