diff --git a/helper/communicator/step_connect_winrm.go b/helper/communicator/step_connect_winrm.go index 2318bc1c0..14f5cfc91 100644 --- a/helper/communicator/step_connect_winrm.go +++ b/helper/communicator/step_connect_winrm.go @@ -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 } diff --git a/provisioner/windows-restart/provisioner.go b/provisioner/windows-restart/provisioner.go index 8c4eb955d..9b77c097e 100644 --- a/provisioner/windows-restart/provisioner.go +++ b/provisioner/windows-restart/provisioner.go @@ -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