mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-22 03:13:12 +00:00
Refactor de la fonction gen_graph + graph quand modification des objets
This commit is contained in:
parent
c62a1ac30e
commit
b2d57ec827
5 changed files with 108 additions and 21 deletions
21
machines/migrations/0081_auto_20180511_1254.py
Normal file
21
machines/migrations/0081_auto_20180511_1254.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.7 on 2018-05-11 17:54
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('machines', '0080_auto_20180502_2334'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='service_link',
|
||||
name='server',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='machines.Interface'),
|
||||
),
|
||||
]
|
|
@ -1373,9 +1373,8 @@ class Service(RevMixin, AclMixin, models.Model):
|
|||
def regen(service):
|
||||
""" Fonction externe pour régérération d'un service, prend un objet service
|
||||
en arg"""
|
||||
obj = Service.objects.filter(service_type=service)
|
||||
if obj:
|
||||
obj[0].ask_regen()
|
||||
obj, created = Service.objects.get_or_create(service_type=service)
|
||||
obj.ask_regen()
|
||||
return
|
||||
|
||||
|
||||
|
@ -1384,7 +1383,12 @@ class Service_link(RevMixin, AclMixin, models.Model):
|
|||
PRETTY_NAME = "Relation entre service et serveur"
|
||||
|
||||
service = models.ForeignKey('Service', on_delete=models.CASCADE)
|
||||
server = models.ForeignKey('Interface', on_delete=models.CASCADE)
|
||||
server = models.ForeignKey(
|
||||
'Interface',
|
||||
on_delete=models.CASCADE,
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
last_regen = models.DateTimeField(auto_now_add=True)
|
||||
asked_regen = models.BooleanField(default=False)
|
||||
|
||||
|
@ -1525,6 +1529,8 @@ def machine_post_save(**kwargs):
|
|||
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
|
||||
regen('dhcp')
|
||||
regen('mac_ip_list')
|
||||
if user == preferences.models.OptionalMachine.get_cached_value('utilisateur_asso'):
|
||||
regen('graph_topo')
|
||||
|
||||
|
||||
@receiver(post_delete, sender=Machine)
|
||||
|
@ -1549,6 +1555,8 @@ def interface_post_save(**kwargs):
|
|||
# Regen services
|
||||
regen('dhcp')
|
||||
regen('mac_ip_list')
|
||||
if interface.machine.user == preferences.models.OptionalMachine.get_cached_value('utilisateur_asso'):
|
||||
regen('graph_topo')
|
||||
|
||||
|
||||
@receiver(post_delete, sender=Interface)
|
||||
|
@ -1659,3 +1667,11 @@ def srv_post_save(**_kwargs):
|
|||
def srv_post_delete(**_kwargs):
|
||||
"""Regeneration dns après modification d'un SRV"""
|
||||
regen('dns')
|
||||
|
||||
|
||||
@receiver(post_save, sender=Service)
|
||||
def service_post_save(**kwargs):
|
||||
"""Création d'un service_link si non existant"""
|
||||
service = kwargs['instance']
|
||||
service_link, created = Service_link.objects.get_or_create(service=service)
|
||||
|
||||
|
|
25
topologie/migrations/0060_server.py
Normal file
25
topologie/migrations/0060_server.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.7 on 2018-05-11 17:54
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('machines', '0081_auto_20180511_1254'),
|
||||
('topologie', '0059_auto_20180415_2249'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Server',
|
||||
fields=[
|
||||
],
|
||||
options={
|
||||
'proxy': True,
|
||||
},
|
||||
bases=('machines.machine',),
|
||||
),
|
||||
]
|
|
@ -492,26 +492,47 @@ class Room(AclMixin, RevMixin, models.Model):
|
|||
def ap_post_save(**_kwargs):
|
||||
"""Regeneration des noms des bornes vers le controleur"""
|
||||
regen('unifi-ap-names')
|
||||
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_delete, sender=AccessPoint)
|
||||
def ap_post_delete(**_kwargs):
|
||||
"""Regeneration des noms des bornes vers le controleur"""
|
||||
regen('unifi-ap-names')
|
||||
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_delete, sender=Stack)
|
||||
def stack_post_delete(**_kwargs):
|
||||
"""Vide les id des switches membres d'une stack supprimée"""
|
||||
Switch.objects.filter(stack=None).update(stack_member_id=None)
|
||||
|
||||
#@receiver(post_save, sender=Port)
|
||||
#@receiver(post_save, sender=AccessPoint)
|
||||
#@receiver(post_save, sender=ModelSwitch)
|
||||
#@receiver(post_save, sender=Building)
|
||||
#@receiver(post_save, sender=Building)
|
||||
#@receiver(post_save, sender=Server)
|
||||
@receiver(pre_save, sender=Switch)
|
||||
def delete_graph(**_kwargs):
|
||||
if isfile("media/images/switchs.png"):
|
||||
remove("media/images/switchs.png")
|
||||
@receiver(post_save, sender=Port)
|
||||
def port_post_save(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_delete, sender=Port)
|
||||
def port_post_delete(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_save, sender=ModelSwitch)
|
||||
def modelswitch_post_save(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_delete, sender=ModelSwitch)
|
||||
def modelswitch_post_delete(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_save, sender=Building)
|
||||
def building_post_save(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_delete, sender=Building)
|
||||
def building_post_delete(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_save, sender=Switch)
|
||||
def switch_post_save(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
||||
@receiver(post_delete, sender=Switch)
|
||||
def switch_post_delete(**_kwargs):
|
||||
regen("graph_topo")
|
||||
|
|
|
@ -57,13 +57,14 @@ from re2o.acl import (
|
|||
can_view,
|
||||
can_view_all,
|
||||
)
|
||||
from re2o.settings import MEDIA_ROOT
|
||||
from machines.forms import (
|
||||
DomainForm,
|
||||
EditInterfaceForm,
|
||||
AddInterfaceForm
|
||||
)
|
||||
from machines.views import generate_ipv4_mbf_param
|
||||
from machines.models import Interface
|
||||
from machines.models import Interface, Service_link
|
||||
from preferences.models import AssoOption, GeneralOption
|
||||
|
||||
from .models import (
|
||||
|
@ -118,7 +119,10 @@ def index(request):
|
|||
pagination_number = GeneralOption.get_cached_value('pagination_number')
|
||||
switch_list = re2o_paginator(request, switch_list, pagination_number)
|
||||
|
||||
# make_machine_graph()
|
||||
if any(service_link.need_regen() for service_link in Service_link.objects.filter(service__service_type='graph_topo')):
|
||||
make_machine_graph()
|
||||
for service_link in Service_link.objects.filter(service__service_type='graph_topo'):
|
||||
service_link.done_regen()
|
||||
|
||||
return render(
|
||||
request,
|
||||
|
@ -1022,15 +1026,15 @@ def make_machine_graph():
|
|||
|
||||
#Exportation du dot et génération de l'image
|
||||
dot_data=generate_image(dico)
|
||||
fichier = open("media/images/switchs.dot","w")
|
||||
fichier = open(MEDIA_ROOT + "/images/switchs.dot","w", encoding='utf-8')
|
||||
fichier.write(dot_data)
|
||||
fichier.close()
|
||||
unflatten = Popen(
|
||||
["unflatten","-l", "3", "media/images/switchs.dot"],
|
||||
["unflatten","-l", "3", MEDIA_ROOT + "/images/switchs.dot"],
|
||||
stdout=PIPE
|
||||
)
|
||||
image = Popen(
|
||||
["dot", "-Tpng", "-o", "media/images/switchs.png"],
|
||||
["dot", "-Tpng", "-o", MEDIA_ROOT + "/images/switchs.png"],
|
||||
stdin=unflatten.stdout,
|
||||
stdout=PIPE
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue