Skip to content

Commit

Permalink
connect communicator during Start
Browse files Browse the repository at this point in the history
Match the tested behavior, and that of the ssh implementation, where the
communicator automatically connects when starting a command.

Remove unused import from legacy dependency handling.
  • Loading branch information
jbardin committed Apr 5, 2018
1 parent 82a4552 commit abfb435
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion communicator/ssh/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (c *Communicator) UploadDir(dst string, src string) error {
func (c *Communicator) newSession() (session *ssh.Session, err error) {
log.Println("[DEBUG] opening new ssh session")
if c.client == nil {
err = errors.New("client not available")
err = errors.New("ssh client is not connected")
} else {
session, err = c.client.NewSession()
}
Expand Down
15 changes: 8 additions & 7 deletions communicator/winrm/communicator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package winrm

import (
"errors"
"fmt"
"io"
"log"
Expand All @@ -14,9 +13,6 @@ import (
"github.com/hashicorp/terraform/terraform"
"github.com/masterzen/winrm"
"github.com/packer-community/winrmcp/winrmcp"

// This import is a bit strange, but it's needed so `make updatedeps` can see and download it
_ "github.com/dylanmei/winrmtest"
)

// Communicator represents the WinRM communicator
Expand Down Expand Up @@ -97,13 +93,13 @@ func (c *Communicator) Connect(o terraform.UIOutput) error {
log.Printf("[DEBUG] connecting to remote shell using WinRM")
shell, err := client.CreateShell()
if err != nil {
log.Printf("[ERROR] connection error: %s", err)
log.Printf("[ERROR] error creating shell: %s", err)
return err
}

err = shell.Close()
if err != nil {
log.Printf("[ERROR] error closing connection: %s", err)
log.Printf("[ERROR] error closing shell: %s", err)
return err
}

Expand Down Expand Up @@ -139,8 +135,13 @@ func (c *Communicator) Start(rc *remote.Cmd) error {
rc.Init()
log.Printf("[DEBUG] starting remote command: %s", rc.Command)

// TODO: make sure communicators always connect first, so we can get output
// from the connection.
if c.client == nil {
return errors.New("winrm client is not connected")
log.Println("[WARN] winrm client not connected, attempting to connect")
if err := c.Connect(nil); err != nil {
return err
}
}

status, err := c.client.Run(rc.Command, rc.Stdout, rc.Stderr)
Expand Down

0 comments on commit abfb435

Please sign in to comment.