From 12b9004c76e377a00317f093cd1555349b8efe2c Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 29 Mar 2019 11:14:01 -0700 Subject: [PATCH] reduce duplicated code --- builder/docker/communicator.go | 6 +- builder/docker/step_connect_docker.go | 2 + .../docker/windows_container_communicator.go | 58 ------------------- 3 files changed, 5 insertions(+), 61 deletions(-) diff --git a/builder/docker/communicator.go b/builder/docker/communicator.go index e898ecd1c..48b3de7c6 100644 --- a/builder/docker/communicator.go +++ b/builder/docker/communicator.go @@ -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:]...)...) diff --git a/builder/docker/step_connect_docker.go b/builder/docker/step_connect_docker.go index 6f08ac326..583e69b36 100644 --- a/builder/docker/step_connect_docker.go +++ b/builder/docker/step_connect_docker.go @@ -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) } diff --git a/builder/docker/windows_container_communicator.go b/builder/docker/windows_container_communicator.go index 46650a103..dbb74f406 100644 --- a/builder/docker/windows_container_communicator.go +++ b/builder/docker/windows_container_communicator.go @@ -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) -}