2013-12-24 01:27:01 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-12-24 13:00:51 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-12-24 01:27:01 -05:00
|
|
|
"io/ioutil"
|
2013-12-24 13:00:51 -05:00
|
|
|
"log"
|
2013-12-24 01:27:01 -05:00
|
|
|
"os"
|
|
|
|
|
2014-09-03 23:23:39 -04:00
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2013-12-24 13:00:51 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
2014-09-03 23:23:39 -04:00
|
|
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
2013-12-24 01:27:01 -05:00
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
)
|
|
|
|
|
2013-12-24 13:31:57 -05:00
|
|
|
func SSHAddressFunc(config *SSHConfig) func(multistep.StateBag) (string, error) {
|
2013-12-24 13:00:51 -05:00
|
|
|
return func(state multistep.StateBag) (string, error) {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
|
|
|
|
2014-03-28 18:22:37 -04:00
|
|
|
if config.SSHHost != "" {
|
|
|
|
return fmt.Sprintf("%s:%d", config.SSHHost, config.SSHPort), nil
|
|
|
|
}
|
|
|
|
|
2013-12-24 13:00:51 -05: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{
|
|
|
|
Driver: driver,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipAddress == "" {
|
|
|
|
log.Println("IP is blank, no IP yet.")
|
|
|
|
return "", errors.New("IP is blank")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Detected IP: %s", ipAddress)
|
|
|
|
return fmt.Sprintf("%s:%d", ipAddress, config.SSHPort), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 13:31:57 -05:00
|
|
|
func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
|
2013-12-24 13:00:51 -05:00
|
|
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
2014-04-10 04:48:55 -04:00
|
|
|
auth := []gossh.AuthMethod{
|
|
|
|
gossh.Password(config.SSHPassword),
|
|
|
|
gossh.KeyboardInteractive(
|
2013-12-24 13:00:51 -05:00
|
|
|
ssh.PasswordKeyboardInteractive(config.SSHPassword)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SSHKeyPath != "" {
|
2014-09-03 23:23:39 -04:00
|
|
|
signer, err := commonssh.FileSigner(config.SSHKeyPath)
|
2013-12-24 13:00:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
auth = append(auth, gossh.PublicKeys(signer))
|
2013-12-24 13:00:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: config.SSHUser,
|
|
|
|
Auth: auth,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|