github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_configure_ip.go (about)

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