github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/vmware/common/ssh.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  
     8  	commonssh "github.com/hashicorp/packer/common/ssh"
     9  	"github.com/hashicorp/packer/communicator/ssh"
    10  	"github.com/hashicorp/packer/helper/multistep"
    11  	gossh "golang.org/x/crypto/ssh"
    12  )
    13  
    14  func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) {
    15  	return func(state multistep.StateBag) (string, error) {
    16  		driver := state.Get("driver").(Driver)
    17  
    18  		if config.Comm.SSHHost != "" {
    19  			return config.Comm.SSHHost, nil
    20  		}
    21  
    22  		ipAddress, err := driver.GuestIP(state)
    23  		if err != nil {
    24  			log.Printf("IP lookup failed: %s", err)
    25  			return "", fmt.Errorf("IP lookup failed: %s", err)
    26  		}
    27  
    28  		if ipAddress == "" {
    29  			log.Println("IP is blank, no IP yet.")
    30  			return "", errors.New("IP is blank")
    31  		}
    32  
    33  		log.Printf("Detected IP: %s", ipAddress)
    34  		return ipAddress, nil
    35  	}
    36  }
    37  
    38  func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
    39  	return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
    40  		auth := []gossh.AuthMethod{
    41  			gossh.Password(config.Comm.SSHPassword),
    42  			gossh.KeyboardInteractive(
    43  				ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
    44  		}
    45  
    46  		if config.Comm.SSHPrivateKey != "" {
    47  			signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
    48  			if err != nil {
    49  				return nil, err
    50  			}
    51  
    52  			auth = append(auth, gossh.PublicKeys(signer))
    53  		}
    54  
    55  		return &gossh.ClientConfig{
    56  			User:            config.Comm.SSHUsername,
    57  			Auth:            auth,
    58  			HostKeyCallback: gossh.InsecureIgnoreHostKey(),
    59  		}, nil
    60  	}
    61  }