connect communicator during Start
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.
This commit is contained in:
parent
82a4552030
commit
abfb43555a
|
@ -406,7 +406,7 @@ func (c *Communicator) UploadDir(dst string, src string) error {
|
||||||
func (c *Communicator) newSession() (session *ssh.Session, err error) {
|
func (c *Communicator) newSession() (session *ssh.Session, err error) {
|
||||||
log.Println("[DEBUG] opening new ssh session")
|
log.Println("[DEBUG] opening new ssh session")
|
||||||
if c.client == nil {
|
if c.client == nil {
|
||||||
err = errors.New("client not available")
|
err = errors.New("ssh client is not connected")
|
||||||
} else {
|
} else {
|
||||||
session, err = c.client.NewSession()
|
session, err = c.client.NewSession()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package winrm
|
package winrm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
@ -14,9 +13,6 @@ import (
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/masterzen/winrm"
|
"github.com/masterzen/winrm"
|
||||||
"github.com/packer-community/winrmcp/winrmcp"
|
"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
|
// Communicator represents the WinRM communicator
|
||||||
|
@ -97,13 +93,13 @@ func (c *Communicator) Connect(o terraform.UIOutput) error {
|
||||||
log.Printf("[DEBUG] connecting to remote shell using WinRM")
|
log.Printf("[DEBUG] connecting to remote shell using WinRM")
|
||||||
shell, err := client.CreateShell()
|
shell, err := client.CreateShell()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[ERROR] connection error: %s", err)
|
log.Printf("[ERROR] error creating shell: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = shell.Close()
|
err = shell.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[ERROR] error closing connection: %s", err)
|
log.Printf("[ERROR] error closing shell: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,8 +135,13 @@ func (c *Communicator) Start(rc *remote.Cmd) error {
|
||||||
rc.Init()
|
rc.Init()
|
||||||
log.Printf("[DEBUG] starting remote command: %s", rc.Command)
|
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 {
|
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)
|
status, err := c.client.Run(rc.Command, rc.Stdout, rc.Stderr)
|
||||||
|
|
Loading…
Reference in New Issue