2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 07:36:07 -04:00
|
|
|
"fmt"
|
2015-10-30 04:23:30 -04:00
|
|
|
"log"
|
2015-06-21 07:36:07 -04:00
|
|
|
"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"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepConfigureIp struct {
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepConfigureIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
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 {
|
2015-10-30 04:23:30 -04:00
|
|
|
cmdOut, err := driver.GetVirtualMachineNetworkAdapterAddress(vmName)
|
2015-06-21 07:36:07 -04:00
|
|
|
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)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
if ip != "False" {
|
2015-10-30 04:23:30 -04:00
|
|
|
break
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(fmt.Sprintf("Waiting for another %v minutes...", uint(duration)))
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
count--
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
if count == 0 {
|
2015-06-21 07:36:07 -04:00
|
|
|
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)
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
hostName, err := driver.GetHostName(ip)
|
2015-06-21 07:36:07 -04:00
|
|
|
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
|
|
|
|
}
|