2013-06-05 23:53:34 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
2013-06-06 00:51:16 -04:00
|
|
|
"errors"
|
2013-06-05 23:53:34 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-06 00:51:16 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2013-06-05 23:53:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step waits for SSH to become available and establishes an SSH
|
|
|
|
// connection.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
|
|
|
// ui packer.Ui
|
2013-06-06 00:51:16 -04:00
|
|
|
// vmx_path string
|
2013-06-05 23:53:34 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
|
|
|
type stepWaitForSSH struct{}
|
|
|
|
|
2013-06-06 00:51:16 -04:00
|
|
|
func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction {
|
2013-06-05 23:53:34 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
2013-06-06 00:51:16 -04:00
|
|
|
vmxPath := state["vmx_path"].(string)
|
2013-06-05 23:53:34 -04:00
|
|
|
|
|
|
|
ui.Say("Waiting for SSH to become available...")
|
|
|
|
for {
|
2013-06-06 00:51:16 -04:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
log.Println("Lookup up IP information...")
|
2013-06-05 23:53:34 -04:00
|
|
|
// First we wait for the IP to become available...
|
2013-06-06 00:51:16 -04:00
|
|
|
ipLookup, err := s.dhcpLeaseLookup(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Can't lookup via DHCP lease: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := ipLookup.GuestIP()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("IP lookup failed: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Detected IP: %s", ip)
|
2013-06-05 23:53:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-06-06 00:51:16 -04:00
|
|
|
func (s *stepWaitForSSH) Cleanup(map[string]interface{}) {}
|
|
|
|
|
|
|
|
// Reads the network information for lookup via DHCP.
|
|
|
|
func (s *stepWaitForSSH) dhcpLeaseLookup(vmxPath string) (GuestIPFinder, error) {
|
|
|
|
f, err := os.Open(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
vmxBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxData := ParseVMX(string(vmxBytes))
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
macAddress := ""
|
|
|
|
if macAddress, ok = vmxData["ethernet0.address"]; !ok || macAddress == "" {
|
|
|
|
if macAddress, ok = vmxData["ethernet0.generatedAddress"]; !ok || macAddress == "" {
|
|
|
|
return nil, errors.New("couldn't find MAC address in VMX")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DHCPLeaseGuestLookup{"vmnet8", macAddress}, nil
|
|
|
|
}
|