diff --git a/gestion/trigger/parsers/__init__.py b/gestion/trigger/parsers/__init__.py new file mode 100644 index 00000000..6102face --- /dev/null +++ b/gestion/trigger/parsers/__init__.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Nauthing diff --git a/gestion/trigger/parsers/dhcp.py b/gestion/trigger/parsers/dhcp.py new file mode 100644 index 00000000..1cf55db5 --- /dev/null +++ b/gestion/trigger/parsers/dhcp.py @@ -0,0 +1,40 @@ +#!/bin/bash /usr/scripts/python.sh +# -*- coding: utf-8 -*- +# +# DHCP's trigger parser. +# +# Author : Pierre-Elliott Bécue +# Licence : GPLv3 +# Date : 19/11/2014 +# +# TODO : Take care of each XXX or TODO and think about updating +# gestion/config/dhcp when submitting for production, because +# of .test files. +""" +Handles the parser for trigger about dhcp service +This currently is used for mac/IP updates of LDAP database. +""" + +import lc_ldap.attributs +from gestion.trigger.host import record_parser + +@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 + + """ + 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)]) + hostnames = tuple([body[i].get(lc_ldap.attributs.host.ldap_name, [''])[0] for i in xrange(1, 3)]) + + # Régénération du DHCP : + if not macs[0]: + # Création d'une nouvelle machine. + dhcp_dict = {'add': [(macs[1], ips[1], hostnames[1])]} + elif not macs[1]: + # Destruction d'une machine. + dhcp_dict = {'delete': [(macs[0], ips[0])]} + else: + # Mise à jour. + dhcp_dict = {'update': [(macs[0], ips[0], macs[1], ips[1], hostnames[1])]} + return ("dhcp", dhcp_dict) diff --git a/gestion/trigger/parsers/event.py b/gestion/trigger/parsers/event.py new file mode 100644 index 00000000..fa147c1c --- /dev/null +++ b/gestion/trigger/parsers/event.py @@ -0,0 +1,12 @@ +#!//usr/bin/env python +# -*- coding: utf-8 -*- +# +# Parser for event service. Contains nothing. +# +# Author : Pierre-Elliott Bécue +# License : GPLv3 +# Date : 19/11/2014 +""" +This parser is not usefull, but still will be imported by service event.py +on civet. +""" diff --git a/gestion/trigger/parsers/firewall.py b/gestion/trigger/parsers/firewall.py new file mode 100644 index 00000000..3e51f906 --- /dev/null +++ b/gestion/trigger/parsers/firewall.py @@ -0,0 +1,35 @@ +#!/bin/bash /usr/scripts/python.sh +# -*- coding: utf-8 -*- +# +# Parser for firewall service. +# +# Author : Pierre-Elliott Bécue +# Licence : GPLv3 +# Date : 15/06/2014 +""" +This is the parser for firewall service. +""" + +import lc_ldap.attributs +from gestion.trigger.host import record_parser + +@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 + + """ + 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)]) + + # 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)) + diff --git a/gestion/trigger/services/dhcp.py b/gestion/trigger/services/dhcp.py index 9b012a9f..249e31b4 100644 --- a/gestion/trigger/services/dhcp.py +++ b/gestion/trigger/services/dhcp.py @@ -8,7 +8,7 @@ # Licence : GPLv3 # Date : 14/07/2014 # -# TODO : Take care of each XXX or TODO think about updating +# TODO : Take care of each XXX or TODO and think about updating # gestion/config/dhcp when submitting for production, because # of .test files. @@ -30,7 +30,7 @@ import gestion.secrets_new as secrets_new import gestion.affichage as affichage import gestion.iptools as iptools from gestion.trigger.pypureomapi import pack_ip, pack_mac, OMAPI_OP_UPDATE, Omapi, OmapiMessage -from gestion.trigger.host import record_service, record_parser, TriggerFactory +from gestion.trigger.host import record_service if "dhcp" in trigger_config.services[hostname]: dhcp_omapi_keyname = secrets_new.get("dhcp_omapi_keyname") @@ -41,27 +41,6 @@ else: dhcp_omapi_key = None ldap_conn = None -@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 - - """ - 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)]) - hostnames = tuple([body[i].get(lc_ldap.attributs.host.ldap_name, [''])[0] for i in xrange(1, 3)]) - - # Régénération du DHCP : - if not macs[0]: - # Création d'une nouvelle machine. - dhcp_dict = {'add': [(macs[1], ips[1], hostnames[1])]} - elif not macs[1]: - # Destruction d'une machine. - dhcp_dict = {'delete': [(macs[0], ips[0])]} - else: - # Mise à jour. - dhcp_dict = {'update': [(macs[0], ips[0], macs[1], ips[1], hostnames[1])]} - return ("dhcp", dhcp_dict) - @record_service def dhcp(body=None): """Regenerates dhcp service taking body into account. diff --git a/gestion/trigger/services/event.py b/gestion/trigger/services/event.py index 24bd373a..da20320b 100644 --- a/gestion/trigger/services/event.py +++ b/gestion/trigger/services/event.py @@ -37,7 +37,7 @@ logger = clogger.CLogger("trigger", "event", trigger_config.log_level, trigger_c services = [] for config_service in trigger_config.all_services: try: - services.append(importlib.import_module("gestion.trigger.services.%s" % (config_service,))) + services.append(importlib.import_module("gestion.trigger.parsers.%s" % (config_service,))) except Exception as e: logger.critical("Fatal : import of %s failed, see following traceback. %s", config_service, traceback.format_exc()) @@ -175,7 +175,7 @@ def event(body=()): # #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])])) + functions = list(set([function for function in itertools.chain(*[TriggerFactory.get_parser(key) for key in diff]) if function is not None])) msg_to_send = [function(body, diff) for function in functions] for msg in msg_to_send: diff --git a/gestion/trigger/services/firewall.py b/gestion/trigger/services/firewall.py index 624a4863..bf20ad37 100644 --- a/gestion/trigger/services/firewall.py +++ b/gestion/trigger/services/firewall.py @@ -20,7 +20,7 @@ logger = clogger.CLogger("trigger", "firewall", trigger_config.log_level, trigge import lc_ldap.shortcuts -from gestion.trigger.host import record_service, record_parser +from gestion.trigger.host import record_service import gestion.trigger.firewall4.firewall4 as firewall4 class FwFactory(object): @@ -41,26 +41,6 @@ class FwFactory(object): def fwrecord(fun): FwFactory.register(fun.func_name, fun) -@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 - - """ - 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)]) - - # 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