From 45bdec473449ec9c8b1468a810f105894faa4806 Mon Sep 17 00:00:00 2001
From: Karsten Hopp <karsten@redhat.com>
Date: Fri, 8 Nov 2019 22:34:47 +0100
Subject: [PATCH 13/14] add callbacks for fedora-messaging

Signed-off-by: Karsten Hopp <karsten@redhat.com>
---
 callback_plugins/fedora_messaging_callback.py |  85 +++++++++++++++
 .../fedora_messaging_callback2.py             | 103 ++++++++++++++++++
 2 files changed, 188 insertions(+)
 create mode 100644 callback_plugins/fedora_messaging_callback.py
 create mode 100644 callback_plugins/fedora_messaging_callback2.py

diff --git a/callback_plugins/fedora_messaging_callback.py b/callback_plugins/fedora_messaging_callback.py
new file mode 100644
index 000000000..31e918dde
--- /dev/null
+++ b/callback_plugins/fedora_messaging_callback.py
@@ -0,0 +1,85 @@
+# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
+# based on the log_plays example
+# skvidal@fedoraproject.org
+# rbean@redhat.com
+# karsten@redhat.com  changes for fedora-messaging
+
+# Ansible 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.
+#
+# Ansible 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 Ansible.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import pwd
+
+from fedora_messaging.api import Message, publish
+from fedora_messaging.exceptions import PublishReturned, ConnectionException
+
+try:
+    from ansible.plugins.callback import CallbackBase
+except ImportError:
+    # Ansible v1 compat
+    CallbackBase = object
+
+
+def getlogin():
+    try:
+        user = os.getlogin()
+    except OSError as e:
+        user = pwd.getpwuid(os.geteuid())[0]
+    return user
+
+
+class CallbackModule(CallbackBase):
+    """ Publish playbook starts and stops to fedora-messaging. """
+
+    playbook_path = None
+
+    def __init__(self):
+        pass
+
+    def playbook_on_play_start(self, pattern):
+        # This gets called once for each play.. but we just issue a message once
+        # for the first one.  One per "playbook"
+        play = getattr(self, "play", None)
+        if play:
+            # figure out where the playbook FILE is
+            path = os.path.abspath(play.playbook.filename)
+
+            # Bail out early without publishing if we're in --check mode
+            if play.playbook.check:
+                return
+
+            if not self.playbook_path:
+                msg = Message(
+                    topic="ansible.playbook.start",
+                    body=dict(
+                        playbook=path,
+                        userid=getlogin(),
+                        extra_vars=play.playbook.extra_vars,
+                        inventory=play.playbook.inventory.host_list,
+                        playbook_checksum=play.playbook.check,
+                        check=play.playbook.check,
+                    ),
+                )
+                publish(msg)
+                self.playbook_path = path
+
+    def playbook_on_stats(self, stats):
+        if not self.playbook_path:
+            return
+
+        results = dict([(h, stats.summarize(h)) for h in stats.processed])
+        msg = Message(
+            topic="ansible.playbook.complete",
+            body=dict(playbook=self.playbook_path, userid=getlogin(), results=results),
+        )
+        publish(msg)
diff --git a/callback_plugins/fedora_messaging_callback2.py b/callback_plugins/fedora_messaging_callback2.py
new file mode 100644
index 000000000..3d1e357ac
--- /dev/null
+++ b/callback_plugins/fedora_messaging_callback2.py
@@ -0,0 +1,103 @@
+# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
+# based on the log_plays example
+# skvidal@fedoraproject.org
+# rbean@redhat.com
+# karsten@redhat.com  changes for fedora-messaging
+
+# Ansible 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.
+#
+# Ansible 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 Ansible.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import pwd
+
+from fedora_messaging.api import Message, publish
+from fedora_messaging.exceptions import PublishReturned, ConnectionException
+
+try:
+    from ansible.plugins.callback import CallbackBase
+except ImportError:
+    # Ansible v1 compat
+    CallbackBase = object
+
+try:
+    from ansible.utils.hashing import secure_hash
+except ImportError:
+    from ansible.utils import md5 as secure_hash
+
+
+def getlogin():
+    try:
+        user = os.getlogin()
+    except OSError as e:
+        user = pwd.getpwuid(os.geteuid())[0]
+    return user
+
+
+class CallbackModule(CallbackBase):
+    """ Publish playbook starts and stops to fedora_messaging. """
+
+    CALLBACK_NAME = "fedora_messaging_callback2"
+    CALLBACK_TYPE = "notification"
+    CALLBACK_VERSION = 2.0
+    CALLBACK_NEEDS_WHITELIST = True
+
+    playbook_path = None
+
+    def __init__(self):
+        self.play = None
+        self.playbook = None
+
+        super(CallbackModule, self).__init__()
+
+    def set_play_context(self, play_context):
+        self.play_context = play_context
+
+    def v2_playbook_on_start(self, playbook):
+        self.playbook = playbook
+
+    def v2_playbook_on_play_start(self, play):
+        # This gets called once for each play.. but we just issue a message once
+        # for the first one.  One per "playbook"
+        if self.playbook:
+            # figure out where the playbook FILE is
+            path = os.path.abspath(self.playbook._file_name)
+
+            # Bail out early without publishing if we're in --check mode
+            if self.play_context.check_mode:
+                return
+
+            if not self.playbook_path:
+                msg = Message(
+                    topic="ansible.playbook.start",
+                    body=dict(
+                        playbook=path,
+                        userid=getlogin(),
+                        extra_vars=play._variable_manager.extra_vars,
+                        inventory=play._variable_manager._inventory._sources,
+                        playbook_checksum=secure_hash(path),
+                        check=self.play_context.check_mode,
+                    ),
+                )
+                publish(msg)
+                self.playbook_path = path
+
+    def v2_playbook_on_stats(self, stats):
+        if not self.playbook_path:
+            return
+
+        results = dict([(h, stats.summarize(h)) for h in stats.processed])
+        msg = Message(
+            topic="ansible.playbook.complete",
+            body=dict(playbook=self.playbook_path, userid=getlogin(), results=results),
+        )
+        publish(msg)
-- 
2.21.0

