diff --git a/doc/configuration.rst b/doc/configuration.rst index 51a112d..a8213f4 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -403,6 +403,63 @@ the users in the admin groups listed above as well as admin rights to all projects hosted on this pagure instance. +Stomp Options +------------- + +Pagure integration with Stomp allows you to emit messages to any +stomp-compliant message bus. + +STOMP_NOTIFICATIONS +~~~~~~~~~~~~~~~~~~~ + +This configuration key allows to turn on or off notifications via +`stomp protocol `_. All other stomp-related +settings don't need to be present if this is set to ``False``. + +Defaults to: ``False``. + +STOMP_BROKERS +~~~~~~~~~~~~~ + +List of 2-tuples with broker domain names and ports. For example +``[('primary.msg.bus.com', 6543), ('backup.msg.bus.com`, 6543)]``. + +STOMP_HIERARCHY +~~~~~~~~~~~~~~~ + +Base name of the hierarchy to emit messages to. For example +``/queue/some.hierarchy.``. Note that this **must** end with +a dot. Pagure will append queue names such as ``project.new`` +to this value, resulting in queue names being e.g. +``/queue/some.hierarchy.project.new``. + +STOMP_SSL +~~~~~~~~~ + +Whether or not to use SSL when connecting to message brokers. + +Defaults to: ``False``. + +STOMP_KEY_FILE +~~~~~~~~~~~~~~ + +Absolute path to key file for SSL connection. Only required if +``STOMP_SSL`` is set to ``True``. + +STOMP_CERT_FILE +~~~~~~~~~~~~~~~ + +Absolute path to certificate file for SSL connection. Only required if +``STOMP_SSL`` is set to ``True``. + +STOMP_CREDS_PASSWORD +~~~~~~~~~~~~~~~~~~~~ + +Password for decoding ``STOMP_CERT_FILE`` and ``STOMP_KEY_FILE``. Only +required if ``STOMP_SSL`` is set to ``True`` and credentials files are +password-encoded. + + API token ACLs -------------- diff --git a/pagure/default_config.py b/pagure/default_config.py index 271a56f..43a12fe 100644 --- a/pagure/default_config.py +++ b/pagure/default_config.py @@ -329,6 +329,14 @@ FLAG_PENDING = 'pending' # easy denial of service to the system if enabled. ALLOW_PROJECT_DOWAIT = False +# Settings for Stomp message sending +STOMP_NOTIFICATIONS = False +STOMP_BROKERS = [] +STOMP_SSL = False +STOMP_KEY_FILE = None +STOMP_CERT_FILE = None +STOMP_CREDS_PASSWORD = None +STOMP_HIERARCHY = None LOGGING = { 'version': 1, diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 8738f54..4725b24 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -526,6 +526,7 @@ class Project(BASE): 'always_merge': False, 'issues_default_to_private': False, 'fedmsg_notifications': True, + 'stomp_notifications': True, 'pull_request_access_only': False, 'roadmap_on_issues_page': False, 'notify_on_pull-request_flag': False, diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index ee18148..1498ac1 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -16,6 +16,7 @@ from __future__ import print_function import datetime import hashlib +import json import logging import urlparse import re @@ -57,6 +58,42 @@ def fedmsg_publish(*args, **kwargs): # pragma: no cover _log.exception('Error sending fedmsg') +stomp_conn = None + + +def stomp_publish(topic, message): + ''' Try to publish a message on a Stomp-compliant message bus. ''' + if not pagure_config.get('STOMP_NOTIFICATIONS', True): + return + # We catch Exception if we want :-p + # pylint: disable=broad-except + # Ignore message about fedmsg import + # pylint: disable=import-error + try: + import stomp + global stomp_conn + if not stomp_conn: + stomp_conn = stomp.Connection12(pagure_config['STOMP_BROKERS']) + if pagure_config.get('STOMP_SSL'): + stomp_conn.set_ssl( + pagure_config['STOMP_BROKERS'], + key_file=pagure_config.get('STOMP_KEY_FILE'), + cert_file=pagure_config.get('STOMP_CERT_FILE'), + password=pagure_config.get('STOMP_CREDS_PASSWORD'), + ) + from stomp import PrintingListener + stomp_conn.set_listener('', PrintingListener()) + stomp_conn.start() + stomp_conn.connect(wait=True) + hierarchy = pagure_config['STOMP_HIERARCHY'] + stomp_conn.send( + destination=hierarchy + topic, + body=json.dumps(message) + ) + except Exception: + _log.exception('Error sending stomp message') + + def log(project, topic, msg, redis=None): ''' This is the place where we send notifications to user about actions occuring in pagure. @@ -67,6 +104,11 @@ def log(project, topic, msg, redis=None): and not project.private): fedmsg_publish(topic, msg) + # Send stomp notification (if stomp is there and set-up) + if not project or (project.settings.get('stomp_notifications', True) + and not project.private): + stomp_publish(topic, msg) + if redis and project and not project.private: pagure.lib.tasks_services.webhook_notification.delay( topic=topic,