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 | |
Sapan Bhatia | b734bca | 2017-03-20 07:55:10 +0100 | [diff] [blame] | 27 | build_features = [] |
| 28 | |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 29 | try: |
| 30 | mp = Mixpanel("7d5ca3b3ff870dbeda89c9d5af59e9e9") |
| 31 | hostname = socket.gethostname() |
| 32 | |
| 33 | context = {} |
| 34 | context['debian-version'] = open('/etc/debian_version').read().strip() |
| 35 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 36 | os.chdir(corddir+'/orchestration/xos') |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 37 | p = subprocess.Popen(['git','rev-parse','HEAD'], stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE) |
| 38 | |
| 39 | git_hash,err = p.communicate() |
| 40 | |
| 41 | context['last-commit'] = git_hash |
Sapan Bhatia | b734bca | 2017-03-20 07:55:10 +0100 | [diff] [blame] | 42 | context['build-features'] = build_features |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 43 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 44 | if (args.status): |
| 45 | context['status'] = args.status |
| 46 | |
| 47 | config = open(corddir+'/build/config/cord_in_a_box.yml').read().splitlines() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 48 | for l in config: |
| 49 | if 'cord_profile:' in l: |
| 50 | context['profile'] = l.rstrip().rsplit(' ')[-1].replace("'","") |
| 51 | |
| 52 | context['user'] = getpass.getuser() |
| 53 | |
| 54 | name = '' |
| 55 | idx=0 |
| 56 | lst = hostname.split('.') |
| 57 | lst.reverse() |
| 58 | for i in lst: |
| 59 | name='.'.join([name,i]).lstrip('.') |
| 60 | |
| 61 | context['level-%d'%idx] = name |
| 62 | idx+=1 |
| 63 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 64 | if (args.finish): |
| 65 | logfile = open(corddir+'/install.out').read().splitlines() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 66 | snapshot = logfile[-20:] |
| 67 | snapshot = filter(lambda x:x, snapshot) |
| 68 | context['snapshot'] = snapshot |
| 69 | |
| 70 | try: |
Sapan Bhatia | 77f2667 | 2017-03-13 18:30:25 +0100 | [diff] [blame] | 71 | build_id = open(build_version_file).read().rstrip() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 72 | except: |
| 73 | build_id = context['user'] + name |
Sapan Bhatia | 77f2667 | 2017-03-13 18:30:25 +0100 | [diff] [blame] | 74 | |
| 75 | # Figure out whether we're in master or what |
| 76 | try: |
| 77 | cord_version = open(cord_version_file).read().rstrip() |
| 78 | except: |
| 79 | os.chdir(corddir) |
| 80 | possible_versions = ['master','cord-1.0','cord-2.0'] |
| 81 | |
| 82 | cord_version = 'personal' |
| 83 | try: |
| 84 | for v in possible_versions: |
| 85 | 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() |
| 86 | if (not diff): |
| 87 | cord_version = v |
| 88 | break |
| 89 | except: |
| 90 | pass |
| 91 | |
| 92 | open(cord_version_file,'w').write(cord_version) |
| 93 | context['version'] = cord_version |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame] | 94 | mp.track(build_id, args.event, context) |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 95 | except Exception,e: |
| 96 | print str(e) |
| 97 | pass |