summaryrefslogtreecommitdiff
path: root/classes/tinderclient.bbclass
blob: 168b7dec4ed5ea2425527e9588ecf06805f79834 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def tinder_tz_offset(off):
    # get the offset.n minutes Either it is a number like
    # +200 or -300
    try:
        return int(off)
    except ValueError:
        if off == "Europe/Berlin":
            return 120
        else:
            return 0

def tinder_tinder_time(offset):
    import datetime
    td   = datetime.timedelta(minutes=tinder_tz_offset(offset))
    time = datetime.datetime.utcnow() + td
    return time.strftime('%m/%d/%Y %H:%M:%S')

def tinder_tinder_start(date,offset):
    import datetime, time
    td   = datetime.timedelta(minutes=tinder_tz_offset(offset))
    ti   = time.strptime(date, "%m/%d/%Y %H:%M:%S")
    time = datetime.datetime(*ti[0:7])+td
    return time.strftime('%m/%d/%Y %H:%M:%S')

def tinder_send_email(da, header, log):
    import smtplib
    from bb import data
    from email.MIMEText import MIMEText
    msg = MIMEText(header +'\n' + log)
    msg['Subject'] = data.getVar('TINDER_SUBJECT',da, True) or "Tinder-Client build log"
    msg['To']      = data.getVar('TINDER_MAILTO' ,da, True)
    msg['From']    = data.getVar('TINDER_FROM',   da, True)


    s = smtplib.SMTP()
    s.connect()
    s.sendmail(data.getVar('TINDER_FROM', da, True), [data.getVar('TINDER_MAILTO', da, True)], msg.as_string())
    s.close()

def tinder_send_http(da, header, log):
    from bb import data
    import httplib, urllib
    cont = "\n%s\n%s" % ( header, log)
    headers = {"Content-type": "multipart/form-data" }
    print cont

    conn = httplib.HTTPConnection(data.getVar('TINDER_HOST',da, True))
    conn.request("POST", data.getVar('TINDER_URL',da,True), cont, headers)

    resp = conn.getresponse()
    print resp.status, resp.reason
    data = resp.read()
    print data

    conn.close() 


# Prepare tinderbox mail header
def tinder_prepare_mail_header(da, status):
    from bb import data

    str  = "tinderbox: administrator: %s\n" % data.getVar('TINDER_ADMIN', da, True)
    str += "tinderbox: starttime: %s\n"     % tinder_tinder_start(data.getVar('TINDER_START', da, True) or data.getVar('BUILDSTART', da, True), data.getVar('TINDER_TZ', da, True))
    str += "tinderbox: buildname: %s\n"     % data.getVar('TINDER_BUILD', da, True)
    str += "tinderbox: errorparser: %s\n"   % data.getVar('TINDER_ERROR', da, True)
    str += "tinderbox: status: %s\n"        % status
    str += "tinderbox: timenow: %s\n"       % tinder_tinder_time(data.getVar('TINDER_TZ', da, True))
    str += "tinderbox: tree: %s\n"          % data.getVar('TINDER_TREE', da, True)
    str += "tinderbox: buildfamily: %s\n"   % "unix"
    str += "tinderbox: END"

    return str

def tinder_do_tinder_report(event):
    """
    Report to the tinderbox. Either we will report every step
    (depending on TINDER_VERBOSE_REPORT) at the end we will send the
    tinderclient.log
    """
    from bb.event import getName
    from bb import data, mkdirhier
    import os, glob

    # variables
    name = getName(event)
    log  = ""
    header = ""
    verbose = data.getVar('TINDER_VERBOSE_REPORT', event.data, True) == "1"

    # Check what we need to do Build* shows we start or are done
    if name == "BuildStarted":
        header = tinder_prepare_mail_header(event.data, 'building')
        # generate
        for var in os.environ:
            log += "%s=%s\n" % (var, os.environ[var])

        mkdirhier(data.getVar('TMPDIR', event.data, True))
        file = open(data.getVar('TINDER_LOG', event.data, True), 'w')
        file.write(log)

        if not verbose:
            header = ""

    if name == "PkgFailed" or name == "BuildCompleted":
        status = 'build_failed'
        if name == "BuildCompleted":
            status = "success"
        header = tinder_prepare_mail_header(event.data, status)
        # append the log
        log_file = data.getVar('TINDER_LOG', event.data, True)
        file     = open(log_file, 'r')
        for line in file.readlines():
            log += line

    if verbose and name == "TaskStarted":
        header = tinder_prepare_mail_header(event.data, 'building')
        log    = "Task %s started" % event.task

    if verbose and name == "PkgStarted":
        header = tinder_prepare_mail_header(event.data, 'building')
        log    = "Package %s started" % data.getVar('P', event.data, True)

    if verbose and name == "PkgSucceeded":
        header = tinder_prepare_mail_header(event.data, 'building')
        log    = "Package %s done" % data.getVar('P', event.data, True)

    # Append the Task Log
    if name == "TaskSucceeded" or name == "TaskFailed":
        log_file = glob.glob("%s/log.%s.*" % (data.getVar('T', event.data, True), event.task))

        if len(log_file) != 0:
            to_file  = data.getVar('TINDER_LOG', event.data, True)
            log_txt  = open(log_file[0], 'r').readlines()
            to_file  = open(to_file, 'a')

            to_file.writelines(log_txt)

            # append to the log
            if verbose:
                header = tinder_prepare_mail_header(event.data, 'building')
                for line in log_txt:
                    log += line

    # now mail the log
    if len(log) == 0 or len(header) == 0:
        return

    log_post_method = tinder_send_email
    if data.getVar('TINDER_SENDLOG', event.data, True) == "http":
        log_post_method = tinder_send_http

    log_post_method(event.data, header, log)


addhandler tinderclient_eventhandler
python tinderclient_eventhandler() {
    from bb import note, error, data
    from bb.event import NotHandled

    do_tinder_report = data.getVar('TINDER_REPORT', e.data, True)
    if do_tinder_report and do_tinder_report == "1":
        tinder_do_tinder_report(e)

    return NotHandled
}