From a949ff0abd3ac602335096e4b273db4165f971ad Mon Sep 17 00:00:00 2001 From: jingjing Date: Dec 13 2018 18:59:33 +0000 Subject: Add support for the MQTT message bus Fixes #3869 --- diff --git a/pagure/default_config.py b/pagure/default_config.py index 59f7a17..5d9c209 100644 --- a/pagure/default_config.py +++ b/pagure/default_config.py @@ -413,6 +413,19 @@ 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_CA_CERTS = None +MQTT_CERTFILE = None +MQTT_KEYFILE = None +MQTT_CERT_REQS = None +MQTT_TLS_VERSION = None +MQTT_CIPHERS = None + # Settings for Stomp message sending STOMP_NOTIFICATIONS = False STOMP_BROKERS = [] diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 6f4252b..d0562a8 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 2ee36b8..4c35103 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -98,12 +98,44 @@ def stomp_publish(topic, message): except Exception: _log.exception("Error sending stomp message") - def blinker_publish(topic, message): _log.info("Sending blinker signal to: pagure - topic: %s", topic) ready = blinker.signal("pagure") ready.send("pagure", topic=topic, message=message) +def mqtt_publish(topic, message): + """ 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_ca_certs = pagure_config.get("MQTT_CA_CERTS") + mqtt_certfile = pagure_config.get("MQTT_CERTFILE") + mqtt_keyfile = pagure_config.get("MQTT_KEYFILE") + mqtt_cert_reqs = pagure_config.get("MQTT_CERT_REQS", "ssl.CERT_REQUIRED") + mqtt_tls_version = pagure_config.get("MQTT_TLS_VERSION","ssl.PROTOCOL_TLS") + mqtt_ciphers = pagure_config.get("MQTT_CIPHERS") + + client = mqtt.Client(os.uname()[1]) + client.tls_set(ca_certs=mqtt_ca_certs, certfile=mqtt_certfile, keyfile=mqtt_keyfile, cert_reqs=mqtt_cert_reqs, tls_version=mqtt_tls_version, cliphers=mqtt_ciphers) + client.username_pw_set(mqtt_username, mqtt_pass) + client.connect(mqtt_host, mqtt_port) + client.publish(topic, json.dumps(message)) + client.disconnect() + + except Exception: + _log.exception("Error sending mqtt message") + def log(project, topic, msg, webhook=True): """ This is the place where we send notifications to user about actions