2013-07-14 22:41:48 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2014-04-26 14:23:26 -04:00
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2013-07-14 22:41:48 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
2013-07-16 14:15:47 -04:00
|
|
|
"strings"
|
2013-07-14 22:41:48 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepConnectSSH is a multistep Step implementation that waits for SSH
|
|
|
|
// to become available. It gets the connection information from a single
|
|
|
|
// configuration when creating the step.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// communicator packer.Communicator
|
|
|
|
type StepConnectSSH struct {
|
|
|
|
// SSHAddress is a function that returns the TCP address to connect to
|
|
|
|
// for SSH. This is a function so that you can query information
|
|
|
|
// if necessary for this address.
|
2013-08-31 15:17:59 -04:00
|
|
|
SSHAddress func(multistep.StateBag) (string, error)
|
2013-07-14 22:41:48 -04:00
|
|
|
|
|
|
|
// SSHConfig is a function that returns the proper client configuration
|
|
|
|
// for SSH access.
|
2013-08-31 15:17:59 -04:00
|
|
|
SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error)
|
2013-07-14 22:41:48 -04:00
|
|
|
|
|
|
|
// SSHWaitTimeout is the total timeout to wait for SSH to become available.
|
|
|
|
SSHWaitTimeout time.Duration
|
|
|
|
|
2013-08-27 19:51:05 -04:00
|
|
|
// NoPty, if true, will not request a Pty from the remote end.
|
|
|
|
NoPty bool
|
|
|
|
|
2013-07-31 12:23:04 -04:00
|
|
|
comm packer.Communicator
|
2013-07-14 22:41:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
func (s *StepConnectSSH) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-14 22:41:48 -04:00
|
|
|
|
|
|
|
var comm packer.Communicator
|
|
|
|
var err error
|
|
|
|
|
2013-07-31 12:23:04 -04:00
|
|
|
cancel := make(chan struct{})
|
2013-07-14 22:41:48 -04:00
|
|
|
waitDone := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
ui.Say("Waiting for SSH to become available...")
|
2013-07-31 12:23:04 -04:00
|
|
|
comm, err = s.waitForSSH(state, cancel)
|
2013-07-14 22:41:48 -04:00
|
|
|
waitDone <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("Waiting for SSH, up to timeout: %s", s.SSHWaitTimeout)
|
|
|
|
timeout := time.After(s.SSHWaitTimeout)
|
|
|
|
WaitLoop:
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Connected to SSH!")
|
|
|
|
s.comm = comm
|
2013-08-31 15:17:59 -04:00
|
|
|
state.Put("communicator", comm)
|
2013-07-14 22:41:48 -04:00
|
|
|
break WaitLoop
|
|
|
|
case <-timeout:
|
2013-10-02 17:55:28 -04:00
|
|
|
err := fmt.Errorf("Timeout waiting for SSH.")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
2013-07-31 12:23:04 -04:00
|
|
|
close(cancel)
|
2013-07-14 22:41:48 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
case <-time.After(1 * time.Second):
|
2013-08-31 15:17:59 -04:00
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
2013-07-15 01:10:30 -04:00
|
|
|
// The step sequence was cancelled, so cancel waiting for SSH
|
|
|
|
// and just start the halting process.
|
2013-07-31 12:23:04 -04:00
|
|
|
close(cancel)
|
2013-07-14 22:41:48 -04:00
|
|
|
log.Println("Interrupt detected, quitting waiting for SSH.")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
func (s *StepConnectSSH) Cleanup(multistep.StateBag) {
|
2013-07-14 22:41:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, cancel <-chan struct{}) (packer.Communicator, error) {
|
2013-07-14 22:41:48 -04:00
|
|
|
handshakeAttempts := 0
|
|
|
|
|
|
|
|
var comm packer.Communicator
|
2014-09-10 17:03:57 -04:00
|
|
|
first := true
|
2013-07-14 22:41:48 -04:00
|
|
|
for {
|
2014-09-10 17:03:57 -04:00
|
|
|
// Don't check for cancel or wait on first iteration
|
|
|
|
if !first {
|
|
|
|
select {
|
|
|
|
case <-cancel:
|
|
|
|
log.Println("SSH wait cancelled. Exiting loop.")
|
|
|
|
return nil, errors.New("SSH wait cancelled")
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
}
|
2013-07-14 22:41:48 -04:00
|
|
|
}
|
2014-09-10 17:03:57 -04:00
|
|
|
first = false
|
2013-07-14 22:41:48 -04:00
|
|
|
|
|
|
|
// First we request the TCP connection information
|
2013-07-15 01:06:41 -04:00
|
|
|
address, err := s.SSHAddress(state)
|
2013-07-14 22:41:48 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting SSH address: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the SSH configuration
|
2013-07-15 01:06:41 -04:00
|
|
|
sshConfig, err := s.SSHConfig(state)
|
2013-07-14 22:41:48 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting SSH config: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to connect to SSH port
|
2013-08-22 14:53:31 -04:00
|
|
|
connFunc := ssh.ConnectFunc("tcp", address)
|
2013-07-14 22:41:48 -04:00
|
|
|
nc, err := connFunc()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("TCP connection to SSH ip/port failed: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nc.Close()
|
|
|
|
|
|
|
|
// Then we attempt to connect via SSH
|
|
|
|
config := &ssh.Config{
|
|
|
|
Connection: connFunc,
|
|
|
|
SSHConfig: sshConfig,
|
2013-08-27 19:51:05 -04:00
|
|
|
NoPty: s.NoPty,
|
2013-07-14 22:41:48 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 12:23:04 -04:00
|
|
|
log.Println("Attempting SSH connection...")
|
2014-04-10 04:48:55 -04:00
|
|
|
comm, err = ssh.New(address, config)
|
2013-07-14 22:41:48 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("SSH handshake err: %s", err)
|
|
|
|
|
2013-07-16 14:15:47 -04:00
|
|
|
// Only count this as an attempt if we were able to attempt
|
|
|
|
// to authenticate. Note this is very brittle since it depends
|
|
|
|
// on the string of the error... but I don't see any other way.
|
|
|
|
if strings.Contains(err.Error(), "authenticate") {
|
|
|
|
log.Printf("Detected authentication error. Increasing handshake attempts.")
|
|
|
|
handshakeAttempts += 1
|
|
|
|
}
|
|
|
|
|
2013-07-14 22:41:48 -04:00
|
|
|
if handshakeAttempts < 10 {
|
|
|
|
// Try to connect via SSH a handful of times
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return comm, nil
|
|
|
|
}
|