2017-04-06 05:19:17 -04:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-06 08:56:46 -05:00
|
|
|
"net"
|
|
|
|
"os"
|
2017-04-06 05:19:17 -04:00
|
|
|
|
2018-01-05 03:43:52 -05:00
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
2018-02-05 19:50:32 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-05 03:43:52 -05:00
|
|
|
"golang.org/x/crypto/ssh"
|
2018-01-06 08:56:46 -05:00
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2017-04-06 05:19:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func commHost(state multistep.StateBag) (string, error) {
|
|
|
|
ipAddress := state.Get("server_ip").(string)
|
|
|
|
return ipAddress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
config := state.Get("config").(Config)
|
2018-01-05 03:43:52 -05:00
|
|
|
|
|
|
|
var auth []ssh.AuthMethod
|
|
|
|
|
2018-01-06 08:56:46 -05:00
|
|
|
if config.Comm.SSHAgentAuth {
|
|
|
|
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)
|
|
|
|
}
|
2018-01-05 03:43:52 -05:00
|
|
|
auth = []ssh.AuthMethod{
|
2018-01-06 08:56:46 -05:00
|
|
|
ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Comm.SSHPassword != "" {
|
|
|
|
auth = append(auth,
|
2018-01-05 03:43:52 -05:00
|
|
|
ssh.Password(config.Comm.SSHPassword),
|
|
|
|
ssh.KeyboardInteractive(
|
|
|
|
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
|
2018-01-06 08:56:46 -05:00
|
|
|
)
|
2018-01-05 03:43:52 -05:00
|
|
|
}
|
2017-04-06 05:19:17 -04:00
|
|
|
|
2018-05-03 06:33:28 -04:00
|
|
|
// Use key based auth if there is a private key in the state bag
|
|
|
|
if privateKey, ok := state.GetOk("private_key"); ok {
|
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
|
2018-01-05 03:43:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
auth = append(auth, ssh.PublicKeys(signer))
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ssh.ClientConfig{
|
2018-01-05 03:43:52 -05:00
|
|
|
User: config.Comm.SSHUsername,
|
|
|
|
Auth: auth,
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2017-04-06 05:19:17 -04:00
|
|
|
}, nil
|
|
|
|
}
|