Merge "Add local.sh support and samples of local.sh and locarc"
diff --git a/exercises/aggregates.sh b/exercises/aggregates.sh
new file mode 100755
index 0000000..38fac12
--- /dev/null
+++ b/exercises/aggregates.sh
@@ -0,0 +1,141 @@
+#!/usr/bin/env bash
+
+# **aggregates.sh**
+
+# This script demonstrates how to use host aggregates:
+#  *  Create an Aggregate
+#  *  Updating Aggregate details
+#  *  Testing Aggregate metadata
+#  *  Testing Aggregate delete
+#  *  TODO(johngar) - test adding a host (idealy with two hosts)
+
+echo "**************************************************"
+echo "Begin DevStack Exercise: $0"
+echo "**************************************************"
+
+# This script exits on an error so that errors don't compound and you see
+# only the first error that occured.
+set -o errexit
+
+# Print the commands being run so that we can see the command that triggers
+# an error.  It is also useful for following allowing as the install occurs.
+set -o xtrace
+
+
+# Settings
+# ========
+
+# Keep track of the current directory
+EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
+TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
+
+# Import common functions
+source $TOP_DIR/functions
+
+# Import configuration
+source $TOP_DIR/openrc
+
+# Import exercise configuration
+source $TOP_DIR/exerciserc
+
+# run test as the admin user
+_OLD_USERNAME=$OS_USERNAME
+OS_USERNAME=admin
+
+
+# Create an aggregate
+# ===================
+
+AGGREGATE_NAME=test_aggregate_$RANDOM
+AGGREGATE_A_ZONE=nova
+
+exit_if_aggregate_present() {
+    aggregate_name=$1
+
+    if [ `nova aggregate-list | grep -c " $aggregate_name "` == 0 ]; then
+        echo "SUCCESS $aggregate_name not present"
+    else
+        echo "ERROR found aggregate: $aggregate_name"
+        exit -1
+    fi
+}
+
+exit_if_aggregate_present $AGGREGATE_NAME
+
+AGGREGATE_ID=`nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE | grep " $AGGREGATE_NAME " | get_field 1`
+
+# check aggregate created
+nova aggregate-list | grep -q " $AGGREGATE_NAME " || die "Aggregate $AGGREGATE_NAME not created"
+
+
+# Ensure creating a duplicate fails
+# =================================
+
+if nova aggregate-create $AGGREGATE_NAME $AGGREGATE_A_ZONE; then
+    echo "ERROR could create duplicate aggregate"
+    exit -1
+fi
+
+
+# Test aggregate-update (and aggregate-details)
+# =============================================
+AGGREGATE_NEW_NAME=test_aggregate_$RANDOM
+
+nova aggregate-update $AGGREGATE_ID $AGGREGATE_NEW_NAME
+nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NEW_NAME
+nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE
+
+nova aggregate-update $AGGREGATE_ID $AGGREGATE_NAME $AGGREGATE_A_ZONE
+nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_NAME
+nova aggregate-details $AGGREGATE_ID | grep $AGGREGATE_A_ZONE
+
+
+# Test aggregate-set-metadata
+# ===========================
+META_DATA_1_KEY=asdf
+META_DATA_2_KEY=foo
+META_DATA_3_KEY=bar
+
+#ensure no metadata is set
+nova aggregate-details $AGGREGATE_ID | grep {}
+
+nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_1_KEY}=123
+nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY
+nova aggregate-details $AGGREGATE_ID | grep 123
+
+nova aggregate-set-metadata $AGGREGATE_ID ${META_DATA_2_KEY}=456
+nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY
+nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY
+
+nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_2_KEY ${META_DATA_3_KEY}=789
+nova aggregate-details $AGGREGATE_ID | grep $META_DATA_1_KEY
+nova aggregate-details $AGGREGATE_ID | grep $META_DATA_3_KEY
+
+nova aggregate-details $AGGREGATE_ID | grep $META_DATA_2_KEY && die "ERROR metadata was not cleared"
+
+nova aggregate-set-metadata $AGGREGATE_ID $META_DATA_3_KEY $META_DATA_1_KEY
+nova aggregate-details $AGGREGATE_ID | grep {}
+
+
+# Test aggregate-add/remove-host
+# ==============================
+if [ "$VIRT_DRIVER" == "xenserver" ]; then
+    echo "TODO(johngarbutt) add tests for add/remove host from aggregate"
+fi
+
+
+# Test aggregate-delete
+# =====================
+nova aggregate-delete $AGGREGATE_ID
+exit_if_aggregate_present $AGGREGATE_NAME
+
+
+# Test complete
+# =============
+OS_USERNAME=$_OLD_USERNAME
+echo "AGGREGATE TEST PASSED"
+
+set +o xtrace
+echo "**************************************************"
+echo "End DevStack Exercise: $0"
+echo "**************************************************"
diff --git a/stack.sh b/stack.sh
index fd2daaf..430710c 100755
--- a/stack.sh
+++ b/stack.sh
@@ -1637,17 +1637,8 @@
     TOKEN=`curl -s -d  "{\"auth\":{\"passwordCredentials\": {\"username\": \"$ADMIN_USER\", \"password\": \"$ADMIN_PASSWORD\"}, \"tenantName\": \"$ADMIN_TENANT\"}}" -H "Content-type: application/json" http://$HOST_IP:5000/v2.0/tokens | python -c "import sys; import json; tok = json.loads(sys.stdin.read()); print tok['access']['token']['id'];"`
 
     # Option to upload legacy ami-tty, which works with xenserver
-    if [ $UPLOAD_LEGACY_TTY ]; then
-        if [ ! -f $FILES/tty.tgz ]; then
-            wget -c http://images.ansolabs.com/tty.tgz -O $FILES/tty.tgz
-        fi
-
-        tar -zxf $FILES/tty.tgz -C $FILES/images
-        RVAL=`glance add --silent-upload -A $TOKEN name="tty-kernel" is_public=true container_format=aki disk_format=aki < $FILES/images/aki-tty/image`
-        KERNEL_ID=`echo $RVAL | cut -d":" -f2 | tr -d " "`
-        RVAL=`glance add --silent-upload -A $TOKEN name="tty-ramdisk" is_public=true container_format=ari disk_format=ari < $FILES/images/ari-tty/image`
-        RAMDISK_ID=`echo $RVAL | cut -d":" -f2 | tr -d " "`
-        glance add -A $TOKEN name="tty" is_public=true container_format=ami disk_format=ami kernel_id=$KERNEL_ID ramdisk_id=$RAMDISK_ID < $FILES/images/ami-tty/image
+    if [[ -n "$UPLOAD_LEGACY_TTY" ]]; then
+        IMAGE_URLS="${IMAGE_URLS:+${IMAGE_URLS},}http://images.ansolabs.com/tty.tgz"
     fi
 
     for image_url in ${IMAGE_URLS//,/ }; do
@@ -1659,6 +1650,8 @@
 
         KERNEL=""
         RAMDISK=""
+        DISK_FORMAT=""
+        CONTAINER_FORMAT=""
         case "$IMAGE_FNAME" in
             *.tar.gz|*.tgz)
                 # Extract ami and aki files
@@ -1669,39 +1662,54 @@
                 rm -Rf "$xdir";
                 mkdir "$xdir"
                 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
-                KERNEL=$(for f in "$xdir/"*-vmlinuz*; do
+                KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
                          [ -f "$f" ] && echo "$f" && break; done; true)
-                RAMDISK=$(for f in "$xdir/"*-initrd*; do
+                RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
                          [ -f "$f" ] && echo "$f" && break; done; true)
-                IMAGE=$(for f in "$xdir/"*.img; do
+                IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
                          [ -f "$f" ] && echo "$f" && break; done; true)
-                [ -n "$IMAGE_NAME" ]
-                IMAGE_NAME=$(basename "$IMAGE" ".img")
+                if [[ -z "$IMAGE_NAME" ]]; then
+                    IMAGE_NAME=$(basename "$IMAGE" ".img")
+                fi
                 ;;
             *.img)
                 IMAGE="$FILES/$IMAGE_FNAME";
                 IMAGE_NAME=$(basename "$IMAGE" ".img")
+                DISK_FORMAT=raw
+                CONTAINER_FORMAT=bare
                 ;;
             *.img.gz)
                 IMAGE="$FILES/${IMAGE_FNAME}"
                 IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
+                DISK_FORMAT=raw
+                CONTAINER_FORMAT=bare
+                ;;
+            *.qcow2)
+                IMAGE="$FILES/${IMAGE_FNAME}"
+                IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
+                DISK_FORMAT=qcow2
+                CONTAINER_FORMAT=bare
                 ;;
             *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
         esac
 
-        # Use glance client to add the kernel the root filesystem.
-        # We parse the results of the first upload to get the glance ID of the
-        # kernel for use when uploading the root filesystem.
-        KERNEL_ID=""; RAMDISK_ID="";
-        if [ -n "$KERNEL" ]; then
-            RVAL=`glance add --silent-upload -A $TOKEN name="$IMAGE_NAME-kernel" is_public=true container_format=aki disk_format=aki < "$KERNEL"`
-            KERNEL_ID=`echo $RVAL | cut -d":" -f2 | tr -d " "`
+        if [ "$CONTAINER_FORMAT" = "bare" ]; then
+            glance add --silent-upload -A $TOKEN name="$IMAGE_NAME" is_public=true container_format=$CONTAINER_FORMAT disk_format=$DISK_FORMAT < <(zcat --force "${IMAGE}")
+        else
+            # Use glance client to add the kernel the root filesystem.
+            # We parse the results of the first upload to get the glance ID of the
+            # kernel for use when uploading the root filesystem.
+            KERNEL_ID=""; RAMDISK_ID="";
+            if [ -n "$KERNEL" ]; then
+                RVAL=`glance add --silent-upload -A $TOKEN name="$IMAGE_NAME-kernel" is_public=true container_format=aki disk_format=aki < "$KERNEL"`
+                KERNEL_ID=`echo $RVAL | cut -d":" -f2 | tr -d " "`
+            fi
+            if [ -n "$RAMDISK" ]; then
+                RVAL=`glance add --silent-upload -A $TOKEN name="$IMAGE_NAME-ramdisk" is_public=true container_format=ari disk_format=ari < "$RAMDISK"`
+                RAMDISK_ID=`echo $RVAL | cut -d":" -f2 | tr -d " "`
+            fi
+            glance add -A $TOKEN name="${IMAGE_NAME%.img}" is_public=true container_format=ami disk_format=ami ${KERNEL_ID:+kernel_id=$KERNEL_ID} ${RAMDISK_ID:+ramdisk_id=$RAMDISK_ID} < <(zcat --force "${IMAGE}")
         fi
-        if [ -n "$RAMDISK" ]; then
-            RVAL=`glance add --silent-upload -A $TOKEN name="$IMAGE_NAME-ramdisk" is_public=true container_format=ari disk_format=ari < "$RAMDISK"`
-            RAMDISK_ID=`echo $RVAL | cut -d":" -f2 | tr -d " "`
-        fi
-        glance add -A $TOKEN name="${IMAGE_NAME%.img}" is_public=true container_format=ami disk_format=ami ${KERNEL_ID:+kernel_id=$KERNEL_ID} ${RAMDISK_ID:+ramdisk_id=$RAMDISK_ID} < <(zcat --force "${IMAGE}")
     done
 fi