check stdout to make sure echo command actually goes through before counting computer as fully restarted

This commit is contained in:
Megan Marsh 2017-08-23 15:23:50 -07:00
parent 89b058604d
commit b2923b4da6
2 changed files with 17 additions and 4 deletions

View File

@ -266,7 +266,6 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
if _, err := f.Seek(0, 0); err != nil {
return err
}
if err := comm.Upload(p.config.RemotePath, f, nil); err != nil {
return fmt.Errorf("Error uploading script: %s", err)
}

View File

@ -1,8 +1,11 @@
package restart
import (
"bytes"
"fmt"
"io"
"log"
"strings"
"sync"
"time"
@ -14,7 +17,7 @@ import (
)
var DefaultRestartCommand = "shutdown /r /f /t 0 /c \"packer restart\""
var DefaultRestartCheckCommand = winrm.Powershell(`echo "${env:COMPUTERNAME} restarted."`)
var DefaultRestartCheckCommand = winrm.Powershell(`if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'}; echo "${env:COMPUTERNAME} restarted."`)
var retryableSleep = 5 * time.Second
var TryCheckReboot = "shutdown.exe -f -r -t 60"
var AbortReboot = "shutdown.exe -a"
@ -119,6 +122,7 @@ var waitForRestart = func(p *Provisioner, comm packer.Communicator) error {
// Couldn't execute, we assume machine is rebooting already
break
}
if cmd.ExitStatus == 1115 || cmd.ExitStatus == 1190 {
// Reboot already in progress but not completed
log.Printf("Reboot already in progress, waiting...")
@ -170,9 +174,11 @@ WaitLoop:
}
var waitForCommunicator = func(p *Provisioner) error {
cmd := &packer.RemoteCmd{Command: p.config.RestartCheckCommand}
for {
cmd := &packer.RemoteCmd{Command: p.config.RestartCheckCommand}
var buf, buf2 bytes.Buffer
cmd.Stdout = &buf
cmd.Stdout = io.MultiWriter(cmd.Stdout, &buf2)
select {
case <-p.cancel:
log.Println("Communicator wait canceled, exiting loop")
@ -183,12 +189,20 @@ var waitForCommunicator = func(p *Provisioner) error {
log.Printf("Checking that communicator is connected with: '%s'", cmd.Command)
err := cmd.StartWithUi(p.comm, p.ui)
if err != nil {
log.Printf("Communication connection err: %s", err)
continue
}
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
}
break
}