Port scripts/fedmsg_notification.py to fedora-messaging

This commit also adds a --config argument allowing to
override/specify a specific fedora-messaging configuration
file to use.

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
This commit is contained in:
Pierre-Yves Chibon 2020-06-16 17:26:22 +02:00
parent b6605827b3
commit ad1a3360bc

View File

@ -1,26 +1,45 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import fedmsg
import fedmsg.config
import fedora_messaging.api
import fedora_messaging.config
import fedora_messaging.exceptions
import json
import sys
def send(cmd, data):
topic = "compose.%s" % cmd.replace("-", ".").lower()
fedmsg.publish(topic=topic, modname="pungi", msg=data)
try:
msg = fedora_messaging.api.Message(topic="pungi.{}".format(topic), body=data)
fedora_messaging.api.publish(msg)
except fedora_messaging.exceptions.PublishReturned as e:
print("Fedora Messaging broker rejected message %s: %s" % (msg.id, e))
sys.exit(1)
except fedora_messaging.exceptions.ConnectionException as e:
print("Error sending message %s: %s" % (msg.id, e))
sys.exit(1)
except Exception as e:
print("Error sending fedora-messaging message: %s" % (e))
sys.exit(1)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("cmd")
parser.add_argument(
"--config",
dest="config",
help="fedora-messaging configuration file to use. "
"This allows overriding the default "
"/etc/fedora-messaging/config.toml.",
)
opts = parser.parse_args()
config = fedmsg.config.load_config()
config["active"] = True # Connect out to a fedmsg-relay instance
config["cert_prefix"] = "releng" # Use this cert.
fedmsg.init(**config)
if opts.config:
fedora_messaging.config.conf.load_config(opts.config)
data = json.load(sys.stdin)
send(opts.cmd, data)