From a5e61348195383154b9f667c45d36d5270bf00cc Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 3 Oct 2017 15:55:32 -0700 Subject: [PATCH] implement dir check for ssh communicator --- communicator/ssh/communicator.go | 23 +++++++++++++++++++++++ communicator/winrm/communicator.go | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/communicator/ssh/communicator.go b/communicator/ssh/communicator.go index c10e97dcc..827925f2c 100644 --- a/communicator/ssh/communicator.go +++ b/communicator/ssh/communicator.go @@ -533,6 +533,29 @@ func (c *comm) scpUploadSession(path string, input io.Reader, fi *os.FileInfo) e target_dir := filepath.Dir(path) target_file := filepath.Base(path) + // find out if it's a directory + testDirectoryCommand := fmt.Sprintf("if [ -d \"%s\" ]; then echo directory; fi", path) + cmd := &packer.RemoteCmd{Command: testDirectoryCommand} + var buf, buf2 bytes.Buffer + cmd.Stdout = &buf + cmd.Stdout = io.MultiWriter(cmd.Stdout, &buf2) + + err := c.Start(cmd) + + if err != nil { + log.Printf("Unable to check whether remote path is a dir: %s", err) + return err + } + + stdoutToRead := buf2.String() + if strings.Contains(stdoutToRead, "directory") { + log.Printf("upload locale is a directory") + target_dir = path + target_file = filepath.Base((*fi).Name()) + } + + log.Printf("target_file was %s", target_file) + // On windows, filepath.Dir uses backslash seperators (ie. "\tmp"). // This does not work when the target host is unix. Switch to forward slash // which works for unix and windows diff --git a/communicator/winrm/communicator.go b/communicator/winrm/communicator.go index a725fe51f..3970d1af8 100644 --- a/communicator/winrm/communicator.go +++ b/communicator/winrm/communicator.go @@ -122,7 +122,7 @@ func runCommand(shell *winrm.Shell, cmd *winrm.Command, rc *packer.RemoteCmd) { } // Upload implementation of communicator.Communicator interface -func (c *Communicator) Upload(path string, input io.Reader, _ *os.FileInfo) error { +func (c *Communicator) Upload(path string, input io.Reader, fi *os.FileInfo) error { wcp, err := c.newCopyClient() if err != nil { return err