packer-cn/builder/hyperv/common/step_configure_ip.go

77 lines
1.5 KiB
Go
Raw Normal View History

package common
import (
"fmt"
"log"
"strings"
"time"
2017-03-28 21:47:10 -04:00
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
2017-03-28 21:47:10 -04:00
"github.com/mitchellh/multistep"
)
type StepConfigureIp struct {
}
func (s *StepConfigureIp) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
errorMsg := "Error configuring ip address: %s"
vmName := state.Get("vmName").(string)
ui.Say("Configuring ip address...")
count := 60
var duration time.Duration = 1
sleepTime := time.Minute * duration
var ip string
for count != 0 {
cmdOut, err := driver.GetVirtualMachineNetworkAdapterAddress(vmName)
if err != nil {
err := fmt.Errorf(errorMsg, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
2017-03-28 21:47:10 -04:00
ip = strings.TrimSpace(cmdOut)
if ip != "False" {
break
}
log.Println(fmt.Sprintf("Waiting for another %v minutes...", uint(duration)))
time.Sleep(sleepTime)
count--
}
if count == 0 {
err := fmt.Errorf(errorMsg, "IP address assigned to the adapter is empty")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say("ip address is " + ip)
hostName, err := driver.GetHostName(ip)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say("hostname is " + hostName)
state.Put("ip", ip)
state.Put("hostname", hostName)
return multistep.ActionContinue
}
func (s *StepConfigureIp) Cleanup(state multistep.StateBag) {
// do nothing
}