builder/amazon/common: be more gentle on AWS API while getting SSHAddr
This commit is contained in:
parent
4e8db89403
commit
c8508ade17
|
@ -12,27 +12,32 @@ import (
|
||||||
// for determining the SSH address based on the instance DNS name.
|
// for determining the SSH address based on the instance DNS name.
|
||||||
func SSHAddress(e *ec2.EC2, port int) func(map[string]interface{}) (string, error) {
|
func SSHAddress(e *ec2.EC2, port int) func(map[string]interface{}) (string, error) {
|
||||||
return func(state map[string]interface{}) (string, error) {
|
return func(state map[string]interface{}) (string, error) {
|
||||||
var host string
|
for j := 0; j < 2; j++ {
|
||||||
i := state["instance"].(*ec2.Instance)
|
var host string
|
||||||
r, err := e.Instances([]string{i.InstanceId}, ec2.NewFilter())
|
i := state["instance"].(*ec2.Instance)
|
||||||
if err != nil {
|
if i.DNSName != "" {
|
||||||
return "", err
|
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 "", errors.New("couldn't determine IP address for instance")
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue