Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from mixpanel import Mixpanel |
| 4 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame^] | 5 | import argparse |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 6 | import sys |
| 7 | import getpass |
| 8 | import socket |
| 9 | import subprocess |
| 10 | import os |
| 11 | |
| 12 | from os.path import expanduser |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame^] | 13 | |
| 14 | parse = argparse.ArgumentParser(description='Send CORD logs to mixpanel.') |
| 15 | parse.add_argument('--corddir', dest='corddir', action='store',default=None, help='CORD Directory') |
| 16 | parse.add_argument('--event', dest='event', action='store',default=None, help='Event Name') |
| 17 | parse.add_argument('--finish', dest='finish', action='store_true',default=False, help='Finish') |
| 18 | parse.add_argument('--status', dest='status', action='store',default=None, help='Exit status') |
| 19 | |
| 20 | args = parse.parse_args() |
| 21 | |
| 22 | corddir = expanduser(args.corddir) |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 23 | |
| 24 | try: |
| 25 | mp = Mixpanel("7d5ca3b3ff870dbeda89c9d5af59e9e9") |
| 26 | hostname = socket.gethostname() |
| 27 | |
| 28 | context = {} |
| 29 | context['debian-version'] = open('/etc/debian_version').read().strip() |
| 30 | |
| 31 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame^] | 32 | os.chdir(corddir+'/orchestration/xos') |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 33 | p = subprocess.Popen(['git','rev-parse','HEAD'], stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE) |
| 34 | |
| 35 | git_hash,err = p.communicate() |
| 36 | |
| 37 | context['last-commit'] = git_hash |
| 38 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame^] | 39 | if (args.status): |
| 40 | context['status'] = args.status |
| 41 | |
| 42 | config = open(corddir+'/build/config/cord_in_a_box.yml').read().splitlines() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 43 | for l in config: |
| 44 | if 'cord_profile:' in l: |
| 45 | context['profile'] = l.rstrip().rsplit(' ')[-1].replace("'","") |
| 46 | |
| 47 | context['user'] = getpass.getuser() |
| 48 | |
| 49 | name = '' |
| 50 | idx=0 |
| 51 | lst = hostname.split('.') |
| 52 | lst.reverse() |
| 53 | for i in lst: |
| 54 | name='.'.join([name,i]).lstrip('.') |
| 55 | |
| 56 | context['level-%d'%idx] = name |
| 57 | idx+=1 |
| 58 | |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame^] | 59 | if (args.finish): |
| 60 | logfile = open(corddir+'/install.out').read().splitlines() |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 61 | snapshot = logfile[-20:] |
| 62 | snapshot = filter(lambda x:x, snapshot) |
| 63 | context['snapshot'] = snapshot |
| 64 | |
| 65 | try: |
| 66 | build_id = open('/tmp/cord-build').read().rstrip() |
| 67 | except: |
| 68 | build_id = context['user'] + name |
Sapan Bhatia | eb7819c | 2017-03-01 15:26:17 +0100 | [diff] [blame^] | 69 | mp.track(build_id, args.event, context) |
Sapan Bhatia | a94c2db | 2017-02-27 20:18:24 +0100 | [diff] [blame] | 70 | except Exception,e: |
| 71 | print str(e) |
| 72 | pass |