github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/builder/vmware/common/ssh.go (about)

     1  package common
     2  
     3  import (
     4  	gossh "code.google.com/p/go.crypto/ssh"
     5  	"errors"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"log"
     9  	"os"
    10  
    11  	"github.com/mitchellh/multistep"
    12  	"github.com/mitchellh/packer/communicator/ssh"
    13  )
    14  
    15  func SSHAddressFunc(config *SSHConfig) func(multistep.StateBag) (string, error) {
    16  	return func(state multistep.StateBag) (string, error) {
    17  		driver := state.Get("driver").(Driver)
    18  		vmxPath := state.Get("vmx_path").(string)
    19  
    20  		if config.SSHHost != "" {
    21  			return fmt.Sprintf("%s:%d", config.SSHHost, config.SSHPort), nil
    22  		}
    23  
    24  		log.Println("Lookup up IP information...")
    25  		f, err := os.Open(vmxPath)
    26  		if err != nil {
    27  			return "", err
    28  		}
    29  		defer f.Close()
    30  
    31  		vmxBytes, err := ioutil.ReadAll(f)
    32  		if err != nil {
    33  			return "", err
    34  		}
    35  
    36  		vmxData := ParseVMX(string(vmxBytes))
    37  
    38  		var ok bool
    39  		macAddress := ""
    40  		if macAddress, ok = vmxData["ethernet0.address"]; !ok || macAddress == "" {
    41  			if macAddress, ok = vmxData["ethernet0.generatedaddress"]; !ok || macAddress == "" {
    42  				return "", errors.New("couldn't find MAC address in VMX")
    43  			}
    44  		}
    45  
    46  		ipLookup := &DHCPLeaseGuestLookup{
    47  			Driver:     driver,
    48  			Device:     "vmnet8",
    49  			MACAddress: macAddress,
    50  		}
    51  
    52  		ipAddress, err := ipLookup.GuestIP()
    53  		if err != nil {
    54  			log.Printf("IP lookup failed: %s", err)
    55  			return "", fmt.Errorf("IP lookup failed: %s", err)
    56  		}
    57  
    58  		if ipAddress == "" {
    59  			log.Println("IP is blank, no IP yet.")
    60  			return "", errors.New("IP is blank")
    61  		}
    62  
    63  		log.Printf("Detected IP: %s", ipAddress)
    64  		return fmt.Sprintf("%s:%d", ipAddress, config.SSHPort), nil
    65  	}
    66  }
    67  
    68  func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
    69  	return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
    70  		auth := []gossh.AuthMethod{
    71  			gossh.Password(config.SSHPassword),
    72  			gossh.KeyboardInteractive(
    73  				ssh.PasswordKeyboardInteractive(config.SSHPassword)),
    74  		}
    75  
    76  		if config.SSHKeyPath != "" {
    77  			signer, err := sshKeyToSigner(config.SSHKeyPath)
    78  			if err != nil {
    79  				return nil, err
    80  			}
    81  
    82  			auth = append(auth, gossh.PublicKeys(signer))
    83  		}
    84  
    85  		return &gossh.ClientConfig{
    86  			User: config.SSHUser,
    87  			Auth: auth,
    88  		}, nil
    89  	}
    90  }
    91  
    92  func sshKeyToSigner(path string) (gossh.Signer, error) {
    93  	f, err := os.Open(path)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	defer f.Close()
    98  
    99  	keyBytes, err := ioutil.ReadAll(f)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	signer, err := gossh.ParsePrivateKey(keyBytes)
   105  	if err != nil {
   106  		return nil, fmt.Errorf("Error setting up SSH config: %s", err)
   107  	}
   108  
   109  	return signer, nil
   110  }