builder/vmware: Connect to SSH
This commit is contained in:
parent
a23897f52d
commit
c0e6fbd8ae
|
@ -1,11 +1,15 @@
|
||||||
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 (
|
||||||
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue