land: Cherry-pick tridges changes to autobuild.
[kai/samba-autobuild/.git] / script / land.py
1 #!/usr/bin/env python
2 # run tests on all Samba subprojects and push to a git tree on success
3 # Copyright Andrew Tridgell 2010
4 # Copyright Jelmer Vernooij 2010
5 # released under GNU GPL v3 or later
6
7 from subprocess import call, check_call, Popen, PIPE
8 import os, tarfile, sys, time
9 from optparse import OptionParser
10 import smtplib
11 from email.mime.text import MIMEText
12
13 samba_master = os.getenv('SAMBA_MASTER', 'git://git.samba.org/samba.git')
14 samba_master_ssh = os.getenv('SAMBA_MASTER_SSH', 'git+ssh://git.samba.org/data/git/samba.git')
15
16 cleanup_list = []
17
18 os.putenv('CC', "ccache gcc")
19
20 tasks = {
21     "source3" : [ ("autogen", "./autogen.sh", "text/plain"),
22                   ("configure", "./configure.developer ${PREFIX}", "text/plain"),
23                   ("make basics", "make basics", "text/plain"),
24                   ("make", "make -j 4 everything", "text/plain"), # don't use too many processes
25                   ("install", "make install", "text/plain"),
26                   ("test", "TDB_NO_FSYNC=1 make subunit-test", "text/x-subunit") ],
27
28     "source4" : [ ("configure", "./configure.developer ${PREFIX}", "text/plain"),
29                   ("make", "make -j", "text/plain"),
30                   ("install", "make install", "text/plain"),
31                   ("test", "TDB_NO_FSYNC=1 make subunit-test", "text/x-subunit") ],
32
33     "source4/lib/ldb" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
34                           ("make", "make -j", "text/plain"),
35                           ("install", "make install", "text/plain"),
36                           ("test", "make test", "text/plain") ],
37
38     "lib/tdb" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
39                   ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
40                   ("make", "make -j", "text/plain"),
41                   ("install", "make install", "text/plain"),
42                   ("test", "make test", "text/plain") ],
43
44     "lib/talloc" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
45                      ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
46                      ("make", "make -j", "text/plain"),
47                      ("install", "make install", "text/plain"),
48                      ("test", "make test", "text/plain"), ],
49
50     "lib/replace" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
51                       ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
52                       ("make", "make -j", "text/plain"),
53                       ("install", "make install", "text/plain"),
54                       ("test", "make test", "text/plain"), ],
55
56     "lib/tevent" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
57                      ("make", "make -j", "text/plain"),
58                      ("install", "make install", "text/plain"),
59                      ("test", "make test", "text/plain"), ],
60 }
61
62 retry_task = [ ( "retry",
63                  '''set -e
64                 git remote add -t master master %s
65                 git fetch master
66                 while :; do
67                   sleep 60
68                   git describe master/master > old_master.desc
69                   git fetch master
70                   git describe master/master > master.desc
71                   diff old_master.desc master.desc
72                 done
73                ''' % samba_master, "test/plain" ) ]
74
75
76 def run_cmd(cmd, dir=None, show=None, output=False, checkfail=True, shell=False):
77     if show is None:
78         show = options.verbose
79     if show:
80         print("Running: '%s' in '%s'" % (cmd, dir))
81     if output:
82         return Popen(cmd, stdout=PIPE, cwd=dir, shell=shell).communicate()[0]
83     elif checkfail:
84         return check_call(cmd, cwd=dir, shell=shell)
85     else:
86         return call(cmd, cwd=dir, shell=shell)
87
88
89 class TreeStageBuilder(object):
90     """Handle building of a particular stage for a tree.
91     """
92
93     def __init__(self, tree, name, command, output_mime_type, fail_quickly=False):
94         self.tree = tree
95         self.name = name
96         self.command = command
97         self.output_mime_type = output_mime_type
98         self.fail_quickly = fail_quickly
99         self.status = None
100
101     def start(self):
102         if self.output_mime_type == "text/x-subunit":
103             if self.fail_quickly:
104                 self.command += " | %s --fail-immediately" % (os.path.join(os.path.dirname(__file__), "selftest/filter-subunit"))
105             self.command += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
106         print '%s: [%s] Running %s' % (self.name, self.stage, self.command)
107         self.proc = Popen(self.command, shell=True, cwd=self.tree.dir,
108                           stdout=self.tree.stdout, stderr=self.tree.stderr, stdin=self.tree.stdin)
109
110     def poll(self):
111         self.status = self.proc.poll()
112         return self.status
113
114     def kill(self):
115         if self.proc is not None:
116             try:
117                 run_cmd(["killbysubdir", self.tree.sdir], checkfail=False)
118             except OSError:
119                 # killbysubdir doesn't exist ?
120                 pass
121             self.proc.terminate()
122             self.proc.wait()
123             self.proc = None
124
125
126 class TreeBuilder(object):
127     '''handle build of one directory'''
128
129     def __init__(self, name, sequence, fail_quickly=False):
130         self.name = name
131         self.fail_quickly = fail_quickly
132
133         self.tag = self.name.replace('/', '_')
134         self.sequence = sequence
135         self.next = 0
136         self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
137         self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
138         if options.verbose:
139             print("stdout for %s in %s" % (self.name, self.stdout_path))
140             print("stderr for %s in %s" % (self.name, self.stderr_path))
141         if os.path.exists(self.stdout_path):
142             os.unlink(self.stdout_path)
143         if os.path.exists(self.stderr_path):
144             os.unlink(self.stderr_path)
145         self.stdout = open(self.stdout_path, 'w')
146         self.stderr = open(self.stderr_path, 'w')
147         self.stdin  = open(os.devnull, 'r')
148         self.sdir = "%s/%s" % (testbase, self.tag)
149         if name in ['pass', 'fail', 'retry']:
150             self.dir = self.sdir
151         else:
152             self.dir = os.path.join(self.sdir, self.name)
153         self.prefix = os.path.join(testbase, "prefix", self.tag)
154         run_cmd(["rm", "-rf", self.sdir])
155         cleanup_list.append(self.sdir)
156         cleanup_list.append(self.prefix)
157         os.makedirs(self.sdir)
158         run_cmd(["rm",  "-rf", self.sdir])
159         run_cmd(["git", "clone", "--shared", gitroot, self.sdir])
160         self.start_next()
161
162     def start_next(self):
163         if self.next == len(self.sequence):
164             print '%s: Completed OK' % self.name
165             self.done = True
166             return
167         (stage_name, cmd, output_mime_type) = self.sequence[self.next]
168         cmd = cmd.replace("${PREFIX}", "--prefix=%s" % self.prefix)
169         self.stage = TreeStageBuilder(self, stage_name, cmd, output_mime_type, self.fail_quickly)
170         self.next += 1
171
172     def remove_logs(self):
173         os.unlink(self.stdout_path)
174         os.unlink(self.stderr_path)
175
176     @property
177     def status(self):
178         return self.stage.status
179
180     def poll(self):
181         return self.stage.poll()
182
183     def kill(self):
184         self.stage.kill()
185         self.stage = None
186
187     @property
188     def failed(self):
189         return (os.WIFSIGNALED(self.status) or os.WEXITSTATUS(self.status) != 0)
190
191     @property
192     def failure_reason(self):
193         return "%s: [%s] failed '%s' with status %d" % (self.name, self.stage.name, self.stage.cmd, self.stage.status)
194
195
196 class BuildList(object):
197     '''handle build of multiple directories'''
198
199     def __init__(self, tasklist, tasknames):
200         global tasks
201         self.tlist = []
202         self.tail_proc = None
203         self.retry = None
204         if tasknames == ['pass']:
205             tasks = { 'pass' : [ ("pass", '/bin/true', "text/plain") ]}
206         if tasknames == ['fail']:
207             tasks = { 'fail' : [ ("fail", '/bin/false', "text/plain") ]}
208         if tasknames == []:
209             tasknames = tasklist
210         for n in tasknames:
211             b = TreeBuilder(n, tasks[n], not options.fail_slowly)
212             self.tlist.append(b)
213         if options.retry:
214             self.retry = TreeBuilder('retry', retry_task, not options.fail_slowly)
215             self.need_retry = False
216
217     def kill_kids(self):
218         if self.tail_proc is not None:
219             self.tail_proc.terminate()
220             self.tail_proc.wait()
221             self.tail_proc = None
222         if self.retry is not None:
223             self.retry.proc.terminate()
224             self.retry.proc.wait()
225             self.retry = None
226         for b in self.tlist:
227             b.kill()
228
229     def wait_one(self):
230         while True:
231             none_running = True
232             for b in self.tlist:
233                 if b.stage is None:
234                     continue
235                 none_running = False
236                 if b.poll() is None:
237                     continue
238                 b.stage = None
239                 return b
240             if options.retry:
241                 ret = self.retry.proc.poll()
242                 if ret is not None:
243                     self.need_retry = True
244                     self.retry = None
245                     return None
246             if none_running:
247                 return None
248             time.sleep(0.1)
249
250     def run(self):
251         while True:
252             b = self.wait_one()
253             if options.retry and self.need_retry:
254                 self.kill_kids()
255                 print("retry needed")
256                 return (0, None, None, None, "retry")
257             if b is None:
258                 break
259             if b.failed:
260                 self.kill_kids()
261                 return (b.status, b.name, b.stage, b.tag, b.failure_reason)
262             b.start_next()
263         self.kill_kids()
264         return (0, None, None, None, "All OK")
265
266     def tarlogs(self, fname):
267         tar = tarfile.open(fname, "w:gz")
268         for b in self.tlist:
269             tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
270             tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
271         tar.add("autobuild.log")
272         tar.close()
273
274     def remove_logs(self):
275         for b in self.tlist:
276             b.remove_logs()
277
278     def start_tail(self):
279         cmd = "tail -f *.stdout *.stderr"
280         self.tail_proc = Popen(cmd, shell=True, cwd=gitroot)
281
282
283 def cleanup():
284     if options.nocleanup:
285         return
286     print("Cleaning up ....")
287     for d in cleanup_list:
288         run_cmd(["rm", "-rf", d])
289
290
291 def find_git_root(p):
292     '''get to the top of the git repo'''
293     while p != '/':
294         if os.path.isdir(os.path.join(p, ".git")):
295             return p
296         p = os.path.abspath(os.path.join(p, '..'))
297     return None
298
299
300 def daemonize(logfile):
301     pid = os.fork()
302     if pid == 0: # Parent
303         os.setsid()
304         pid = os.fork()
305         if pid != 0: # Actual daemon
306             os._exit(0)
307     else: # Grandparent
308         os._exit(0)
309
310     import resource      # Resource usage information.
311     maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
312     if maxfd == resource.RLIM_INFINITY:
313         maxfd = 1024 # Rough guess at maximum number of open file descriptors.
314     for fd in range(0, maxfd):
315         try:
316             os.close(fd)
317         except OSError:
318             pass
319     os.open(logfile, os.O_RDWR | os.O_CREAT)
320     os.dup2(0, 1)
321     os.dup2(0, 2)
322
323
324 def rebase_tree(url):
325     print("Rebasing on %s" % url)
326     run_cmd(["git", "remote", "add", "-t", "master", "master", url], show=True, dir=test_master)
327     run_cmd(["git", "fetch", "master"], show=True, dir=test_master)
328     if options.fix_whitespace:
329         run_cmd(["git", "rebase", "--whitespace=fix", "master/master"], show=True, dir=test_master)
330     else:
331         run_cmd(["git", "rebase", "master/master"], show=True, dir=test_master)
332     diff = run_cmd(["git", "--no-pager", "diff", "HEAD", "master/master"], dir=test_master, output=True)
333     if diff == '':
334         print("No differences between HEAD and master/master - exiting")
335         sys.exit(0)
336
337 def push_to(url):
338     print("Pushing to %s" % url)
339     if options.mark:
340         run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master, shell=True)
341         # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
342         # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
343     run_cmd(["git", "remote", "add", "-t", "master", "pushto", url], show=True, dir=test_master)
344     run_cmd(["git", "push", "pushto", "+HEAD:master"], show=True, dir=test_master)
345
346 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
347
348 parser = OptionParser()
349 parser.add_option("", "--repository", help="repository to run tests for", default=None, type=str)
350 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
351 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
352 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
353 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
354                   default=def_testbase)
355 parser.add_option("", "--passcmd", help="command to run on success", default=None)
356 parser.add_option("", "--verbose", help="show all commands as they are run",
357                   default=False, action="store_true")
358 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
359                   default=None, type='str')
360 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
361                   default=False, action='store_true')
362 parser.add_option("", "--pushto", help="push to a git url on success",
363                   default=None, type='str')
364 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
365                   default=False, action='store_true')
366 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
367                   default=False, action="store_true")
368 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
369                   default=False, action="store_true")
370 parser.add_option("", "--retry", help="automatically retry if master changes",
371                   default=False, action="store_true")
372 parser.add_option("", "--email", help="send email to the given address on failure",
373                   type='str', default=None)
374 parser.add_option("", "--always-email", help="always send email, even on success",
375                   action="store_true")
376 parser.add_option("", "--daemon", help="daemonize after initial setup",
377                   action="store_true")
378 parser.add_option("", "--fail-slowly", help="continue running tests even after one has already failed",
379                   action="store_true")
380
381
382 def email_failure(status, failed_task, failed_stage, failed_tag, errstr):
383     '''send an email to options.email about the failure'''
384     user = os.getenv("USER")
385     text = '''
386 Dear Developer,
387
388 Your autobuild failed when trying to test %s with the following error:
389    %s
390
391 the autobuild has been abandoned. Please fix the error and resubmit.
392
393 You can see logs of the failed task here:
394
395   http://git.samba.org/%s/samba-autobuild/%s.stdout
396   http://git.samba.org/%s/samba-autobuild/%s.stderr
397
398 A summary of the autobuild process is here:
399
400   http://git.samba.org/%s/samba-autobuild/autobuild.log
401
402 or you can get full logs of all tasks in this job here:
403
404   http://git.samba.org/%s/samba-autobuild/logs.tar.gz
405
406 The top commit for the tree that was built was:
407
408 %s
409
410 ''' % (failed_task, errstr, user, failed_tag, user, failed_tag, user, user, top_commit_msg)
411     msg = MIMEText(text)
412     msg['Subject'] = 'autobuild failure for task %s during %s' % (failed_task, failed_stage)
413     msg['From'] = 'autobuild@samba.org'
414     msg['To'] = options.email
415
416     s = smtplib.SMTP()
417     s.connect()
418     s.sendmail(msg['From'], [msg['To']], msg.as_string())
419     s.quit()
420
421 def email_success():
422     '''send an email to options.email about a successful build'''
423     user = os.getenv("USER")
424     text = '''
425 Dear Developer,
426
427 Your autobuild has succeeded.
428
429 '''
430
431     if options.keeplogs:
432         text += '''
433
434 you can get full logs of all tasks in this job here:
435
436   http://git.samba.org/%s/samba-autobuild/logs.tar.gz
437
438 ''' % user
439
440     text += '''
441 The top commit for the tree that was built was:
442
443 %s
444 ''' % top_commit_msg
445
446     msg = MIMEText(text)
447     msg['Subject'] = 'autobuild success'
448     msg['From'] = 'autobuild@samba.org'
449     msg['To'] = options.email
450
451     s = smtplib.SMTP()
452     s.connect()
453     s.sendmail(msg['From'], [msg['To']], msg.as_string())
454     s.quit()
455
456
457 (options, args) = parser.parse_args()
458
459 if options.retry:
460     if not options.rebase_master and options.rebase is None:
461         raise Exception('You can only use --retry if you also rebase')
462
463 testbase = "%s/b%u" % (options.testbase, os.getpid())
464 test_master = os.path.join(testbase, "master")
465
466 if options.repository is not None:
467     repository = options.repository
468 else:
469     repository = os.getcwd()
470
471 gitroot = find_git_root(repository)
472 if gitroot is None:
473     raise Exception("Failed to find git root under %s" % repository)
474
475 # get the top commit message, for emails
476 top_commit_msg = run_cmd("git log -1", dir=gitroot, output=True)
477
478 try:
479     os.makedirs(testbase)
480 except Exception, reason:
481     raise Exception("Unable to create %s : %s" % (testbase, reason))
482 cleanup_list.append(testbase)
483
484 if options.daemon:
485     logfile = os.path.join(testbase, "log")
486     print "Forking into the background, writing progress to %s" % logfile
487     daemonize(logfile)
488
489 while True:
490     try:
491         run_cmd(["rm", "-rf", test_master])
492         cleanup_list.append(test_master)
493         run_cmd(["git", "clone", "--shared", gitroot, test_master])
494     except:
495         cleanup()
496         raise
497
498     try:
499         if options.rebase is not None:
500             rebase_tree(options.rebase)
501         elif options.rebase_master:
502             rebase_tree(samba_master)
503         blist = BuildList(tasks, args)
504         if options.tail:
505             blist.start_tail()
506         (status, failed_task, failed_stage, failed_tag, errstr) = blist.run()
507         if status != 0 or errstr != "retry":
508             break
509         cleanup()
510     except:
511         cleanup()
512         raise
513
514 blist.kill_kids()
515 if options.tail:
516     print("waiting for tail to flush")
517     time.sleep(1)
518
519 if status == 0:
520     print errstr
521     if options.passcmd is not None:
522         print("Running passcmd: %s" % options.passcmd)
523         run_cmd(options.passcmd, dir=test_master, shell=True)
524     if options.pushto is not None:
525         push_to(options.pushto)
526     elif options.push_master:
527         push_to(samba_master_ssh)
528     if options.keeplogs:
529         blist.tarlogs("logs.tar.gz")
530         print("Logs in logs.tar.gz")
531     if options.always_email:
532         email_success()
533     blist.remove_logs()
534     cleanup()
535     print(errstr)
536 else:
537     # something failed, gather a tar of the logs
538     blist.tarlogs("logs.tar.gz")
539
540     if options.email is not None:
541         email_failure(status, failed_task, failed_stage, failed_tag, errstr)
542
543     cleanup()
544     print(errstr)
545     print("Logs in logs.tar.gz")
546 sys.exit(status)