summaryrefslogtreecommitdiff
path: root/builder_logic.py
blob: e544897a41cb360aad9fd118e512421a7fb66b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import subprocess

ORCHE_URL = 'http://127.0.0.1:8080/'
SYSTEM='genivi-demo-platform-x86_64-generic.morph'
BUILD_SCRIPT = 'build_a_system.sh'
DEPLOY_SCRIPT = 'deploy_a_system.sh'
DEFINITIONS_DIR='definitions'
REF = "cu010-trove/br6/firehose-test-1"
TESTING_REPO = 'ssh://git@cu010-trove.codethink.com/cu010-trove/br6/ciat-tester'
BUILDSLAVE_SCRIPTS_REPO = 'ssh://git@cu010-trove.codethink.com/cu010-trove/br6/buildslave-scripts'

whitelist = [
        SYSTEM,
        'clusters/tlsa.morph',
        'strata/build-essential.morph',
        'strata/core.morph',
        'strata/foundation.morph',
        'strata/bsp-x86_64-generic.morph',
    ]

LOGFILE = '/home/williamholland/orchestration/trigger_log'

log_file = open(LOGFILE,'a')

def log(msg):
    ''' write message to log file with timestamp and script name '''
    import datetime
    global log_file
    dt = str(datetime.datetime.now()).split('.')[0]
    log_file.write("[%s] Builder Trigger: %s\n" % (dt, msg))

def _exit(exit_val):
    if exit_val: log('exiting unhappily')
    exit(exit_val)

def files_changed():
    ''' return a list of files changed in latest commit to definitions'''
    import os
    owd = os.getcwd()
    os.chdir(DEFINITIONS_DIR)
    SHAcmd = ['git', 'log', REF, '--format=format:%H', '-2']
    SHAproc = subprocess.Popen(SHAcmd, stdout=subprocess.PIPE)
    SHAout, SHAerr = SHAproc.communicate()
    SHA = SHAout.split()
    cmd = ['git', 'diff', '--name-only', SHA[0], SHA[1]]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out, err = p.communicate()
    os.chdir(owd)
    return out.split()

def find_systems_affected_by_change():
    # TODO for each file changed, separate into chunks, strata, systems and clusters
    # TODO for each strata get it's system
    pass

def find_clusters_affected_by_change():
    changed_systems = find_systems_affected_by_change()
    # TODO for each system get it's custers

def build(system):
    log('building %s' % system)
    exit_val =  subprocess.call(['sh','%s' % BUILD_SCRIPT, '%s' % system])
    log('build complete')
    return exit_val

def trigger_deploy(system):
    import requests
    global url
    global buildslave_scripts_sha
    global definitions_sha
    global testing_sha
    url = '%sbuild_complete' % ORCHE_URL
    if not system: return 0
    payload = {
        'system':system,
        'buildslave_scripts_sha':buildslave_scripts_sha,
        'definitions_sha':definitions_sha,
        'testing_sha':testing_sha,
    }
    log('triggering deploy')
    r = requests.post(url,data=payload)
    return not r.ok

def do_build_deploy(system):
    build_exit_val = build(system)
    if build_exit_val: _exit(build_exit_val)
    return trigger_deploy(system)

def get_buildslave_scripts_sha():
    _cmd = ['git','ls-remote',BUILDSLAVE_SCRIPTS_REPO]
    _proc = subprocess.Popen(_cmd, stdout=subprocess.PIPE)
    _out, _err = _proc.communicate()
    return _out.split()[0]

def get_definitions_sha():
    import os
    owd = os.getcwd()
    os.chdir(DEFINITIONS_DIR)
    _cmd = ['git', 'log', REF, '--format=format:%H', '-1']
    _proc = subprocess.Popen(_cmd, stdout=subprocess.PIPE)
    _out, _err = _proc.communicate()
    os.chdir(owd)
    return _out.split()[0]

def get_testing_sha():
    _cmd = ['git','ls-remote',TESTING_REPO]
    _proc = subprocess.Popen(_cmd, stdout=subprocess.PIPE)
    _out, _err = _proc.communicate()
    return _out.split()[0]

if __name__ == '__main__':
    import sys
    force = ""
    global buildslave_scripts_sha
    global definitions_sha
    global testing_sha
    buildslave_scripts_sha = get_buildslave_scripts_sha()
    definitions_sha = get_definitions_sha()
    testing_sha = get_testing_sha()
    try:
        force = sys.argv[1]
    except:
        pass
    if force=="force":
        _exit(do_build_deploy(SYSTEM))
    _files_changed = files_changed()
    systems_list = []
    #for f in _files_changed:
    #    if f in whitelist:
    #        _exit(do_build_deploy(SYSTEM))
    _exit(do_build_deploy(SYSTEM))
    log('nothing whitelisted changed. No build started.')