github.com/rothwerx/packer@v0.9.0/builder/docker/comm.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/communicator/ssh"
     9  	"github.com/mitchellh/packer/helper/communicator"
    10  	gossh "golang.org/x/crypto/ssh"
    11  )
    12  
    13  func commHost(state multistep.StateBag) (string, error) {
    14  	containerId := state.Get("container_id").(string)
    15  	driver := state.Get("driver").(Driver)
    16  	return driver.IPAddress(containerId)
    17  }
    18  
    19  func sshConfig(comm *communicator.Config) func(state multistep.StateBag) (*gossh.ClientConfig, error) {
    20  	return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
    21  		if comm.SSHPrivateKey != "" {
    22  			// key based auth
    23  			bytes, err := ioutil.ReadFile(comm.SSHPrivateKey)
    24  			if err != nil {
    25  				return nil, fmt.Errorf("Error setting up SSH config: %s", err)
    26  			}
    27  			privateKey := string(bytes)
    28  
    29  			signer, err := gossh.ParsePrivateKey([]byte(privateKey))
    30  			if err != nil {
    31  				return nil, fmt.Errorf("Error setting up SSH config: %s", err)
    32  			}
    33  
    34  			return &gossh.ClientConfig{
    35  				User: comm.SSHUsername,
    36  				Auth: []gossh.AuthMethod{
    37  					gossh.PublicKeys(signer),
    38  				},
    39  			}, nil
    40  		} else {
    41  			// password based auth
    42  			return &gossh.ClientConfig{
    43  				User: comm.SSHUsername,
    44  				Auth: []gossh.AuthMethod{
    45  					gossh.Password(comm.SSHPassword),
    46  					gossh.KeyboardInteractive(
    47  						ssh.PasswordKeyboardInteractive(comm.SSHPassword)),
    48  				},
    49  			}, nil
    50  		}
    51  	}
    52  }