packer-cn/common/step_wait_for_ip.go

50 lines
1012 B
Go

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
}