2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-06-12 00:16:43 -04:00
|
|
|
"log"
|
2017-03-10 11:45:13 -05:00
|
|
|
"net"
|
|
|
|
"os"
|
2013-08-27 00:57:23 -04:00
|
|
|
"time"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2017-04-04 16:39:01 -04:00
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
2015-06-12 00:16:43 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"golang.org/x/crypto/ssh"
|
2017-03-10 11:45:13 -05:00
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
// CommHost looks up the host for the communicator.
|
|
|
|
func CommHost(
|
2015-06-12 00:16:43 -04:00
|
|
|
client *gophercloud.ServiceClient,
|
2016-02-13 15:53:46 -05:00
|
|
|
sshinterface string,
|
|
|
|
sshipversion string) func(multistep.StateBag) (string, error) {
|
2013-08-31 15:37:07 -04:00
|
|
|
return func(state multistep.StateBag) (string, error) {
|
2015-06-12 00:16:43 -04:00
|
|
|
s := state.Get("server").(*servers.Server)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2015-06-12 14:28:27 -04:00
|
|
|
// If we have a specific interface, try that
|
|
|
|
if sshinterface != "" {
|
2016-02-13 15:53:46 -05:00
|
|
|
if addr := sshAddrFromPool(s, sshinterface, sshipversion); addr != "" {
|
2016-02-16 04:55:21 -05:00
|
|
|
log.Printf("[DEBUG] Using IP address %s from specified interface %s to connect", addr, sshinterface)
|
2015-06-12 14:28:27 -04:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
// If we have a floating IP, use that
|
2016-11-27 19:59:26 -05:00
|
|
|
ip := state.Get("access_ip").(*floatingips.FloatingIP)
|
2015-06-12 10:02:04 -04:00
|
|
|
if ip != nil && ip.IP != "" {
|
2016-02-16 04:55:21 -05:00
|
|
|
log.Printf("[DEBUG] Using floating IP %s to connect", ip.IP)
|
2015-06-13 19:23:33 -04:00
|
|
|
return ip.IP, nil
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
if s.AccessIPv4 != "" {
|
2016-02-16 04:55:21 -05:00
|
|
|
log.Printf("[DEBUG] Using AccessIPv4 %s to connect", s.AccessIPv4)
|
2015-06-13 19:23:33 -04:00
|
|
|
return s.AccessIPv4, nil
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2015-06-12 14:28:27 -04:00
|
|
|
// Try to get it from the requested interface
|
2016-02-13 15:53:46 -05:00
|
|
|
if addr := sshAddrFromPool(s, sshinterface, sshipversion); addr != "" {
|
2016-02-16 04:55:21 -05:00
|
|
|
log.Printf("[DEBUG] Using IP address %s to connect", addr)
|
2015-06-12 14:28:27 -04:00
|
|
|
return addr, nil
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
s, err := servers.Get(client, s.ID).Extract()
|
2014-02-27 03:34:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
state.Put("server", s)
|
2014-02-27 03:34:24 -05:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
return "", errors.New("couldn't determine IP address for server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHConfig returns a function that can be used for the SSH communicator
|
2016-10-08 17:18:19 -04:00
|
|
|
// config for connecting to the instance created over SSH using a private key
|
|
|
|
// or a password.
|
2017-03-10 11:45:13 -05:00
|
|
|
func SSHConfig(useAgent bool, username, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
|
2014-04-10 04:48:55 -04:00
|
|
|
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
2017-03-10 11:45:13 -05:00
|
|
|
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),
|
|
|
|
},
|
2017-05-18 15:01:44 -04:00
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2017-03-10 11:45:13 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2016-10-08 17:18:19 -04:00
|
|
|
privateKey, hasKey := state.GetOk("privateKey")
|
|
|
|
|
|
|
|
if hasKey {
|
|
|
|
|
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeys(signer),
|
|
|
|
},
|
2017-05-18 15:01:44 -04:00
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2016-10-08 17:18:19 -04:00
|
|
|
}, nil
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2016-10-08 17:18:19 -04:00
|
|
|
} else {
|
|
|
|
|
|
|
|
return &ssh.ClientConfig{
|
2017-05-18 15:01:44 -04:00
|
|
|
User: username,
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2016-10-08 17:18:19 -04:00
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.Password(password),
|
|
|
|
ssh.KeyboardInteractive(
|
|
|
|
packerssh.PasswordKeyboardInteractive(password)),
|
|
|
|
}}, nil
|
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-12 14:28:27 -04:00
|
|
|
|
2016-02-13 15:53:46 -05:00
|
|
|
func sshAddrFromPool(s *servers.Server, desired string, sshIPVersion string) string {
|
2015-06-12 14:28:27 -04:00
|
|
|
// Get all the addresses associated with this server. This
|
|
|
|
// was taken directly from Terraform.
|
|
|
|
for pool, networkAddresses := range s.Addresses {
|
|
|
|
// If we have an SSH interface specified, skip it if no match
|
|
|
|
if desired != "" && pool != desired {
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Skipping pool %s, doesn't match requested %s",
|
|
|
|
pool, desired)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
elements, ok := networkAddresses.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
log.Printf(
|
|
|
|
"[ERROR] Unknown return type for address field: %#v",
|
|
|
|
networkAddresses)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, element := range elements {
|
|
|
|
var addr string
|
|
|
|
address := element.(map[string]interface{})
|
|
|
|
if address["OS-EXT-IPS:type"] == "floating" {
|
|
|
|
addr = address["addr"].(string)
|
2016-02-14 15:23:56 -05:00
|
|
|
} else if sshIPVersion == "4" {
|
2016-02-13 15:53:46 -05:00
|
|
|
if address["version"].(float64) == 4 {
|
|
|
|
addr = address["addr"].(string)
|
|
|
|
}
|
2016-02-14 15:23:56 -05:00
|
|
|
} else if sshIPVersion == "6" {
|
2016-02-13 15:53:46 -05:00
|
|
|
if address["version"].(float64) == 6 {
|
|
|
|
addr = fmt.Sprintf("[%s]", address["addr"].(string))
|
|
|
|
}
|
2015-06-12 14:28:27 -04:00
|
|
|
} else {
|
2015-07-14 19:11:46 -04:00
|
|
|
if address["version"].(float64) == 6 {
|
|
|
|
addr = fmt.Sprintf("[%s]", address["addr"].(string))
|
|
|
|
} else {
|
2015-06-12 14:28:27 -04:00
|
|
|
addr = address["addr"].(string)
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 19:11:46 -04:00
|
|
|
|
2015-06-12 14:28:27 -04:00
|
|
|
if addr != "" {
|
2015-07-14 19:11:46 -04:00
|
|
|
log.Printf("[DEBUG] Detected address: %s", addr)
|
2015-06-15 12:34:35 -04:00
|
|
|
return addr
|
2015-06-12 14:28:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|