XenServer: new build and install scripts

Change-Id: Ia13a9c8073e59edf98415ba5b9f3a9cbd1453d32
diff --git a/tools/xen/build_xva.sh b/tools/xen/build_xva.sh
index c235485..2f28b5f 100755
--- a/tools/xen/build_xva.sh
+++ b/tools/xen/build_xva.sh
@@ -1,5 +1,27 @@
 #!/bin/bash
 
+set -e
+
+declare -a on_exit_hooks
+
+on_exit()
+{
+    for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0)
+    do
+        eval "${on_exit_hooks[$i]}"
+    done
+}
+
+add_on_exit()
+{
+    local n=${#on_exit_hooks[*]}
+    on_exit_hooks[$n]="$*"
+    if [[ $n -eq 0 ]]
+    then
+        trap on_exit EXIT
+    fi
+}
+
 # Abort if localrc is not set
 if [ ! -e ../../localrc ]; then
     echo "You must have a localrc with ALL necessary passwords defined before proceeding."
@@ -16,27 +38,11 @@
 # Echo commands
 set -o xtrace
 
+GUEST_NAME="$1"
+
 # Directory where we stage the build
-STAGING_DIR=$TOP_DIR/stage
-
-# Option to clean out old stuff
-CLEAN=${CLEAN:-0}
-if [ "$CLEAN" = "1" ]; then
-    rm -rf $STAGING_DIR
-fi
-
-# Download our base image.  This image is made using prepare_guest.sh
-BASE_IMAGE_URL=${BASE_IMAGE_URL:-http://images.ansolabs.com/xen/stage.tgz}
-if [ ! -e $STAGING_DIR ]; then
-    if [ ! -e /tmp/stage.tgz ]; then
-        wget $BASE_IMAGE_URL -O /tmp/stage.tgz
-    fi
-    tar xfz /tmp/stage.tgz
-    cd $TOP_DIR
-fi
-
-# Free up precious disk space
-rm -f /tmp/stage.tgz
+STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*")
+add_on_exit "$TOP_DIR/scripts/manage-vdi close $GUEST_NAME 0 1"
 
 # Make sure we have a stage
 if [ ! -d $STAGING_DIR/etc ]; then
@@ -55,63 +61,26 @@
 UBUNTU_VERSION=`cat $STAGING_DIR/etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//"`
 KERNEL_VERSION=`ls $STAGING_DIR/boot/vmlinuz* | head -1 | sed "s/.*vmlinuz-//"`
 
-# Directory for xvas
-XVA_DIR=$TOP_DIR/xvas
-
-# Create xva dir
-mkdir -p $XVA_DIR
-
-# Path to xva
-XVA=$XVA_DIR/$GUEST_NAME.xva
-
-# Setup fake grub
-rm -rf $STAGING_DIR/boot/grub/
-mkdir -p $STAGING_DIR/boot/grub/
-cp $TEMPLATES_DIR/menu.lst.in $STAGING_DIR/boot/grub/menu.lst
-sed -e "s,@KERNEL_VERSION@,$KERNEL_VERSION,g" -i $STAGING_DIR/boot/grub/menu.lst
-
-# Setup fstab, tty, and other system stuff
-cp $FILES_DIR/fstab $STAGING_DIR/etc/fstab
-cp $FILES_DIR/hvc0.conf $STAGING_DIR/etc/init/
-
-# Put the VPX into UTC.
-rm -f $STAGING_DIR/etc/localtime
-
 # Configure dns (use same dns as dom0)
 cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf
 
 # Copy over devstack
 rm -f /tmp/devstack.tar
-tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar $TOP_DIR/../../../devstack
-cd $STAGING_DIR/opt/stack/
-tar xf /tmp/devstack.tar
+cd $TOP_DIR/../../
+tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar .
+mkdir -p $STAGING_DIR/opt/stack/devstack
+tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack
 cd $TOP_DIR
 
-# Configure OVA
-VDI_SIZE=$(($VDI_MB*1024*1024))
-PRODUCT_BRAND=${PRODUCT_BRAND:-openstack}
-PRODUCT_VERSION=${PRODUCT_VERSION:-001}
-BUILD_NUMBER=${BUILD_NUMBER:-001}
-LABEL="$PRODUCT_BRAND $PRODUCT_VERSION-$BUILD_NUMBER"
-OVA=$STAGING_DIR/tmp/ova.xml
-cp $TEMPLATES_DIR/ova.xml.in  $OVA
-sed -e "s,@VDI_SIZE@,$VDI_SIZE,g" -i $OVA
-sed -e "s,@PRODUCT_BRAND@,$PRODUCT_BRAND,g" -i $OVA
-sed -e "s,@PRODUCT_VERSION@,$PRODUCT_VERSION,g" -i $OVA
-sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA
-
 # Run devstack on launch
 cat <<EOF >$STAGING_DIR/etc/rc.local
 # network restart required for getting the right gateway
 /etc/init.d/networking restart
 GUEST_PASSWORD=$GUEST_PASSWORD STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh > /opt/stack/prepare_guest.log 2>&1
-su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack
+su -c "/opt/stack/run.sh > /opt/stack/run.sh.log 2>&1" stack
 exit 0
 EOF
 
-# Clean old xva. In the future may not do this every time.
-rm -f $XVA
-
 # Configure the hostname
 echo $GUEST_NAME > $STAGING_DIR/etc/hostname
 
@@ -151,10 +120,6 @@
     sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
 fi
 
-if [ -h $STAGING_DIR/sbin/dhclient3 ]; then
-    rm -f $STAGING_DIR/sbin/dhclient3
-fi
-
 # Gracefully cp only if source file/dir exists
 function cp_it {
     if [ -e $1 ] || [ -d $1 ]; then
@@ -181,11 +146,4 @@
 EOF
 chmod 755 $STAGING_DIR/opt/stack/run.sh
 
-# Create xva
-if [ ! -e $XVA ]; then
-    rm -rf /tmp/mkxva*
-    UID=0 $SCRIPT_DIR/mkxva -o $XVA -t xva -x $OVA $STAGING_DIR $VDI_MB /tmp/
-fi
-
-echo "Built $(basename $XVA).  If your dom0 is on a different machine, copy this to [devstackdir]/tools/xen/$(basename $XVA)"
-echo "Also copy your localrc to [devstackdir]"
+echo "Done"