packer-cn/common/ssh.go

50 lines
1.2 KiB
Go

package common
import (
"fmt"
"io/ioutil"
packerssh "github.com/hashicorp/packer/communicator/ssh"
"github.com/mitchellh/multistep"
"golang.org/x/crypto/ssh"
"github.com/hashicorp/packer/helper/communicator"
)
func CommHost(state multistep.StateBag) (string, error) {
return state.Get("ip").(string), nil
}
func SshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
comm := state.Get("comm").(*communicator.Config)
var auth []ssh.AuthMethod
if comm.SSHPrivateKey != "" {
privateKey, err := ioutil.ReadFile(comm.SSHPrivateKey)
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)
}
auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
} else {
auth = []ssh.AuthMethod{
ssh.Password(comm.SSHPassword),
ssh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(comm.SSHPassword)),
}
}
clientConfig := &ssh.ClientConfig{
User: comm.SSHUsername,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
clientConfig.Auth = auth
return clientConfig, nil
}