Merge pull request #6606 from hashicorp/fix_6301

Make Windows-Restart provisioner work even when using ssh communicator
This commit is contained in:
Megan Marsh 2018-08-21 13:44:01 -07:00 committed by GitHub
commit af48214ddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 9 deletions

View File

@ -17,11 +17,11 @@ import (
"github.com/masterzen/winrm"
)
var DefaultRestartCommand = "shutdown /r /f /t 0 /c \"packer restart\""
var DefaultRestartCommand = `shutdown /r /f /t 0 /c "packer restart"`
var DefaultRestartCheckCommand = winrm.Powershell(`echo "${env:COMPUTERNAME} restarted."`)
var retryableSleep = 5 * time.Second
var TryCheckReboot = "shutdown.exe -f -r -t 60"
var AbortReboot = "shutdown.exe -a"
var TryCheckReboot = `shutdown /r /f /t 60 /c "packer restart test"`
var AbortReboot = `shutdown /a`
type Config struct {
common.PackerConfig `mapstructure:",squash"`
@ -124,21 +124,29 @@ var waitForRestart = func(p *Provisioner, comm packer.Communicator) error {
for {
log.Printf("Check if machine is rebooting...")
cmd = &packer.RemoteCmd{Command: trycommand}
err = cmd.StartWithUi(comm, ui)
if err != nil {
if err := p.comm.Start(cmd); err != nil {
// Couldn't execute, we assume machine is rebooting already
break
}
if cmd.ExitStatus == 1115 || cmd.ExitStatus == 1190 {
cmd.Wait()
if cmd.ExitStatus == 1 {
// SSH provisioner, and we're already rebooting. SSH can reconnect
// without our help; exit this wait loop.
break
}
if cmd.ExitStatus == 1115 || cmd.ExitStatus == 1190 || cmd.ExitStatus == 1717 {
// Reboot already in progress but not completed
log.Printf("Reboot already in progress, waiting...")
time.Sleep(10 * time.Second)
break
}
if cmd.ExitStatus == 0 {
// Cancel reboot we created to test if machine was already rebooting
cmd = &packer.RemoteCmd{Command: abortcommand}
cmd.StartWithUi(comm, ui)
if err := p.comm.Start(cmd); err != nil {
return err
}
cmd.Wait()
break
}
}
@ -175,7 +183,6 @@ WaitLoop:
return fmt.Errorf("Interrupt detected, quitting waiting for machine to restart")
}
}
return nil
}