2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2017-04-04 16:39:01 -04:00
|
|
|
commonssh "github.com/hashicorp/packer/common/ssh"
|
|
|
|
"github.com/hashicorp/packer/communicator/ssh"
|
2015-06-21 07:36:07 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2015-06-22 17:26:06 -04:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func CommHost(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 ip, nil
|
|
|
|
}
|
|
|
|
|
2015-06-22 17:26:06 -04:00
|
|
|
func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
auth := []gossh.AuthMethod{
|
|
|
|
gossh.Password(config.Comm.SSHPassword),
|
|
|
|
gossh.KeyboardInteractive(
|
|
|
|
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2015-06-22 17:26:06 -04:00
|
|
|
if config.Comm.SSHPrivateKey != "" {
|
2015-06-21 07:36:07 -04:00
|
|
|
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-22 17:26:06 -04:00
|
|
|
auth = append(auth, gossh.PublicKeys(signer))
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2015-06-22 17:26:06 -04:00
|
|
|
return &gossh.ClientConfig{
|
2017-05-20 16:17:04 -04:00
|
|
|
User: config.Comm.SSHUsername,
|
|
|
|
Auth: auth,
|
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2015-06-21 07:36:07 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|