Merge pull request #6481 from hashicorp/fix_6476

don't try to determine file or folder with winrm communicator; not al…
This commit is contained in:
Megan Marsh 2018-07-13 09:25:47 -07:00 committed by GitHub
commit 9432067449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 14 deletions

View File

@ -124,24 +124,13 @@ 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, fi *os.FileInfo) error {
wcp, err := c.newCopyClient()
if err != nil {
return err
}
if err != nil {
return fmt.Errorf("Was unable to create winrm client: %s", err)
}
client, err := c.newWinRMClient()
stdout, _, _, err := client.RunWithString(fmt.Sprintf("powershell -Command \"(Get-Item %s) -is [System.IO.DirectoryInfo]\"", path), "")
if err != nil {
return fmt.Errorf("Couldn't determine whether destination was a folder or file: %s", err)
if strings.HasSuffix(path, `\`) {
// path is a directory
path += filepath.Base((*fi).Name())
}
if strings.Contains(stdout, "True") {
// The path exists and is a directory.
// Upload file into the directory instead of overwriting.
path = filepath.Join(path, filepath.Base((*fi).Name()))
}
log.Printf("Uploading file to '%s'", path)
return wcp.Write(path, input)
}