packer-cn/builder/openstack/ssh.go

93 lines
2.5 KiB
Go
Raw Normal View History

package openstack
import (
"errors"
"fmt"
2015-06-12 00:16:43 -04:00
"log"
"time"
2015-06-12 00:16:43 -04:00
"github.com/mitchellh/multistep"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"golang.org/x/crypto/ssh"
)
// SSHAddress returns a function that can be given to the SSH communicator
// for determining the SSH address based on the server AccessIPv4 setting..
2015-06-12 00:16:43 -04:00
func SSHAddress(
client *gophercloud.ServiceClient,
sshinterface string, port int) 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)
2015-06-12 00:16:43 -04:00
// If we have a floating IP, use that
ip := state.Get("access_ip").(*floatingip.FloatingIP)
2015-06-12 10:02:04 -04:00
if ip != nil && ip.IP != "" {
return fmt.Sprintf("%s:%d", ip.IP, port), nil
2014-02-27 03:34:24 -05:00
}
2015-06-12 00:16:43 -04:00
if s.AccessIPv4 != "" {
return fmt.Sprintf("%s:%d", s.AccessIPv4, port), nil
2014-02-27 03:34:24 -05:00
}
2015-06-12 00:16:43 -04:00
// Get all the addresses associated with this server. This
// was taken directly from Terraform.
for _, networkAddresses := range s.Addresses {
elements, ok := networkAddresses.([]interface{})
if !ok {
log.Printf(
"[ERROR] Unknown return type for address field: %#v",
networkAddresses)
continue
}
2015-06-12 00:16:43 -04:00
for _, element := range elements {
var addr string
address := element.(map[string]interface{})
if address["OS-EXT-IPS:type"] == "floating" {
addr = address["addr"].(string)
} else {
if address["version"].(float64) == 4 {
addr = address["addr"].(string)
2014-02-27 03:34:24 -05:00
}
}
2015-06-12 00:16:43 -04:00
if addr != "" {
return fmt.Sprintf("%s:%d", addr, port), 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
}
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)
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.
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)
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
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),
},
}, nil
}
}