Commit 51937a9a authored by Pierre-Elliott Bécue's avatar Pierre-Elliott Bécue

[trigger] Going back to simplified version

parent 8068f057
......@@ -7,6 +7,7 @@
import itertools
debug = True
log_level = "info"
# Serveur maître
master = "rabbitmq.adm.crans.org"
......
......@@ -10,27 +10,50 @@
# License : GPLv3
# Date : 28/04/2014
import collections
class TriggerFactory(object):
"""Factory containing which function is part of the trigger set
"""
_meths = {}
_services = {}
_parsers = collections.defaultdict(list)
@classmethod
def register(cls, key, value):
cls._meths[key] = value
def register_service(cls, key, value):
cls._services[key] = value
@classmethod
def get(cls, key):
return cls._meths.get(key, None)
def get_service(cls, key):
return cls._services.get(key, None)
@classmethod
def get_services(cls):
return cls._meths.values()
return cls._services.values()
@classmethod
def register_parser(cls, keys, parser):
for key in keys:
cls._parsers[key].append(parser)
@classmethod
def get_parser(cls, keyword):
return cls._parsers[keyword]
def record_service(func):
"""Records in the triggerfactory the function
The function provided are services to regen
"""
TriggerFactory.register_service(func.func_name, func)
def record(cls):
TriggerFactory.register(cls.__name__.lower(), cls)
def trigger_service(what):
return TriggerFactory.get_service(what)
def trigger(what):
return TriggerFactory.get(what)
def record_parser(*args):
def find_parser(func):
TriggerFactory.register_parser(args, func)
return func
return find_parser
This diff is collapsed.
......@@ -21,10 +21,10 @@ import itertools
import traceback
import gestion.secrets_new as secrets
# Trigger features
import gestion.config.trigger as trigger_config
from gestion.trigger.host import TriggerFactory
from gestion.trigger.services.service import BasicService
from gestion.trigger.host import TriggerFactory, record_service, record_parser
# Clogger
import cranslib.clogger as clogger
......@@ -32,7 +32,7 @@ import cranslib.clogger as clogger
# lc_ldap
import lc_ldap.attributs
logger = clogger.CLogger("trigger", "event", "debug", trigger_config.debug)
logger = clogger.CLogger("trigger", "event", trigger_config.log_level, trigger_config.debug)
services = []
for config_service in trigger_config.all_services:
......@@ -136,63 +136,52 @@ def compare_lists(list1, list2):
return moins, plus
class Event(BasicService):
"""Event service class. It extends BasicService, but should not implement
any change trigger, since it's this service which is designed to call
change triggers of other services.
"""
@classmethod
def get_changes(cls, body, diff):
"""Compute changes from diff"""
return [None]
@record_service
def event(body=()):
"""When any event arrives on trigger-civet-event, this method is called
and designed to transcript the body (ldap data) in something usable for
the services. Afterwards, it sends these transcripts on the good way
using routing_key.
@classmethod
def regen(cls, body=()):
"""When any event arrives on trigger-civet-event, this method is called
and designed to transcript the body (ldap data) in something usable for
the services. Afterwards, it sends these transcripts on the good way
using routing_key.
body is a 3-tuple, containing LDAP dn, the former state of the object
(a simple dict), and the later state. The data are non-binding-dependant.
body is a 3-tuple, containing LDAP dn, the former state of the object
(a simple dict), and the later state. The data are non-binding-dependant.
A new object has body[1] to None, a deleted one has body[2] to None.
A new object has body[1] to None, a deleted one has body[2] to None.
"""
"""
logger.info("Received message %r…", body)
diff = diff_o_matic(body)
# Now, diff is a dict containing attributes which has been modified.
# diff['macAddress'] could look like (['aa:bb:cc:dd:ee:fg'], ['aa:bb:cc:dd:ee:ff']),
# where the list on the left is the former value of attributes, and the list on the
# right the latter values.
# -*- Explain -*-
#In [11]: import itertools
#
#In [12]: a = [[(3, 'lol'), ('7', 3)], [(5, 6), None], [None], [('lol', 'lal')]]
#
#In [13]: a
#Out[13]: [[(3, 'lol'), ('7', 3)], [(5, 6), None], [None], [('lol', 'lal')]]
#
#In [14]: list(set([message for message in itertools.chain(*a)]))
#Out[14]: [('7', 3), (5, 6), None, ('lol', 'lal'), (3, 'lol')] # Only one None from a, since [None, x, y, None] is equivalent for itertools to [x, y]
#
#In [15]: b = list(set([message for message in itertools.chain(*a) if message is not None]))
#
#In [16]: b
#Out[16]: [('7', 3), (5, 6), ('lol', 'lal'), (3, 'lol')]
msg_to_send = [message for message in itertools.chain(*[service.get_changes(body, diff) for service in TriggerFactory.get_services()]) if message is not None]
for msg in msg_to_send:
logger.info("Sending %r on the road \\o/", msg)
# XXX - uncomment this when in production
# trigger_send(*msg)
logger.info("Received message %r…", body)
diff = diff_o_matic(body)
# Now, diff is a dict containing attributes which has been modified.
# diff['macAddress'] could look like (['aa:bb:cc:dd:ee:fg'], ['aa:bb:cc:dd:ee:ff']),
# where the list on the left is the former value of attributes, and the list on the
# right the latter values.
# -*- Explain -*-
#In [11]: import itertools
#
#In [12]: a = [[(3, 'lol'), ('7', 3)], [(5, 6), None], [None], [('lol', 'lal')]]
#
#In [13]: a
#Out[13]: [[(3, 'lol'), ('7', 3)], [(5, 6), None], [None], [('lol', 'lal')]]
#
#In [14]: list(set([message for message in itertools.chain(*a)]))
#Out[14]: [('7', 3), (5, 6), None, ('lol', 'lal'), (3, 'lol')] # Only one None from a, since [None, x, y, None] is equivalent for itertools to [x, y]
#
#In [15]: b = list(set([message for message in itertools.chain(*a) if message is not None]))
#
#In [16]: b
#Out[16]: [('7', 3), (5, 6), ('lol', 'lal'), (3, 'lol')]
functions = list(set([function for function in itertools.chain(*[TriggerFactory.get_parser(key) for key in diff])]))
msg_to_send = [function(body, diff) for function in functions]
for msg in msg_to_send:
logger.info("Sending %r on the road \\o/", msg)
# XXX - uncomment this when in production
trigger_send(*msg)
def trigger_send(routing_key, body):
sender = EventProducer("civet")
......
......@@ -16,67 +16,75 @@ it to regenerate what needs to.
import cranslib.clogger as clogger
import gestion.config.trigger as trigger_config
logger = clogger.CLogger("trigger", "firewall", "debug", trigger_config.debug)
logger = clogger.CLogger("trigger", "firewall", trigger_config.log_level, trigger_config.debug)
import lc_ldap.shortcuts
from gestion.trigger.services.service import BasicService
from gestion.trigger.host import record_service, record_parser
import gestion.trigger.firewall4.firewall4 as firewall4
class Firewall(BasicService):
"""Firewall service that handles any modification in the firewall.
class FwFactory(object):
"""Records firewall functions, and provide them.
"""
# Class lookup table to define which changes call which function.
changes_trigger = {
lc_ldap.attributs.macAddress.ldap_name: ('send_mac_ip',),
lc_ldap.attributs.ipHostNumber.ldap_name: ('send_mac_ip',),
}
_fwfuns = {}
@classmethod
def send_mac_ip(cls, body, diff):
"""Computes mac_ip data to send from body and diff
def register(cls, key, value):
cls._fwfuns[key] = value
"""
macs = tuple([body[i].get(lc_ldap.attributs.macAddress.ldap_name, [''])[0] for i in xrange(1, 3)])
ips = tuple([body[i].get(lc_ldap.attributs.ipHostNumber.ldap_name, [''])[0] for i in xrange(1, 3)])
@classmethod
def get(cls, key):
return cls._fwfuns.get(key, None)
# Mise à jour du parefeu mac_ip
if not macs[0]:
# Création d'une nouvelle machine.
fw = {'add': [(macs[1], ips[1])]}
elif not macs[1]:
# Destruction d'une machine.
fw = {'delete': [(macs[0], ips[0])]}
else:
# Mise à jour.
fw = {'update': [(macs[0], ips[0], macs[1], ips[1])]}
return ("firewall", ("mac_ip", fw))
def fwrecord(fun):
FwFactory.register(fun.func_name, fun)
@classmethod
def regen(cls, body=()):
"""Regens the specific service
@record_parser(lc_ldap.attributs.macAddress.ldap_name, lc_ldap.attributs.ipHostNumber.ldap_name)
def send_mac_ip(body, diff):
"""Computes mac_ip data to send from body and diff
"""
if len(body) != 2:
logger.warning("Received body %r, this format is incorrect, discarding.", body)
return
(service, data) = body
logger.info("Calling service %s for data %r", service, data)
getattr(cls, service)(data)
"""
macs = tuple([body[i].get(lc_ldap.attributs.macAddress.ldap_name, [''])[0] for i in xrange(1, 3)])
ips = tuple([body[i].get(lc_ldap.attributs.ipHostNumber.ldap_name, [''])[0] for i in xrange(1, 3)])
@classmethod
def mac_ip(cls, body):
host_fw = firewall4.firewall()
if body and isinstance(body, dict):
for (mac, ip) in body.get("add", []):
logger.info("Adding mac_ip %s,%s", mac, ip)
host_fw.mac_ip_append(mac, ip)
for (mac, ip) in body.get("delete", []):
logger.info("Removing mac_ip %s,%s", mac, ip)
host_fw.mac_ip_remove(mac, ip)
for (rmac, rip, mac, ip) in body.get("update", []):
logger.info("Updating mac_ip %s,%s with %s,%s", rmac, rip, mac, ip)
host_fw.mac_ip_remove(rmac, rip)
host_fw.mac_ip_append(mac, ip)
# Mise à jour du parefeu mac_ip
if not macs[0]:
# Création d'une nouvelle machine.
fw = {'add': [(macs[1], ips[1])]}
elif not macs[1]:
# Destruction d'une machine.
fw = {'delete': [(macs[0], ips[0])]}
else:
# Mise à jour.
fw = {'update': [(macs[0], ips[0], macs[1], ips[1])]}
return ("firewall", ("mac_ip", fw))
@record_service
def firewall(body=()):
"""Regens the specific service
"""
if len(body) != 2:
logger.warning("Received body %r, this format is incorrect, discarding.", body)
return
(service, data) = body
logger.info("Calling service %s for data %r", service, data)
# XXX - Uncomment when in prod
#FwFactory.get(service)(data)
@fwrecord
def mac_ip(body):
host_fw = firewall4.firewall()
if body and isinstance(body, dict):
for (mac, ip) in body.get("add", []):
logger.info("Adding mac_ip %s,%s", mac, ip)
host_fw.mac_ip_append(mac, ip)
for (mac, ip) in body.get("delete", []):
logger.info("Removing mac_ip %s,%s", mac, ip)
host_fw.mac_ip_remove(mac, ip)
for (rmac, rip, mac, ip) in body.get("update", []):
logger.info("Updating mac_ip %s,%s with %s,%s", rmac, rip, mac, ip)
host_fw.mac_ip_remove(rmac, rip)
host_fw.mac_ip_append(mac, ip)
......@@ -20,12 +20,12 @@ import pika
import gestion.secrets_new as secrets
import gestion.config.trigger as trigger_config
import gestion.affichage as affichage
from gestion.trigger.host import trigger
from gestion.trigger.host import trigger_service
import cranslib.clogger as clogger
import cmb
hostname = socket.gethostname().split(".")[0]
logger = clogger.CLogger("trigger", "trigger", "info", trigger_config.debug)
logger = clogger.CLogger("trigger", "trigger", trigger_config.log_level, trigger_config.debug)
# Ce bloc contient le peu de "magie" de la librairie, on utilise les services listés dans config/trigger.py
# comme référence. Pour éviter toute redondance, la commande importe donc les services utiles suivant cette
......@@ -70,7 +70,7 @@ class EvenementListener(cmb.AsynchronousConsumer):
# about contient le nom de la fonction à appeler, body lui est filé en argument.
try:
if about in trigger_config.services[hostname]:
trigger(about).regen(body)
trigger_service(about)(body)
else:
raise AttributeError
except AttributeError:
......@@ -122,7 +122,7 @@ if __name__ == '__main__':
for host_service in trigger_config.services[hostname]:
try:
print affichage.style(" (Ré)Génération du service %s" % (host_service,), "cyan")
trigger(host_service).regen(True)
trigger_service(host_service)(True)
except AttributeError:
print "No suitable trigger handle found for service %s on host %s" % (host_service, hostname)
elif args.daemon:
......@@ -133,4 +133,4 @@ if __name__ == '__main__':
for arg_service in trigger_config.services[hostname]:
if getattr(args, arg_service, False) == True:
print affichage.style(" (Ré)Génération du service %s" % (arg_service,), "cyan")
trigger(arg_service).regen(True)
trigger_service(arg_service)(True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment