Clean up security groups in exercises

* fix problem with deleting security groups too early in
  floating_ips.sh and euca.sh
* create and clean up security groups
* cosmetic cleanups

Change-Id: Ie45e03f889c540ec83f27a02b10e787060c5d4d7
diff --git a/exercises/boot_from_volume.sh b/exercises/boot_from_volume.sh
index 7fe81ba..c967e39 100755
--- a/exercises/boot_from_volume.sh
+++ b/exercises/boot_from_volume.sh
@@ -49,6 +49,10 @@
 # Default user
 DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros}
 
+# Security group name
+SECGROUP=${SECGROUP:-boot_secgroup}
+
+
 # Launching servers
 # =================
 
@@ -72,7 +76,6 @@
 fi
 
 # Configure Security Groups
-SECGROUP=${SECGROUP:-test_secgroup}
 nova secgroup-delete $SECGROUP || true
 nova secgroup-create $SECGROUP "$SECGROUP description"
 nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
@@ -246,8 +249,8 @@
     die "Failure deleting instance $INSTANCE_NAME"
 
 # Wait for termination
-if ! timeout $ACTIVE_TIMEOUT sh -c "while nova show $INSTANCE_NAME; do sleep 1; done"; then
-    echo "server didn't terminate!"
+if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
+    echo "Server $NAME not deleted"
     exit 1
 fi
 
@@ -256,8 +259,7 @@
     die "Failure deleting floating IP $FLOATING_IP"
 
 # Delete a secgroup
-nova secgroup-delete $SECGROUP || \
-    die "Failure deleting security group $SECGROUP"
+nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
 
 set +o xtrace
 echo "*********************************************************************"
diff --git a/exercises/euca.sh b/exercises/euca.sh
index 9f7aed1..fb052dd 100755
--- a/exercises/euca.sh
+++ b/exercises/euca.sh
@@ -43,6 +43,9 @@
 # Boot this image, use first AMI-format image if unset
 DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
 
+# Security group name
+SECGROUP=${SECGROUP:-euca_secgroup}
+
 
 # Launching a server
 # ==================
@@ -50,9 +53,6 @@
 # Find a machine image to boot
 IMAGE=`euca-describe-images | grep machine | grep ${DEFAULT_IMAGE_NAME} | cut -f2 | head -n1`
 
-# Define secgroup
-SECGROUP=euca_secgroup
-
 # Add a secgroup
 if ! euca-describe-groups | grep -q $SECGROUP; then
     euca-add-group -d "$SECGROUP description" $SECGROUP
@@ -119,14 +119,13 @@
     die "Failure terminating instance $INSTANCE"
 
 # Assure it has terminated within a reasonable time
-if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
+if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q $INSTANCE; do sleep 1; done"; then
     echo "server didn't terminate within $TERMINATE_TIMEOUT seconds"
     exit 1
 fi
 
 # Delete group
-euca-delete-group $SECGROUP || \
-    die "Failure deleting security group $SECGROUP"
+euca-delete-group $SECGROUP || die "Failure deleting security group $SECGROUP"
 
 set +o xtrace
 echo "*********************************************************************"
diff --git a/exercises/floating_ips.sh b/exercises/floating_ips.sh
index 02259c0..77f020e 100755
--- a/exercises/floating_ips.sh
+++ b/exercises/floating_ips.sh
@@ -200,12 +200,12 @@
 # Delete second floating IP
 nova floating-ip-delete $TEST_FLOATING_IP || die "Failure deleting floating IP $TEST_FLOATING_IP"
 
-# shutdown the server
+# Shutdown the server
 nova delete $VM_UUID || die "Failure deleting instance $NAME"
 
-# make sure the VM shuts down within a reasonable time
-if ! timeout $TERMINATE_TIMEOUT sh -c "while nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
-    echo "server didn't shut down!"
+# Wait for termination
+if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
+    echo "Server $NAME not deleted"
     exit 1
 fi
 
diff --git a/exercises/volumes.sh b/exercises/volumes.sh
index 0f25355..5db10d3 100755
--- a/exercises/volumes.sh
+++ b/exercises/volumes.sh
@@ -43,6 +43,9 @@
 # Boot this image, use first AMi image if unset
 DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
 
+# Security group name
+SECGROUP=${SECGROUP:-vol_secgroup}
+
 
 # Launching a server
 # ==================
@@ -62,6 +65,25 @@
 # Grab the id of the image to launch
 IMAGE=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
 
+# Security Groups
+# ---------------
+
+# List of secgroups:
+nova secgroup-list
+
+# Create a secgroup
+if ! nova secgroup-list | grep -q $SECGROUP; then
+    nova secgroup-create $SECGROUP "$SECGROUP description"
+    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
+        echo "Security group not created"
+        exit 1
+    fi
+fi
+
+# Configure Security Group Rules
+nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
+nova secgroup-add-rule $SECGROUP tcp 22 22 0.0.0.0/0
+
 # determinine instance type
 # -------------------------
 
@@ -171,8 +193,17 @@
     exit 1
 fi
 
-# shutdown the server
-nova delete $NAME || die "Failure deleting instance $NAME"
+# Shutdown the server
+nova delete $VM_UUID || die "Failure deleting instance $NAME"
+
+# Wait for termination
+if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q $VM_UUID; do sleep 1; done"; then
+    echo "Server $NAME not deleted"
+    exit 1
+fi
+
+# Delete a secgroup
+nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
 
 set +o xtrace
 echo "*********************************************************************"