Commit b2609db3 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

provisioner/windows-restart: fix potential panic case

parent 7711e07f
...@@ -3,6 +3,7 @@ package restart ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment