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