github.com/alouche/packer@v0.3.7/builder/vmware/ssh.go (about)

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