reduce duplicated code

pull/7444/head
Megan Marsh 7 years ago
parent 0860edeed8
commit 12b9004c76

@ -25,6 +25,7 @@ type Communicator struct {
Config *Config
ContainerUser string
lock sync.Mutex
EntryPoint []string
}
func (c *Communicator) Start(remote *packer.RemoteCmd) error {
@ -32,10 +33,9 @@ func (c *Communicator) Start(remote *packer.RemoteCmd) error {
"exec",
"-i",
c.ContainerID,
"/bin/sh",
"-c",
fmt.Sprintf("(%s)", remote.Command),
}
dockerArgs = append(dockerArgs, c.EntryPoint...)
dockerArgs = append(dockerArgs, fmt.Sprintf("(%s)", remote.Command))
if c.Config.Pty {
dockerArgs = append(dockerArgs[:2], append([]string{"-t"}, dockerArgs[2:]...)...)

@ -40,6 +40,7 @@ func (s *StepConnectDocker) Run(_ context.Context, state multistep.StateBag) mul
Version: version,
Config: config,
ContainerUser: containerUser,
EntryPoint: []string{"powershell"},
},
}
state.Put("communicator", comm)
@ -52,6 +53,7 @@ func (s *StepConnectDocker) Run(_ context.Context, state multistep.StateBag) mul
Version: version,
Config: config,
ContainerUser: containerUser,
EntryPoint: []string{"/bin/sh", "-c"},
}
state.Put("communicator", comm)
}

@ -7,7 +7,6 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/hashicorp/packer/packer"
@ -17,52 +16,6 @@ type WindowsContainerCommunicator struct {
Communicator
}
func (c *WindowsContainerCommunicator) Start(remote *packer.RemoteCmd) error {
dockerArgs := []string{
"exec",
"-i",
c.ContainerID,
"powershell",
fmt.Sprintf("(%s)", remote.Command),
}
if c.Config.Pty {
dockerArgs = append(dockerArgs[:2], append([]string{"-t"}, dockerArgs[2:]...)...)
}
if c.Config.ExecUser != "" {
dockerArgs = append(dockerArgs[:2],
append([]string{"-u", c.Config.ExecUser}, dockerArgs[2:]...)...)
}
cmd := exec.Command("docker", dockerArgs...)
var (
stdin_w io.WriteCloser
err error
)
stdin_w, err = cmd.StdinPipe()
if err != nil {
return err
}
stderr_r, err := cmd.StderrPipe()
if err != nil {
return err
}
stdout_r, err := cmd.StdoutPipe()
if err != nil {
return err
}
// Run the actual command in a goroutine so that Start doesn't block
go c.run(cmd, remote, stdin_w, stdout_r, stderr_r)
return nil
}
// Upload uses docker exec to copy the file from the host to the container
func (c *WindowsContainerCommunicator) Upload(dst string, src io.Reader, fi *os.FileInfo) error {
// Create a temporary file to store the upload
@ -225,14 +178,3 @@ func (c *WindowsContainerCommunicator) Download(src string, dst io.Writer) error
return nil
}
func (c *WindowsContainerCommunicator) DownloadDir(src string, dst string, exclude []string) error {
return fmt.Errorf("DownloadDir is not implemented for docker")
}
// Runs the given command and blocks until completion
func (c *WindowsContainerCommunicator) run(cmd *exec.Cmd, remote *packer.RemoteCmd, stdin io.WriteCloser, stdout, stderr io.ReadCloser) {
// For Docker, remote communication must be serialized since it
// only supports single execution.
c.Communicator.run(cmd, remote, stdin, stdout, stderr)
}

Loading…
Cancel
Save