github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/profitbricks/ssh.go (about)

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/communicator/ssh"
     7  	gossh "golang.org/x/crypto/ssh"
     8  )
     9  
    10  func commHost(state multistep.StateBag) (string, error) {
    11  	ipAddress := state.Get("server_ip").(string)
    12  	return ipAddress, nil
    13  }
    14  
    15  func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
    16  	config := state.Get("config").(*Config)
    17  	var privateKey string
    18  
    19  	var auth []gossh.AuthMethod
    20  
    21  	if config.Comm.SSHPassword != "" {
    22  		auth = []gossh.AuthMethod{
    23  			gossh.Password(config.Comm.SSHPassword),
    24  			gossh.KeyboardInteractive(
    25  				ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
    26  		}
    27  	}
    28  
    29  	if config.Comm.SSHPrivateKey != "" {
    30  		if priv, ok := state.GetOk("privateKey"); ok {
    31  			privateKey = priv.(string)
    32  		}
    33  		signer, err := gossh.ParsePrivateKey([]byte(privateKey))
    34  		if err != nil {
    35  			return nil, fmt.Errorf("Error setting up SSH config: %s", err)
    36  		}
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  
    41  		auth = append(auth, gossh.PublicKeys(signer))
    42  	}
    43  	return &gossh.ClientConfig{
    44  		User: config.Comm.SSHUsername,
    45  		Auth: auth,
    46  	}, nil
    47  }