Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from mixpanel import Mixpanel |
Sapan Bhatia | 77f2667 | 2017-03-13 18:30:25 +0100 | [diff] [blame^] | 4 | import pdb |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 5 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 6 | import argparse |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 7 | import sys |
| 8 | import getpass |
| 9 | import socket |
| 10 | import subprocess |
| 11 | import os |
| 12 | |
| 13 | from os.path import expanduser |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 14 | |
| 15 | parse = argparse.ArgumentParser(description='Send CORD logs to mixpanel.') |
| 16 | parse.add_argument('--corddir', dest='corddir', action='store',default=None, help='CORD Directory') |
| 17 | parse.add_argument('--event', dest='event', action='store',default=None, help='Event Name') |
| 18 | parse.add_argument('--finish', dest='finish', action='store_true',default=False, help='Finish') |
| 19 | parse.add_argument('--status', dest='status', action='store',default=None, help='Exit status') |
| 20 | |
| 21 | args = parse.parse_args() |
| 22 | |
| 23 | corddir = expanduser(args.corddir) |
Sapan Bhatia | 77f2667 | 2017-03-13 18:30:25 +0100 | [diff] [blame^] | 24 | build_version_file = '/tmp/cord-build' |
| 25 | cord_version_file = '/tmp/cord-build-version' |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 26 | |
| 27 | try: |
| 28 | mp = Mixpanel("7d5ca3b3ff870dbeda89c9d5af59e9e9") |
| 29 | hostname = socket.gethostname() |
| 30 | |
| 31 | context = {} |
| 32 | context['debian-version'] = open('/etc/debian_version').read().strip() |
| 33 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 34 | os.chdir(corddir+'/orchestration/xos') |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 35 | p = subprocess.Popen(['git','rev-parse','HEAD'], stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE) |
| 36 | |
| 37 | git_hash,err = p.communicate() |
| 38 | |
| 39 | context['last-commit'] = git_hash |
| 40 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 41 | if (args.status): |
| 42 | context['status'] = args.status |
| 43 | |
| 44 | config = open(corddir+'/build/config/cord_in_a_box.yml').read().splitlines() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 45 | for l in config: |
| 46 | if 'cord_profile:' in l: |
| 47 | context['profile'] = l.rstrip().rsplit(' ')[-1].replace("'","") |
| 48 | |
| 49 | context['user'] = getpass.getuser() |
| 50 | |
| 51 | name = '' |
| 52 | idx=0 |
| 53 | lst = hostname.split('.') |
| 54 | lst.reverse() |
| 55 | for i in lst: |
| 56 | name='.'.join([name,i]).lstrip('.') |
| 57 | |
| 58 | context['level-%d'%idx] = name |
| 59 | idx+=1 |
| 60 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 61 | if (args.finish): |
| 62 | logfile = open(corddir+'/install.out').read().splitlines() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 63 | snapshot = logfile[-20:] |
| 64 | snapshot = filter(lambda x:x, snapshot) |
| 65 | context['snapshot'] = snapshot |
| 66 | |
| 67 | try: |
Sapan Bhatia | 77f2667 | 2017-03-13 18:30:25 +0100 | [diff] [blame^] | 68 | build_id = open(build_version_file).read().rstrip() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 69 | except: |
| 70 | build_id = context['user'] + name |
Sapan Bhatia | 77f2667 | 2017-03-13 18:30:25 +0100 | [diff] [blame^] | 71 | |
| 72 | # Figure out whether we're in master or what |
| 73 | try: |
| 74 | cord_version = open(cord_version_file).read().rstrip() |
| 75 | except: |
| 76 | os.chdir(corddir) |
| 77 | possible_versions = ['master','cord-1.0','cord-2.0'] |
| 78 | |
| 79 | cord_version = 'personal' |
| 80 | try: |
| 81 | for v in possible_versions: |
| 82 | diff = os.popen('repo forall -c "git stash 2>/dev/null 1>/dev/null;git diff -b opencord/%s 2>/dev/null;git stash pop 2>/dev/null 1>/dev/null"'%v).read() |
| 83 | if (not diff): |
| 84 | cord_version = v |
| 85 | break |
| 86 | except: |
| 87 | pass |
| 88 | |
| 89 | open(cord_version_file,'w').write(cord_version) |
| 90 | context['version'] = cord_version |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 91 | mp.track(build_id, args.event, context) |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 92 | except Exception,e: |
| 93 | print str(e) |
| 94 | pass |