summaryrefslogtreecommitdiffstats
path: root/callback_plugins
diff options
context:
space:
mode:
authorToshio くらとみ <toshio@batcave01.phx2.fedoraproject.org>2016-01-30 04:54:20 +0000
committerToshio くらとみ <toshio@batcave01.phx2.fedoraproject.org>2016-01-30 04:54:20 +0000
commita9062410e556352e3c971084f2c4df8930c462df (patch)
tree64bac04d328ef8c057dd7cb167da0f06532be41a /callback_plugins
parent00856a9bac2c8993e7fa7291af0981d990c49369 (diff)
downloadansible-a9062410e556352e3c971084f2c4df8930c462df.tar.gz
ansible-a9062410e556352e3c971084f2c4df8930c462df.tar.xz
ansible-a9062410e556352e3c971084f2c4df8930c462df.zip
fedmsg callback ported to v2
Diffstat (limited to 'callback_plugins')
-rw-r--r--callback_plugins/fedmsg_callback2.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/callback_plugins/fedmsg_callback2.py b/callback_plugins/fedmsg_callback2.py
new file mode 100644
index 000000000..391191385
--- /dev/null
+++ b/callback_plugins/fedmsg_callback2.py
@@ -0,0 +1,104 @@
+# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
+# based on the log_plays example
+# skvidal@fedoraproject.org
+# rbean@redhat.com
+
+# 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
+
+import fedmsg
+import fedmsg.config
+
+try:
+ from ansible.plugins.callback import CallbackBase
+except ImportError:
+ # Ansible v1 compat
+ CallbackBase = object
+
+def getlogin():
+ try:
+ user = os.getlogin()
+ except OSError, e:
+ user = pwd.getpwuid(os.geteuid())[0]
+ return user
+
+
+class CallbackModule(CallbackBase):
+ """ Publish playbook starts and stops to fedmsg. """
+
+ CALLBACK_NAME = 'fedmsg_callback2'
+ CALLBACK_TYPE = 'notification'
+ CALLBACK_VERSION = 2.0
+ CALLBACK_NEEDS_WHITELIST = True
+
+ playbook_path = None
+
+ def __init__(self):
+ config = fedmsg.config.load_config()
+ config.update(dict(
+ name='relay_inbound',
+ cert_prefix='shell',
+ active=True,
+ ))
+ # It seems like recursive playbooks call this over and over again and
+ # fedmsg doesn't like to be initialized more than once. So, here, just
+ # catch that and ignore it.
+ try:
+ fedmsg.init(**config)
+ except ValueError:
+ pass
+ self.play = None
+
+ super(CallbackModule, self).__init__()
+
+ 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 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:
+ fedmsg.publish(
+ modname="ansible", topic="playbook.start",
+ msg=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,
+ ),
+ )
+ 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])
+ fedmsg.publish(
+ modname="ansible", topic="playbook.complete",
+ msg=dict(
+ playbook=self.playbook_path,
+ userid=getlogin(),
+ results=results,
+ ),
+ )