[trigger] Going back to simplified version

This commit is contained in:
Pierre-Elliott Bécue 2014-07-31 11:50:47 +02:00
parent 8068f057e0
commit 091a2d161e
6 changed files with 287 additions and 292 deletions

View file

@ -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
"""
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))
def register(cls, key, value):
cls._fwfuns[key] = value
@classmethod
def regen(cls, body=()):
"""Regens the specific service
def get(cls, key):
return cls._fwfuns.get(key, None)
"""
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)
def fwrecord(fun):
FwFactory.register(fun.func_name, fun)
@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)
@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
"""
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)