Merge pull request #47 from dtroyer/master

Updates to build_nfs.sh and build_pxe_ramdisk.sh for common functions
diff --git a/build_nfs.sh b/build_nfs.sh
index 6434df4..651bae2 100755
--- a/build_nfs.sh
+++ b/build_nfs.sh
@@ -1,70 +1,117 @@
 #!/bin/bash
 
+PROGDIR=`dirname $0`
+CHROOTCACHE=${CHROOTCACHE:-/var/cache/devstack}
+
 # Source params
 source ./stackrc
 
-# TODO: make dest not hardcoded
+# Store cwd
+CWD=`pwd`
 
 NAME=$1
-DEST="/nfs/$NAME"
+NFSDIR="/nfs/$NAME"
+DEST=${DEST:-/opt/stack}
+
+# Option to use the version of devstack on which we are currently working
+USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
 
 # remove old nfs filesystem if one exists
 rm -rf $DEST
 
-# build a proto image - natty + packages that will install (optimization)
-if [ ! -d proto ]; then
-    debootstrap natty proto
-    cp files/sources.list proto/etc/apt/sources.list
-    chroot proto apt-get update
-    chroot proto apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
-    chroot proto pip install `cat files/pips/*`
-    git_clone $NOVA_REPO proto/opt/nova $NOVA_BRANCH
-    git_clone $GLANCE_REPO proto/opt/glance $GLANCE_BRANCH
-    git_clone $KEYSTONE_REPO proto/opt/keystone $KEYSTONE_BRANCH
-    git_clone $NOVNC_REPO proto/opt/novnc $NOVNC_BRANCH
-    git_clone $DASH_REPO proto/opt/dash $DASH_BRANCH $DASH_TAG
-    git_clone $NOVACLIENT_REPO proto/opt/python-novaclient $NOVACLIENT_BRANCH
-    git_clone $OPENSTACKX_REPO proto/opt/openstackx $OPENSTACKX_BRANCH
-    chroot proto mkdir -p /opt/files
-    wget -c http://images.ansolabs.com/tty.tgz -O proto/opt/files/tty.tgz
+# clean install of natty
+if [ ! -d $CHROOTCACHE/natty-base ]; then
+    $PROGDIR/make_image.sh -C natty $CHROOTCACHE/natty-base
+    # copy kernel modules...  
+    # NOTE(ja): is there a better way to do this?
+    cp -pr /lib/modules/`uname -r` $CHROOTCACHE/natty-base/lib/modules
+    # a simple password - pass
+    echo root:pass | chroot $CHROOTCACHE/natty-base chpasswd
 fi
 
-cp -pr proto $DEST
+# prime natty with as many apt/pips as we can
+if [ ! -d $CHROOTCACHE/natty-dev ]; then
+    rsync -azH $CHROOTCACHE/natty-base/ $CHROOTCACHE/natty-dev/
+    chroot $CHROOTCACHE/natty-dev apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
+    chroot $CHROOTCACHE/natty-dev pip install `cat files/pips/*`
+
+    # Create a stack user that is a member of the libvirtd group so that stack 
+    # is able to interact with libvirt.
+    chroot $CHROOTCACHE/natty-dev groupadd libvirtd
+    chroot $CHROOTCACHE/natty-dev useradd stack -s /bin/bash -d $DEST -G libvirtd
+    mkdir -p $CHROOTCACHE/natty-dev/$DEST
+    chown stack $CHROOTCACHE/natty-dev/$DEST
+
+    # a simple password - pass
+    echo stack:pass | chroot $CHROOTCACHE/natty-dev chpasswd
+
+    # and has sudo ability (in the future this should be limited to only what 
+    # stack requires)
+    echo "stack ALL=(ALL) NOPASSWD: ALL" >> $CHROOTCACHE/natty-dev/etc/sudoers
+fi
+
+# clone git repositories onto the system
+# ======================================
+
+if [ ! -d $CHROOTCACHE/natty-stack ]; then
+    rsync -azH $CHROOTCACHE/natty-dev/ $CHROOTCACHE/natty-stack/
+fi
+
+# git clone only if directory doesn't exist already.  Since ``DEST`` might not
+# be owned by the installation user, we create the directory and change the
+# ownership to the proper user.
+function git_clone {
+
+    # clone new copy or fetch latest changes
+    CHECKOUT=$CHROOTCACHE/natty-stack$2
+    if [ ! -d $CHECKOUT ]; then
+        mkdir -p $CHECKOUT
+        git clone $1 $CHECKOUT
+    else
+        pushd $CHECKOUT
+        git fetch
+        popd
+    fi
+
+    # FIXME(ja): checkout specified version (should works for branches and tags)
+
+    pushd $CHECKOUT
+    # checkout the proper branch/tag
+    git checkout $3
+    # force our local version to be the same as the remote version
+    git reset --hard origin/$3
+    popd
+
+    # give ownership to the stack user
+    chroot $CHROOTCACHE/natty-stack/ chown -R stack $2
+}
+
+git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
+git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
+git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
+git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH
+git_clone $DASH_REPO $DEST/dash $DASH_BRANCH $DASH_TAG
+git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH
+git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
+
+chroot $CHROOTCACHE/natty-stack mkdir -p $DEST/files
+wget -c http://images.ansolabs.com/tty.tgz -O $CHROOTCACHE/natty-stack$DEST/files/tty.tgz
+
+# Use this version of devstack?
+if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
+    rm -rf $CHROOTCACHE/natty-stack/$DEST/devstack
+    cp -pr $CWD $CHROOTCACHE/natty-stack/$DEST/devstack
+fi
+
+cp -pr $CHROOTCACHE/natty-stack $NFSDIR
 
 # set hostname
-echo $NAME > $DEST/etc/hostname
-echo "127.0.0.1 localhost $NAME" > $DEST/etc/hosts
-
-# copy kernel modules
-cp -pr /lib/modules/`uname -r` $DEST/lib/modules
-
-
-# copy openstack installer and requirement lists to a new directory.
-mkdir -p $DEST/opt
-
-# inject stack.sh and dependant files
-cp -r files $DEST/opt/files
-cp stack.sh $DEST/opt/stack.sh
+echo $NAME > $NFSDIR/etc/hostname
+echo "127.0.0.1 localhost $NAME" > $NFSDIR/etc/hosts
 
 # injecting root's public ssh key if it exists
 if [ -f /root/.ssh/id_rsa.pub ]; then
-    mkdir $DEST/root/.ssh
-    chmod 700 $DEST/root/.ssh
-    cp /root/.ssh/id_rsa.pub $DEST/root/.ssh/authorized_keys
+    mkdir $NFSDIR/root/.ssh
+    chmod 700 $NFSDIR/root/.ssh
+    cp /root/.ssh/id_rsa.pub $NFSDIR/root/.ssh/authorized_keys
 fi
-
-# set root password to password
-echo root:pass | chroot $DEST chpasswd
-
-# Create a stack user that is a member of the libvirtd group so that stack 
-# is able to interact with libvirt.
-chroot $DEST groupadd libvirtd
-chroot $DEST useradd stack -s /bin/bash -d /opt -G libvirtd
-# a simple password - pass
-echo stack:pass | chroot $DEST chpasswd
-# give stack ownership over /opt so it may do the work needed
-chroot $DEST chown -R stack /opt
-
-# and has sudo ability (in the future this should be limited to only what 
-# stack requires)
-echo "stack ALL=(ALL) NOPASSWD: ALL" >> $DEST/etc/sudoers
diff --git a/build_pxe_boot.sh b/build_pxe_boot.sh
index 2c4cc0e..4feb14d 100755
--- a/build_pxe_boot.sh
+++ b/build_pxe_boot.sh
@@ -20,6 +20,7 @@
 fi
 
 DEST_DIR=${1:-/tmp}/tftpboot
+PXEDIR=${PXEDIR:-/var/cache/devstack/pxe}
 OPWD=`pwd`
 PROGDIR=`dirname $0`
 
@@ -41,23 +42,23 @@
 
 # Setup devstack boot
 mkdir -p $DEST_DIR/ubuntu
-if [ ! -d $OPWD/pxe ]; then
-    mkdir -p $OPWD/pxe
+if [ ! -d $PXEDIR ]; then
+    mkdir -p $PXEDIR
 fi
-if [ ! -r $OPWD/pxe/vmlinuz-${KVER} ]; then
+if [ ! -r $PXEDIR/vmlinuz-${KVER} ]; then
     sudo chmod 644 /boot/vmlinuz-${KVER}
     if [ ! -r /boot/vmlinuz-${KVER} ]; then
         echo "No kernel found"
     else
-        cp -p /boot/vmlinuz-${KVER} $OPWD/pxe
+        cp -p /boot/vmlinuz-${KVER} $PXEDIR
     fi
 fi
-cp -p $OPWD/pxe/vmlinuz-${KVER} $DEST_DIR/ubuntu
-if [ ! -r $OPWD/pxe/stack-initrd.gz ]; then
+cp -p $PXEDIR/vmlinuz-${KVER} $DEST_DIR/ubuntu
+if [ ! -r $PXEDIR/stack-initrd.gz ]; then
     cd $OPWD
-    sudo $PROGDIR/build_pxe_ramdisk.sh $OPWD/pxe/stack-initrd.gz
+    sudo $PROGDIR/build_pxe_ramdisk.sh $PXEDIR/stack-initrd.gz
 fi
-cp -p $OPWD/pxe/stack-initrd.gz $DEST_DIR/ubuntu
+cp -p $PXEDIR/stack-initrd.gz $DEST_DIR/ubuntu
 cat >>$DEFAULT <<EOF
 
 LABEL devstack
@@ -68,8 +69,8 @@
 EOF
 
 # Get Ubuntu
-if [ -d $OPWD/pxe ]; then
-    cp -p $OPWD/pxe/natty-base-initrd.gz $DEST_DIR/ubuntu
+if [ -d $PXEDIR ]; then
+    cp -p $PXEDIR/natty-base-initrd.gz $DEST_DIR/ubuntu
 fi
 cat >>$DEFAULT <<EOF
 
diff --git a/build_pxe_ramdisk.sh b/build_pxe_ramdisk.sh
index a9b9225..a01656b 100755
--- a/build_pxe_ramdisk.sh
+++ b/build_pxe_ramdisk.sh
@@ -7,44 +7,55 @@
 fi
 
 PROGDIR=`dirname $0`
+CHROOTCACHE=${CHROOTCACHE:-/var/cache/devstack}
 
 # Source params
 source ./stackrc
 
+# Store cwd
+CWD=`pwd`
+
+DEST=${DEST:-/opt/stack}
+
+# Option to use the version of devstack on which we are currently working
+USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
+
 # clean install of natty
-if [ ! -d natty-base ]; then
-    $PROGDIR/make_image.sh -C natty natty-base
+if [ ! -d $CHROOTCACHE/natty-base ]; then
+    $PROGDIR/make_image.sh -C natty $CHROOTCACHE/natty-base
     # copy kernel modules...  
     # NOTE(ja): is there a better way to do this?
-    cp -pr /lib/modules/`uname -r` natty-base/lib/modules
+    cp -pr /lib/modules/`uname -r` $CHROOTCACHE/natty-base/lib/modules
     # a simple password - pass
-    echo root:pass | chroot natty-base chpasswd
+    echo root:pass | chroot $CHROOTCACHE/natty-base chpasswd
 fi
 
 # prime natty with as many apt/pips as we can
-if [ ! -d primed ]; then
-    rsync -azH natty-base/ primed/
-    chroot primed apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
-    chroot primed pip install `cat files/pips/*`
+if [ ! -d $CHROOTCACHE/natty-dev ]; then
+    rsync -azH $CHROOTCACHE/natty-base/ $CHROOTCACHE/natty-dev/
+    chroot $CHROOTCACHE/natty-dev apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
+    chroot $CHROOTCACHE/natty-dev pip install `cat files/pips/*`
 
     # Create a stack user that is a member of the libvirtd group so that stack 
     # is able to interact with libvirt.
-    chroot primed groupadd libvirtd
-    chroot primed useradd stack -s /bin/bash -d /opt -G libvirtd
+    chroot $CHROOTCACHE/natty-dev groupadd libvirtd
+    chroot $CHROOTCACHE/natty-dev useradd stack -s /bin/bash -d $DEST -G libvirtd
+    mkdir -p $CHROOTCACHE/natty-dev/$DEST
+    chown stack $CHROOTCACHE/natty-dev/$DEST
 
     # a simple password - pass
-    echo stack:pass | chroot primed chpasswd
+    echo stack:pass | chroot $CHROOTCACHE/natty-dev chpasswd
 
     # and has sudo ability (in the future this should be limited to only what 
     # stack requires)
-    echo "stack ALL=(ALL) NOPASSWD: ALL" >> primed/etc/sudoers
+    echo "stack ALL=(ALL) NOPASSWD: ALL" >> $CHROOTCACHE/natty-dev/etc/sudoers
 fi
 
 # clone git repositories onto the system
 # ======================================
 
-if [ ! -d cloned ]; then
-    rsync -azH primed/ cloned/
+if [ ! -d $CHROOTCACHE/natty-stack ]; then
+    rsync -azH $CHROOTCACHE/natty-dev/ $CHROOTCACHE/natty-stack/
 fi
 
 # git clone only if directory doesn't exist already.  Since ``DEST`` might not
@@ -53,7 +64,7 @@
 function git_clone {
 
     # clone new copy or fetch latest changes
-    CHECKOUT=cloned$2
+    CHECKOUT=$CHROOTCACHE/natty-stack$2
     if [ ! -d $CHECKOUT ]; then
         mkdir -p $CHECKOUT
         git clone $1 $CHECKOUT
@@ -73,19 +84,35 @@
     popd
 
     # give ownership to the stack user
-    chroot cloned/ chown -R stack $2
+    chroot $CHROOTCACHE/natty-stack/ chown -R stack $2
 }
 
-git_clone $NOVA_REPO /opt/stack/nova $NOVA_BRANCH
-git_clone $GLANCE_REPO /opt/stack/glance $GLANCE_BRANCH
-git_clone $KEYSTONE_REPO /opt/stack/keystone $KEYSTONE_BRANCH
-git_clone $NOVNC_REPO /opt/stack/novnc $NOVNC_BRANCH
-git_clone $DASH_REPO /opt/stack/dash $DASH_BRANCH
-git_clone $NOVACLIENT_REPO /opt/stack/python-novaclient $NOVACLIENT_BRANCH
-git_clone $OPENSTACKX_REPO /opt/stack/openstackx $OPENSTACKX_BRANCH
+git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
+git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
+git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
+git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH
+git_clone $DASH_REPO $DEST/dash $DASH_BRANCH
+git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH
+git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
+
+# Use this version of devstack?
+if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
+    rm -rf $CHROOTCACHE/natty-stack/$DEST/devstack
+    cp -pr $CWD $CHROOTCACHE/natty-stack/$DEST/devstack
+fi
+
+# Configure host network for DHCP
+mkdir -p $CHROOTCACHE/natty-stack/etc/network
+cat > $CHROOTCACHE/natty-stack/etc/network/interfaces <<EOF
+auto lo
+iface lo inet loopback
+
+auto eth0
+iface eth0 inet dhcp
+EOF
 
 # build a new image
-BASE=build.$$
+BASE=$CHROOTCACHE/build.$$
 IMG=$BASE.img
 MNT=$BASE/
 
@@ -97,7 +124,7 @@
 # mount blank image loopback and load it
 mkdir -p $MNT
 mount -o loop $IMG $MNT
-rsync -azH cloned/ $MNT
+rsync -azH $CHROOTCACHE/natty-stack/ $MNT
 
 # umount and cleanup
 umount $MNT
diff --git a/make_image.sh b/make_image.sh
index 81dd97c..0d5074b 100755
--- a/make_image.sh
+++ b/make_image.sh
@@ -144,7 +144,7 @@
 if [ -n "$IMAGEONLY" ]; then
     # Build image from chroot
     sudo vmbuilder $HYPER ubuntu $ARGS \
-      --existing-chroot=$CHR \
+      --existing-chroot=$CHROOTDIR \
       --overwrite \
       --rootsize=$ROOTSIZE \
       --swapsize=$SWAPSIZE \