provisioner/windows-restart: fix potential panic case
This commit is contained in:
parent
7711e07f05
commit
b2609db395
|
@ -3,6 +3,7 @@ package restart
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/masterzen/winrm/winrm"
|
"github.com/masterzen/winrm/winrm"
|
||||||
|
@ -33,10 +34,11 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Provisioner struct {
|
type Provisioner struct {
|
||||||
config Config
|
config Config
|
||||||
comm packer.Communicator
|
comm packer.Communicator
|
||||||
ui packer.Ui
|
ui packer.Ui
|
||||||
cancel chan struct{}
|
cancel chan struct{}
|
||||||
|
cancelLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
|
@ -68,10 +70,13 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
|
p.cancelLock.Lock()
|
||||||
|
p.cancel = make(chan struct{})
|
||||||
|
p.cancelLock.Unlock()
|
||||||
|
|
||||||
ui.Say("Restarting Machine")
|
ui.Say("Restarting Machine")
|
||||||
p.comm = comm
|
p.comm = comm
|
||||||
p.ui = ui
|
p.ui = ui
|
||||||
p.cancel = make(chan struct{})
|
|
||||||
|
|
||||||
var cmd *packer.RemoteCmd
|
var cmd *packer.RemoteCmd
|
||||||
command := p.config.RestartCommand
|
command := p.config.RestartCommand
|
||||||
|
@ -164,7 +169,12 @@ var waitForCommunicator = func(p *Provisioner) error {
|
||||||
|
|
||||||
func (p *Provisioner) Cancel() {
|
func (p *Provisioner) Cancel() {
|
||||||
log.Printf("Received interrupt Cancel()")
|
log.Printf("Received interrupt Cancel()")
|
||||||
close(p.cancel)
|
|
||||||
|
p.cancelLock.Lock()
|
||||||
|
defer p.cancelLock.Unlock()
|
||||||
|
if p.cancel != nil {
|
||||||
|
close(p.cancel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// retryable will retry the given function over and over until a
|
// retryable will retry the given function over and over until a
|
||||||
|
|
Loading…
Reference in New Issue