2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-03 23:23:39 -04:00
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2014-09-03 23:23:39 -04:00
|
|
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
2014-04-06 13:21:22 -04:00
|
|
|
packerssh "github.com/mitchellh/packer/communicator/ssh"
|
2015-06-09 00:34:20 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func SSHAddress(state multistep.StateBag) (string, error) {
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
|
|
|
|
mac, err := driver.Mac(vmName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := driver.IpAddress(mac)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s:22", ip), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
auth := []ssh.AuthMethod{
|
2015-06-13 18:43:27 -04:00
|
|
|
ssh.Password(config.Comm.SSHPassword),
|
2014-04-06 13:21:22 -04:00
|
|
|
ssh.KeyboardInteractive(
|
2015-06-13 18:43:27 -04:00
|
|
|
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.SSHKeyPath != "" {
|
2015-06-13 18:43:27 -04:00
|
|
|
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
|
2014-04-06 13:21:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
auth = append(auth, ssh.PublicKeys(signer))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ssh.ClientConfig{
|
2015-06-13 18:43:27 -04:00
|
|
|
User: config.Comm.SSHUsername,
|
2014-04-06 13:21:22 -04:00
|
|
|
Auth: auth,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|