summaryrefslogtreecommitdiff
path: root/source/bottlerock.py
blob: 23f1519d4d514717fabdd2b07da8b202b4879d37 (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
#!/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

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'

categories = [
        'repo_update',
        'definitions_update',
        'build_complete',
        'deploy_complete']

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 '''

    global categories
    assert category in 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: 
            attr_dict[property] = p
        else:
            raise Status400(property)

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

@post('/repo_update')
def repo_update():
    try:
        properties = get_form("repo_name")
    except Status400 as p:
        return missing_property_response(p)
    if properties['repo_name'] == DEFINITIONS:
        return sendchange('definitions_update')
    elif properties['repo_name'] == TEST_REPO:
        force = {"force":"force"}
        return sendchange('definitions_update',force)
    else:
        return sendchange('repo_update',properties)

@post('/force_build')
def force_build():
    force = {"force":"force"}
    return sendchange('definitions_update',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='0.0.0.0', port=8080, debug=True)