From 82a45520303ced66f3e5935462244ffb34cedbfd Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 5 Apr 2018 10:41:03 -0400 Subject: [PATCH] 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. --- communicator/winrm/communicator.go | 52 +++++++----------------------- 1 file changed, 11 insertions(+), 41 deletions(-) diff --git a/communicator/winrm/communicator.go b/communicator/winrm/communicator.go index 18112851d..b606cc721 100644 --- a/communicator/winrm/communicator.go +++ b/communicator/winrm/communicator.go @@ -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