|
|
|
|
@ -4,6 +4,8 @@ import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
|
@ -12,6 +14,7 @@ import (
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
|
packerssh "github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
"golang.org/x/crypto/ssh/agent"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CommHost looks up the host for the communicator.
|
|
|
|
|
@ -63,8 +66,26 @@ func CommHost(
|
|
|
|
|
// SSHConfig returns a function that can be used for the SSH communicator
|
|
|
|
|
// config for connecting to the instance created over SSH using a private key
|
|
|
|
|
// or a password.
|
|
|
|
|
func SSHConfig(username, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
|
func SSHConfig(useAgent bool, username, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
|
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
|
if useAgent {
|
|
|
|
|
authSock := os.Getenv("SSH_AUTH_SOCK")
|
|
|
|
|
if authSock == "" {
|
|
|
|
|
return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sshAgent, err := net.Dial("unix", authSock)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &ssh.ClientConfig{
|
|
|
|
|
User: username,
|
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
|
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
privateKey, hasKey := state.GetOk("privateKey")
|
|
|
|
|
|
|
|
|
|
|