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-06 12:10:14 -04:00
|
|
|
type stepWaitForSSH struct {
|
|
|
|
cancel bool
|
|
|
|
conn net.Conn
|
|
|
|
}
|
2013-06-05 23:53:34 -04:00
|
|
|
|
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 12:10:14 -04:00
|
|
|
|
|
|
|
var comm packer.Communicator
|
|
|
|
var err error
|
|
|
|
|
|
|
|
waitDone := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
comm, err = s.waitForSSH(state)
|
|
|
|
waitDone <- true
|
|
|
|
}()
|
|
|
|
|
2013-06-06 12:12:58 -04:00
|
|
|
log.Printf("Waiting for SSH, up to timeout: %s", config.SSHWaitTimeout.String())
|
|
|
|
|
2013-06-08 21:55:34 -04:00
|
|
|
timeout := time.After(config.SSHWaitTimeout)
|
2013-06-08 22:26:07 -04:00
|
|
|
WaitLoop:
|
2013-06-08 21:55:34 -04:00
|
|
|
for {
|
|
|
|
// Wait for either SSH to become available, a timeout to occur,
|
|
|
|
// or an interrupt to come through.
|
|
|
|
select {
|
|
|
|
case <-waitDone:
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error waiting for SSH: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state["communicator"] = comm
|
2013-06-08 22:26:07 -04:00
|
|
|
break WaitLoop
|
2013-06-08 21:55:34 -04:00
|
|
|
case <-timeout:
|
|
|
|
ui.Error("Timeout waiting for SSH.")
|
|
|
|
s.cancel = true
|
2013-06-06 12:10:14 -04:00
|
|
|
return multistep.ActionHalt
|
2013-06-08 21:55:34 -04:00
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
if _, ok := state[multistep.StateCancelled]; ok {
|
|
|
|
log.Println("Interrupt detected, quitting waiting for SSH.")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-06 12:10:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepWaitForSSH) Cleanup(map[string]interface{}) {
|
|
|
|
if s.conn != nil {
|
|
|
|
s.conn.Close()
|
|
|
|
s.conn = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// This blocks until SSH becomes available, and sends the communicator
|
|
|
|
// on the given channel.
|
|
|
|
func (s *stepWaitForSSH) waitForSSH(state map[string]interface{}) (packer.Communicator, error) {
|
|
|
|
config := state["config"].(*config)
|
|
|
|
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-06 12:10:14 -04:00
|
|
|
var nc net.Conn
|
2013-06-05 23:53:34 -04:00
|
|
|
for {
|
2013-06-06 12:10:14 -04:00
|
|
|
if nc != nil {
|
|
|
|
nc.Close()
|
|
|
|
}
|
|
|
|
|
2013-06-06 00:51:16 -04:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2013-06-06 12:10:14 -04:00
|
|
|
if s.cancel {
|
|
|
|
log.Println("SSH wait cancelled. Exiting loop.")
|
|
|
|
return nil, errors.New("SSH wait cancelled")
|
|
|
|
}
|
|
|
|
|
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
|
2013-06-06 12:10:14 -04:00
|
|
|
nc, err = net.Dial("tcp", fmt.Sprintf("%s:22", ip))
|
2013-06-06 02:16:40 -04:00
|
|
|
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 {
|
2013-06-06 12:10:14 -04:00
|
|
|
return nil, err
|
2013-06-06 02:16:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Connected via SSH!")
|
|
|
|
break
|
2013-06-05 23:53:34 -04:00
|
|
|
}
|
|
|
|
|
2013-06-06 12:10:14 -04:00
|
|
|
// Store the connection so we can close it later
|
|
|
|
s.conn = nc
|
|
|
|
return comm, nil
|
2013-06-06 00:51:16 -04:00
|
|
|
}
|