Commit a3800625 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: Have an overall ssh wait timeout

parent ca39d236
......@@ -5,6 +5,7 @@ import (
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"time"
)
const BuilderId = "mitchellh.vmware"
......@@ -15,15 +16,18 @@ type Builder struct {
}
type config struct {
DiskName string `mapstructure:"vmdk_name"`
ISOUrl string `mapstructure:"iso_url"`
VMName string `mapstructure:"vm_name"`
OutputDir string `mapstructure:"output_directory"`
HTTPDir string `mapstructure:"http_directory"`
BootCommand []string `mapstructure:"boot_command"`
BootWait uint `mapstructure:"boot_wait"`
SSHUser string `mapstructure:"ssh_user"`
SSHPassword string `mapstructure:"ssh_password"`
DiskName string `mapstructure:"vmdk_name"`
ISOUrl string `mapstructure:"iso_url"`
VMName string `mapstructure:"vm_name"`
OutputDir string `mapstructure:"output_directory"`
HTTPDir string `mapstructure:"http_directory"`
BootCommand []string `mapstructure:"boot_command"`
BootWait uint `mapstructure:"boot_wait"`
SSHUser string `mapstructure:"ssh_user"`
SSHPassword string `mapstructure:"ssh_password"`
SSHWaitTimeout time.Duration
RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
}
func (b *Builder) Prepare(raw interface{}) (err error) {
......@@ -44,6 +48,15 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
b.config.OutputDir = "vmware"
}
if b.config.RawSSHWaitTimeout == "" {
b.config.RawSSHWaitTimeout = "20m"
}
b.config.SSHWaitTimeout, err = time.ParseDuration(b.config.RawSSHWaitTimeout)
if err != nil {
return
}
return nil
}
......
......@@ -24,18 +24,96 @@ import (
//
// Produces:
// communicator packer.Communicator
type stepWaitForSSH struct{}
type stepWaitForSSH struct {
cancel bool
conn net.Conn
}
func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(*config)
ui := state["ui"].(packer.Ui)
var comm packer.Communicator
var err error
waitDone := make(chan bool, 1)
go func() {
comm, err = s.waitForSSH(state)
waitDone <- true
}()
select {
case <-waitDone:
if err != nil {
ui.Error(fmt.Sprintf("Error waiting for SSH: %s", err))
return multistep.ActionHalt
}
state["communicator"] = comm
case <-time.After(config.SSHWaitTimeout):
ui.Error("Timeout waiting for SSH.")
s.cancel = true
return multistep.ActionHalt
}
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)
vmxPath := state["vmx_path"].(string)
ui.Say("Waiting for SSH to become available...")
var comm packer.Communicator
var nc net.Conn
for {
if nc != nil {
nc.Close()
}
time.Sleep(5 * time.Second)
if s.cancel {
log.Println("SSH wait cancelled. Exiting loop.")
return nil, errors.New("SSH wait cancelled")
}
// First we wait for the IP to become available...
log.Println("Lookup up IP information...")
ipLookup, err := s.dhcpLeaseLookup(vmxPath)
......@@ -52,7 +130,7 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction
log.Printf("Detected IP: %s", ip)
// Attempt to connect to SSH port
nc, err := net.Dial("tcp", fmt.Sprintf("%s:22", ip))
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
......@@ -68,43 +146,14 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction
comm, err = ssh.New(nc, sshConfig)
if err != nil {
ui.Error(fmt.Sprintf("Error connecting via SSH: %s", err))
return multistep.ActionHalt
return nil, err
}
ui.Say("Connected via SSH!")
break
}
state["communicator"] = comm
return multistep.ActionContinue
}
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
// Store the connection so we can close it later
s.conn = nc
return comm, nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment