2018-02-12 07:56:29 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-04-25 07:22:38 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-02-12 07:56:29 -05:00
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
|
|
|
"time"
|
2018-04-25 07:22:38 -04:00
|
|
|
"context"
|
2018-02-12 07:56:29 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepWaitForIp struct{}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
func (s *StepWaitForIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-02-12 07:56:29 -05:00
|
|
|
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)
|
2018-02-12 07:56:29 -05:00
|
|
|
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
|
2018-02-12 07:56:29 -05:00
|
|
|
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) {}
|