From c0c6f00698ca88f3c88f90ba211b234096eb3ac6 Mon Sep 17 00:00:00 2001
From: Devananda van der Veen <devananda.vdv@gmail.com>
Date: Fri, 6 Jul 2012 17:49:12 -0700
Subject: [PATCH] enable testing of openvz guests

This patch adds several options to allow using OpenVZ virt layer.
- modifies stack.sh and stackrc to recognize a new VIRT_TYPE option
- set IMAGE_URLS to an openvz image, if VIRT_TYPE == openvz

It also makes a few changes to some tests so that some implicit
defaults (such as the guest user account) can be overridden.

Change-Id: I0dde2dffbf3848fac1dd27eb37af84c0ac73d9aa
---
 exercises/boot_from_volume.sh | 10 ++++----
 exercises/euca.sh             |  5 +++-
 exercises/floating_ips.sh     |  2 +-
 stack.sh                      | 21 ++++++++++++-----
 stackrc                       | 43 +++++++++++++++++++++++++----------
 tools/configure_tempest.sh    | 42 ++++++++++++++++++++++++++++------
 6 files changed, 92 insertions(+), 31 deletions(-)

diff --git a/exercises/boot_from_volume.sh b/exercises/boot_from_volume.sh
index 6a0937ab15..7fe81ba0b4 100755
--- a/exercises/boot_from_volume.sh
+++ b/exercises/boot_from_volume.sh
@@ -46,6 +46,8 @@ DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 # Default floating IP pool name
 DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
 
+# Default user
+DEFAULT_INSTANCE_USER=${DEFAULT_INSTANCE_USER:-cirros}
 
 # Launching servers
 # =================
@@ -150,7 +152,7 @@ fi
 # To do this, ssh to the builder instance, mount volume, and build a volume-backed image.
 STAGING_DIR=/tmp/stage
 CIRROS_DIR=/tmp/cirros
-ssh -o StrictHostKeyChecking=no -i $KEY_FILE cirros@$FLOATING_IP << EOF
+ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
 set -o errexit
 set -o xtrace
 sudo mkdir -p $STAGING_DIR
@@ -168,10 +170,10 @@ if [ ! -e cirros-0.3.0-x86_64-rootfs.img.gz ]; then
 fi
 
 # Copy cirros onto the volume
-scp -o StrictHostKeyChecking=no -i $KEY_FILE cirros-0.3.0-x86_64-rootfs.img.gz cirros@$FLOATING_IP:$STAGING_DIR
+scp -o StrictHostKeyChecking=no -i $KEY_FILE cirros-0.3.0-x86_64-rootfs.img.gz ${DEFAULT_INSTANCE_USER}@$FLOATING_IP:$STAGING_DIR
 
 # Unpack cirros into volume
-ssh -o StrictHostKeyChecking=no -i $KEY_FILE cirros@$FLOATING_IP << EOF
+ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
 set -o errexit
 set -o xtrace
 cd $STAGING_DIR
@@ -221,7 +223,7 @@ if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sle
 fi
 
 # Make sure our volume-backed instance launched
-ssh -o StrictHostKeyChecking=no -i $KEY_FILE cirros@$FLOATING_IP << EOF
+ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP << EOF
 echo "success!"
 EOF
 
diff --git a/exercises/euca.sh b/exercises/euca.sh
index 4a538c6359..9f7aed171f 100755
--- a/exercises/euca.sh
+++ b/exercises/euca.sh
@@ -40,12 +40,15 @@ source $TOP_DIR/exerciserc
 # Instance type to create
 DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 
+# Boot this image, use first AMI-format image if unset
+DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
+
 
 # Launching a server
 # ==================
 
 # Find a machine image to boot
-IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1`
+IMAGE=`euca-describe-images | grep machine | grep ${DEFAULT_IMAGE_NAME} | cut -f2 | head -n1`
 
 # Define secgroup
 SECGROUP=euca_secgroup
diff --git a/exercises/floating_ips.sh b/exercises/floating_ips.sh
index 51019a3432..02259c08dc 100755
--- a/exercises/floating_ips.sh
+++ b/exercises/floating_ips.sh
@@ -185,7 +185,7 @@ fi
 nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0 || die "Failure deleting security group rule from $SECGROUP"
 
 # FIXME (anthony): make xs support security groups
-if [ "$VIRT_DRIVER" != "xenserver" ]; then
+if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then
     # test we can aren't able to 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
         print "Security group failure - ping should not be allowed!"
diff --git a/stack.sh b/stack.sh
index 3827d77614..6b01bad78a 100755
--- a/stack.sh
+++ b/stack.sh
@@ -276,12 +276,6 @@ VOLUME_GROUP=${VOLUME_GROUP:-stack-volumes}
 VOLUME_NAME_PREFIX=${VOLUME_NAME_PREFIX:-volume-}
 INSTANCE_NAME_PREFIX=${INSTANCE_NAME_PREFIX:-instance-}
 
-# Nova hypervisor configuration.  We default to libvirt with **kvm** but will
-# drop back to **qemu** if we are unable to load the kvm module.  ``stack.sh`` can
-# also install an **LXC** based system.
-VIRT_DRIVER=${VIRT_DRIVER:-libvirt}
-LIBVIRT_TYPE=${LIBVIRT_TYPE:-kvm}
-
 # Nova supports pluggable schedulers.  ``FilterScheduler`` should work in most
 # cases.
 SCHEDULER=${SCHEDULER:-nova.scheduler.filter_scheduler.FilterScheduler}
@@ -1957,6 +1951,13 @@ if [ "$VIRT_DRIVER" = 'xenserver' ]; then
     # Need to avoid crash due to new firewall support
     XEN_FIREWALL_DRIVER=${XEN_FIREWALL_DRIVER:-"nova.virt.firewall.IptablesFirewallDriver"}
     add_nova_opt "firewall_driver=$XEN_FIREWALL_DRIVER"
+elif [ "$VIRT_DRIVER" = 'openvz' ]; then
+    # TODO(deva): OpenVZ driver does not yet work if compute_driver is set here.
+    #             Replace connection_type when this is fixed.
+    #             add_nova_opt "compute_driver=openvz.connection.OpenVzConnection"
+    add_nova_opt "connection_type=openvz"
+    LIBVIRT_FIREWALL_DRIVER=${LIBVIRT_FIREWALL_DRIVER:-"nova.virt.libvirt.firewall.IptablesFirewallDriver"}
+    add_nova_opt "firewall_driver=$LIBVIRT_FIREWALL_DRIVER"
 else
     add_nova_opt "compute_driver=libvirt.LibvirtDriver"
     LIBVIRT_FIREWALL_DRIVER=${LIBVIRT_FIREWALL_DRIVER:-"nova.virt.libvirt.firewall.IptablesFirewallDriver"}
@@ -2212,6 +2213,14 @@ if is_service_enabled g-reg; then
             wget -c $image_url -O $FILES/$IMAGE_FNAME
         fi
 
+        # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
+        if [[ "$image_url" =~ 'openvz' ]]; then
+            IMAGE="$FILES/${IMAGE_FNAME}"
+            IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
+            glance --os-auth-token $TOKEN --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format ami --disk-format ami < "$IMAGE"
+            continue
+        fi
+
         KERNEL=""
         RAMDISK=""
         DISK_FORMAT=""
diff --git a/stackrc b/stackrc
index 3bbc4755dc..bd4fe14c73 100644
--- a/stackrc
+++ b/stackrc
@@ -99,6 +99,17 @@ MELANGE_BRANCH=master
 MELANGECLIENT_REPO=${GIT_BASE}/openstack/python-melangeclient.git
 MELANGECLIENT_BRANCH=master
 
+# Nova hypervisor configuration.  We default to libvirt with **kvm** but will
+# drop back to **qemu** if we are unable to load the kvm module.  ``stack.sh`` can
+# also install an **LXC** or **OpenVZ** based system.
+VIRT_DRIVER=${VIRT_DRIVER:-libvirt}
+LIBVIRT_TYPE=${LIBVIRT_TYPE:-kvm}
+
+# allow local overrides of env variables
+if [ -f $RC_DIR/localrc ]; then
+    source $RC_DIR/localrc
+fi
+
 # Specify a comma-separated list of uec images to download and install into glance.
 # supported urls here are:
 #  * "uec-style" images:
@@ -114,19 +125,27 @@ MELANGECLIENT_BRANCH=master
 #      http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-rootfs.img.gz
 #IMAGE_URLS="http://smoser.brickies.net/ubuntu/ttylinux-uec/ttylinux-uec-amd64-11.2_2.6.35-15_1.tar.gz" # old ttylinux-uec image
 #IMAGE_URLS="http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img" # cirros full disk image
-case "$LIBVIRT_TYPE" in
-    lxc) # the cirros root disk in the uec tarball is empty, so it will not work for lxc
-        DEFAULT_IMAGE_NAME=cirros-0.3.0-x86_64-rootfs
-        IMAGE_URLS="http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-rootfs.img.gz";;
-    *)  # otherwise, use the uec style image (with kernel, ramdisk, disk)
-        DEFAULT_IMAGE_NAME=cirros-0.3.0-x86_64-uec
-        IMAGE_URLS="http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-uec.tar.gz";;
+#
+# Set default image based on LIBVIRT_TYPE or VIRT_DRIVER, which may be set in localrc
+# but allow DEFAULT_IMAGE_NAME and IMAGE_URLS to be set directly in localrc, too.
+case "$VIRT_DRIVER" in
+    openvz) # OpenVZ uses its own format of image, and does not support uec style images
+        DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ubuntu-11.10-x86_64}
+        IMAGE_URLS=${IMAGE_URLS:-"http://download.openvz.org/template/precreated/ubuntu-11.10-x86_64.tar.gz"};;
+    libvirt)
+        case "$LIBVIRT_TYPE" in
+            lxc) # the cirros root disk in the uec tarball is empty, so it will not work for lxc
+                DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-cirros-0.3.0-x86_64-rootfs}
+                IMAGE_URLS=${IMAGE_URLS:-"http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-rootfs.img.gz"};;
+            *) # otherwise, use the uec style image (with kernel, ramdisk, disk)
+                DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-cirros-0.3.0-x86_64-uec}
+                IMAGE_URLS=${IMAGE_URLS:-"http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-uec.tar.gz"};;
+        esac
+        ;;
+    *) # otherwise, use the uec style image (with kernel, ramdisk, disk)
+        DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-cirros-0.3.0-x86_64-uec}
+        IMAGE_URLS=${IMAGE_URLS:-"http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-uec.tar.gz"};;
 esac
 
-# allow local overrides of env variables
-if [ -f $RC_DIR/localrc ]; then
-    source $RC_DIR/localrc
-fi
-
 # 5Gb default volume backing file size
 VOLUME_BACKING_FILE_SIZE=${VOLUME_BACKING_FILE_SIZE:-5130M}
diff --git a/tools/configure_tempest.sh b/tools/configure_tempest.sh
index 4d029d841e..5be709aa51 100755
--- a/tools/configure_tempest.sh
+++ b/tools/configure_tempest.sh
@@ -67,15 +67,20 @@ fi
 # Glance should already contain images to be used in tempest
 # testing. Here we simply look for images stored in Glance
 # and set the appropriate variables for use in the tempest config
-# We ignore ramdisk and kernel images and set the IMAGE_UUID to
-# the first image returned and set IMAGE_UUID_ALT to the second,
+# We ignore ramdisk and kernel images, look for the default image
+# DEFAULT_IMAGE_NAME. If not found, we set the IMAGE_UUID to the
+# first image returned and set IMAGE_UUID_ALT to the second,
 # if there is more than one returned...
 # ... Also ensure we only take active images, so we don't get snapshots in process
 IMAGE_LINES=`glance image-list`
 IFS="$(echo -e "\n\r")"
 IMAGES=""
 for line in $IMAGE_LINES; do
-    IMAGES="$IMAGES `echo $line | grep -v "^\(ID\|+--\)" | grep -v "\(aki\|ari\)" | grep 'active' | cut -d' ' -f2`"
+    if [ -z $DEFAULT_IMAGE_NAME ]; then
+        IMAGES="$IMAGES `echo $line | grep -v "^\(ID\|+--\)" | grep -v "\(aki\|ari\)" | grep 'active' | cut -d' ' -f2`"
+    else
+        IMAGES="$IMAGES `echo $line | grep -v "^\(ID\|+--\)" | grep -v "\(aki\|ari\)" | grep 'active' | grep "$DEFAULT_IMAGE_NAME" | cut -d' ' -f2`"
+    fi
 done
 # Create array of image UUIDs...
 IFS=" "
@@ -127,9 +132,31 @@ ALT_USERNAME=${ALT_USERNAME:-alt_demo}
 ALT_TENANT_NAME=${ALT_TENANT_NAME:-alt_demo}
 ALT_PASSWORD=$OS_PASSWORD
 
-# TODO(jaypipes): Support configurable flavor refs here...
-FLAVOR_REF=1
-FLAVOR_REF_ALT=2
+# Check Nova for existing flavors and, if set, look for the
+# DEFAULT_INSTANCE_TYPE and use that. Otherwise, just use the first flavor.
+FLAVOR_LINES=`nova flavor-list`
+IFS="$(echo -e "\n\r")"
+FLAVORS=""
+for line in $FLAVOR_LINES; do
+    if [ -z $DEFAULT_INSTANCE_TYPE ]; then
+        FLAVORS="$FLAVORS `echo $line | grep -v "^\(ID\|+--\)" | cut -d' ' -f2`"
+    else
+        FLAVORS="$FLAVORS `echo $line | grep -v "^\(ID\|+--\)" | grep "$DEFAULT_INSTANCE_TYPE" | cut -d' ' -f2`"
+    fi
+done
+IFS=" "
+FLAVORS=($FLAVORS)
+NUM_FLAVORS=${#FLAVORS[*]}
+echo "Found $NUM_FLAVORS flavors"
+if [[ $NUM_FLAVORS -eq 0 ]]; then
+    echo "Found no valid flavors to use!"
+    exit 1
+fi
+FLAVOR_REF=${FLAVORS[0]}
+FLAVOR_REF_ALT=$FLAVOR_REF
+if [[ $NUM_FLAVORS -gt 1 ]]; then
+    FLAVOR_REF_ALT=${FLAVORS[1]}
+fi
 
 # Do any of the following need to be configurable?
 COMPUTE_CATALOG_TYPE=compute
@@ -141,7 +168,8 @@ COMPUTE_LOG_LEVEL=ERROR
 BUILD_INTERVAL=3
 BUILD_TIMEOUT=400
 RUN_SSH=True
-SSH_USER=$OS_USERNAME
+# Check for DEFAULT_INSTANCE_USER and try to connect with that account
+SSH_USER=${DEFAULT_INSTANCE_USER:-$OS_USERNAME}
 NETWORK_FOR_SSH=private
 IP_VERSION_FOR_SSH=4
 SSH_TIMEOUT=4