From 31ac2652d64ddd364f0bd3ae2652559fc02caad6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 29 May 2015 11:08:41 -0700 Subject: [PATCH] bulder/docker: canExec as sep function --- builder/docker/communicator.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/builder/docker/communicator.go b/builder/docker/communicator.go index 548c1b4d8..6fedf2769 100644 --- a/builder/docker/communicator.go +++ b/builder/docker/communicator.go @@ -29,14 +29,6 @@ type Communicator struct { } func (c *Communicator) Start(remote *packer.RemoteCmd) error { - // Determine if we're using docker exec or not - useExec := false - execConstraint, err := version.NewConstraint(">= 1.4.0") - if err != nil { - return err - } - useExec = execConstraint.Check(c.Version) - // Create a temporary file to store the output. Because of a bug in // Docker, sometimes all the output doesn't properly show up. This // file will capture ALL of the output, and we'll read that. @@ -52,7 +44,7 @@ func (c *Communicator) Start(remote *packer.RemoteCmd) error { exitCodePath := outputFile.Name() + "-exit" var cmd *exec.Cmd - if useExec { + if c.canExec() { cmd = exec.Command("docker", "exec", "-i", c.ContainerId, "/bin/sh") } else { cmd = exec.Command("docker", "attach", c.ContainerId) @@ -202,6 +194,15 @@ func (c *Communicator) Download(src string, dst io.Writer) error { panic("not implemented") } +// canExec tells us whether `docker exec` is supported +func (c *Communicator) canExec() bool { + execConstraint, err := version.NewConstraint(">= 1.4.0") + if err != nil { + panic(err) + } + return execConstraint.Check(c.Version) +} + // Runs the given command and blocks until completion func (c *Communicator) run(cmd *exec.Cmd, remote *packer.RemoteCmd, stdin_w io.WriteCloser, outputFile *os.File, exitCodePath string) { // For Docker, remote communication must be serialized since it