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