From ccdee2550b599e087a92c402e2504006ab89447a Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 20 Mar 2018 11:34:53 -0700 Subject: [PATCH] Treat any output directory test command as error. Surfaces any communications from the remote end during file uploads. For example, we might get notifications if we're logging in with the wrong user. Rather than swallow these, let's show them to the user. --- communicator/ssh/communicator.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/communicator/ssh/communicator.go b/communicator/ssh/communicator.go index 6fedb479c..b00b99220 100644 --- a/communicator/ssh/communicator.go +++ b/communicator/ssh/communicator.go @@ -413,7 +413,12 @@ func (c *comm) sftpUploadFile(path string, input io.Reader, client *sftp.Client, // find out if destination is a directory (this is to replicate rsync behavior) testDirectoryCommand := fmt.Sprintf(`test -d "%s"`, path) - cmd := &packer.RemoteCmd{Command: testDirectoryCommand} + var stdout, stderr bytes.Buffer + cmd := &packer.RemoteCmd{ + Command: testDirectoryCommand, + Stdout: &stdout, + Stderr: &stderr, + } err := c.Start(cmd) @@ -422,6 +427,12 @@ func (c *comm) sftpUploadFile(path string, input io.Reader, client *sftp.Client, return err } cmd.Wait() + if stdout.Len() > 0 { + return fmt.Errorf("%s", stdout.Bytes()) + } + if stderr.Len() > 0 { + return fmt.Errorf("%s", stderr.Bytes()) + } if cmd.ExitStatus == 0 { if fi == nil { return fmt.Errorf("Upload path is a directory, unable to continue.") @@ -579,7 +590,12 @@ func (c *comm) scpUploadSession(path string, input io.Reader, fi *os.FileInfo) e // find out if destination is a directory (this is to replicate rsync behavior) testDirectoryCommand := fmt.Sprintf(`test -d "%s"`, path) - cmd := &packer.RemoteCmd{Command: testDirectoryCommand} + var stdout, stderr bytes.Buffer + cmd := &packer.RemoteCmd{ + Command: testDirectoryCommand, + Stdout: &stdout, + Stderr: &stderr, + } err := c.Start(cmd) @@ -588,6 +604,12 @@ func (c *comm) scpUploadSession(path string, input io.Reader, fi *os.FileInfo) e return err } cmd.Wait() + if stdout.Len() > 0 { + return fmt.Errorf("%s", stdout.Bytes()) + } + if stderr.Len() > 0 { + return fmt.Errorf("%s", stderr.Bytes()) + } if cmd.ExitStatus == 0 { if fi == nil { return fmt.Errorf("Upload path is a directory, unable to continue.")