def publish_message(service, action): """ Send a message using argo messaging service when an action upon a service takes place. """ service_id = str(service.get('id')) service_name = service.get('name') service_data = service.get('data', {}) ams = ArgoMessagingService(endpoint=AMS_ENDPOINT, project=AMS_PROJECT, token=AMS_TOKEN) endpoint = '{0}/api/v2/ext-services/{1}'.format(get_root_url(), service_id) # The value of the data property must be unicode in order to be # encoded in base64 format in AmsMessage data = json.dumps(service_data) try: if not ams.has_topic(AMS_TOPIC): ams.create_topic(AMS_TOPIC) except AmsException as e: print e raise SystemExit(1) msg = AmsMessage(data=data, attributes={ "method": action, "service_id": service_id, "service_name": service_name, "endpoint": endpoint }).dict() try: ret = ams.publish(AMS_TOPIC, msg) print ret except AmsException as e: print e
def main(): parser = ArgumentParser( description="Simple AMS example of subscription pull/consume") parser.add_argument('--host', type=str, default='messaging-devel.argo.grnet.gr', help='FQDN of AMS Service') parser.add_argument('--token', type=str, required=True, help='Given token') parser.add_argument('--project', type=str, required=True, help='Project registered in AMS Service') parser.add_argument('--subscription', type=str, required=True, help='Subscription name') parser.add_argument('--topic', type=str, required=True, help='Topic name') args = parser.parse_args() # initialize service with given token and project try: ams = ArgoMessagingService(endpoint=args.host, token=args.token, project=args.project) if not ams.has_topic(args.topic): ams.create_topic(args.topic) topic = ams.get_topic(args.topic, retobj=True) sub = topic.subscription(args.subscription) msg = sub.pullack(3, timeout=5) if msg: print(msg[0].get_msgid()) except AmsException as e: print e raise SystemExit(1)
class PullPublish(): def __init__(self,config): self.pull_sub = config['pull_sub'] self.pub_topic = config['pub_topic'] self.pull_topic = config['pull_topic'] self.ams = ArgoMessagingService(endpoint=config['host'], token=config['token'], project=config['project']) def pull(self,nummsgs): messages = [] try: if not self.ams.has_sub(self.pull_sub): self.ams.create_sub(self.pull_sub,self.pull_topic) except AmsException as e: print(e) raise SystemExit(1) # try to pull number of messages from subscription. method will # return (ackIds, AmsMessage) tuples from which ackIds and messages # payload will be extracted. ackids = list() for id, msg in self.ams.pull_sub(self.pull_sub, nummsgs): data = msg.get_data() msgid = msg.get_msgid() attr = msg.get_attr() messages.append(json.loads(data)) #print('msgid={0}, data={1}, attr={2}'.format(msgid, data, attr)) ackids.append(id) # pass list of extracted ackIds to AMS Service so that # it can move the offset for the next subscription pull # (basically acknowledging pulled messages) if ackids: self.ams.ack_sub(self.pull_sub, ackids) return messages def publish(self,messages): # messages = [{data:[{id:1},{state:'deployed'}],attributes=''}] try: if not self.ams.has_topic(self.pub_topic): self.ams.create_topic(self.pub_topic) except AmsException as e: print(e) raise SystemExit(1) # publish one message to given topic. message is constructed with # help of AmsMessage which accepts data and attributes keys. # data is Base64 encoded, attributes is dictionary of arbitrary # key/value pairs msg = AmsMessage() msglist = [] for message in messages: msglist.append(msg(data=json.dumps(message['data']),attributes={})) try: ret = self.ams.publish(self.pub_topic, msglist) print(ret) except AmsException as e: print(e)
def main(): parser = ArgumentParser(description="Simple AMS message publish example") parser.add_argument('--host', type=str, default='messaging-devel.argo.grnet.gr', help='FQDN of AMS Service') parser.add_argument('--token', type=str, required=True, help='Given token') parser.add_argument('--project', type=str, required=True, help='Project registered in AMS Service') parser.add_argument('--topic', type=str, required=True, help='Given topic') args = parser.parse_args() # initialize service with given token and project ams = ArgoMessagingService(endpoint=args.host, token=args.token, project=args.project) # ensure that topic is created in first run try: if not ams.has_topic(args.topic): ams.create_topic(args.topic) topic = ams.get_topic(args.topic, retobj=True) except AmsException as e: print e raise SystemExit(1) # publish one message to given topic. message is constructed with # help of AmsMessage which accepts data and attributes keys. # data is Base64 encoded, attributes is dictionary of arbitrary # key/value pairs msg = AmsMessage(data='foo1', attributes={'bar1': 'baz1'}).dict() try: ret = topic.publish(msg) print ret except AmsException as e: print e # publish a list of two messages to given topic. AmsMessage can also be # used as a callable. publish() method accepts either one messages or # list of messages msg = AmsMessage() msglist = [ msg(data='foo2', attributes={'bar2': 'baz2'}), msg(data='foo3', attributes={'bar3': 'baz3'}) ] try: ret = topic.publish(msglist) print ret except AmsException as e: print e
def main(): parser = ArgumentParser(description="Simple AMS example of subscription pull/consume") parser.add_argument('--host', type=str, default='messaging-devel.argo.grnet.gr', help='FQDN of AMS Service') parser.add_argument('--token', type=str, required=True, help='Given token') parser.add_argument('--project', type=str, required=True, help='Project registered in AMS Service') parser.add_argument('--topic', type=str, required=True, help='Given topic') args = parser.parse_args() # initialize service with given token and project try: ams = ArgoMessagingService(endpoint=args.host, token=args.token, project=args.project) ams.create_topic(args.topic) except AmsException as e: print(ams.get_topic(args.topic)) print(e) raise SystemExit(1)
def main(): MSG_NUM = 100 MSG_SIZE = 500 TIMEOUT = 180 parser = ArgumentParser(description="Nagios sensor for AMS") parser.add_argument('-H', dest='host', type=str, default='messaging-devel.argo.grnet.gr', help='FQDN of AMS Service') parser.add_argument('--token', type=str, required=True, help='Given token') parser.add_argument('--project', type=str, required=True, help='Project registered in AMS Service') parser.add_argument('--topic', type=str, default='nagios_sensor_topic', help='Given topic') parser.add_argument('--subscription', type=str, default='nagios_sensor_sub', help='Subscription name') parser.add_argument('-t', dest='timeout', type=int, default=TIMEOUT, help='Timeout') cmd_options = parser.parse_args() nagios = NagiosResponse("All messages received correctly.") ams = ArgoMessagingService(endpoint=cmd_options.host, token=cmd_options.token, project=cmd_options.project) try: if ams.has_topic(cmd_options.topic, timeout=cmd_options.timeout): ams.delete_topic(cmd_options.topic, timeout=cmd_options.timeout) if ams.has_sub(cmd_options.subscription, timeout=cmd_options.timeout): ams.delete_sub(cmd_options.subscription, timeout=cmd_options.timeout) ams.create_topic(cmd_options.topic, timeout=cmd_options.timeout) ams.create_sub(cmd_options.subscription, cmd_options.topic, timeout=cmd_options.timeout) except AmsException as e: nagios.writeCriticalMessage(e.msg) nagios.setCode(nagios.CRITICAL) print(nagios.getMsg()) raise SystemExit(nagios.getCode()) ams_msg = AmsMessage() msg_orig = set() msg_array = [] for i in range(1, MSG_NUM): msg_txt = ''.join( random.choice(string.ascii_letters + string.digits) for i in range(MSG_SIZE)) attr_name = ''.join( random.choice(string.ascii_letters + string.digits) for i in range(4)) attr_value = ''.join( random.choice(string.ascii_letters + string.digits) for i in range(8)) msg_array.append( ams_msg(data=msg_txt, attributes={attr_name: attr_value})) hash_obj = hashlib.md5(msg_txt + attr_name + attr_value) msg_orig.add(hash_obj.hexdigest()) try: msgs = ams.publish(cmd_options.topic, msg_array, timeout=cmd_options.timeout) ackids = [] rcv_msg = set() for id, msg in ams.pull_sub(cmd_options.subscription, MSG_NUM - 1, True, timeout=cmd_options.timeout): attr = msg.get_attr() hash_obj = hashlib.md5(msg.get_data() + attr.keys()[0] + attr.values()[0]) rcv_msg.add(hash_obj.hexdigest()) if ackids: ams.ack_sub(cmd_options.subscription, ackids, timeout=cmd_options.timeout) ams.delete_topic(cmd_options.topic, timeout=cmd_options.timeout) ams.delete_sub(cmd_options.subscription, timeout=cmd_options.timeout) except AmsException as e: nagios.writeCriticalMessage(e.msg) nagios.setCode(nagios.CRITICAL) print(nagios.getMsg()) raise SystemExit(nagios.getCode()) if msg_orig != rcv_msg: nagios.writeCriticalMessage("Messages received incorrectly.") nagios.setCode(nagios.CRITICAL) print(nagios.getMsg()) raise SystemExit(nagios.getCode())
def main(): MSG_NUM = 100 MSG_SIZE = 500 TIMEOUT = 180 parser = ArgumentParser(description="Nagios sensor for AMS") parser.add_argument('-H', dest='host', type=str, default='messaging-devel.argo.grnet.gr', help='FQDN of AMS Service') parser.add_argument('--token', type=str, required=True, help='Given token') parser.add_argument('--project', type=str, required=True, help='Project registered in AMS Service') parser.add_argument('--topic', type=str, default='nagios_sensor_topic', help='Given topic') parser.add_argument('--subscription', type=str, default='nagios_sensor_sub', help='Subscription name') parser.add_argument('-t', dest='timeout', type=int, default=TIMEOUT, help='Timeout') cmd_options = parser.parse_args() nagios = NagiosResponse("All messages received correctly.") ams = ArgoMessagingService(endpoint=cmd_options.host, token=cmd_options.token, project=cmd_options.project) try: if ams.has_topic(cmd_options.topic, timeout=cmd_options.timeout): ams.delete_topic(cmd_options.topic, timeout=cmd_options.timeout) if ams.has_sub(cmd_options.subscription, timeout=cmd_options.timeout): ams.delete_sub(cmd_options.subscription, timeout=cmd_options.timeout) ams.create_topic(cmd_options.topic, timeout=cmd_options.timeout) ams.create_sub(cmd_options.subscription, cmd_options.topic, timeout=cmd_options.timeout) except AmsException as e: nagios.writeCriticalMessage(e.msg) nagios.setCode(nagios.CRITICAL) print(nagios.getMsg()) raise SystemExit(nagios.getCode()) ams_msg = AmsMessage() msg_orig = set() msg_array = [] for i in range(1, MSG_NUM): msg_txt = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(MSG_SIZE)) attr_name = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(4)) attr_value = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(8)) msg_array.append(ams_msg(data=msg_txt, attributes={attr_name: attr_value})) hash_obj = hashlib.md5(msg_txt + attr_name + attr_value) msg_orig.add(hash_obj.hexdigest()) try: msgs = ams.publish(cmd_options.topic, msg_array, timeout=cmd_options.timeout) ackids = [] rcv_msg = set() for id, msg in ams.pull_sub(cmd_options.subscription, MSG_NUM - 1, True, timeout=cmd_options.timeout): attr = msg.get_attr() hash_obj = hashlib.md5(msg.get_data() + attr.keys()[0] + attr.values()[0]) rcv_msg.add(hash_obj.hexdigest()) if ackids: ams.ack_sub(cmd_options.subscription, ackids, timeout=cmd_options.timeout) ams.delete_topic(cmd_options.topic, timeout=cmd_options.timeout) ams.delete_sub(cmd_options.subscription, timeout=cmd_options.timeout) except AmsException as e: nagios.writeCriticalMessage(e.msg) nagios.setCode(nagios.CRITICAL) print(nagios.getMsg()) raise SystemExit(nagios.getCode()) if msg_orig != rcv_msg: nagios.writeCriticalMessage("Messages received incorrectly.") nagios.setCode(nagios.CRITICAL) print(nagios.getMsg()) raise SystemExit(nagios.getCode())