Merge "Add nova's new vnc support to devstack"
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
 
diff --git a/exercises/floating_ips.sh b/exercises/floating_ips.sh
index 9c207cd..f7b5240 100755
--- a/exercises/floating_ips.sh
+++ b/exercises/floating_ips.sh
@@ -24,9 +24,28 @@
 source ./openrc
 popd
 
-# Set some defaults
+# 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}
+
+# Boot this image, use first AMi image if unset
+DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
+
+# Security group name
+SECGROUP=${SECGROUP:-test_secgroup}
+
+# Default floating IP pool name
 DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
+
+# Additional floating IP pool and range
 TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
 
 # Get a token for clients that don't support service catalog
@@ -51,28 +70,32 @@
 nova image-list
 
 # But we recommend using glance directly
-glance -A $TOKEN index
+glance -f -A $TOKEN index
 
-# Let's grab the id of the first AMI image to launch
-IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1`
+# Grab the id of the image to launch
+IMAGE=`glance -f -A $TOKEN index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
 
 # Security Groups
 # ---------------
-SECGROUP=test_secgroup
 
 # List of secgroups:
 nova secgroup-list
 
 # Create a secgroup
-nova secgroup-create $SECGROUP "test_secgroup description"
+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
 
-# determine flavor
-# ----------------
+# determinine instance type
+# -------------------------
 
-# List of flavors:
+# List of instance types:
 nova flavor-list
 
-DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
 if [[ -z "$INSTANCE_TYPE" ]]; then
     # grab the first flavor in the list to launch if default doesn't exist
@@ -81,7 +104,7 @@
 
 NAME="myserver"
 
-nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP
+VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
 
 # Testing
 # =======
@@ -93,23 +116,14 @@
 # Waiting for boot
 # ----------------
 
-# Max time to wait while vm goes from build to active state
-ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
-
-# Max time till the vm is bootable
-BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
-
-# Max time to wait for proper association and dis-association.
-ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
-
 # check that the status is active within ACTIVE_TIMEOUT seconds
-if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $NAME | grep status | grep -q ACTIVE; do sleep 1; done"; then
+if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
     echo "server didn't become active!"
     exit 1
 fi
 
 # get the IP of the server
-IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
+IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
 
 # for single node deployments, we can ping private ips
 MULTI_HOST=${MULTI_HOST:-0}
@@ -129,8 +143,14 @@
 # Security Groups & Floating IPs
 # ------------------------------
 
-# allow icmp traffic (ping)
-nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
+if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
+    # allow icmp traffic (ping)
+    nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
+    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
+        echo "Security group rule not created"
+        exit 1
+    fi
+fi
 
 # List rules for a secgroup
 nova secgroup-list-rules $SECGROUP
@@ -145,7 +165,7 @@
 fi
 
 # add floating ip to our server
-nova add-floating-ip $NAME $FLOATING_IP
+nova add-floating-ip $VM_UUID $FLOATING_IP
 
 # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
@@ -153,7 +173,7 @@
     exit 1
 fi
 
-# Allocate an IP from it
+# Allocate an IP from second floating pool
 TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | cut -d '|' -f2`
 
 # list floating addresses
@@ -182,11 +202,10 @@
 nova floating-ip-delete $TEST_FLOATING_IP
 
 # shutdown the server
-nova delete $NAME
+nova delete $VM_UUID
 
 # Delete a secgroup
 nova secgroup-delete $SECGROUP
 
 # FIXME: validate shutdown within 5 seconds
 # (nova show $NAME returns 1 or status != ACTIVE)?
-
diff --git a/exercises/volumes.sh b/exercises/volumes.sh
index 6ea9a51..c2288de 100755
--- a/exercises/volumes.sh
+++ b/exercises/volumes.sh
@@ -19,6 +19,21 @@
 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}
+
+# Boot this image, use first AMi image if unset
+DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
+
 # Get a token for clients that don't support service catalog
 # ==========================================================
 
@@ -41,10 +56,10 @@
 nova image-list
 
 # But we recommend using glance directly
-glance -A $TOKEN index
+glance -f -A $TOKEN index
 
-# Let's grab the id of the first AMI image to launch
-IMAGE=`glance -A $TOKEN index | egrep ami | head -1 | cut -d" " -f1`
+# Grab the id of the image to launch
+IMAGE=`glance -f -A $TOKEN index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
 
 # determinine instance type
 # -------------------------
@@ -52,7 +67,6 @@
 # List of instance types:
 nova flavor-list
 
-DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
 if [[ -z "$INSTANCE_TYPE" ]]; then
     # grab the first flavor in the list to launch if default doesn't exist
@@ -73,24 +87,14 @@
 # Waiting for boot
 # ----------------
 
-# 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:-15}
-
-# Max time to wait for proper association and dis-association.
-ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
-
 # check that the status is active within ACTIVE_TIMEOUT seconds
-if ! timeout $BOOT_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
+if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
     echo "server didn't become active!"
     exit 1
 fi
 
 # get the IP of the server
 IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
-#VM_UUID=`nova list | grep $NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'`
 
 # for single node deployments, we can ping private ips
 MULTI_HOST=${MULTI_HOST:-0}
diff --git a/openrc b/openrc
index 4395975..0f327d2 100644
--- a/openrc
+++ b/openrc
@@ -53,13 +53,13 @@
 # export NOVACLIENT_DEBUG=1
 
 # Max time till the vm is bootable
-export BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
+export BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
 
 # Max time to wait while vm goes from build to active state
-export ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
+export ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
 
 # Max time from run instance command until it is running
 export RUNNING_TIMEOUT=${RUNNING_TIMEOUT:-$(($BOOT_TIMEOUT + $ACTIVE_TIMEOUT))}
 
 # Max time to wait for proper IP association and dis-association.
-export ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
+export ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
diff --git a/stack.sh b/stack.sh
index 758cc21..cf121af 100755
--- a/stack.sh
+++ b/stack.sh
@@ -66,6 +66,12 @@
 # We try to have sensible defaults, so you should be able to run ``./stack.sh``
 # in most cases.
 #
+# We support HTTP and HTTPS proxy servers via the usual environment variables
+# http_proxy and https_proxy.  They can be set in localrc if necessary or
+# on the command line::
+#
+#     http_proxy=http://proxy.example.com:3128/ ./stack.sh
+#
 # We source our settings from ``stackrc``.  This file is distributed with devstack
 # and contains locations for what repositories to use.  If you want to use other
 # repositories and branches, you can add your own settings with another file called
@@ -85,8 +91,9 @@
     [[ "$OFFLINE" = "True" ]] && return
     local sudo="sudo"
     [ "$(id -u)" = "0" ] && sudo="env"
-    $sudo DEBIAN_FRONTEND=noninteractive apt-get \
-        --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
+    $sudo DEBIAN_FRONTEND=noninteractive \
+        http_proxy=$http_proxy https_proxy=$https_proxy \
+        apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
 }
 
 # Check to see if we are already running a stack.sh
@@ -513,7 +520,10 @@
 
 function pip_install {
     [[ "$OFFLINE" = "True" ]] && return
-    sudo PIP_DOWNLOAD_CACHE=/var/cache/pip pip install --use-mirrors $@
+    sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \
+        HTTP_PROXY=$http_proxy \
+        HTTPS_PROXY=$https_proxy \
+        pip install --use-mirrors $@
 }
 
 # install apt requirements
@@ -1007,7 +1017,7 @@
        # We need a special version of bin/swift which understand the
        # OpenStack api 2.0, we download it until this is getting
        # integrated in swift.
-       sudo curl -s -o/usr/local/bin/swift \
+       sudo https_proxy=$https_proxy curl -s -o/usr/local/bin/swift \
            'https://review.openstack.org/gitweb?p=openstack/swift.git;a=blob_plain;f=bin/swift;hb=48bfda6e2fdf3886c98bd15649887d54b9a2574e'
    else
        swift_auth_server=tempauth
@@ -1321,7 +1331,7 @@
 if [[ "$ENABLED_SERVICES" =~ "g-api" ]]; then
     screen_it g-api "cd $GLANCE_DIR; bin/glance-api --config-file=etc/glance-api.conf"
     echo "Waiting for g-api ($GLANCE_HOSTPORT) to start..."
-    if ! timeout $SERVICE_TIMEOUT sh -c "while ! wget -q -O- http://$GLANCE_HOSTPORT; do sleep 1; done"; then
+    if ! timeout $SERVICE_TIMEOUT sh -c "while ! http_proxy= wget -q -O- http://$GLANCE_HOSTPORT; do sleep 1; done"; then
       echo "g-api did not start"
       exit 1
     fi
@@ -1331,7 +1341,7 @@
 if [[ "$ENABLED_SERVICES" =~ "key" ]]; then
     screen_it key "cd $KEYSTONE_DIR && $KEYSTONE_DIR/bin/keystone --config-file $KEYSTONE_CONF $KEYSTONE_LOG_CONFIG -d"
     echo "Waiting for keystone to start..."
-    if ! timeout $SERVICE_TIMEOUT sh -c "while ! wget -q -O- $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT; do sleep 1; done"; then
+    if ! timeout $SERVICE_TIMEOUT sh -c "while ! http_proxy= wget -q -O- $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT; do sleep 1; done"; then
       echo "keystone did not start"
       exit 1
     fi
@@ -1341,7 +1351,7 @@
 if [[ "$ENABLED_SERVICES" =~ "n-api" ]]; then
     screen_it n-api "cd $NOVA_DIR && $NOVA_DIR/bin/nova-api"
     echo "Waiting for nova-api to start..."
-    if ! timeout $SERVICE_TIMEOUT sh -c "while ! wget -q -O- http://127.0.0.1:8774; do sleep 1; done"; then
+    if ! timeout $SERVICE_TIMEOUT sh -c "while ! http_proxy= wget -q -O- http://127.0.0.1:8774; do sleep 1; done"; then
       echo "nova-api did not start"
       exit 1
     fi