2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-08-31 15:37:07 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-08-27 00:57:23 -04:00
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
"github.com/rackspace/gophercloud"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SSHAddress returns a function that can be given to the SSH communicator
|
|
|
|
// for determining the SSH address based on the server AccessIPv4 setting..
|
2013-08-31 15:37:07 -04:00
|
|
|
func SSHAddress(csp gophercloud.CloudServersProvider, port int) func(multistep.StateBag) (string, error) {
|
|
|
|
return func(state multistep.StateBag) (string, error) {
|
2013-08-27 00:57:23 -04:00
|
|
|
for j := 0; j < 2; j++ {
|
2013-08-31 15:37:07 -04:00
|
|
|
s := state.Get("server").(*gophercloud.Server)
|
2013-08-27 00:57:23 -04:00
|
|
|
if s.AccessIPv4 != "" {
|
|
|
|
return fmt.Sprintf("%s:%d", s.AccessIPv4, port), nil
|
|
|
|
}
|
2013-09-01 01:39:30 -04:00
|
|
|
if s.AccessIPv6 != "" {
|
|
|
|
return fmt.Sprintf("[%s]:%d", s.AccessIPv6, port), nil
|
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
serverState, err := csp.ServerById(s.Id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:37:07 -04:00
|
|
|
state.Put("server", serverState)
|
2013-08-27 00:57:23 -04: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.
|
2013-08-31 15:37:07 -04:00
|
|
|
func SSHConfig(username string) func(multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|
|
|
privateKey := state.Get("privateKey").(string)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
keyring := new(ssh.SimpleKeychain)
|
|
|
|
if err := keyring.AddPEMKey(privateKey); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gossh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []gossh.ClientAuth{
|
|
|
|
gossh.ClientAuthKeyring(keyring),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|