Extract common functions into a separate file

This is the start of an effort to organize devstack's code
to better document the requirements for configuring the individual
components.

Change-Id: I3476b76b9d1f9ee63687fb9898a98729118cbd84
diff --git a/tools/build_ramdisk.sh b/tools/build_ramdisk.sh
index feaa8a9..7c1600b 100755
--- a/tools/build_ramdisk.sh
+++ b/tools/build_ramdisk.sh
@@ -47,7 +47,10 @@
 
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
+
+# Import common functions
+. $TOP_DIR/functions
 
 # Store cwd
 CWD=`pwd`
@@ -170,35 +173,6 @@
     chroot $MNTDIR apt-get install -y linux-generic
 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=${MNTDIR}$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 $MNTDIR 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
diff --git a/tools/build_tempest.sh b/tools/build_tempest.sh
index aa44766..230e8f9 100755
--- a/tools/build_tempest.sh
+++ b/tools/build_tempest.sh
@@ -26,7 +26,10 @@
 
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
+
+# Import common functions
+. $TOP_DIR/functions
 
 # Abort if localrc is not set
 if [ ! -e $TOP_DIR/localrc ]; then
@@ -43,42 +46,8 @@
 
 TEMPEST_DIR=$DEST/tempest
 
-DIST_NAME=${DIST_NAME:-oneiric}
-
-# 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 {
-
-    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 $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)
-        find $GIT_DEST -name '*.pyc' -delete
-        git checkout -f origin/$GIT_BRANCH
-        # a local branch might not exist
-        git branch -D $GIT_BRANCH || true
-        git checkout -b $GIT_BRANCH
-    fi
-}
-
 # Install tests and prerequisites
-sudo PIP_DOWNLOAD_CACHE=/var/cache/pip pip install --use-mirrors `cat $TOP_DIR/files/pips/tempest`
+pip_install `cat $TOP_DIR/files/pips/tempest`
 
 git_clone $TEMPEST_REPO $TEMPEST_DIR $TEMPEST_BRANCH
 
diff --git a/tools/build_uec.sh b/tools/build_uec.sh
index 04e1a45..ed5a017 100755
--- a/tools/build_uec.sh
+++ b/tools/build_uec.sh
@@ -8,7 +8,10 @@
 
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
+
+# Import common functions
+. $TOP_DIR/functions
 
 cd $TOP_DIR
 
@@ -34,7 +37,7 @@
 
 # Install deps if needed
 DEPS="kvm libvirt-bin kpartx cloud-utils curl"
-apt-get install -y --force-yes $DEPS || true # allow this to fail gracefully for concurrent builds
+apt_get install -y --force-yes $DEPS || true # allow this to fail gracefully for concurrent builds
 
 # Where to store files and instances
 WORK_DIR=${WORK_DIR:-/opt/uecstack}
diff --git a/tools/build_uec_ramdisk.sh b/tools/build_uec_ramdisk.sh
index 174eaac..32f90c0 100755
--- a/tools/build_uec_ramdisk.sh
+++ b/tools/build_uec_ramdisk.sh
@@ -40,7 +40,10 @@
 
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
+
+# Import common functions
+. $TOP_DIR/functions
 
 cd $TOP_DIR
 
@@ -68,7 +71,7 @@
 
 # Install deps if needed
 DEPS="kvm libvirt-bin kpartx cloud-utils curl"
-apt-get install -y --force-yes $DEPS
+apt_get install -y --force-yes $DEPS
 
 # Where to store files and instances
 CACHEDIR=${CACHEDIR:-/opt/stack/cache}
@@ -113,35 +116,6 @@
     chroot $MNT_DIR apt-get install -y linux-generic
 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=${MNT_DIR}$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 $MNT_DIR 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
diff --git a/tools/configure_tempest.sh b/tools/configure_tempest.sh
index 00add9a..f6ef0d3 100755
--- a/tools/configure_tempest.sh
+++ b/tools/configure_tempest.sh
@@ -30,7 +30,10 @@
 
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
+
+# Import common functions
+. $TOP_DIR/functions
 
 # Abort if localrc is not set
 if [ ! -e $TOP_DIR/localrc ]; then
diff --git a/tools/copy_dev_environment_to_uec.sh b/tools/copy_dev_environment_to_uec.sh
index c949b32..d5687dc 100755
--- a/tools/copy_dev_environment_to_uec.sh
+++ b/tools/copy_dev_environment_to_uec.sh
@@ -8,7 +8,10 @@
 
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
+
+# Import common functions
+. $TOP_DIR/functions
 
 # Change dir to top of devstack
 cd $TOP_DIR
@@ -47,13 +50,6 @@
 ( umask 226 && echo "stack ALL=(ALL) NOPASSWD:ALL" \
     > $STAGING_DIR/etc/sudoers.d/50_stack_sh )
 
-# Gracefully cp only if source file/dir exists
-function cp_it {
-    if [ -e $1 ] || [ -d $1 ]; then
-        cp -pRL $1 $2
-    fi
-}
-
 # Copy over your ssh keys and env if desired
 cp_it ~/.ssh $STAGING_DIR/$DEST/.ssh
 cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/$DEST/.ssh/authorized_keys
diff --git a/tools/get_uec_image.sh b/tools/get_uec_image.sh
index f66f2bc..0963074 100755
--- a/tools/get_uec_image.sh
+++ b/tools/get_uec_image.sh
@@ -6,7 +6,10 @@
 
 # Keep track of the current directory
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=`cd $TOOLS_DIR/..; pwd`
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
+
+# Import common functions
+. $TOP_DIR/functions
 
 # exit on error to stop unexpected errors
 set -o errexit