2013-07-20 23:03:00 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-07-31 12:30:52 -04:00
|
|
|
"errors"
|
2013-07-20 23:03:00 -04:00
|
|
|
"fmt"
|
2016-10-23 22:43:47 -04:00
|
|
|
"net"
|
|
|
|
"os"
|
2013-08-03 19:38:21 -04:00
|
|
|
"time"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
packerssh "github.com/hashicorp/packer/communicator/ssh"
|
2015-04-05 17:58:48 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2015-05-28 11:24:41 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
2016-10-23 22:43:47 -04:00
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2013-07-20 23:03:00 -04:00
|
|
|
)
|
|
|
|
|
2016-09-07 11:15:53 -04:00
|
|
|
type ec2Describer interface {
|
|
|
|
DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// modified in tests
|
|
|
|
sshHostSleepDuration = time.Second
|
|
|
|
)
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
// SSHHost returns a function that can be given to the SSH communicator
|
2013-07-20 23:04:28 -04:00
|
|
|
// for determining the SSH address based on the instance DNS name.
|
2017-11-21 21:49:38 -05:00
|
|
|
func SSHHost(e ec2Describer, sshInterface string) func(multistep.StateBag) (string, error) {
|
2013-08-31 16:00:43 -04:00
|
|
|
return func(state multistep.StateBag) (string, error) {
|
2016-09-07 11:15:53 -04:00
|
|
|
const tries = 2
|
|
|
|
// <= with current structure to check result of describing `tries` times
|
|
|
|
for j := 0; j <= tries; j++ {
|
2013-08-03 19:37:34 -04:00
|
|
|
var host string
|
2013-08-31 16:00:43 -04:00
|
|
|
i := state.Get("instance").(*ec2.Instance)
|
2017-11-21 21:49:38 -05:00
|
|
|
if sshInterface != "" {
|
|
|
|
switch sshInterface {
|
|
|
|
case "public_ip":
|
|
|
|
if i.PublicIpAddress != nil {
|
|
|
|
host = *i.PublicIpAddress
|
|
|
|
}
|
|
|
|
case "private_ip":
|
|
|
|
if i.PrivateIpAddress != nil {
|
|
|
|
host = *i.PrivateIpAddress
|
|
|
|
}
|
|
|
|
case "public_dns":
|
|
|
|
if i.PublicDnsName != nil {
|
|
|
|
host = *i.PublicDnsName
|
|
|
|
}
|
|
|
|
case "private_dns":
|
|
|
|
if i.PrivateDnsName != nil {
|
|
|
|
host = *i.PrivateDnsName
|
|
|
|
}
|
|
|
|
default:
|
2017-11-27 17:46:01 -05:00
|
|
|
panic(fmt.Sprintf("Unknown interface type: %s", sshInterface))
|
2017-11-21 21:49:38 -05:00
|
|
|
}
|
|
|
|
} else if i.VpcId != nil && *i.VpcId != "" {
|
|
|
|
if i.PublicIpAddress != nil && *i.PublicIpAddress != "" {
|
2015-08-17 20:44:01 -04:00
|
|
|
host = *i.PublicIpAddress
|
2016-09-07 11:15:53 -04:00
|
|
|
} else if i.PrivateIpAddress != nil && *i.PrivateIpAddress != "" {
|
2015-08-17 20:44:01 -04:00
|
|
|
host = *i.PrivateIpAddress
|
2013-11-25 23:45:06 -05:00
|
|
|
}
|
2015-08-17 20:44:01 -04:00
|
|
|
} else if i.PublicDnsName != nil && *i.PublicDnsName != "" {
|
|
|
|
host = *i.PublicDnsName
|
2013-08-03 19:37:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if host != "" {
|
2015-06-13 19:23:33 -04:00
|
|
|
return host, nil
|
2013-08-03 19:37:34 -04:00
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
r, err := e.DescribeInstances(&ec2.DescribeInstancesInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
InstanceIds: []*string{i.InstanceId},
|
2015-04-05 17:58:48 -04:00
|
|
|
})
|
2013-08-03 19:37:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Reservations) == 0 || len(r.Reservations[0].Instances) == 0 {
|
2015-08-17 20:44:01 -04:00
|
|
|
return "", fmt.Errorf("instance not found: %s", *i.InstanceId)
|
2013-08-03 19:37:34 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 11:15:53 -04:00
|
|
|
state.Put("instance", r.Reservations[0].Instances[0])
|
|
|
|
time.Sleep(sshHostSleepDuration)
|
2013-08-03 19:21:01 -04:00
|
|
|
}
|
|
|
|
|
2017-11-21 21:49:38 -05:00
|
|
|
return "", errors.New("couldn't determine address for instance")
|
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
|
2016-10-02 16:20:36 -04:00
|
|
|
// config for connecting to the instance created over SSH using the private key
|
|
|
|
// or password.
|
2016-10-23 22:43:47 -04: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) {
|
2016-10-23 22:43:47 -04: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(),
|
2016-10-23 22:43:47 -04:00
|
|
|
}, nil
|
|
|
|
}
|
2013-07-20 23:03:00 -04:00
|
|
|
|
2016-10-02 16:20:36 -04:00
|
|
|
privateKey, hasKey := state.GetOk("privateKey")
|
|
|
|
if hasKey {
|
2013-07-20 23:03:00 -04:00
|
|
|
|
2016-10-02 16:20:36 -04:00
|
|
|
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-02 16:20:36 -04:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return &ssh.ClientConfig{
|
2017-05-18 15:01:44 -04:00
|
|
|
User: username,
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
2016-10-02 16:20:36 -04:00
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.Password(password),
|
|
|
|
ssh.KeyboardInteractive(
|
|
|
|
packerssh.PasswordKeyboardInteractive(password)),
|
|
|
|
}}, nil
|
|
|
|
}
|
2013-07-20 23:03:00 -04:00
|
|
|
}
|
|
|
|
}
|