Make waiting for IP address interruptible

fixes #24
This commit is contained in:
Andrei Tonkikh 2018-02-12 15:56:29 +03:00
parent 78d53c81ce
commit 4875b82d12
4 changed files with 51 additions and 9 deletions

View File

@ -50,6 +50,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepRun{
Config: &b.config.RunConfig,
},
&common.StepWaitForIp{},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: common.CommHost,

View File

@ -39,15 +39,6 @@ func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}
ui.Say("Waiting for IP...")
ip, err := vm.WaitForIP()
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("ip", ip)
ui.Say(fmt.Sprintf("IP address: %v", ip))
return multistep.ActionContinue
}

View File

@ -0,0 +1,49 @@
package common
import (
"github.com/mitchellh/multistep"
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"time"
)
type StepWaitForIp struct{}
func (s *StepWaitForIp) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Waiting for IP...")
ipChan := make(chan string)
errChan := make(chan error)
go func() {
ip, err := vm.WaitForIP()
if err != nil {
errChan <- err
} else {
ipChan <- ip
}
}()
for {
select {
case err := <-errChan:
state.Put("error", err)
return multistep.ActionHalt
case ip := <-ipChan:
state.Put("ip", ip)
ui.Say(fmt.Sprintf("IP address: %v", ip))
return multistep.ActionContinue
case <-time.After(1 * time.Second):
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return multistep.ActionHalt
}
}
}
}
func (s *StepWaitForIp) Cleanup(state multistep.StateBag) {
// nothing
}

View File

@ -57,6 +57,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepRun{
Config: &b.config.RunConfig,
},
&common.StepWaitForIp{},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: common.CommHost,