2013-07-15 01:22:13 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-08-31 15:50:25 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-07-15 01:22:13 -04:00
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func sshAddress(state multistep.StateBag) (string, error) {
|
|
|
|
config := state.Get("config").(*config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
2013-07-15 01:22:13 -04:00
|
|
|
|
|
|
|
log.Println("Lookup up IP information...")
|
|
|
|
f, err := os.Open(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
vmxBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxData := ParseVMX(string(vmxBytes))
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
macAddress := ""
|
|
|
|
if macAddress, ok = vmxData["ethernet0.address"]; !ok || macAddress == "" {
|
|
|
|
if macAddress, ok = vmxData["ethernet0.generatedAddress"]; !ok || macAddress == "" {
|
|
|
|
return "", errors.New("couldn't find MAC address in VMX")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipLookup := &DHCPLeaseGuestLookup{
|
2013-07-01 15:17:47 -04:00
|
|
|
Driver: driver,
|
2013-07-15 01:22:13 -04:00
|
|
|
Device: "vmnet8",
|
|
|
|
MACAddress: macAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
ipAddress, err := ipLookup.GuestIP()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("IP lookup failed: %s", err)
|
|
|
|
return "", fmt.Errorf("IP lookup failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-07-16 14:05:47 -04:00
|
|
|
if ipAddress == "" {
|
|
|
|
log.Println("IP is blank, no IP yet.")
|
|
|
|
return "", errors.New("IP is blank")
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:22:13 -04:00
|
|
|
log.Printf("Detected IP: %s", ipAddress)
|
|
|
|
return fmt.Sprintf("%s:%d", ipAddress, config.SSHPort), nil
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
config := state.Get("config").(*config)
|
2013-07-15 01:22:13 -04:00
|
|
|
|
2013-08-28 01:57:42 -04:00
|
|
|
auth := []gossh.ClientAuth{
|
|
|
|
gossh.ClientAuthPassword(ssh.Password(config.SSHPassword)),
|
|
|
|
gossh.ClientAuthKeyboardInteractive(
|
|
|
|
ssh.PasswordKeyboardInteractive(config.SSHPassword)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SSHKeyPath != "" {
|
|
|
|
keyring, err := sshKeyToKeyring(config.SSHKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
auth = append(auth, gossh.ClientAuthKeyring(keyring))
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:22:13 -04:00
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: config.SSHUser,
|
2013-08-28 01:57:42 -04:00
|
|
|
Auth: auth,
|
2013-07-15 01:22:13 -04:00
|
|
|
}, nil
|
|
|
|
}
|
2013-08-28 01:57:42 -04:00
|
|
|
|
|
|
|
func sshKeyToKeyring(path string) (gossh.ClientKeyring, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
keyBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyring := new(ssh.SimpleKeychain)
|
|
|
|
if err := keyring.AddPEMKey(string(keyBytes)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyring, nil
|
|
|
|
}
|