2013-07-20 23:03:00 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2013-07-31 12:30:52 -04:00
|
|
|
"errors"
|
2013-07-20 23:03:00 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
|
|
|
)
|
|
|
|
|
2013-07-20 23:04:28 -04:00
|
|
|
// SSHAddress returns a function that can be given to the SSH communicator
|
|
|
|
// for determining the SSH address based on the instance DNS name.
|
2013-07-20 23:03:00 -04:00
|
|
|
func SSHAddress(port int) func(map[string]interface{}) (string, error) {
|
|
|
|
return func(state map[string]interface{}) (string, error) {
|
2013-07-22 01:48:58 -04:00
|
|
|
var host string
|
2013-07-20 23:03:00 -04:00
|
|
|
instance := state["instance"].(*ec2.Instance)
|
2013-07-31 12:30:52 -04:00
|
|
|
if instance.DNSName != "" {
|
|
|
|
host = instance.DNSName
|
|
|
|
} else if instance.VpcId == "" {
|
2013-07-22 01:48:58 -04:00
|
|
|
host = instance.PrivateIpAddress
|
|
|
|
} else {
|
2013-07-31 12:30:52 -04:00
|
|
|
return "", errors.New("couldn't determine IP address for instance")
|
2013-07-22 01:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s:%d", host, port), nil
|
2013-07-20 23:03:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 23:04:28 -04:00
|
|
|
// 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-07-20 23:03:00 -04:00
|
|
|
func SSHConfig(username string) func(map[string]interface{}) (*gossh.ClientConfig, error) {
|
|
|
|
return func(state map[string]interface{}) (*gossh.ClientConfig, error) {
|
|
|
|
privateKey := state["privateKey"].(string)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|