github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/hyperv/common/step_configure_ip.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/mitchellh/multistep"
    11  )
    12  
    13  type StepConfigureIp struct {
    14  }
    15  
    16  func (s *StepConfigureIp) Run(state multistep.StateBag) multistep.StepAction {
    17  	driver := state.Get("driver").(Driver)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	errorMsg := "Error configuring ip address: %s"
    21  	vmName := state.Get("vmName").(string)
    22  
    23  	ui.Say("Configuring ip address...")
    24  
    25  	count := 60
    26  	var duration time.Duration = 1
    27  	sleepTime := time.Minute * duration
    28  	var ip string
    29  
    30  	for count != 0 {
    31  		cmdOut, err := driver.GetVirtualMachineNetworkAdapterAddress(vmName)
    32  		if err != nil {
    33  			err := fmt.Errorf(errorMsg, err)
    34  			state.Put("error", err)
    35  			ui.Error(err.Error())
    36  			return multistep.ActionHalt
    37  		}
    38  
    39  		ip = strings.TrimSpace(cmdOut)
    40  
    41  		if ip != "False" {
    42  			break
    43  		}
    44  
    45  		log.Println(fmt.Sprintf("Waiting for another %v minutes...", uint(duration)))
    46  		time.Sleep(sleepTime)
    47  		count--
    48  	}
    49  
    50  	if count == 0 {
    51  		err := fmt.Errorf(errorMsg, "IP address assigned to the adapter is empty")
    52  		state.Put("error", err)
    53  		ui.Error(err.Error())
    54  		return multistep.ActionHalt
    55  	}
    56  
    57  	ui.Say("ip address is " + ip)
    58  
    59  	hostName, err := driver.GetHostName(ip)
    60  	if err != nil {
    61  		state.Put("error", err)
    62  		ui.Error(err.Error())
    63  		return multistep.ActionHalt
    64  	}
    65  
    66  	ui.Say("hostname is " + hostName)
    67  
    68  	state.Put("ip", ip)
    69  	state.Put("hostname", hostName)
    70  
    71  	return multistep.ActionContinue
    72  }
    73  
    74  func (s *StepConfigureIp) Cleanup(state multistep.StateBag) {
    75  	// do nothing
    76  }