Merge pull request #102 from cloudbuilders/cache_images_kvm

Cache images kvm
diff --git a/stack.sh b/stack.sh
index b093616..14bcfb2 100755
--- a/stack.sh
+++ b/stack.sh
@@ -302,20 +302,31 @@
 # be owned by the installation user, we create the directory and change the
 # ownership to the proper user.
 function git_clone {
-    # if there is an existing checkout, move it out of the way
-    if [[ "$RECLONE" == "yes" ]]; then
-        # FIXME(ja): if we were smarter we could speed up RECLONE by
-        # using the old git repo as the basis of our new clone...
-        if [ -d $2 ]; then
-            mv $2 /tmp/stack.`date +%s`
-        fi
-    fi
 
-    if [ ! -d $2 ]; then
-        git clone $1 $2
+    GIT_REMOTE=$1
+    GIT_DEST=$2
+    GIT_BRANCH=$3
+
+    # do a full clone only if the directory doesn't exist
+    if [ ! -d $GIT_DEST ]; then
+        git clone $GIT_REMOTE $GIT_DEST
         cd $2
         # This checkout syntax works for both branches and tags
-        git checkout $3
+        git checkout $GIT_BRANCH
+    elif [[ "$RECLONE" == "yes" ]]; then
+        # if it does exist then simulate what clone does if asked to RECLONE
+        cd $GIT_DEST
+        # set the url to pull from and fetch
+        git remote set-url origin $GIT_REMOTE
+        git fetch origin
+        # remove the existing ignored files (like pyc) as they cause breakage
+        # (due to the py files having older timestamps than our pyc, so python
+        # thinks the pyc files are correct using them)
+        sudo git clean -f -d
+        git checkout -f origin/$GIT_BRANCH
+        # a local branch might not exist
+        git branch -D $GIT_BRANCH || true
+        git checkout -b $GIT_BRANCH
     fi
 }
 
@@ -470,11 +481,15 @@
 # Nova
 # ----
 
-# We are going to use the sample http middleware configuration from the keystone
-# project to launch nova.  This paste config adds the configuration required
-# for nova to validate keystone tokens - except we need to switch the config
-# to use our service token instead (instead of the invalid token 999888777666).
-sudo sed -e "s,999888777666,$SERVICE_TOKEN,g" -i $KEYSTONE_DIR/examples/paste/nova-api-paste.ini
+if [[ "$ENABLED_SERVICES" =~ "n-api" ]]; then
+    # We are going to use the sample http middleware configuration from the
+    # keystone project to launch nova.  This paste config adds the configuration
+    # required for nova to validate keystone tokens - except we need to switch
+    # the config to use our service token instead (instead of the invalid token
+    # 999888777666).
+    cp $KEYSTONE_DIR/examples/paste/nova-api-paste.ini $NOVA_DIR/bin
+    sed -e "s,999888777666,$SERVICE_TOKEN,g" -i $NOVA_DIR/bin/nova-api-paste.ini
+fi
 
 if [[ "$ENABLED_SERVICES" =~ "n-cpu" ]]; then
 
@@ -591,7 +606,7 @@
 add_nova_flag "--osapi_extensions_path=$OPENSTACKX_DIR/extensions"
 add_nova_flag "--vncproxy_url=http://$HOST_IP:6080"
 add_nova_flag "--vncproxy_wwwroot=$NOVNC_DIR/"
-add_nova_flag "--api_paste_config=$KEYSTONE_DIR/examples/paste/nova-api-paste.ini"
+add_nova_flag "--api_paste_config=$NOVA_DIR/bin/nova-api-paste.ini"
 add_nova_flag "--image_service=nova.image.glance.GlanceImageService"
 add_nova_flag "--ec2_dmz_host=$EC2_DMZ_HOST"
 add_nova_flag "--rabbit_host=$RABBIT_HOST"
diff --git a/tools/get_uec_image.sh b/tools/get_uec_image.sh
new file mode 100755
index 0000000..e9d708d
--- /dev/null
+++ b/tools/get_uec_image.sh
@@ -0,0 +1,159 @@
+#!/bin/bash
+# get_uec_image.sh - Prepare Ubuntu images in various formats
+#
+# Supported formats: qcow (kvm), vmdk (vmserver), vdi (vbox), vhd (vpc), raw
+#
+# Required to run as root
+
+CACHEDIR=${CACHEDIR:-/var/cache/devstack}
+FORMAT=${FORMAT:-qcow2}
+ROOTSIZE=${ROOTSIZE:-2000}
+MIN_PKGS=${MIN_PKGS:-"apt-utils gpgv openssh-server"}
+
+usage() {
+    echo "Usage: $0 - Prepare Ubuntu images"
+    echo ""
+    echo "$0 [-f format] [-r rootsize] release imagefile"
+    echo ""
+    echo "-f format - image format: qcow2 (default), vmdk, vdi, vhd, xen, raw, fs"
+    echo "-r size   - root fs size in MB (min 2000MB)"
+    echo "release   - Ubuntu release: jaunty - oneric"
+    echo "imagefile - output image file"
+    exit 1
+}
+
+while getopts f:hmr: c; do
+    case $c in
+        f)  FORMAT=$OPTARG
+            ;;
+        h)  usage
+            ;;
+        m)  MINIMAL=1
+            ;;
+        r)  ROOTSIZE=$OPTARG
+            if $(( ROOTSIZE < 2000 )); then
+                echo "root size must be greater than 2000MB"
+                exit 1
+            fi
+            ;;
+    esac
+done
+shift `expr $OPTIND - 1`
+
+if [ ! "$#" -eq "2" ]; then
+    usage
+fi
+
+# Default args
+DIST_NAME=$1
+IMG_FILE=$2
+
+case $FORMAT in
+    kvm|qcow2)  FORMAT=qcow2
+                QFORMAT=qcow2
+                ;;
+    vmserver|vmdk)
+                FORMAT=vmdk
+                QFORMAT=vmdk
+                ;;
+    vbox|vdi)   FORMAT=vdi
+                QFORMAT=vdi
+                ;;
+    vhd|vpc)    FORMAT=vhd
+                QFORMAT=vpc
+                ;;
+    xen)        FORMAT=raw
+                QFORMAT=raw
+                ;;
+    raw)        FORMAT=raw
+                QFORMAT=raw
+                ;;
+    *)          echo "Unknown format: $FORMAT"
+                usage
+esac
+
+case $DIST_NAME in
+    oneiric)    ;;
+    natty)      ;;
+    maverick)   ;;
+    lucid)      ;;
+    karmic)     ;;
+    jaunty)     ;;
+    *)          echo "Unknown release: $DIST_NAME"
+                usage
+                ;;
+esac
+
+# Set up nbd
+modprobe nbd max_part=63
+NBD=${NBD:-/dev/nbd9}
+NBD_DEV=`basename $NBD`
+
+# Prepare the base image
+
+# Get the UEC image
+UEC_NAME=$DIST_NAME-server-cloudimg-amd64
+if [ ! -e $CACHEDIR/$UEC_NAME-disk1.img ]; then
+    (cd $CACHEDIR; wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME-disk1.img)
+
+
+    # Connect to nbd and wait till it is ready
+    qemu-nbd -d $NBD
+    qemu-nbd -c $NBD $CACHEDIR/$UEC_NAME-disk1.img
+    if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then
+        echo "Couldn't connect $NBD"
+        exit 1
+    fi
+    MNTDIR=`mktemp -d mntXXXXXXXX`
+    mount -t ext4 ${NBD}p1 $MNTDIR
+
+    # Install our required packages
+    cp -p files/sources.list $MNTDIR/etc/apt/sources.list
+    cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
+    chroot $MNTDIR apt-get update
+    chroot $MNTDIR apt-get install -y $MIN_PKGS
+    rm -f $MNTDIR/etc/resolv.conf
+
+    umount $MNTDIR
+    rmdir $MNTDIR
+    qemu-nbd -d $NBD
+fi
+
+if [ "$FORMAT" = "qcow2" ]; then
+    # Just copy image
+    cp -p $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE
+else
+    # Convert image
+    qemu-img convert -O $QFORMAT $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE
+fi
+
+# Resize the image if necessary
+if [ $ROOTSIZE -gt 2000 ]; then
+    # Resize the container
+    qemu-img resize $IMG_FILE +$((ROOTSIZE - 2000))M
+
+    # Connect to nbd and wait till it is ready
+    qemu-nbd -c $NBD $IMG_FILE
+    if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then
+        echo "Couldn't connect $NBD"
+        exit 1
+    fi
+
+    # Resize partition 1 to full size of the disk image
+    echo "d
+n
+p
+1
+2
+
+t
+83
+a
+1
+w
+" | fdisk $NBD
+    fsck -t ext4 -f ${NBD}p1
+    resize2fs ${NBD}p1
+
+    qemu-nbd -d $NBD
+fi
diff --git a/tools/make_image.sh b/tools/make_image.sh
index d81cef5..e957c75 100755
--- a/tools/make_image.sh
+++ b/tools/make_image.sh
@@ -65,6 +65,13 @@
     RELEASE="pass"
 fi
 
+# Make sure that we have the proper version of ubuntu
+UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
+if [ "$UBUNTU_VERSION" = "natty" -a "$RELEASE" = "oneiric" ]; then
+    echo "natty installs can't build oneiric images"
+    exit 1
+fi
+
 case $FORMAT in
     kvm|qcow2)  FORMAT=qcow2
                 QFORMAT=qcow2