2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
2015-06-22 17:26:06 -04:00
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
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{
|
2015-06-21 07:36:07 -04:00
|
|
|
User: config.Comm.SSHUsername,
|
|
|
|
Auth: auth,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|