packer-cn/common/step_wait_for_ip.go

51 lines
1.1 KiB
Go
Raw Normal View History

package common
import (
2018-04-25 07:22:38 -04:00
"github.com/hashicorp/packer/helper/multistep"
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"time"
2018-04-25 07:22:38 -04:00
"context"
)
type StepWaitForIp struct{}
2018-05-16 09:05:08 -04:00
func (s *StepWaitForIp) Run(ctx context.Context, 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() {
2018-05-16 09:05:08 -04:00
ip, err := vm.WaitForIP(ctx)
if err != nil {
errChan <- err
} else {
ipChan <- ip
}
}()
for {
select {
case err := <-errChan:
state.Put("error", err)
return multistep.ActionHalt
2018-05-16 09:05:08 -04:00
case <-ctx.Done():
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
}
}
}
}
2018-05-06 17:26:04 -04:00
func (s *StepWaitForIp) Cleanup(state multistep.StateBag) {}