summaryrefslogtreecommitdiff
path: root/builder_logic.py
blob: 03909e8b5d3e95fe88ce7f812d1e3ea2f2b6fb2d (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
import subprocess

ORCHE_URL = 'http://127.0.0.1:8080/'
BUILD_SCRIPT = 'bild_a_system.sh'
DEPLOY_SCRIPT = 'deploy_a_system.sh'
DEFINITIONS_DIR='/home/patrickdarley/definitions'
REF = "cu010-trove/br6/firehose-test-1"

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

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 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(cmd, stdout=subprocess.PIPE)
    SHAout, SHAerr = p.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)
    return subprocess.call(['sh','%s' % BUILD_SCRIPT, '%s' % system])

def deploy(cluster):
    log('deploying %s' % cluster)
    return subprocess.call(['sh','%s' % DEPLOY_SCRIPT, '%s' % cluster])

def trigger_testing(build_id):
    import requests
    global url
    url = '%sbuild_complete' % ORCHE_URL
    payload = {'artefact':build_id}
    r = requests.post(url,data=payload)
    return r.ok

if __name__ == '__main__':
    _files_changed = files_changed()
    for f in _files_changed:
        if f in whitelist:
            build_exit_val = build('systems/base-system-x86_64-generic.morph')
            if build_exit_val: exit(build_exit_val)
            deploy_exit_val = deploy('clusters/tlsa.morph')
            if deploy_exit_val: exit(deploy_exit_val)
            exit(trigger_testing())
    log('nothing whitelisted changed. No build started.')