2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-08-31 15:37:07 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2015-06-09 00:34:20 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
2013-08-27 00:57:23 -04:00
|
|
|
"time"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/gophercloud-fork-40444fb"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SSHAddress returns a function that can be given to the SSH communicator
|
|
|
|
// for determining the SSH address based on the server AccessIPv4 setting..
|
2015-02-17 00:23:21 -05:00
|
|
|
func SSHAddress(csp gophercloud.CloudServersProvider, sshinterface string, port int) func(multistep.StateBag) (string, error) {
|
2013-08-31 15:37:07 -04:00
|
|
|
return func(state multistep.StateBag) (string, error) {
|
2014-02-27 03:34:24 -05:00
|
|
|
s := state.Get("server").(*gophercloud.Server)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2014-02-27 03:34:24 -05:00
|
|
|
if ip := state.Get("access_ip").(gophercloud.FloatingIp); ip.Ip != "" {
|
|
|
|
return fmt.Sprintf("%s:%d", ip.Ip, port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ip_pools, err := s.AllAddressPools()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New("Error parsing SSH addresses")
|
|
|
|
}
|
|
|
|
for pool, addresses := range ip_pools {
|
2015-02-17 00:23:21 -05:00
|
|
|
if sshinterface != "" {
|
|
|
|
if pool != sshinterface {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-02-27 03:34:24 -05:00
|
|
|
if pool != "" {
|
|
|
|
for _, address := range addresses {
|
2014-06-15 03:12:52 -04:00
|
|
|
if address.Addr != "" && address.Version == 4 {
|
2014-02-27 03:34:24 -05:00
|
|
|
return fmt.Sprintf("%s:%d", address.Addr, port), nil
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
serverState, err := csp.ServerById(s.Id)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2014-02-27 03:34:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2014-02-27 03:34:24 -05:00
|
|
|
state.Put("server", serverState)
|
|
|
|
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
|
|
|
|
// config for connecting to the instance created over SSH using the generated
|
|
|
|
// private key.
|
2014-04-10 04:48:55 -04:00
|
|
|
func SSHConfig(username string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
|
2013-08-31 15:37:07 -04:00
|
|
|
privateKey := state.Get("privateKey").(string)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
|
|
|
|
if err != nil {
|
2013-08-27 00:57:23 -04:00
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:48:55 -04:00
|
|
|
return &ssh.ClientConfig{
|
2013-08-27 00:57:23 -04:00
|
|
|
User: username,
|
2014-04-10 04:48:55 -04:00
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeys(signer),
|
2013-08-27 00:57:23 -04:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|