From c8508ade17696ff07199c9c3c0aa503fbd20d230 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 3 Aug 2013 16:37:34 -0700 Subject: [PATCH] builder/amazon/common: be more gentle on AWS API while getting SSHAddr --- builder/amazon/common/ssh.go | 43 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/builder/amazon/common/ssh.go b/builder/amazon/common/ssh.go index 808a868f6..a5ddf8fae 100644 --- a/builder/amazon/common/ssh.go +++ b/builder/amazon/common/ssh.go @@ -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") } }