summaryrefslogtreecommitdiff
path: root/source/bottlerock.py
blob: 715a2765983a8380969023a3fc4165ac2490ba50 (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
#!/usr/bin/env python2.7

# This script provides an interface for sandboxed code to trigger buildbot
# using plain HTTP

from bottle import post, request, run, HTTPResponse
import imp
orch_config = imp.load_source('orch_config', '../source/orch_config.py')

LOGFILE = '../orch.log'
DEFINITIONS = 'ssh://git@cu010-trove.codethink.com/baserock/baserock/definitions'
TEST_REPO = 'ssh://git@cu010-trove.codethink.com/cu010-trove/br6/ciat-tester'

log_file = open(LOGFILE,'a')

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

def sendchange(category,properties):
    ''' sendchange to buildbot with category and a dictionary of property names
        and their values '''

    assert category in orch_config.categories
    import subprocess
    IP="127.0.0.1"
    bb_port=9999
    user='orchestration'
    password='orchestration'
    cmd = [
            '../orchenv-master/bin/buildbot',
            'sendchange',
            '-m%s:%d' % (IP,bb_port),
            '-a%s:%s' % (user,password),
            '-Wscriptbot', 
            '-C%s' % category]
    for property in properties.items():
        cmd.append('-p%s:%s' % property)
    log(cmd)
    if subprocess.call(cmd):
        return HTTPResponse(status=500)
    else: return 0

class Status400(Exception): pass

def get_form(*properties):
    ''' get properties from POST form and return as a dict, if property not
        sent in form raise Status400 '''

    property_dict = {}
    for property in properties:
        p = request.forms.get(property)
        if p: 
            property_dict[property] = p
        else:
            raise Status400(property)
    return property_dict

def missing_property_response(property):
    return HTTPResponse(
            status=400,
            body="400: A %s is required" % property)

@post('/repo_update')
def repo_update():
    print request.json
    repo_name = "no repo name"
    for url in request.json['urls']:
        if url.startswith('ssh'):
            repo_name = url
            break
    changes = request.json['changes']
    for change in changes:
        ref = change['ref']
        sha = change['new']
        if repo_name == DEFINITIONS:
            if ref == orch_config.definitions_base_ref:
                # if baseref changes trigger firehose
                properties = {'repo_name':repo_name,'ref':ref}
                return sendchange('repo_update',properties)
            elif ref in orch_config.candidate_refs.keys():
                slave = orch_config.candidate_refs[ref]
                properties = {"ref":ref,"sha":sha}
                return sendchange('definitions_update_%s' % slave,properties)
        elif repo_name == TEST_REPO:
            force = {"ref":"force","sha":sha}
            return sendchange('definitions_update',force)
        else:
            properties = {'repo_name':repo_name,'ref':ref}
            return sendchange('repo_update',properties)

@post('/force_build')
def force_build():
    force = {"ref":"force"}
    return sendchange('definitions_update_local-slave',force)

@post('/build_complete')
def build_complete():
    try:
        properties = get_form(
                "system",
                "buildslave_scripts_sha",
                "definitions_sha",
                "testing_sha")
    except Status400 as p:
        return missing_property_response(p)
    return sendchange('build_complete',properties)

@post('/deploy_complete')
def deploy_complete():
    try:
        properties = get_form(
                'artefact',
                'testing_sha')
    except Status400 as p:
        return missing_property_response(p)
    return sendchange('deploy_complete',properties)

if __name__ == '__main__':
    run(host='ciat.baserock.org', port=8080, debug=True)