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

78 lines
1.6 KiB
Go
Raw Normal View History

package common
import (
"context"
"fmt"
"log"
"strings"
"time"
2017-03-28 21:47:10 -04:00
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
type StepConfigureIp struct {
}
func (s *StepConfigureIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.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
}