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 ( ...@@ -5,6 +5,7 @@ import (
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"time"
) )
const BuilderId = "mitchellh.vmware" const BuilderId = "mitchellh.vmware"
...@@ -24,6 +25,9 @@ type config struct { ...@@ -24,6 +25,9 @@ type config struct {
BootWait uint `mapstructure:"boot_wait"` BootWait uint `mapstructure:"boot_wait"`
SSHUser string `mapstructure:"ssh_user"` SSHUser string `mapstructure:"ssh_user"`
SSHPassword string `mapstructure:"ssh_password"` SSHPassword string `mapstructure:"ssh_password"`
SSHWaitTimeout time.Duration
RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
} }
func (b *Builder) Prepare(raw interface{}) (err error) { func (b *Builder) Prepare(raw interface{}) (err error) {
...@@ -44,6 +48,15 @@ func (b *Builder) Prepare(raw interface{}) (err error) { ...@@ -44,6 +48,15 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
b.config.OutputDir = "vmware" 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 return nil
} }
......
...@@ -24,18 +24,96 @@ import ( ...@@ -24,18 +24,96 @@ import (
// //
// Produces: // Produces:
// communicator packer.Communicator // communicator packer.Communicator
type stepWaitForSSH struct{} type stepWaitForSSH struct {
cancel bool
conn net.Conn
}
func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction { func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(*config) config := state["config"].(*config)
ui := state["ui"].(packer.Ui) 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) vmxPath := state["vmx_path"].(string)
ui.Say("Waiting for SSH to become available...") ui.Say("Waiting for SSH to become available...")
var comm packer.Communicator var comm packer.Communicator
var nc net.Conn
for { for {
if nc != nil {
nc.Close()
}
time.Sleep(5 * time.Second) 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... // First we wait for the IP to become available...
log.Println("Lookup up IP information...") log.Println("Lookup up IP information...")
ipLookup, err := s.dhcpLeaseLookup(vmxPath) ipLookup, err := s.dhcpLeaseLookup(vmxPath)
...@@ -52,7 +130,7 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction ...@@ -52,7 +130,7 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction
log.Printf("Detected IP: %s", ip) log.Printf("Detected IP: %s", ip)
// Attempt to connect to SSH port // 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 { if err != nil {
log.Printf("TCP connection to SSH ip/port failed: %s", err) log.Printf("TCP connection to SSH ip/port failed: %s", err)
continue continue
...@@ -68,43 +146,14 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction ...@@ -68,43 +146,14 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction
comm, err = ssh.New(nc, sshConfig) comm, err = ssh.New(nc, sshConfig)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error connecting via SSH: %s", err)) return nil, err
return multistep.ActionHalt
} }
ui.Say("Connected via SSH!") ui.Say("Connected via SSH!")
break break
} }
state["communicator"] = comm // Store the connection so we can close it later
s.conn = nc
return multistep.ActionContinue return comm, nil
}
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
} }
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