Merge branch 'docker-exec-by-version' of https://github.com/donaldguy/packer into donaldguy-docker-exec-by-version
This commit is contained in:
commit
9587a926a2
|
@ -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,31 @@ 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, 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 {
|
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 +68,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
|
||||||
|
|
Loading…
Reference in New Issue