2013-06-05 23:53:34 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
2013-06-06 02:16:40 -04:00
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2013-06-06 00:51:16 -04:00
|
|
|
"errors"
|
2013-06-06 02:16:40 -04:00
|
|
|
"fmt"
|
2013-06-05 23:53:34 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-06-06 02:16:40 -04:00
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
2013-06-05 23:53:34 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-06 00:51:16 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2013-06-06 02:16:40 -04:00
|
|
|
"net"
|
2013-06-06 00:51:16 -04:00
|
|
|
"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:
|
2013-06-06 02:16:40 -04:00
|
|
|
// communicator packer.Communicator
|
2013-06-05 23:53:34 -04:00
|
|
|
type stepWaitForSSH struct{}
|
|
|
|
|
2013-06-06 00:51:16 -04:00
|
|
|
func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction {
|
2013-06-06 02:16:40 -04:00
|
|
|
config := state["config"].(*config)
|
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...")
|
2013-06-06 02:16:40 -04:00
|
|
|
var comm packer.Communicator
|
2013-06-05 23:53:34 -04:00
|
|
|
for {
|
2013-06-06 00:51:16 -04:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2013-06-05 23:53:34 -04:00
|
|
|
// First we wait for the IP to become available...
|
2013-06-06 02:16:40 -04:00
|
|
|
log.Println("Lookup up IP information...")
|
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-06 02:16:40 -04:00
|
|
|
|
|
|
|
// Attempt to connect to SSH port
|
|
|
|
nc, err := net.Dial("tcp", fmt.Sprintf("%s:22", ip))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("TCP connection to SSH ip/port failed: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we attempt to connect via SSH
|
|
|
|
sshConfig := &gossh.ClientConfig{
|
|
|
|
User: config.SSHUser,
|
|
|
|
Auth: []gossh.ClientAuth{
|
|
|
|
gossh.ClientAuthPassword(ssh.Password(config.SSHPassword)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
comm, err = ssh.New(nc, sshConfig)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error connecting via SSH: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Connected via SSH!")
|
|
|
|
break
|
2013-06-05 23:53:34 -04:00
|
|
|
}
|
|
|
|
|
2013-06-06 02:16:40 -04:00
|
|
|
state["communicator"] = comm
|
|
|
|
|
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
|
|
|
|
}
|