From b2923b4da6c6bebc11edeca36585331829015ff4 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 23 Aug 2017 15:23:50 -0700 Subject: [PATCH] check stdout to make sure echo command actually goes through before counting computer as fully restarted --- provisioner/powershell/provisioner.go | 1 - provisioner/windows-restart/provisioner.go | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index 1183b919b..eb80a0f20 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -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) } diff --git a/provisioner/windows-restart/provisioner.go b/provisioner/windows-restart/provisioner.go index 06641432b..8c4eb955d 100644 --- a/provisioner/windows-restart/provisioner.go +++ b/provisioner/windows-restart/provisioner.go @@ -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 }