Ansible launcher: add job result to console log

As well as a nice generalized way to append to the log with anisble.

Change-Id: If1c5810a9219abaf9b80be0e075aa3056a415004
diff --git a/zuul/ansible/library/zuul_log.py b/zuul/ansible/library/zuul_log.py
new file mode 100644
index 0000000..e3a3458
--- /dev/null
+++ b/zuul/ansible/library/zuul_log.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+
+# Copyright (c) 2016 IBM Corp.
+# Copyright (c) 2016 Red Hat
+#
+# This module is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This software is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this software.  If not, see <http://www.gnu.org/licenses/>.
+
+import datetime
+
+
+class Console(object):
+    def __enter__(self):
+        self.logfile = open('/tmp/console.txt', 'a', 0)
+        return self
+
+    def __exit__(self, etype, value, tb):
+        self.logfile.close()
+
+    def addLine(self, ln):
+        ts = datetime.datetime.now()
+        outln = '%s %s' % (str(ts), ln)
+        self.logfile.write(outln)
+
+
+def log(msg):
+    with Console() as console:
+        console.addLine("[Zuul] %s\n" % msg)
+
+
+def main():
+    module = AnsibleModule(
+        argument_spec=dict(
+            msg=dict(required=True, default=''),
+        )
+    )
+
+    p = module.params
+    ret = log(p['msg'])
+    module.exit_json(changed=True, rc=ret)
+
+from ansible.module_utils.basic import *  # noqa
+
+if __name__ == '__main__':
+    main()
diff --git a/zuul/ansible/library/zuul_runner.py b/zuul/ansible/library/zuul_runner.py
index a230448..bf32a5c 100644
--- a/zuul/ansible/library/zuul_runner.py
+++ b/zuul/ansible/library/zuul_runner.py
@@ -69,7 +69,7 @@
             console.addLine(line)
 
         ret = proc.wait()
-        console.addLine("[Zuul] Exit code: %s\n" % ret)
+        console.addLine("[Zuul] Task exit code: %s\n" % ret)
     return ret
 
 
diff --git a/zuul/launcher/ansiblelaunchserver.py b/zuul/launcher/ansiblelaunchserver.py
index b068eab..a6f18c6 100644
--- a/zuul/launcher/ansiblelaunchserver.py
+++ b/zuul/launcher/ansiblelaunchserver.py
@@ -770,26 +770,38 @@
 
         with open(jobdir.playbook, 'w') as playbook:
             tasks = []
+            main_block = []
+            error_block = []
+            tasks.append(dict(block=main_block,
+                              rescue=error_block))
 
             task = dict(file=dict(path='/tmp/console.txt', state='absent'))
-            tasks.append(task)
+            main_block.append(task)
 
             task = dict(zuul_console=dict(path='/tmp/console.txt', port=8088))
-            tasks.append(task)
+            main_block.append(task)
 
             task = dict(file=dict(path=parameters['WORKSPACE'],
                                   state='directory'))
-            tasks.append(task)
+            main_block.append(task)
 
             # TODO: remove once zuul-worker DIB element has landed
-            tasks.append(dict(shell="[ -f /usr/bin/yum ] && "
-                              "sudo /usr/bin/yum install libselinux-python || "
-                              "/bin/true"))
+            main_block.append(dict(shell="[ -f /usr/bin/yum ] && "
+                                   "sudo /usr/bin/yum install "
+                                   "libselinux-python || "
+                                   "/bin/true"))
 
             for builder in jjb_job.get('builders', []):
                 if 'shell' in builder:
-                    tasks.extend(self._makeBuilderTask(jobdir, builder,
-                                                       parameters, timeout))
+                    main_block.extend(
+                        self._makeBuilderTask(jobdir, builder,
+                                              parameters, timeout))
+            task = dict(zuul_log=dict(msg="Job complete, result: SUCCESS"))
+            main_block.append(task)
+
+            task = dict(zuul_log=dict(msg="Job complete, result: FAILURE"))
+            error_block.append(task)
+
             play = dict(hosts='node', name='Job body',
                         tasks=tasks)
             playbook.write(yaml.dump([play]))