diff --git a/gestion/trigger/host.py b/gestion/trigger/host.py index fd365c7b..3539e2e2 100644 --- a/gestion/trigger/host.py +++ b/gestion/trigger/host.py @@ -4,8 +4,7 @@ # Basic trigger host, will be imported from any other # Contains a TriggerFactory, which records the host functions # decorated with @record. -# Contains a trigger function which should be imported as its with -# record, to be used in hosts contained in hosts/ directory. +# Contains a trigger which calls good functions from factory. # # Author : Pierre-Elliott Bécue # License : GPLv3 diff --git a/gestion/trigger/services/event.py b/gestion/trigger/services/event.py index e4fa935b..e231c958 100644 --- a/gestion/trigger/services/event.py +++ b/gestion/trigger/services/event.py @@ -128,7 +128,7 @@ def event(body=()): """ - logger.info("Received message %s…", body) + logger.info("Received message %r…", body) diff = diff_o_matic(body) @@ -157,14 +157,18 @@ def trigger_mac_ip(body, diff): fw = {'add': (macs[1], ips[1])} elif not macs[1]: # Destruction d'une machine. - to_send = {'delete': (macs[0], ips[0])} + dhcp = {'delete': (macs[0], ips[0])} fw = {'delete': (macs[0], ips[0])} else: # Mise à jour. - to_send = {'update': (macs[0], ips[0], macs[1], ips[1], hostnames[1])} + dhcp = {'update': (macs[0], ips[0], macs[1], ips[1], hostnames[1])} fw = {'update': (macs[0], ips[0], macs[1], ips[1])} - trigger_send('dhcp', to_send) - trigger_send('firewall_mac_ip', fw) + logger.info("Sending DHCP trigger with body %r", dhcp) + trigger_send("dhcp", dhcp) + logger.info("Sending firewall trigger for mac_ip with body %r", fw) + trigger_send("firewall", ("mac_ip", fw)) + logger.info("trigger_mac_ip done.") -def trigger_send(ttype, to_send): - print "Sending trigger %s with %s…" % (ttype, to_send) +def trigger_send(routing_key, body): + sender = Event("civet") + sender.send_message(routing_key, body) diff --git a/gestion/trigger/services/firewall.py b/gestion/trigger/services/firewall.py index 99b094e9..5e8e387a 100644 --- a/gestion/trigger/services/firewall.py +++ b/gestion/trigger/services/firewall.py @@ -2,20 +2,63 @@ # -*- coding: utf-8 -*- # # Service in charge of firewall for trigger. +# Contains multiple subservices for each special +# part of firewall. # -# Author : Pierre-Elliott Bécue +# Author : Pierre-Elliott Bécue # Licence : GPLv3 +# Date : 15/06/2014 import lc_ldap.shortcuts from gestion.trigger.host import record import cranslib.clogger as clogger -import gestion.config.dhcp as dhcp_config -import gestion.secrets_new as secrets_new -import socket -import gestion.affichage as affichage +import gestion.config.firewall as firewall_config +import gestion.trigger.firewall4.firewall4 as firewall4 import os import sys -import gestion.iptools as iptools logger = clogger.CLogger("trigger.firewall", "debug") -hostname = socket.gethostname().split(".")[0] + ".adm.crans.org" + +class FwFunFactory(object): + """Factory containing which function is part of the trigger set + + """ + + _meths = {} + + @classmethod + def register(cls, key, value): + cls._meths[key] = value + + @classmethod + def get(cls, key): + return cls._meths.get(key, None) + +def fwrecord(function): + FwFunFactory.register(function.func_name, function) + +def fwcall(fwfun): + return FwFunFactory.get(fwfun) + +@record +def firewall(body=()): + if len(body) != 2: + logger.warning("Received body %r, this format is incorrect, discarding.", body) + (service, data) = body + logger.info("Calling service %s for data %r", service, data) + fwcall(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)