2013-12-24 01:27:01 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-12-24 13:00:51 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2013-12-24 01:27:01 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
commonssh "github.com/hashicorp/packer/common/ssh"
|
|
|
|
"github.com/hashicorp/packer/communicator/ssh"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2015-06-09 00:34:20 -04:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2013-12-24 01:27:01 -05:00
|
|
|
)
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
func CommHost(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)
|
|
|
|
|
2015-06-13 18:52:44 -04:00
|
|
|
if config.Comm.SSHHost != "" {
|
2015-06-13 19:23:33 -04:00
|
|
|
return config.Comm.SSHHost, nil
|
2014-03-28 18:22:37 -04:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
ipAddress, err := driver.GuestIP(state)
|
2013-12-24 13:00:51 -05:00
|
|
|
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)
|
2015-06-13 19:23:33 -04:00
|
|
|
return ipAddress, nil
|
2013-12-24 13:00:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
2015-06-13 18:52:44 -04:00
|
|
|
gossh.Password(config.Comm.SSHPassword),
|
2014-04-10 04:48:55 -04:00
|
|
|
gossh.KeyboardInteractive(
|
2015-06-13 18:52:44 -04:00
|
|
|
ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
|
2013-12-24 13:00:51 -05:00
|
|
|
}
|
|
|
|
|
2015-06-13 18:52:44 -04:00
|
|
|
if config.Comm.SSHPrivateKey != "" {
|
|
|
|
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
|
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{
|
2017-05-20 16:17:04 -04:00
|
|
|
User: config.Comm.SSHUsername,
|
|
|
|
Auth: auth,
|
|
|
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
2013-12-24 13:00:51 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|