Merge pull request #5272 from hashicorp/fix_5064

Fix post-restart powershell failure
This commit is contained in:
Megan Marsh 2017-08-28 11:41:37 -07:00 committed by GitHub
commit f15304aa59
4 changed files with 25 additions and 8 deletions

View File

@ -21,8 +21,6 @@ import (
"github.com/hashicorp/packer/template/interpolate"
)
const DefaultRemotePath = "c:/Windows/Temp/script.ps1"
var retryableSleep = 2 * time.Second
type Config struct {
@ -137,7 +135,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
if p.config.RemotePath == "" {
p.config.RemotePath = DefaultRemotePath
uuid := uuid.TimeOrderedUUID()
p.config.RemotePath = fmt.Sprintf(`c:/Windows/Temp/script-%s.ps1`, uuid)
}
if p.config.Scripts == nil {
@ -267,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

@ -67,7 +67,8 @@ func TestProvisionerPrepare_Defaults(t *testing.T) {
t.Fatalf("err: %s", err)
}
if p.config.RemotePath != DefaultRemotePath {
matched, _ := regexp.MatchString("c:/Windows/Temp/script-.*.ps1", p.config.RemotePath)
if !matched {
t.Errorf("unexpected remote path: %s", p.config.RemotePath)
}
@ -471,6 +472,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
config["scripts"] = []string{tempFile.Name()}
config["packer_build_name"] = "foobuild"
config["packer_builder_type"] = "footype"
config["remote_path"] = "c:/Windows/Temp/script.ps1"
ui := testUi()
p := new(Provisioner)
@ -517,6 +519,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
envVars[0] = "FOO=BAR"
envVars[1] = "BAR=BAZ"
config["environment_vars"] = envVars
config["remote_path"] = "c:/Windows/Temp/script.ps1"
p := new(Provisioner)
comm := new(packer.MockCommunicator)
@ -626,6 +629,7 @@ func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) {
func TestProvision_createCommandText(t *testing.T) {
config := testConfig()
config["remote_path"] = "c:/Windows/Temp/script.ps1"
p := new(Provisioner)
comm := new(packer.MockCommunicator)
p.communicator = comm

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
}

View File

@ -242,6 +242,7 @@ func TestProvision_waitForCommunicator(t *testing.T) {
p.comm = comm
p.ui = ui
comm.StartStderr = "WinRM terminated"
comm.StartStdout = "WIN-V4CEJ7MC5SN restarted."
comm.StartExitStatus = 1
p.Prepare(config)
err := waitForCommunicator(p)