mirror of
https://github.com/hashicorp/packer.git
synced 2026-03-02 05:21:02 -05:00
This commit moves the Amazon builders of Packer away from the Hashicorp fork of the goamz library to the official AWS SDK for Go, in order that third party plugins may depend on the more complete official library more easily.
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package common
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"code.google.com/p/go.crypto/ssh"
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
|
"github.com/mitchellh/multistep"
|
|
)
|
|
|
|
// SSHAddress returns a function that can be given to the SSH communicator
|
|
// for determining the SSH address based on the instance DNS name.
|
|
func SSHAddress(e *ec2.EC2, port int, private bool) func(multistep.StateBag) (string, error) {
|
|
return func(state multistep.StateBag) (string, error) {
|
|
for j := 0; j < 2; j++ {
|
|
var host string
|
|
i := state.Get("instance").(*ec2.Instance)
|
|
if *i.VPCID != "" {
|
|
if *i.PublicIPAddress != "" && !private {
|
|
host = *i.PublicIPAddress
|
|
} else {
|
|
host = *i.PrivateIPAddress
|
|
}
|
|
} else if *i.PublicDNSName != "" {
|
|
host = *i.PublicDNSName
|
|
}
|
|
|
|
if host != "" {
|
|
return fmt.Sprintf("%s:%d", host, port), nil
|
|
}
|
|
|
|
r, err := e.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
InstanceIDs: []*string{i.InstanceID},
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if len(r.Reservations) == 0 || len(r.Reservations[0].Instances) == 0 {
|
|
return "", fmt.Errorf("instance not found: %s", *i.InstanceID)
|
|
}
|
|
|
|
state.Put("instance", &r.Reservations[0].Instances[0])
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
|
|
return "", errors.New("couldn't determine IP address for instance")
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
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
|
|
}
|
|
}
|