packer-cn/ssh.go

49 lines
1.1 KiB
Go
Raw Normal View History

2017-08-25 01:08:08 -04:00
package main
import (
"fmt"
"io/ioutil"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/mitchellh/multistep"
"golang.org/x/crypto/ssh"
)
func commHost(state multistep.StateBag) (string, error) {
return state.Get("ip").(string), nil
}
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
config := state.Get("config").(*Config)
2017-08-31 08:05:26 -04:00
var auth []ssh.AuthMethod
2017-08-25 01:08:08 -04:00
2017-08-31 08:05:26 -04:00
if config.Comm.SSHPrivateKey != "" {
privateKey, err := ioutil.ReadFile(config.Comm.SSHPrivateKey)
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{
ssh.Password(config.Comm.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
}
}
clientConfig := &ssh.ClientConfig{
User: config.Comm.SSHUsername,
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
}