builder/amazon/common: be more gentle on AWS API while getting SSHAddr

This commit is contained in:
Mitchell Hashimoto 2013-08-03 16:37:34 -07:00
parent 798b7cb25f
commit 4acdeb2832
1 changed files with 24 additions and 19 deletions

View File

@ -12,27 +12,32 @@ import (
// for determining the SSH address based on the instance DNS name.
func SSHAddress(e *ec2.EC2, port int) func(map[string]interface{}) (string, error) {
return func(state map[string]interface{}) (string, error) {
var host string
i := state["instance"].(*ec2.Instance)
r, err := e.Instances([]string{i.InstanceId}, ec2.NewFilter())
if err != nil {
return "", err
for j := 0; j < 2; j++ {
var host string
i := state["instance"].(*ec2.Instance)
if i.DNSName != "" {
host = i.DNSName
} else if i.VpcId == "" {
host = i.PrivateIpAddress
}
if host != "" {
return fmt.Sprintf("%s:%d", host, port), nil
}
r, err := e.Instances([]string{i.InstanceId}, ec2.NewFilter())
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["instance"] = &r.Reservations[0].Instances[0]
}
if len(r.Reservations) == 0 || len(r.Reservations[0].Instances) == 0 {
return "", fmt.Errorf("instance not found: %s", i.InstanceId)
}
i = &r.Reservations[0].Instances[0]
if i.DNSName != "" {
host = i.DNSName
} else if i.VpcId == "" {
host = i.PrivateIpAddress
} else {
return "", errors.New("couldn't determine IP address for instance")
}
return fmt.Sprintf("%s:%d", host, port), nil
return "", errors.New("couldn't determine IP address for instance")
}
}