Merge pull request #5300 from hashicorp/5298

add echo test to winrm connection.
This commit is contained in:
Megan Marsh 2017-09-05 10:46:33 -07:00 committed by GitHub
commit 78f9c8a619
2 changed files with 37 additions and 2 deletions

View File

@ -1,13 +1,17 @@
package communicator
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"strings"
"time"
"github.com/hashicorp/packer/communicator/winrm"
"github.com/hashicorp/packer/packer"
winrmcmd "github.com/masterzen/winrm"
"github.com/mitchellh/multistep"
)
@ -141,6 +145,39 @@ func (s *StepConnectWinRM) waitForWinRM(state multistep.StateBag, cancel <-chan
break
}
// run an "echo" command to make sure winrm is actually connected before moving on.
var connectCheckCommand = winrmcmd.Powershell(`if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'}; echo "WinRM connected."`)
var retryableSleep = 5 * time.Second
// run an "echo" command to make sure that the winrm is connected
for {
cmd := &packer.RemoteCmd{Command: connectCheckCommand}
var buf, buf2 bytes.Buffer
cmd.Stdout = &buf
cmd.Stdout = io.MultiWriter(cmd.Stdout, &buf2)
select {
case <-cancel:
log.Println("WinRM wait canceled, exiting loop")
return comm, fmt.Errorf("WinRM wait canceled")
case <-time.After(retryableSleep):
}
log.Printf("Checking that WinRM is connected with: '%s'", connectCheckCommand)
ui := state.Get("ui").(packer.Ui)
err := cmd.StartWithUi(comm, ui)
if err != nil {
log.Printf("Communication connection err: %s", err)
continue
}
log.Printf("Connected to machine")
stdoutToRead := buf2.String()
if !strings.Contains(stdoutToRead, "WinRM connected.") {
log.Printf("echo didn't succeed; retrying...")
continue
}
break
}
return comm, nil
}

View File

@ -197,8 +197,6 @@ var waitForCommunicator = func(p *Provisioner) error {
log.Printf("Connected to machine")
stdoutToRead := buf2.String()
buf2.Reset()
buf.Reset()
if !strings.Contains(stdoutToRead, "restarted.") {
log.Printf("echo didn't succeed; retrying...")
continue