CORD-933: Add Mixpanel aggregate analytics to cord in a box to track failures, errors, and also track cord-in-a-box usage.

Change-Id: I8d443198b9861bed079a47c6d282533c6eceae3b
diff --git a/elk-logger/mixpanel b/elk-logger/mixpanel
new file mode 100644
index 0000000..6a473e7
--- /dev/null
+++ b/elk-logger/mixpanel
@@ -0,0 +1,59 @@
+#!/usr/bin/python
+
+from mixpanel import Mixpanel
+
+import sys
+import getpass
+import socket
+import subprocess
+import os
+
+from os.path import expanduser
+home = expanduser("~")
+
+try:
+    mp = Mixpanel("7d5ca3b3ff870dbeda89c9d5af59e9e9")
+    hostname = socket.gethostname()
+
+    context = {}
+    context['debian-version'] = open('/etc/debian_version').read().strip()
+
+
+    os.chdir(home+'/cord/orchestration/xos')
+    p = subprocess.Popen(['git','rev-parse','HEAD'], stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
+
+    git_hash,err = p.communicate()
+    
+    context['last-commit'] = git_hash
+
+    config = open(home + '/cord/build/config/cord_in_a_box.yml').read().splitlines()
+    for l in config:
+        if 'cord_profile:' in l:
+            context['profile'] = l.rstrip().rsplit(' ')[-1].replace("'","")
+
+    context['user'] = getpass.getuser()
+
+    name = ''
+    idx=0
+    lst = hostname.split('.')
+    lst.reverse()
+    for i in lst:
+        name='.'.join([name,i]).lstrip('.')
+
+        context['level-%d'%idx] = name
+        idx+=1
+
+    if (len(sys.argv)>2 and 'finish' in sys.argv[2]):
+        logfile = open(home + '/cord/install.out').read().splitlines()
+        snapshot = logfile[-20:]
+        snapshot = filter(lambda x:x, snapshot)
+        context['snapshot'] = snapshot
+
+    try:
+        build_id = open('/tmp/cord-build').read().rstrip()
+    except:
+        build_id = context['user'] + name
+    mp.track(build_id, sys.argv[1], context)
+except Exception,e:
+    print str(e)
+    pass
diff --git a/scripts/cord-in-a-box.sh b/scripts/cord-in-a-box.sh
index df48a12..afee8ec 100755
--- a/scripts/cord-in-a-box.sh
+++ b/scripts/cord-in-a-box.sh
@@ -11,6 +11,12 @@
 REPO_BRANCH="master"
 VERSION_STRING="CiaB development version"
 
+function finish {
+    set +x
+    python $CORDDIR/build/elk-logger/mixpanel FINISH --finish
+}
+
+trap finish EXIT
 
 function add_box() {
   vagrant box list | grep $1 | grep virtualbox || vagrant box add $1
@@ -18,9 +24,15 @@
 }
 
 function run_stage {
+    cd $CORDDIR
+    python build/elk-logger/mixpanel $1-start
+
     echo "==> "$1": Starting"
     $1
     echo "==> "$1": Complete"
+
+    cd $CORDDIR
+    python build/elk-logger/mixpanel $1-end
 }
 
 function cleanup_from_previous_test() {
@@ -39,12 +51,13 @@
 }
 
 function bootstrap() {
+  cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 > /tmp/cord-build
   cd ~
   sudo apt-get update
   [ -e vagrant_1.8.5_x86_64.deb ] || wget https://releases.hashicorp.com/vagrant/1.8.5/vagrant_1.8.5_x86_64.deb
   dpkg -l vagrant || sudo dpkg -i vagrant_1.8.5_x86_64.deb
   sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev curl nfs-kernel-server git build-essential python-pip
-  sudo pip install pyparsing python-logstash
+  sudo pip install pyparsing python-logstash mixpanel
 
   [ -e ~/.ssh/id_rsa ] || ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
 
@@ -295,7 +308,7 @@
 echo "Preparing to install $VERSION_STRING ($REPO_BRANCH branch)"
 echo ""
 
-run_stage bootstrap
+bootstrap
 run_stage cloudlab_setup
 run_stage elk_up
 run_stage vagrant_vms_up