[trigger] On commence un début de gestionnaire d'événements.
* À terme, il sera ajouté aux bindings, dans l'objectif de gérer les envois de modifications sans que ceux-ci n'aient à implémenter la moindre autre chose qu'un producer standard qui balance des diff d'objets.
This commit is contained in:
parent
9c81aa2a23
commit
9cac0c2531
6 changed files with 219 additions and 0 deletions
50
gestion/trigger/event.py
Normal file
50
gestion/trigger/event.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
#!/bin/bash /usr/scripts/python.sh
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Trigger library, designed to send events messages.
|
||||
#
|
||||
# Author : Pierre-Elliott Bécue <becue@crans.org>
|
||||
# License : GPLv3
|
||||
# Date : 18/05/2014
|
||||
|
||||
import cmb
|
||||
import cPickle
|
||||
import gestion.config.trigger as trigger_config
|
||||
import cranslib.clogger as clogger
|
||||
import pika
|
||||
|
||||
logger = clogger.CLogger("trigger", "info")
|
||||
|
||||
class Event(cmb.BasicProducer):
|
||||
"""
|
||||
Event tracker
|
||||
"""
|
||||
def __init__(self, app_id):
|
||||
"""Extended
|
||||
|
||||
"""
|
||||
logger.info("Starting trigger Event program…")
|
||||
super(Event, self).__init__(trigger_config.master, 'trigger.event', app_id)
|
||||
self._connection = self.connect()
|
||||
self.get_chan()
|
||||
|
||||
def send_message(self, routing_key, body):
|
||||
"""Sends basic message with app_id and body
|
||||
|
||||
"""
|
||||
try:
|
||||
logger.info("Sending message %s with routing_key %s.", body, routing_key)
|
||||
body = cPickle.dumps(body)
|
||||
self._channel.basic_publish(exchange=self._exchange_name,
|
||||
routing_key=routing_key,
|
||||
body=body,
|
||||
properties=pika.BasicProperties(
|
||||
delivery_mode=2,
|
||||
app_id=self._app_id,
|
||||
))
|
||||
except:
|
||||
print "Failure in trigger.event"
|
||||
raise
|
||||
|
||||
def announce(self, body):
|
||||
self.send_message("trigger.announce", body)
|
5
gestion/trigger/hosts/civet.py
Normal file
5
gestion/trigger/hosts/civet.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash /usr/scripts/python.sh
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from gestion.trigger.civet import civet
|
||||
from gestion.trigger.host import trigger
|
4
gestion/trigger/readme
Normal file
4
gestion/trigger/readme
Normal file
|
@ -0,0 +1,4 @@
|
|||
Documentation succincte de trigger
|
||||
==================================
|
||||
|
||||
|
80
gestion/trigger/transcriptor.py
Normal file
80
gestion/trigger/transcriptor.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
#!/bin/bash /usr/scripts/python.sh
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Author : Pierre-Elliott Bécue <becue@crans.org>
|
||||
# License : GPLv3
|
||||
# Date : 29/04/2014
|
||||
|
||||
import argparse
|
||||
import cranslib.clogger as clogger
|
||||
import cmb
|
||||
import cPickle
|
||||
import socket
|
||||
import gestion.config.trigger as trigger_config
|
||||
import gestion.affichage as affichage
|
||||
import sys
|
||||
|
||||
logger = clogger.CLogger("transcriptor", "info")
|
||||
|
||||
def process_event(body):
|
||||
"""
|
||||
Makes the transcription between the ldap modlist generated
|
||||
by lc_ldap and sent to the event queue and the message one
|
||||
have to send to the services.
|
||||
|
||||
Example :
|
||||
{}
|
||||
|
||||
"""
|
||||
|
||||
class EvenementListener(cmb.AsynchronousConsumer):
|
||||
"""
|
||||
Gestionnaire d'événement
|
||||
"""
|
||||
|
||||
def on_message(self, channel, basic_deliver, properties, body):
|
||||
"""Invoked by pika when a message is delivered from RabbitMQ. The
|
||||
channel is passed for your convenience. The basic_deliver object that
|
||||
is passed in carries the exchange, routing key, delivery tag and
|
||||
a redelivered flag for the message. The properties passed in is an
|
||||
instance of BasicProperties with the message properties and the body
|
||||
is the message that was sent.
|
||||
|
||||
:param pika.channel.Channel channel: The channel object
|
||||
:param pika.Spec.Basic.Deliver: basic_deliver method
|
||||
:param pika.Spec.BasicProperties: properties
|
||||
:param str|unicode body: The message body
|
||||
|
||||
"""
|
||||
origin = properties.app_id
|
||||
message_id = properties.message_id
|
||||
logger.info('Received message # %s from %s: %s',
|
||||
basic_deliver.delivery_tag, properties.app_id, body)
|
||||
body = cPickle.loads(body)
|
||||
process_event(body)
|
||||
self.acknowledge_message(basic_deliver.delivery_tag)
|
||||
|
||||
def run(self):
|
||||
"""Run the example consumer by connecting to RabbitMQ and then
|
||||
starting the IOLoop to block and allow the SelectConnection to operate.
|
||||
|
||||
"""
|
||||
logger.info("""Crans Message Broker
|
||||
+--------------------------------------------+
|
||||
| Welcome on Transcriptor |
|
||||
+--------------------------------------------+""")
|
||||
self._connection = self.connect()
|
||||
self.add_queue("trigger-event", "trigger.event")
|
||||
self._connection.ioloop.start()
|
||||
|
||||
def daemonize():
|
||||
listener = EvenementListener(trigger_config.master, "trigger", "topic")
|
||||
try:
|
||||
listener.run()
|
||||
except KeyboardInterrupt:
|
||||
logger.warning("Caught SIGINT, will now go for shutdown.")
|
||||
listener.stop()
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Daemonize the trigger app, in order to listen and execute commands from civet.
|
||||
daemonize()
|
Loading…
Add table
Add a link
Reference in a new issue