Increase exercise robustness

* increase some timeouts
* tolerate existing security groups and rules
* add optional DEFAULT_IMAGE_NAME to select the image to boot
* fix image lists via glance

Change-Id: I31ae743e602f69a2c9f872273273f542fc4afda3
diff --git a/exercises/euca.sh b/exercises/euca.sh
index 2f7a17b..e569196 100755
--- a/exercises/euca.sh
+++ b/exercises/euca.sh
@@ -20,6 +20,18 @@
 source ./openrc
 popd
 
+# Max time to wait while vm goes from build to active state
+ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
+
+# Max time till the vm is bootable
+BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
+
+# Max time to wait for proper association and dis-association.
+ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
+
+# Instance type to create
+DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
+
 # Find a machine image to boot
 IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1`
 
@@ -27,10 +39,15 @@
 SECGROUP=euca_secgroup
 
 # Add a secgroup
-euca-add-group -d description $SECGROUP
+if ! euca-describe-group | grep -q $SECGROUP; then
+    euca-add-group -d "$SECGROUP description" $SECGROUP
+    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! euca-describe-group | grep -q $SECGROUP; do sleep 1; done"; then
+        echo "Security group not created"
+        exit 1
+    fi
+fi
 
 # Launch it
-DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2`
 
 # Assure it has booted within a reasonable time
@@ -42,15 +59,13 @@
 # Allocate floating address
 FLOATING_IP=`euca-allocate-address | cut -f2`
 
-# Release floating address
+# Associate floating address
 euca-associate-address -i $INSTANCE $FLOATING_IP
 
-
 # Authorize pinging
 euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
 
 # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
-ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
     echo "Couldn't ping server with floating ip"
     exit 1
@@ -65,6 +80,12 @@
 # Release floating address
 euca-disassociate-address $FLOATING_IP
 
+# Wait just a tick for everything above to complete so release doesn't fail
+if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then
+    echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds"
+    exit 1
+fi
+
 # Release floating address
 euca-release-address $FLOATING_IP