Skip to content

Commit

Permalink
use Run instead of Shell.Execute in winrm
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jbardin committed Apr 5, 2018
1 parent 3c30f04 commit 82a4552
Showing 1 changed file with 11 additions and 41 deletions.
52 changes: 11 additions & 41 deletions communicator/winrm/communicator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package winrm

import (
"errors"
"fmt"
"io"
"log"
"math/rand"
"strconv"
"strings"
"sync"
"time"

"github.com/hashicorp/terraform/communicator/remote"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down

0 comments on commit 82a4552

Please sign in to comment.