Build a ONOS docker image from current onos source on the host.
Or can be built with a prebuilt package.
./build-onos-docker.sh -h
for options.

./build-onos-docker.sh -o $HOME/onos -b -t myonos:clustertest
would quickly build a ONOS docker image myonos:clustertest

This can be then used to test with cord-tester by specifying the --onos option to setup, run or cleanup commands

Change-Id: I1499f58fe30123b25c00afea60e816d065a307be
diff --git a/build-onos-docker.sh b/build-onos-docker.sh
new file mode 100755
index 0000000..efb413c
--- /dev/null
+++ b/build-onos-docker.sh
@@ -0,0 +1,127 @@
+#!/usr/bin/env bash
+
+function show_help {
+    echo "Usage: ${0#*/} -h | this help -o <onos source path> -t <onos docker tag> -p <onos package> -b | build onos package"
+    exit 1
+}
+
+OPTIND=1
+onos_src_dir="$HOME/onos"
+onos_tag="test/onos:clustertest"
+onos_package=
+onos_build=0
+
+while getopts "h?o:t:p:b" opt; do
+    case "$opt" in
+        h|\?)
+            show_help
+            ;;
+        o)
+            onos_src_dir=$OPTARG
+            ;;
+        p)
+            onos_package=$OPTARG
+            ;;
+        t)
+            onos_tag=$OPTARG
+            ;;
+        b)
+            onos_build=1
+            ;;
+        *)
+            show_help
+            ;;
+    esac
+done
+
+shift $((OPTIND-1))
+if [ $# -gt 0 ]; then
+    echo "Invalid arguments"
+    show_help
+fi
+mydir=$(dirname $0)
+if [ x"$onos_package" = "x" ]; then
+    if [ ! -d $onos_src_dir ]; then
+        onos_build=1
+    fi
+    onos_package=$onos_src_dir/buck-out/gen/tools/package/onos-package/onos.tar.gz
+fi
+
+onos_cloned=0
+
+#if onos package is not built, then exit
+if [ $onos_build -eq 1 ]; then
+    if [ ! -d $onos_src_dir ]; then
+        mkdir -p $onos_src_dir
+        onos_cloned=1
+      ( cd $onos_src_dir && git clone http://github.com/opennetworkinglab/onos.git . )
+    else
+      ( cd $onos_src_dir && git pull --ff-only origin master || git clone http://github.com/opennetworkinglab/onos.git . )
+    fi
+    ( cd $onos_src_dir && tools/build/onos-buck build onos ) && echo "ONOS build success" || {
+      echo "ONOS build failure. Exiting ..." && exit 1
+    }
+    onos_package=$onos_src_dir/buck-out/gen/tools/package/onos-package/onos.tar.gz
+fi
+
+if [ ! -f $onos_package ]; then
+    echo "ONOS package $onos_package does not exist. Exiting ..."
+    exit 1
+fi
+
+cp -v $onos_package $mydir
+
+function finish {
+    if [ $onos_cloned -eq 1 ]; then
+        rm -rf $onos_src_dir
+    fi
+    rm -f onos.tar.gz
+    rm -f Dockerfile.cord-tester
+}
+
+trap finish EXIT
+
+#create a ONOS docker file
+cat > $mydir/Dockerfile.cord-tester <<EOF
+FROM onosproject/onos:latest
+
+MAINTAINER Ali Al-Shabibi <ali@onlab.us>
+
+# Add Java 8 repository
+# Set the environment variables
+ENV HOME /root
+ENV JAVA_HOME /usr/lib/jvm/java-8-oracle
+ENV ONOS_ROOT /src/onos
+ENV KARAF_VERSION 3.0.5
+ENV KARAF_ROOT /root/onos/apache-karaf-3.0.5
+ENV KARAF_LOG /root/onos/apache-karaf-3.0.5/data/log/karaf.log
+ENV BUILD_NUMBER docker
+ENV PATH \$PATH:\$KARAF_ROOT/bin
+
+#Download and Build ONOS
+# Change to /root directory
+WORKDIR /root
+COPY ./onos.tar.gz /tmp
+#Install ONOS
+
+RUN rm -rf onos && mkdir onos && \
+   mv /tmp/onos.tar.gz . && \
+   tar -xf onos.tar.gz -C onos --strip-components=1 && \
+   rm -rf onos.tar.gz
+
+
+# Ports
+# 6653 - OpenFlow
+# 8181 - GUI
+# 8101 - ONOS CLI
+# 9876 - ONOS CLUSTER COMMUNICATION
+EXPOSE 6653 8181 8101 9876
+
+# Get ready to run command
+WORKDIR /root/onos
+ENTRYPOINT ["./bin/onos-service"]
+EOF
+
+#Now build the docker image
+
+docker build -t $onos_tag -f $mydir/Dockerfile.cord-tester $mydir