github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/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/packer/communicator/ssh"
     8  	"io/ioutil"
     9  	"log"
    10  	"os"
    11  )
    12  
    13  func sshAddress(state map[string]interface{}) (string, error) {
    14  	config := state["config"].(*config)
    15  	driver := state["driver"].(Driver)
    16  	vmxPath := state["vmx_path"].(string)
    17  
    18  	log.Println("Lookup up IP information...")
    19  	f, err := os.Open(vmxPath)
    20  	if err != nil {
    21  		return "", err
    22  	}
    23  	defer f.Close()
    24  
    25  	vmxBytes, err := ioutil.ReadAll(f)
    26  	if err != nil {
    27  		return "", err
    28  	}
    29  
    30  	vmxData := ParseVMX(string(vmxBytes))
    31  
    32  	var ok bool
    33  	macAddress := ""
    34  	if macAddress, ok = vmxData["ethernet0.address"]; !ok || macAddress == "" {
    35  		if macAddress, ok = vmxData["ethernet0.generatedAddress"]; !ok || macAddress == "" {
    36  			return "", errors.New("couldn't find MAC address in VMX")
    37  		}
    38  	}
    39  
    40  	ipLookup := &DHCPLeaseGuestLookup{
    41  		Driver:     driver,
    42  		Device:     "vmnet8",
    43  		MACAddress: macAddress,
    44  	}
    45  
    46  	ipAddress, err := ipLookup.GuestIP()
    47  	if err != nil {
    48  		log.Printf("IP lookup failed: %s", err)
    49  		return "", fmt.Errorf("IP lookup failed: %s", err)
    50  	}
    51  
    52  	if ipAddress == "" {
    53  		log.Println("IP is blank, no IP yet.")
    54  		return "", errors.New("IP is blank")
    55  	}
    56  
    57  	log.Printf("Detected IP: %s", ipAddress)
    58  	return fmt.Sprintf("%s:%d", ipAddress, config.SSHPort), nil
    59  }
    60  
    61  func sshConfig(state map[string]interface{}) (*gossh.ClientConfig, error) {
    62  	config := state["config"].(*config)
    63  
    64  	return &gossh.ClientConfig{
    65  		User: config.SSHUser,
    66  		Auth: []gossh.ClientAuth{
    67  			gossh.ClientAuthPassword(ssh.Password(config.SSHPassword)),
    68  			gossh.ClientAuthKeyboardInteractive(
    69  				ssh.PasswordKeyboardInteractive(config.SSHPassword)),
    70  		},
    71  	}, nil
    72  }