xenapi: Extract plugin installation functions

This change extracts the plugin installation functions, and covers the
extracted functions with tests. Use:

    ./test_funtions.sh run_tests

to run the tests.

Change-Id: I1d78d9e8cc4d52ee2df83d07e4c74dda4805f21a
diff --git a/tools/xen/functions b/tools/xen/functions
new file mode 100644
index 0000000..5b4a661
--- /dev/null
+++ b/tools/xen/functions
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+function xapi_plugin_location {
+    for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"
+    do
+        if [ -d $PLUGIN_DIR ]
+        then
+            echo $PLUGIN_DIR
+            return 0
+        fi
+    done
+    return 1
+}
+
+function zip_snapshot_location {
+    echo $1 | sed "s:\.git$::;s:$:/zipball/$2:g"
+}
+
+function create_directory_for_kernels {
+    mkdir -p "/boot/guest"
+}
+
+function extract_remote_zipball {
+    local ZIPBALL_URL=$1
+
+    local LOCAL_ZIPBALL=$(mktemp)
+    local EXTRACTED_FILES=$(mktemp -d)
+
+    (
+        wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate
+        unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES
+        rm -f $LOCAL_ZIPBALL
+    ) >&2
+
+    echo "$EXTRACTED_FILES"
+}
+
+function find_xapi_plugins_dir {
+    find $1 -path '*/xapi.d/plugins' -type d -print
+}
+
+function install_xapi_plugins_from_zipball {
+    local XAPI_PLUGIN_DIR
+    local EXTRACTED_FILES
+    local EXTRACTED_PLUGINS_DIR
+
+    XAPI_PLUGIN_DIR=$(xapi_plugin_location)
+
+    EXTRACTED_FILES=$(extract_remote_zipball $1)
+    EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES)
+
+    cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR
+    rm -rf $EXTRACTED_FILES
+    chmod a+x ${XAPI_PLUGIN_DIR}*
+}