class/tinderclient.bbclass: Send bigger files in smaller chunks to avoid broken firewalls
[vuplus_openembedded] / classes / tinderclient.bbclass
1 def tinder_http_post(server, selector, content_type, body):
2     import httplib
3     # now post it
4     for i in range(0,5):
5        try:
6            h = httplib.HTTP(server)
7            h.putrequest('POST', selector)
8            h.putheader('content-type', content_type)
9            h.putheader('content-length', str(len(body)))
10            h.endheaders()
11            h.send(body)
12            errcode, errmsg, headers = h.getreply()
13            #print errcode, errmsg, headers
14            return (errcode,errmsg, headers, h.file)
15        except:
16            print "Error sending the report!"
17            # try again
18            pass
19
20     # return some garbage
21     return (-1, "unknown", "unknown", None)
22
23 def tinder_form_data(bound, dict, log):
24     output = []
25     # for each key in the dictionary
26     for name in dict:
27         output.append( "--" + bound )
28         output.append( 'Content-Disposition: form-data; name="%s"' % name )
29         output.append( "" )
30         output.append( dict[name] )
31     if log:
32         output.append( "--" + bound )
33         output.append( 'Content-Disposition: form-data; name="log"; filename="log.txt"' )
34         output.append( '' )
35         output.append( log )
36     output.append( '--' + bound + '--' )
37     output.append( '' )
38
39     return "\r\n".join(output)
40
41 def tinder_time_string():
42     """
43     Return the time as GMT
44     """
45     return ""
46
47 def tinder_format_http_post(d,status,log):
48     """
49     Format the Tinderbox HTTP post with the data needed
50     for the tinderbox to be happy.
51     """
52
53     from bb import data, build
54     import os,random
55
56     # the variables we will need to send on this form post
57     variables =  {
58         "tree"         : data.getVar('TINDER_TREE',    d, True),
59         "machine_name" : data.getVar('TINDER_MACHINE', d, True),
60         "os"           : os.uname()[0],
61         "os_version"   : os.uname()[2],
62         "compiler"     : "gcc",
63         "clobber"      : data.getVar('TINDER_CLOBBER', d, True)
64     }
65
66     # optionally add the status
67     if status:
68         variables["status"] = str(status)
69
70     # try to load the machine id
71     # we only need on build_status.pl but sending it
72     # always does not hurt
73     try:
74         f = file(data.getVar('TMPDIR',d,True)+'/tinder-machine.id', 'r')
75         id = f.read()
76         variables['machine_id'] = id
77     except:
78         pass
79
80     # the boundary we will need
81     boundary = "----------------------------------%d" % int(random.random()*1000000000000)
82
83     # now format the body
84     body = tinder_form_data( boundary, variables, log )
85
86     return ("multipart/form-data; boundary=%s" % boundary),body
87
88
89 def tinder_build_start(d):
90     """
91     Inform the tinderbox that a build is starting. We do this
92     by posting our name and tree to the build_start.pl script
93     on the server.
94     """
95     from bb import data
96
97     # get the body and type
98     content_type, body = tinder_format_http_post(d,None,None)
99     server = data.getVar('TINDER_HOST', d, True )
100     url    = data.getVar('TINDER_URL',  d, True )
101
102     selector = url + "/xml/build_start.pl"
103
104     #print "selector %s and url %s" % (selector, url)
105
106     # now post it
107     errcode, errmsg, headers, h_file = tinder_http_post(server,selector,content_type, body)
108     #print errcode, errmsg, headers
109     report = h_file.read()
110
111     # now let us find the machine id that was assigned to us
112     search = "<machine id='"
113     report = report[report.find(search)+len(search):]
114     report = report[0:report.find("'")]
115
116     import bb
117     bb.note("Machine ID assigned by tinderbox: %s" % report )
118
119     # now we will need to save the machine number
120     # we will override any previous numbers
121     f = file(data.getVar('TMPDIR', d, True)+"/tinder-machine.id", 'w')
122     f.write(report)
123
124
125 def tinder_send_http(d, status, _log):
126     """
127     Send this log as build status
128     """
129     from bb import data
130
131
132     # get the body and type
133     server = data.getVar('TINDER_HOST', d, True )
134     url    = data.getVar('TINDER_URL',  d, True )
135
136     selector = url + "/xml/build_status.pl"
137
138     # now post it - in chunks of 10.000 charachters
139     new_log = _log
140     while len(new_log) > 0:
141         content_type, body = tinder_format_http_post(d,status,new_log[0:18000])
142         errcode, errmsg, headers, h_file = tinder_http_post(server,selector,content_type, body)
143         #print errcode, errmsg, headers
144         #print h.file.read()
145         new_log = new_log[18000:]
146
147
148 def tinder_print_info(d):
149     """
150     Print the TinderBox Info
151         Including informations of the BaseSystem and the Tree
152         we use.
153     """
154
155     from   bb import data
156     import os
157     # get the local vars
158
159     time    = tinder_time_string()
160     ops     = os.uname()[0]
161     version = os.uname()[2]
162     url     = data.getVar( 'TINDER_URL' , d, True )
163     tree    = data.getVar( 'TINDER_TREE', d, True )
164     branch  = data.getVar( 'TINDER_BRANCH', d, True )
165     srcdate = data.getVar( 'SRCDATE', d, True )
166     machine = data.getVar( 'MACHINE', d, True )
167     distro  = data.getVar( 'DISTRO',  d, True )
168     bbfiles = data.getVar( 'BBFILES', d, True )
169     tarch   = data.getVar( 'TARGET_ARCH', d, True )
170     fpu     = data.getVar( 'TARGET_FPU', d, True )
171     oerev   = data.getVar( 'OE_REVISION', d, True ) or "unknown"
172
173     # there is a bug with tipple quoted strings
174     # i will work around but will fix the original
175     # bug as well
176     output = []
177     output.append("== Tinderbox Info" )
178     output.append("Time: %(time)s" )
179     output.append("OS: %(ops)s" )
180     output.append("%(version)s" )
181     output.append("Compiler: gcc" )
182     output.append("Tinderbox Client: 0.1" )
183     output.append("Tinderbox Client Last Modified: yesterday" )
184     output.append("Tinderbox Protocol: 0.1" )
185     output.append("URL: %(url)s" )
186     output.append("Tree: %(tree)s" )
187     output.append("Config:" )
188     output.append("branch = '%(branch)s'" )
189     output.append("TARGET_ARCH = '%(tarch)s'" )
190     output.append("TARGET_FPU = '%(fpu)s'" )
191     output.append("SRCDATE = '%(srcdate)s'" )
192     output.append("MACHINE = '%(machine)s'" )
193     output.append("DISTRO = '%(distro)s'" )
194     output.append("BBFILES = '%(bbfiles)s'" )
195     output.append("OEREV = '%(oerev)s'" )
196     output.append("== End Tinderbox Client Info" )
197
198     # now create the real output
199     return "\n".join(output) % vars()
200
201
202 def tinder_print_env():
203     """
204     Print the environment variables of this build
205     """
206     from bb import data
207     import os
208
209     time_start = tinder_time_string()
210     time_end   = tinder_time_string()
211
212     # build the environment
213     env = ""
214     for var in os.environ:
215         env += "%s=%s\n" % (var, os.environ[var])
216
217     output = []
218     output.append( "---> TINDERBOX RUNNING env %(time_start)s" )
219     output.append( env )
220     output.append( "<--- TINDERBOX FINISHED (SUCCESS) %(time_end)s" )
221
222     return "\n".join(output) % vars()
223
224 def tinder_tinder_start(d, event):
225     """
226     PRINT the configuration of this build
227     """
228
229     time_start = tinder_time_string()
230     config = tinder_print_info(d)
231     #env    = tinder_print_env()
232     time_end   = tinder_time_string()
233     packages = " ".join( event.getPkgs() ) 
234
235     output = []
236     output.append( "---> TINDERBOX PRINTING CONFIGURATION %(time_start)s" )
237     output.append( config )
238     #output.append( env    )
239     output.append( "<--- TINDERBOX FINISHED PRINTING CONFIGURATION %(time_end)s" )
240     output.append( "---> TINDERBOX BUILDING '%(packages)s'" )
241     output.append( "<--- TINDERBOX STARTING BUILD NOW" )
242
243     output.append( "" ) 
244  
245     return "\n".join(output) % vars()
246
247 def tinder_do_tinder_report(event):
248     """
249     Report to the tinderbox:
250         On the BuildStart we will inform the box directly
251         On the other events we will write to the TINDER_LOG and
252         when the Task is finished we will send the report.
253
254     The above is not yet fully implemented. Currently we send
255     information immediately. The caching/queuing needs to be
256     implemented. Also sending more or less information is not
257     implemented yet.
258     """
259     from bb.event import getName
260     from bb import data, mkdirhier, build
261     import os, glob
262
263     # variables
264     name = getName(event)
265     log  = ""
266     status = 1
267     #print asd 
268     # Check what we need to do Build* shows we start or are done
269     if name == "BuildStarted":
270         tinder_build_start(event.data)
271         log = tinder_tinder_start(event.data,event)
272
273         try:
274             # truncate the tinder log file
275             f = file(data.getVar('TINDER_LOG', event.data, True), 'rw+')
276             f.truncate(0)
277             f.close()
278         except IOError:
279             pass
280
281     # Append the Task-Log (compile,configure...) to the log file
282     # we will send to the server
283     if name == "TaskSucceeded" or name == "TaskFailed":
284         log_file = glob.glob("%s/log.%s.*" % (data.getVar('T', event.data, True), event.task))
285
286         if len(log_file) != 0:
287             to_file  = data.getVar('TINDER_LOG', event.data, True)
288             log     += "".join(open(log_file[0], 'r').readlines())
289
290     # set the right 'HEADER'/Summary for the TinderBox
291     if name == "TaskStarted":
292         log += "---> TINDERBOX Task %s started\n" % event.task
293     elif name == "TaskSucceeded":
294         log += "<--- TINDERBOX Task %s done (SUCCESS)\n" % event.task
295     elif name == "TaskFailed":
296         log += "<--- TINDERBOX Task %s failed (FAILURE)\n" % event.task
297     elif name == "PkgStarted":
298         log += "---> TINDERBOX Package %s started\n" % data.getVar('P', event.data, True)
299     elif name == "PkgSucceeded":
300         log += "<--- TINDERBOX Package %s done (SUCCESS)\n" % data.getVar('P', event.data, True)
301     elif name == "PkgFailed":
302         build.exec_task('do_clean', event.data)
303         log += "<--- TINDERBOX Package %s failed (FAILURE)\n" % data.getVar('P', event.data, True)
304         status = 200
305     elif name == "BuildCompleted":
306         log += "Build Completed\n"
307         status = 100
308     elif name == "MultipleProviders":
309         log += "---> TINDERBOX Multiple Providers\n"
310         log += "multiple providers are available (%s);\n" % ", ".join(event.getCandidates())
311         log += "consider defining PREFERRED_PROVIDER_%s\n" % event.getItem()
312         log += "is runtime: %d\n" % event.isRuntime()
313         log += "<--- TINDERBOX Multiple Providers\n"
314     elif name == "NoProvider":
315         log += "Error: No Provider for: %s\n" % event.getItem()
316         log += "Error:Was Runtime: %d\n" % event.isRuntime()
317         status = 200
318
319     # now post the log
320     if len(log) == 0:
321         return
322
323     # for now we will use the http post method as it is the only one
324     log_post_method = tinder_send_http
325     log_post_method(event.data, status, log)
326
327
328 # we want to be an event handler
329 addhandler tinderclient_eventhandler
330 python tinderclient_eventhandler() {
331     from bb import note, error, data
332     from bb.event import NotHandled
333     do_tinder_report = data.getVar('TINDER_REPORT', e.data, True)
334     if do_tinder_report and do_tinder_report == "1":
335         tinder_do_tinder_report(e)
336
337     return NotHandled
338 }