2017-01-15 23:01:18 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2017 Gabriel Détraz
|
|
|
|
# Copyright © 2017 Goulven Kermarec
|
|
|
|
# Copyright © 2017 Augustin Lemesle
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
from django.db import models
|
2017-07-21 03:06:53 +00:00
|
|
|
from django.db.models.signals import post_save, pre_delete, post_delete
|
2016-07-25 21:54:40 +00:00
|
|
|
from django.dispatch import receiver
|
2016-07-06 20:49:16 +00:00
|
|
|
from django.forms import ValidationError
|
2017-07-21 03:06:53 +00:00
|
|
|
from django.utils.functional import cached_property
|
2017-08-08 04:56:19 +00:00
|
|
|
from django.utils import timezone
|
2016-07-03 14:49:29 +00:00
|
|
|
from macaddress.fields import MACAddressField
|
2017-08-28 22:35:03 +00:00
|
|
|
from netaddr import mac_bare, EUI, IPSet, IPRange, IPNetwork, IPAddress
|
2016-11-15 14:25:27 +00:00
|
|
|
from django.core.validators import MinValueValidator,MaxValueValidator
|
2017-07-21 03:06:53 +00:00
|
|
|
import re
|
2017-07-21 04:49:39 +00:00
|
|
|
from reversion import revisions as reversion
|
2017-08-08 17:40:30 +00:00
|
|
|
from datetime import timedelta
|
2016-07-02 22:27:22 +00:00
|
|
|
|
2017-08-08 17:40:30 +00:00
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
class Machine(models.Model):
|
2017-07-23 03:03:56 +00:00
|
|
|
""" Class définissant une machine, object parent user, objets fils interfaces"""
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Machine"
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
|
2016-07-04 01:08:53 +00:00
|
|
|
name = models.CharField(max_length=255, help_text="Optionnel", blank=True, null=True)
|
2016-07-05 10:21:43 +00:00
|
|
|
active = models.BooleanField(default=True)
|
2016-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-04 00:48:24 +00:00
|
|
|
return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
|
2016-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
class MachineType(models.Model):
|
2017-07-23 03:03:56 +00:00
|
|
|
""" Type de machine, relié à un type d'ip, affecté aux interfaces"""
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Type de machine"
|
|
|
|
|
2016-10-22 22:55:58 +00:00
|
|
|
type = models.CharField(max_length=255)
|
|
|
|
ip_type = models.ForeignKey('IpType', on_delete=models.PROTECT, blank=True, null=True)
|
|
|
|
|
2017-07-23 03:03:56 +00:00
|
|
|
def all_interfaces(self):
|
|
|
|
return Interface.objects.filter(type=self)
|
|
|
|
|
2016-10-22 22:55:58 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
class IpType(models.Model):
|
2017-07-23 03:03:56 +00:00
|
|
|
""" Type d'ip, définissant un range d'ip, affecté aux machine types"""
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Type d'ip"
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
type = models.CharField(max_length=255)
|
2016-07-08 15:54:06 +00:00
|
|
|
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2016-10-13 00:11:23 +00:00
|
|
|
need_infra = models.BooleanField(default=False)
|
2017-08-28 22:35:03 +00:00
|
|
|
domaine_ip_start = models.GenericIPAddressField(protocol='IPv4')
|
|
|
|
domaine_ip_stop = models.GenericIPAddressField(protocol='IPv4')
|
2017-08-26 13:10:18 +00:00
|
|
|
vlan = models.ForeignKey('Vlan', on_delete=models.PROTECT, blank=True, null=True)
|
2016-07-02 22:27:22 +00:00
|
|
|
|
2017-07-21 03:06:53 +00:00
|
|
|
@cached_property
|
2017-08-28 22:35:03 +00:00
|
|
|
def ip_range(self):
|
|
|
|
return IPRange(self.domaine_ip_start, end=self.domaine_ip_stop)
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ip_set(self):
|
2017-08-28 22:35:03 +00:00
|
|
|
return IPSet(self.ip_range)
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ip_set_as_str(self):
|
|
|
|
return [str(x) for x in self.ip_set]
|
|
|
|
|
|
|
|
def ip_objects(self):
|
2017-07-21 04:49:39 +00:00
|
|
|
return IpList.objects.filter(ip_type=self)
|
|
|
|
|
|
|
|
def free_ip(self):
|
|
|
|
return IpList.objects.filter(interface__isnull=True).filter(ip_type=self)
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
def gen_ip_range(self):
|
|
|
|
# Creation du range d'ip dans les objets iplist
|
2017-08-28 22:35:03 +00:00
|
|
|
networks = []
|
|
|
|
for net in self.ip_range.cidrs():
|
|
|
|
networks += net.iter_hosts()
|
|
|
|
ip_obj = [IpList(ip_type=self, ipv4=str(ip)) for ip in networks]
|
2017-08-28 23:04:15 +00:00
|
|
|
listes_ip = IpList.objects.filter(ipv4__in=[str(ip) for ip in networks])
|
|
|
|
# Si il n'y a pas d'ip, on les crée
|
|
|
|
if not listes_ip:
|
|
|
|
IpList.objects.bulk_create(ip_obj)
|
|
|
|
# Sinon on update l'ip_type
|
|
|
|
else:
|
|
|
|
listes_ip.update(ip_type=self)
|
2017-08-23 21:26:49 +00:00
|
|
|
return
|
2017-07-21 03:06:53 +00:00
|
|
|
|
|
|
|
def del_ip_range(self):
|
2017-07-23 03:03:56 +00:00
|
|
|
""" Methode dépréciée, IpList est en mode cascade et supprimé automatiquement"""
|
2017-07-21 04:49:39 +00:00
|
|
|
if Interface.objects.filter(ipv4__in=self.ip_objects()):
|
2017-07-21 03:06:53 +00:00
|
|
|
raise ValidationError("Une ou plusieurs ip du range sont affectées, impossible de supprimer le range")
|
|
|
|
for ip in self.ip_objects():
|
|
|
|
ip.delete()
|
|
|
|
|
|
|
|
def clean(self):
|
2017-08-28 22:35:03 +00:00
|
|
|
if IPAddress(self.domaine_ip_start) > IPAddress(self.domaine_ip_stop):
|
|
|
|
raise ValidationError("Domaine end doit être après start...")
|
|
|
|
# On ne crée pas plus grand qu'un /16
|
|
|
|
if self.ip_range.size > 65536:
|
|
|
|
raise ValidationError("Le range est trop gros, vous ne devez pas créer plus grand qu'un /16")
|
2017-07-21 03:06:53 +00:00
|
|
|
# On check que les / ne se recoupent pas
|
2017-08-26 13:10:18 +00:00
|
|
|
for element in IpType.objects.all().exclude(pk=self.pk):
|
2017-07-21 03:06:53 +00:00
|
|
|
if not self.ip_set.isdisjoint(element.ip_set):
|
|
|
|
raise ValidationError("Le range indiqué n'est pas disjoint des ranges existants")
|
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.clean()
|
|
|
|
super(IpType, self).save(*args, **kwargs)
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.type
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2017-08-26 13:10:18 +00:00
|
|
|
class Vlan(models.Model):
|
|
|
|
PRETTY_NAME = "Vlans"
|
|
|
|
|
|
|
|
vlan_id = models.IntegerField()
|
|
|
|
name = models.CharField(max_length=256)
|
|
|
|
comment = models.CharField(max_length=256, blank=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-07-08 15:54:06 +00:00
|
|
|
class Extension(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Extensions dns"
|
|
|
|
|
2016-11-19 17:52:59 +00:00
|
|
|
name = models.CharField(max_length=255, unique=True)
|
2016-11-19 22:19:44 +00:00
|
|
|
need_infra = models.BooleanField(default=False)
|
2016-11-19 17:52:59 +00:00
|
|
|
origin = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
|
2016-07-08 15:54:06 +00:00
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
|
|
|
return "@ IN A " + str(self.origin)
|
|
|
|
|
2016-07-08 15:54:06 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2016-11-19 16:44:43 +00:00
|
|
|
class Mx(models.Model):
|
|
|
|
PRETTY_NAME = "Enregistrements MX"
|
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
|
|
|
priority = models.IntegerField(unique=True)
|
2016-12-24 17:07:09 +00:00
|
|
|
name = models.OneToOneField('Domain', on_delete=models.PROTECT)
|
2016-11-19 16:44:43 +00:00
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
|
|
|
return "@ IN MX " + str(self.priority) + " " + str(self.name)
|
|
|
|
|
2016-11-19 16:44:43 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name)
|
|
|
|
|
|
|
|
class Ns(models.Model):
|
|
|
|
PRETTY_NAME = "Enregistrements NS"
|
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2016-12-26 16:43:41 +00:00
|
|
|
ns = models.OneToOneField('Domain', on_delete=models.PROTECT)
|
2016-11-19 16:44:43 +00:00
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
|
|
|
return "@ IN NS " + str(self.ns)
|
|
|
|
|
2016-11-19 17:21:05 +00:00
|
|
|
def __str__(self):
|
2016-12-26 16:43:41 +00:00
|
|
|
return str(self.zone) + ' ' + str(self.ns)
|
2016-11-19 17:21:05 +00:00
|
|
|
|
2017-09-05 16:18:41 +00:00
|
|
|
class Text(models.Model):
|
|
|
|
PRETTY_NAME = "Enregistrement text"
|
|
|
|
|
|
|
|
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
|
|
|
field1 = models.CharField(max_length=255)
|
|
|
|
field2 = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.zone) + " : " + str(self.field1) + " " + str(self.field2)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
|
|
|
return str(self.field1) + " IN TXT " + str(self.field2)
|
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
class Interface(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Interface"
|
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
ipv4 = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
|
2016-07-04 00:48:24 +00:00
|
|
|
#ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
|
|
|
|
mac_address = MACAddressField(integer=False, unique=True)
|
2016-07-18 03:30:35 +00:00
|
|
|
machine = models.ForeignKey('Machine', on_delete=models.CASCADE)
|
2016-07-18 17:14:48 +00:00
|
|
|
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
|
2016-07-04 00:48:24 +00:00
|
|
|
details = models.CharField(max_length=255, blank=True)
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2017-05-26 22:43:51 +00:00
|
|
|
@cached_property
|
2016-07-18 21:22:59 +00:00
|
|
|
def is_active(self):
|
|
|
|
""" Renvoie si une interface doit avoir accès ou non """
|
|
|
|
machine = self.machine
|
|
|
|
user = self.machine.user
|
2017-07-18 01:49:36 +00:00
|
|
|
return machine.active and user.has_access()
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2016-11-20 16:57:10 +00:00
|
|
|
def mac_bare(self):
|
|
|
|
return str(EUI(self.mac_address, dialect=mac_bare)).lower()
|
|
|
|
|
2017-09-08 14:02:11 +00:00
|
|
|
def filter_macaddress(self):
|
|
|
|
mac_address = str(EUI(self.mac_address))
|
|
|
|
if mac_address:
|
|
|
|
self.mac_address = mac_address
|
|
|
|
else:
|
|
|
|
raise ValidationError("La mac donnée est invalide")
|
|
|
|
|
2016-11-12 08:09:28 +00:00
|
|
|
def clean(self, *args, **kwargs):
|
2017-09-08 14:02:11 +00:00
|
|
|
self.filter_macaddress()
|
2017-08-07 22:01:45 +00:00
|
|
|
self.mac_address = str(EUI(self.mac_address)) or None
|
2017-07-23 02:22:22 +00:00
|
|
|
if not self.ipv4 or self.type.ip_type != self.ipv4.ip_type:
|
2017-07-21 04:49:39 +00:00
|
|
|
self.assign_ipv4()
|
2017-08-07 22:01:45 +00:00
|
|
|
super(Interface, self).clean(*args, **kwargs)
|
2017-07-21 04:49:39 +00:00
|
|
|
|
|
|
|
def assign_ipv4(self):
|
|
|
|
""" Assigne une ip à l'interface """
|
|
|
|
free_ips = self.type.ip_type.free_ip()
|
|
|
|
if free_ips:
|
|
|
|
self.ipv4 = free_ips[0]
|
|
|
|
else:
|
|
|
|
raise ValidationError("Il n'y a plus d'ip disponibles dans le slash")
|
|
|
|
return
|
|
|
|
|
|
|
|
def unassign_ipv4(self):
|
|
|
|
self.ipv4 = None
|
2016-11-12 08:09:28 +00:00
|
|
|
|
2017-07-23 03:03:56 +00:00
|
|
|
def update_type(self):
|
|
|
|
""" Lorsque le machinetype est changé de type d'ip, on réassigne"""
|
|
|
|
self.clean()
|
|
|
|
self.save()
|
|
|
|
|
2017-07-23 02:22:22 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2017-09-08 14:02:11 +00:00
|
|
|
self.filter_macaddress()
|
2017-07-23 02:22:22 +00:00
|
|
|
# On verifie la cohérence en forçant l'extension par la méthode
|
|
|
|
if self.type.ip_type != self.ipv4.ip_type:
|
|
|
|
raise ValidationError("L'ipv4 et le type de la machine ne correspondent pas")
|
|
|
|
super(Interface, self).save(*args, **kwargs)
|
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
def __str__(self):
|
2016-12-24 19:04:53 +00:00
|
|
|
try:
|
|
|
|
domain = self.domain
|
|
|
|
except:
|
|
|
|
domain = None
|
|
|
|
return str(domain)
|
2016-07-03 14:49:29 +00:00
|
|
|
|
2016-12-24 17:07:09 +00:00
|
|
|
class Domain(models.Model):
|
|
|
|
PRETTY_NAME = "Domaine dns"
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2016-12-24 17:07:09 +00:00
|
|
|
interface_parent = models.OneToOneField('Interface', on_delete=models.CASCADE, blank=True, null=True)
|
|
|
|
name = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255)
|
2016-11-18 16:36:30 +00:00
|
|
|
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
|
2016-12-24 17:07:09 +00:00
|
|
|
cname = models.ForeignKey('self', null=True, blank=True, related_name='related_domain')
|
2016-11-18 16:36:30 +00:00
|
|
|
|
|
|
|
class Meta:
|
2017-08-01 04:57:56 +00:00
|
|
|
unique_together = (("name", "extension"),)
|
2016-11-18 16:36:30 +00:00
|
|
|
|
2017-07-23 02:22:22 +00:00
|
|
|
def get_extension(self):
|
|
|
|
if self.interface_parent:
|
|
|
|
return self.interface_parent.type.ip_type.extension
|
2017-08-01 04:57:56 +00:00
|
|
|
elif hasattr(self,'extension'):
|
2017-07-23 02:22:22 +00:00
|
|
|
return self.extension
|
2017-08-01 04:57:56 +00:00
|
|
|
else:
|
|
|
|
return None
|
2017-07-23 02:22:22 +00:00
|
|
|
|
2016-12-24 17:07:09 +00:00
|
|
|
def clean(self):
|
2017-08-01 04:57:56 +00:00
|
|
|
if self.get_extension():
|
|
|
|
self.extension=self.get_extension()
|
2017-07-21 03:06:53 +00:00
|
|
|
""" Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """
|
2016-12-24 17:07:09 +00:00
|
|
|
if self.interface_parent and self.cname:
|
|
|
|
raise ValidationError("On ne peut créer à la fois A et CNAME")
|
2016-12-24 19:04:53 +00:00
|
|
|
if self.cname==self:
|
2016-12-24 17:07:09 +00:00
|
|
|
raise ValidationError("On ne peut créer un cname sur lui même")
|
2017-07-21 03:06:53 +00:00
|
|
|
HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
|
|
|
|
dns = self.name.lower()
|
|
|
|
if len(dns) > 63:
|
|
|
|
raise ValidationError("Le nom de domaine %s est trop long (maximum de 63 caractères)." % dns)
|
|
|
|
if not HOSTNAME_LABEL_PATTERN.match(dns):
|
|
|
|
raise ValidationError("Ce nom de domaine %s contient des carractères interdits." % dns)
|
2017-08-01 04:57:56 +00:00
|
|
|
self.validate_unique()
|
|
|
|
super(Domain, self).clean()
|
|
|
|
|
2017-09-06 09:07:36 +00:00
|
|
|
@cached_property
|
|
|
|
def dns_entry(self):
|
|
|
|
if self.cname:
|
2017-09-06 09:22:43 +00:00
|
|
|
return str(self.name) + " IN CNAME " + str(self.cname) + "."
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2017-07-23 02:22:22 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2017-08-01 04:57:56 +00:00
|
|
|
if not self.get_extension():
|
|
|
|
raise ValidationError("Extension invalide")
|
|
|
|
self.full_clean()
|
2017-07-23 02:22:22 +00:00
|
|
|
super(Domain, self).save(*args, **kwargs)
|
|
|
|
|
2016-11-01 01:14:06 +00:00
|
|
|
def __str__(self):
|
2017-08-06 18:00:29 +00:00
|
|
|
return str(self.name) + str(self.extension)
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2016-07-03 14:49:29 +00:00
|
|
|
class IpList(models.Model):
|
2016-11-01 01:14:06 +00:00
|
|
|
PRETTY_NAME = "Addresses ipv4"
|
|
|
|
|
2016-07-04 00:48:24 +00:00
|
|
|
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
|
2017-07-21 03:06:53 +00:00
|
|
|
ip_type = models.ForeignKey('IpType', on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def need_infra(self):
|
|
|
|
return self.ip_type.need_infra
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if not str(self.ipv4) in self.ip_type.ip_set_as_str:
|
|
|
|
raise ValidationError("L'ipv4 et le range de l'iptype ne correspondent pas!")
|
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.clean()
|
|
|
|
super(IpList, self).save(*args, **kwargs)
|
2016-07-03 14:49:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-04 00:48:24 +00:00
|
|
|
return self.ipv4
|
|
|
|
|
2017-08-08 04:56:19 +00:00
|
|
|
class Service(models.Model):
|
|
|
|
""" Definition d'un service (dhcp, dns, etc)"""
|
|
|
|
service_type = models.CharField(max_length=255, blank=True, unique=True)
|
2017-08-09 04:08:41 +00:00
|
|
|
min_time_regen = models.DurationField(default=timedelta(minutes=1), help_text="Temps minimal avant nouvelle génération du service")
|
|
|
|
regular_time_regen = models.DurationField(default=timedelta(hours=1), help_text="Temps maximal avant nouvelle génération du service")
|
2017-08-08 04:56:19 +00:00
|
|
|
servers = models.ManyToManyField('Interface', through='Service_link')
|
|
|
|
|
|
|
|
def ask_regen(self):
|
2017-08-08 17:40:30 +00:00
|
|
|
""" Marque à True la demande de régénération pour un service x """
|
2017-08-18 22:01:06 +00:00
|
|
|
Service_link.objects.filter(service=self).exclude(asked_regen=True).update(asked_regen=True)
|
|
|
|
return
|
2017-08-08 04:56:19 +00:00
|
|
|
|
|
|
|
def process_link(self, servers):
|
2017-08-08 17:40:30 +00:00
|
|
|
""" Django ne peut créer lui meme les relations manytomany avec table intermediaire explicite"""
|
2017-08-08 04:56:19 +00:00
|
|
|
for serv in servers.exclude(pk__in=Interface.objects.filter(service=self)):
|
|
|
|
link = Service_link(service=self, server=serv)
|
|
|
|
link.save()
|
2017-08-18 22:01:06 +00:00
|
|
|
Service_link.objects.filter(service=self).exclude(server__in=servers).delete()
|
2017-08-08 04:56:19 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
super(Service, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.service_type)
|
|
|
|
|
2017-08-08 17:40:30 +00:00
|
|
|
def regen(service):
|
|
|
|
""" Fonction externe pour régérération d'un service, prend un objet service en arg"""
|
2017-09-01 01:15:54 +00:00
|
|
|
obj = Service.objects.filter(service_type=service)
|
|
|
|
if obj:
|
|
|
|
obj[0].ask_regen()
|
2017-08-08 17:40:30 +00:00
|
|
|
return
|
|
|
|
|
2017-08-08 04:56:19 +00:00
|
|
|
class Service_link(models.Model):
|
|
|
|
""" Definition du lien entre serveurs et services"""
|
|
|
|
service = models.ForeignKey('Service', on_delete=models.CASCADE)
|
|
|
|
server = models.ForeignKey('Interface', on_delete=models.CASCADE)
|
|
|
|
last_regen = models.DateTimeField(auto_now_add=True)
|
|
|
|
asked_regen = models.BooleanField(default=False)
|
|
|
|
|
2017-08-08 17:40:30 +00:00
|
|
|
def done_regen(self):
|
|
|
|
""" Appellé lorsqu'un serveur a regénéré son service"""
|
|
|
|
self.last_regen = timezone.now()
|
|
|
|
self.asked_regen = False
|
|
|
|
self.save()
|
2017-08-08 04:56:19 +00:00
|
|
|
|
|
|
|
def need_regen(self):
|
2017-08-08 17:40:30 +00:00
|
|
|
""" Décide si le temps minimal écoulé est suffisant pour provoquer une régénération de service"""
|
2017-08-09 04:08:41 +00:00
|
|
|
if (self.asked_regen and (self.last_regen + self.service.min_time_regen) < timezone.now()) or (self.last_regen + self.service.regular_time_regen) < timezone.now():
|
2017-08-08 04:56:19 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.server) + " " + str(self.service)
|
|
|
|
|
2017-03-09 23:05:53 +00:00
|
|
|
@receiver(post_save, sender=Machine)
|
|
|
|
def machine_post_save(sender, **kwargs):
|
|
|
|
user = kwargs['instance'].user
|
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2017-08-08 17:40:30 +00:00
|
|
|
regen('dhcp')
|
2017-08-24 19:36:59 +00:00
|
|
|
regen('mac_ip_list')
|
2017-03-09 23:05:53 +00:00
|
|
|
|
|
|
|
@receiver(post_delete, sender=Machine)
|
|
|
|
def machine_post_delete(sender, **kwargs):
|
|
|
|
machine = kwargs['instance']
|
|
|
|
user = machine.user
|
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2017-09-01 01:15:54 +00:00
|
|
|
regen('dhcp')
|
|
|
|
regen('mac_ip_list')
|
2016-07-25 21:54:40 +00:00
|
|
|
|
|
|
|
@receiver(post_save, sender=Interface)
|
|
|
|
def interface_post_save(sender, **kwargs):
|
2017-07-21 04:49:39 +00:00
|
|
|
interface = kwargs['instance']
|
|
|
|
user = interface.machine.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2017-08-08 17:40:30 +00:00
|
|
|
regen('dhcp')
|
2017-08-24 19:36:59 +00:00
|
|
|
regen('mac_ip_list')
|
2016-07-25 21:54:40 +00:00
|
|
|
|
|
|
|
@receiver(post_delete, sender=Interface)
|
|
|
|
def interface_post_delete(sender, **kwargs):
|
2016-12-14 17:09:24 +00:00
|
|
|
interface = kwargs['instance']
|
|
|
|
user = interface.machine.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
2016-12-14 17:09:24 +00:00
|
|
|
|
2017-07-21 03:06:53 +00:00
|
|
|
@receiver(post_save, sender=IpType)
|
|
|
|
def iptype_post_save(sender, **kwargs):
|
|
|
|
iptype = kwargs['instance']
|
2017-08-28 23:04:15 +00:00
|
|
|
iptype.gen_ip_range()
|
2017-07-21 03:06:53 +00:00
|
|
|
|
2017-07-23 03:03:56 +00:00
|
|
|
@receiver(post_save, sender=MachineType)
|
|
|
|
def machine_post_save(sender, **kwargs):
|
|
|
|
machinetype = kwargs['instance']
|
|
|
|
for interface in machinetype.all_interfaces():
|
|
|
|
interface.update_type()
|
2017-09-01 16:47:34 +00:00
|
|
|
|
|
|
|
@receiver(post_save, sender=Domain)
|
|
|
|
def domain_post_save(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
2017-09-05 16:23:25 +00:00
|
|
|
@receiver(post_delete, sender=Domain)
|
|
|
|
def domain_post_delete(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
2017-09-08 23:07:28 +00:00
|
|
|
@receiver(post_save, sender=Extension)
|
|
|
|
def extension_post_save(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Extension)
|
|
|
|
def extension_post_selete(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
2017-09-05 16:23:25 +00:00
|
|
|
@receiver(post_save, sender=Mx)
|
|
|
|
def mx_post_save(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Mx)
|
|
|
|
def mx_post_delete(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Ns)
|
|
|
|
def ns_post_save(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Ns)
|
|
|
|
def ns_post_delete(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Text)
|
|
|
|
def text_post_save(sender, **kwargs):
|
|
|
|
regen('dns')
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Text)
|
|
|
|
def text_post_delete(sender, **kwargs):
|
2017-09-01 16:47:34 +00:00
|
|
|
regen('dns')
|