Commit c0e6fbd8 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: Connect to SSH

parent a23897f5
package vmware package vmware
import ( import (
gossh "code.google.com/p/go.crypto/ssh"
"errors" "errors"
"fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/communicator/ssh"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"os" "os"
"time" "time"
) )
...@@ -19,19 +23,21 @@ import ( ...@@ -19,19 +23,21 @@ import (
// vmx_path string // vmx_path string
// //
// Produces: // Produces:
// <nothing> // communicator packer.Communicator
type stepWaitForSSH struct{} type stepWaitForSSH struct{}
func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction { func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(*config)
ui := state["ui"].(packer.Ui) 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
for { for {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
log.Println("Lookup up IP information...")
// First we wait for the IP to become available... // First we wait for the IP to become available...
log.Println("Lookup up IP information...")
ipLookup, err := s.dhcpLeaseLookup(vmxPath) ipLookup, err := s.dhcpLeaseLookup(vmxPath)
if err != nil { if err != nil {
log.Printf("Can't lookup via DHCP lease: %s", err) log.Printf("Can't lookup via DHCP lease: %s", err)
...@@ -44,8 +50,34 @@ func (s *stepWaitForSSH) Run(state map[string]interface{}) multistep.StepAction ...@@ -44,8 +50,34 @@ 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
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
}
state["communicator"] = comm
return multistep.ActionContinue return multistep.ActionContinue
} }
......
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