2015-06-14 14:14:47 -04:00
|
|
|
package restart
|
|
|
|
|
|
|
|
import (
|
2017-08-23 18:23:50 -04:00
|
|
|
"bytes"
|
2015-06-14 14:14:47 -04:00
|
|
|
"fmt"
|
2017-08-23 18:23:50 -04:00
|
|
|
"io"
|
2015-06-14 14:14:47 -04:00
|
|
|
"log"
|
2017-08-23 18:23:50 -04:00
|
|
|
"strings"
|
2015-06-17 12:22:27 -04:00
|
|
|
"sync"
|
2015-06-14 14:14:47 -04:00
|
|
|
"time"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2016-10-01 16:34:42 -04:00
|
|
|
"github.com/masterzen/winrm"
|
2015-06-14 14:14:47 -04:00
|
|
|
)
|
|
|
|
|
2015-08-19 00:09:43 -04:00
|
|
|
var DefaultRestartCommand = "shutdown /r /f /t 0 /c \"packer restart\""
|
2017-08-23 18:23:50 -04:00
|
|
|
var DefaultRestartCheckCommand = winrm.Powershell(`if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'}; echo "${env:COMPUTERNAME} restarted."`)
|
2015-06-14 14:14:47 -04:00
|
|
|
var retryableSleep = 5 * time.Second
|
2015-08-06 20:18:17 -04:00
|
|
|
var TryCheckReboot = "shutdown.exe -f -r -t 60"
|
|
|
|
var AbortReboot = "shutdown.exe -a"
|
2015-06-14 14:14:47 -04:00
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
// The command used to restart the guest machine
|
|
|
|
RestartCommand string `mapstructure:"restart_command"`
|
|
|
|
|
|
|
|
// The command used to check if the guest machine has restarted
|
|
|
|
// The output of this command will be displayed to the user
|
|
|
|
RestartCheckCommand string `mapstructure:"restart_check_command"`
|
|
|
|
|
|
|
|
// The timeout for waiting for the machine to restart
|
|
|
|
RestartTimeout time.Duration `mapstructure:"restart_timeout"`
|
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2015-06-17 12:22:27 -04:00
|
|
|
config Config
|
|
|
|
comm packer.Communicator
|
|
|
|
ui packer.Ui
|
|
|
|
cancel chan struct{}
|
|
|
|
cancelLock sync.Mutex
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:26:54 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-06-14 14:14:47 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RestartCommand == "" {
|
|
|
|
p.config.RestartCommand = DefaultRestartCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RestartCheckCommand == "" {
|
|
|
|
p.config.RestartCheckCommand = DefaultRestartCheckCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RestartTimeout == 0 {
|
|
|
|
p.config.RestartTimeout = 5 * time.Minute
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
2015-06-17 12:22:27 -04:00
|
|
|
p.cancelLock.Lock()
|
|
|
|
p.cancel = make(chan struct{})
|
|
|
|
p.cancelLock.Unlock()
|
|
|
|
|
2015-06-14 14:14:47 -04:00
|
|
|
ui.Say("Restarting Machine")
|
|
|
|
p.comm = comm
|
|
|
|
p.ui = ui
|
|
|
|
|
|
|
|
var cmd *packer.RemoteCmd
|
|
|
|
command := p.config.RestartCommand
|
|
|
|
err := p.retryable(func() error {
|
|
|
|
cmd = &packer.RemoteCmd{Command: command}
|
|
|
|
return cmd.StartWithUi(comm, ui)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Restart script exited with non-zero exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
|
|
|
|
2015-08-06 20:18:17 -04:00
|
|
|
return waitForRestart(p, comm)
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 20:18:17 -04:00
|
|
|
var waitForRestart = func(p *Provisioner, comm packer.Communicator) error {
|
2015-06-14 14:14:47 -04:00
|
|
|
ui := p.ui
|
|
|
|
ui.Say("Waiting for machine to restart...")
|
|
|
|
waitDone := make(chan bool, 1)
|
|
|
|
timeout := time.After(p.config.RestartTimeout)
|
|
|
|
var err error
|
|
|
|
|
2015-08-06 20:18:17 -04:00
|
|
|
p.comm = comm
|
|
|
|
var cmd *packer.RemoteCmd
|
|
|
|
trycommand := TryCheckReboot
|
|
|
|
abortcommand := AbortReboot
|
|
|
|
// Stolen from Vagrant reboot checker
|
|
|
|
for {
|
|
|
|
log.Printf("Check if machine is rebooting...")
|
|
|
|
cmd = &packer.RemoteCmd{Command: trycommand}
|
|
|
|
err = cmd.StartWithUi(comm, ui)
|
|
|
|
if err != nil {
|
2017-03-28 20:45:01 -04:00
|
|
|
// Couldn't execute, we assume machine is rebooting already
|
2015-08-06 20:18:17 -04:00
|
|
|
break
|
|
|
|
}
|
2017-08-23 18:23:50 -04:00
|
|
|
|
2015-08-19 17:00:38 -04:00
|
|
|
if cmd.ExitStatus == 1115 || cmd.ExitStatus == 1190 {
|
2015-08-06 20:18:17 -04:00
|
|
|
// Reboot already in progress but not completed
|
|
|
|
log.Printf("Reboot already in progress, waiting...")
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus == 0 {
|
|
|
|
// Cancel reboot we created to test if machine was already rebooting
|
|
|
|
cmd = &packer.RemoteCmd{Command: abortcommand}
|
|
|
|
cmd.StartWithUi(comm, ui)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 14:14:47 -04:00
|
|
|
go func() {
|
|
|
|
log.Printf("Waiting for machine to become available...")
|
|
|
|
err = waitForCommunicator(p)
|
|
|
|
waitDone <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("Waiting for machine to reboot with timeout: %s", p.config.RestartTimeout)
|
|
|
|
|
|
|
|
WaitLoop:
|
|
|
|
for {
|
|
|
|
// Wait for either WinRM to become available, a timeout to occur,
|
|
|
|
// or an interrupt to come through.
|
|
|
|
select {
|
|
|
|
case <-waitDone:
|
|
|
|
if err != nil {
|
2017-06-21 13:51:23 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error waiting for machine to restart: %s", err))
|
2015-06-14 14:14:47 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Machine successfully restarted, moving on")
|
|
|
|
close(p.cancel)
|
|
|
|
break WaitLoop
|
|
|
|
case <-timeout:
|
2017-06-21 13:51:23 -04:00
|
|
|
err := fmt.Errorf("Timeout waiting for machine to restart.")
|
2015-06-14 14:14:47 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
close(p.cancel)
|
|
|
|
return err
|
|
|
|
case <-p.cancel:
|
|
|
|
close(waitDone)
|
|
|
|
return fmt.Errorf("Interrupt detected, quitting waiting for machine to restart")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var waitForCommunicator = func(p *Provisioner) error {
|
|
|
|
for {
|
2017-08-23 18:23:50 -04:00
|
|
|
cmd := &packer.RemoteCmd{Command: p.config.RestartCheckCommand}
|
|
|
|
var buf, buf2 bytes.Buffer
|
|
|
|
cmd.Stdout = &buf
|
|
|
|
cmd.Stdout = io.MultiWriter(cmd.Stdout, &buf2)
|
2015-06-14 14:14:47 -04:00
|
|
|
select {
|
|
|
|
case <-p.cancel:
|
2017-03-28 20:45:01 -04:00
|
|
|
log.Println("Communicator wait canceled, exiting loop")
|
|
|
|
return fmt.Errorf("Communicator wait canceled")
|
2015-06-14 14:14:47 -04:00
|
|
|
case <-time.After(retryableSleep):
|
|
|
|
}
|
|
|
|
|
2017-06-26 15:56:55 -04:00
|
|
|
log.Printf("Checking that communicator is connected with: '%s'", cmd.Command)
|
2015-06-14 14:14:47 -04:00
|
|
|
|
|
|
|
err := cmd.StartWithUi(p.comm, p.ui)
|
2017-08-23 18:23:50 -04:00
|
|
|
|
2015-06-14 14:14:47 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Communication connection err: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Connected to machine")
|
2017-08-23 18:23:50 -04:00
|
|
|
stdoutToRead := buf2.String()
|
|
|
|
if !strings.Contains(stdoutToRead, "restarted.") {
|
|
|
|
log.Printf("echo didn't succeed; retrying...")
|
|
|
|
continue
|
|
|
|
}
|
2015-06-14 14:14:47 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Cancel() {
|
|
|
|
log.Printf("Received interrupt Cancel()")
|
2015-06-17 12:22:27 -04:00
|
|
|
|
|
|
|
p.cancelLock.Lock()
|
|
|
|
defer p.cancelLock.Unlock()
|
|
|
|
if p.cancel != nil {
|
|
|
|
close(p.cancel)
|
|
|
|
}
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// retryable will retry the given function over and over until a
|
|
|
|
// non-error is returned.
|
|
|
|
func (p *Provisioner) retryable(f func() error) error {
|
|
|
|
startTimeout := time.After(p.config.RestartTimeout)
|
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
if err = f(); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an error and log it
|
|
|
|
err = fmt.Errorf("Retryable error: %s", err)
|
2017-03-28 21:06:50 -04:00
|
|
|
log.Print(err.Error())
|
2015-06-14 14:14:47 -04:00
|
|
|
|
|
|
|
// Check if we timed out, otherwise we retry. It is safe to
|
|
|
|
// retry since the only error case above is if the command
|
|
|
|
// failed to START.
|
|
|
|
select {
|
|
|
|
case <-startTimeout:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
time.Sleep(retryableSleep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|