2018-01-24 06:04:39 -05:00
|
|
|
package common
|
2017-08-25 01:08:08 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-04-25 07:22:38 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-08-25 01:08:08 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
2018-10-31 17:42:24 -04:00
|
|
|
"io/ioutil"
|
2017-08-25 01:08:08 -04:00
|
|
|
)
|
|
|
|
|
2018-01-24 06:04:39 -05:00
|
|
|
func CommHost(state multistep.StateBag) (string, error) {
|
2017-08-25 01:08:08 -04:00
|
|
|
return state.Get("ip").(string), nil
|
|
|
|
}
|
|
|
|
|
2018-01-24 06:04:39 -05:00
|
|
|
func SshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
comm := state.Get("comm").(*communicator.Config)
|
2017-08-25 01:08:08 -04:00
|
|
|
|
2017-08-31 08:05:26 -04:00
|
|
|
var auth []ssh.AuthMethod
|
2017-08-25 01:08:08 -04:00
|
|
|
|
2018-12-17 18:11:05 -05:00
|
|
|
if comm.SSHPrivateKeyFile != "" {
|
|
|
|
privateKey, err := ioutil.ReadFile(comm.SSHPrivateKeyFile)
|
2017-08-25 01:08:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error loading configured private key file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := ssh.ParsePrivateKey(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-08-31 08:05:26 -04:00
|
|
|
auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
|
|
|
} else {
|
|
|
|
auth = []ssh.AuthMethod{
|
2018-01-24 06:04:39 -05:00
|
|
|
ssh.Password(comm.SSHPassword),
|
2017-08-31 08:05:26 -04:00
|
|
|
ssh.KeyboardInteractive(
|
2018-01-24 06:04:39 -05:00
|
|
|
packerssh.PasswordKeyboardInteractive(comm.SSHPassword)),
|
2017-08-31 08:05:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConfig := &ssh.ClientConfig{
|
2018-01-24 06:04:39 -05:00
|
|
|
User: comm.SSHUsername,
|
2017-08-31 08:05:26 -04:00
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2017-08-25 01:08:08 -04:00
|
|
|
}
|
2017-08-31 08:05:26 -04:00
|
|
|
clientConfig.Auth = auth
|
2017-08-25 01:08:08 -04:00
|
|
|
|
|
|
|
return clientConfig, nil
|
|
|
|
}
|