From 698b58227b12f584514aa818f3e3d9cbe6205a1e Mon Sep 17 00:00:00 2001 From: jingjing Date: Oct 16 2018 08:15:04 +0000 Subject: Add support for the MQTT message bus Fixes #3869 --- diff --git a/pagure/default_config.py b/pagure/default_config.py index 2a13a42..1cc3f86 100644 --- a/pagure/default_config.py +++ b/pagure/default_config.py @@ -390,6 +390,14 @@ FLAG_PENDING = "pending" # easy denial of service to the system if enabled. ALLOW_PROJECT_DOWAIT = False +# Settings for MQTT message sending +MQTT_NOTIFICATIONS = False +MQTT_HOST = None +MQTT_PORT = None +MQTT_USERNAME = None +MQTT_PASSWORD = None +MQTT_CACERT = None + # Settings for Stomp message sending STOMP_NOTIFICATIONS = False STOMP_BROKERS = [] diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 2725665..0254641 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -627,6 +627,7 @@ class Project(BASE): "issues_default_to_private": False, "fedmsg_notifications": True, "stomp_notifications": True, + "mqtt_notifications": True, "pull_request_access_only": False, "notify_on_pull-request_flag": False, "notify_on_commit_flag": False, diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index 20b8eea..32b99fe 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -98,6 +98,35 @@ def stomp_publish(topic, message): _log.exception("Error sending stomp message") +def mqtt_publish(topic, msg): + """ Try to publish a message on a MQTT message bus. """ + if not pagure_config.get("MQTT_NOTIFICATIONS", True): + return + # We catch Exception if we want :-p + # pylint: disable=broad-except + # Ignore message about mqtt import + # pylint: disable=import-error + try: + import paho.mqtt.client as mqtt + import os + + mqtt_host = pagure_config.get("MQTT_HOST") + mqtt_port = pagure_config.get("MQTT_PORT") + mqtt_username = pagure_config.get("MQTT_USERNAME") + mqtt_pass = pagure_config.get("MQTT_PASSWORD") + mqtt_cacert = pagure_config.get("MQTT_CACERT") + + client = mqtt.Client(os.uname()[1]) + client.tls_set(mqtt_cacert, tls_version=2) + client.username_pw_set(mqtt_username, mqtt_pass) + client.connect(mqtt_host, mqtt_port) + client.publish(topic, message) + client.disconnect() + + except Exception: + _log.exception("Error sending mqtt message") + + def log(project, topic, msg, redis=None): """ This is the place where we send notifications to user about actions occuring in pagure.