use Run instead of Shell.Execute in winrm

The error from a remote command is not exported, and only exposed via
the Run method. Otherwise the Run method works exactly like the
runCommand function being removed.
This commit is contained in:
James Bardin 2018-04-05 10:41:03 -04:00
parent 3c30f04e0e
commit 82a4552030
1 changed files with 11 additions and 41 deletions

View File

@ -1,13 +1,13 @@
package winrm
import (
"errors"
"fmt"
"io"
"log"
"math/rand"
"strconv"
"strings"
"sync"
"time"
"github.com/hashicorp/terraform/communicator/remote"
@ -94,16 +94,16 @@ func (c *Communicator) Connect(o terraform.UIOutput) error {
))
}
log.Printf("connecting to remote shell using WinRM")
log.Printf("[DEBUG] connecting to remote shell using WinRM")
shell, err := client.CreateShell()
if err != nil {
log.Printf("connection error: %s", err)
log.Printf("[ERROR] connection error: %s", err)
return err
}
err = shell.Close()
if err != nil {
log.Printf("error closing connection: %s", err)
log.Printf("[ERROR] error closing connection: %s", err)
return err
}
@ -137,55 +137,25 @@ func (c *Communicator) ScriptPath() string {
// Start implementation of communicator.Communicator interface
func (c *Communicator) Start(rc *remote.Cmd) error {
rc.Init()
log.Printf("[DEBUG] starting remote command: %s", rc.Command)
err := c.Connect(nil)
if err != nil {
return err
if c.client == nil {
return errors.New("winrm client is not connected")
}
shell, err := c.client.CreateShell()
if err != nil {
return err
}
status, err := c.client.Run(rc.Command, rc.Stdout, rc.Stderr)
rc.SetExitStatus(status, err)
log.Printf("starting remote command: %s", rc.Command)
cmd, err := shell.Execute(rc.Command)
if err != nil {
return err
}
go runCommand(shell, cmd, rc)
return nil
}
func runCommand(shell *winrm.Shell, cmd *winrm.Command, rc *remote.Cmd) {
defer shell.Close()
var wg sync.WaitGroup
go func() {
wg.Add(1)
io.Copy(rc.Stdout, cmd.Stdout)
wg.Done()
}()
go func() {
wg.Add(1)
io.Copy(rc.Stderr, cmd.Stderr)
wg.Done()
}()
cmd.Wait()
wg.Wait()
rc.SetExitStatus(cmd.ExitCode(), nil)
}
// Upload implementation of communicator.Communicator interface
func (c *Communicator) Upload(path string, input io.Reader) error {
wcp, err := c.newCopyClient()
if err != nil {
return err
}
log.Printf("Uploading file to '%s'", path)
log.Printf("[DEBUG] Uploading file to '%s'", path)
return wcp.Write(path, input)
}
@ -196,7 +166,7 @@ func (c *Communicator) UploadScript(path string, input io.Reader) error {
// UploadDir implementation of communicator.Communicator interface
func (c *Communicator) UploadDir(dst string, src string) error {
log.Printf("Uploading dir '%s' to '%s'", src, dst)
log.Printf("[DEBUG] Uploading dir '%s' to '%s'", src, dst)
wcp, err := c.newCopyClient()
if err != nil {
return err