2015-01-05 10:35:00 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepWaitForIP struct {
|
|
|
|
Chan <-chan string
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StepWaitForIP) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-13 03:21:07 -05:00
|
|
|
c := state.Get("client").(*Connection)
|
2015-05-18 10:13:02 -05:00
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2015-01-05 10:35:00 -06:00
|
|
|
|
|
|
|
ui.Say("Step: Wait for VM's IP to become known to us.")
|
|
|
|
|
|
|
|
uuid := state.Get("instance_uuid").(string)
|
2020-09-13 03:21:07 -05:00
|
|
|
instance, err := c.client.VM.GetByUUID(c.session, uuid)
|
2015-01-05 10:35:00 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
var ip string
|
|
|
|
err = InterruptibleWait{
|
|
|
|
Timeout: self.Timeout,
|
|
|
|
PredicateInterval: 5 * time.Second,
|
|
|
|
Predicate: func() (result bool, err error) {
|
2015-04-27 11:44:38 -05:00
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
if config.IPGetter == "auto" || config.IPGetter == "http" {
|
|
|
|
|
|
|
|
// Snoop IP from HTTP fetch
|
|
|
|
select {
|
|
|
|
case ip = <-self.Chan:
|
|
|
|
ui.Message(fmt.Sprintf("Got IP '%s' from HTTP request", ip))
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.IPGetter == "auto" || config.IPGetter == "tools" {
|
|
|
|
|
|
|
|
// Look for PV IP
|
2020-09-13 03:21:07 -05:00
|
|
|
m, err := c.client.VM.GetGuestMetrics(c.session, instance)
|
2015-05-18 10:13:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-09-13 03:21:07 -05:00
|
|
|
if m != "" {
|
|
|
|
metrics, err := c.client.VMGuestMetrics.GetRecord(c.session, m)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
networks := metrics.Networks
|
|
|
|
if ip, ok := networks["0/ip"]; ok {
|
|
|
|
if ip != "" {
|
2015-05-18 10:13:02 -05:00
|
|
|
ui.Message(fmt.Sprintf("Got IP '%s' from XenServer tools", ip))
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-01-05 10:35:00 -06:00
|
|
|
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
}.Wait(state)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not get IP address of VM: %s", err.Error()))
|
|
|
|
// @todo: give advice on what went wrong (no HTTP server? no PV drivers?)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Got IP address '%s'", ip))
|
|
|
|
state.Put("instance_ssh_address", ip)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StepWaitForIP) Cleanup(state multistep.StateBag) {}
|
|
|
|
|
|
|
|
func InstanceSSHIP(state multistep.StateBag) (string, error) {
|
|
|
|
ip := state.Get("instance_ssh_address").(string)
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func InstanceSSHPort(state multistep.StateBag) (uint, error) {
|
|
|
|
return 22, nil
|
|
|
|
}
|