github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/communicator/ssh/connect.go (about)

     1  package ssh
     2  
     3  import (
     4  	"errors"
     5  	"log"
     6  	"net"
     7  	"time"
     8  )
     9  
    10  // ConnectFunc is a convenience method for returning a function
    11  // that just uses net.Dial to communicate with the remote end that
    12  // is suitable for use with the SSH communicator configuration.
    13  func ConnectFunc(network, addr string, timeout time.Duration) func() (net.Conn, error) {
    14  	return func() (net.Conn, error) {
    15  		timeoutCh := time.After(timeout)
    16  
    17  		for {
    18  			select {
    19  			case <-timeoutCh:
    20  				return nil, errors.New("timeout connecting to remote machine")
    21  			default:
    22  			}
    23  
    24  			log.Printf("Opening conn for SSH to %s %s", network, addr)
    25  			nc, err := net.DialTimeout(network, addr, 15*time.Second)
    26  			if err == nil {
    27  				return nc, nil
    28  			}
    29  
    30  			time.Sleep(500 * time.Millisecond)
    31  		}
    32  	}
    33  }