2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2017-04-04 16:39:01 -04:00
|
|
|
commonssh "github.com/hashicorp/packer/common/ssh"
|
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
2014-04-06 13:21:22 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2015-06-09 00:34:20 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// CommHost returns the VM's IP address which should be used to access it by SSH.
|
2015-06-13 19:23:33 -04:00
|
|
|
func CommHost(state multistep.StateBag) (string, error) {
|
2014-04-06 13:21:22 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
mac, err := driver.MAC(vmName)
|
2014-04-06 13:21:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:49:54 -05:00
|
|
|
ip, err := driver.IPAddress(mac)
|
2014-04-06 13:21:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
return ip, nil
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// SSHConfigFunc returns SSH credentials to access the VM by SSH.
|
2014-04-06 13:21:22 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-13 11:35:30 -04:00
|
|
|
if config.Comm.SSHPrivateKey != "" {
|
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
|
|
|
|
}
|
|
|
|
}
|