From 7f6729c2a7e5dd4b67ecf7331d7e8294f5a6415e Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Tue, 15 May 2018 20:42:00 +0200 Subject: [PATCH 1/9] bug list vide --- topologie/views.py | 213 +++++++++++++++++++++++++++------------------ 1 file changed, 129 insertions(+), 84 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 0c9eb0ab..871b5bbc 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -43,6 +43,10 @@ from django.db import IntegrityError from django.db.models import ProtectedError, Prefetch from django.core.exceptions import ValidationError from django.contrib.staticfiles.storage import staticfiles_storage +from django.template.loader import get_template +from django.template import Context, Template, loader + +import pprint from users.views import form from re2o.utils import re2o_paginator, SortTable @@ -53,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 ( @@ -71,7 +76,8 @@ from .models import ( ConstructorSwitch, AccessPoint, SwitchBay, - Building + Building, + Server ) from .forms import ( EditPortForm, @@ -112,6 +118,12 @@ def index(request): ) pagination_number = GeneralOption.get_cached_value('pagination_number') switch_list = re2o_paginator(request, switch_list, pagination_number) + + 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, 'topologie/index.html', @@ -937,93 +949,126 @@ def make_machine_graph(): """ Crée le fichier dot et l'image du graph des Switchs """ - #Syntaxe DOT temporaire, A mettre dans un template: - lignes=['''digraph Switchs { -node [ -fontname=Helvetica -fontsize=8 -shape=plaintext] -edge[arrowhead=odot,arrowtail=dot]'''] - node_fixe='''node [label=< - - - - - -''' - node_ports=''' -''' - cluster='''subgraph cluster_{} {{ -color=blue; -label="Batiment {}";''' - end_table='''
- -{} -
-{} - -{} -
-{} - -{} -
-{} - -{} -
->] \"{}_{}\" ;''' - switch_alone='''{} [label=< - - -
- -{} -
->]''' - missing=[] - detected=[] - for sw in Switch.objects.all(): - if(sw not in detected): - missing.append(sw) + dico = { + 'subs': [], + 'links' : [], + 'alone': [], + 'colors': { + 'head': "#7f0505", + 'back': "#b5adad", + 'texte': "#563d01", + 'border_bornes': "#02078e", + 'head_bornes': "#25771c", + 'head_server': "#1c3777" + } + } + missing = list(Switch.objects.all()) + detected = [] + + #Parcours tous les batiments for building in Building.objects.all(): - lignes.append(cluster.format(len(lignes),building)) + dico['subs'].append( + { + 'bat_id': building.id, + 'bat_name': building, + 'switchs': [], + 'bornes': [], + 'machines': [] + } + ) + #Parcours tous les switchs de ce batiment for switch in Switch.objects.filter(switchbay__building=building): - lignes.append(node_fixe.format(switch.main_interface().domain.name,"Modèle",switch.model,"Nombre de ports",switch.number)) - for p in switch.ports.all().filter(related__isnull=False): - lignes.append(node_ports.format(p.port,p.related.switch.main_interface().domain.name)) - lignes.append(end_table.format(building.id,switch.id)) - lignes.append("}") - while(missing!=[]): - lignes,new_detected=recursive_switchs(missing[0].ports.all().filter(related=None).first(),None,lignes,[missing[0]]) - missing=[i for i in missing if i not in new_detected] - detected+=new_detected - for switch in Switch.objects.all().filter(switchbay__isnull=True).exclude(ports__related__isnull=False): - lignes.append(switch_alone.format(switch.id,switch.main_interface().domain.name)) - lignes.append("}") - fichier = open("media/images/switchs.dot","w") - for ligne in lignes: - fichier.write(ligne+"\n") + dico['subs'][-1]['switchs'].append({ + 'name': switch.main_interface().domain.name, + 'nombre': switch.number, + 'model': switch.model, + 'id': switch.id, + 'batiment': building, + 'ports': [] + }) + #Parcours tous les ports liés de ce switch, on ajoute les switchs relié à celui-ci + for port in switch.ports.filter(related__isnull=False): + dico['subs'][-1]['switchs'][-1]['ports'].append({ + 'numero': port.port, + 'related': port.related.switch.main_interface().domain.name + }) + + for ap in AccessPoint.all_ap_in(building): + dico['subs'][-1]['bornes'].append({ + 'name': ap.short_name, + 'switch': ap.switch()[0].main_interface().domain.name, + 'port': ap.switch()[0].ports.filter( + machine_interface__machine=ap + )[0].port + }) + for server in Server.all_server_in(building): + dico['subs'][-1]['machines'].append({ + 'name': server.short_name, + 'switch': server.switch()[0].main_interface().domain.name, + 'port': Port.objects.filter(machine_interface__machine=server)[0].port + }) + + #Tant que la liste des oubliés n'est pas vide i.e on les a pas tous passer + while missing: + print("depart initial sur:{}".format(missing[0])) + if missing[0].ports.count():#le switch à des ports + links, new_detected = recursive_switchs(missing[0].ports.first(), None, [missing[0]]) + for link in links: + dico['links'].append(link) + #On recrée la liste des oubliés et des detectés + missing=[i for i in missing if i not in new_detected] + detected += new_detected + else:#Si le switch n'a pas de port, on explore pas et on passe au suivant + del missing[0] + #Tous ceux qui ne sont ni connectés ni dans un batiment + for switch in Switch.objects.filter(switchbay__isnull=True).exclude(ports__related__isnull=False): + dico['alone'].append({ + 'id': switch.id, + 'name': switch.main_interface().domain.name + }) + + #Exportation du dot et génération de l'image + dot_data=generate_image(dico) + 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"], stdout=PIPE) - image = Popen(["dot", "-Tpng", "-o", "media/images/switchs.png"], stdin=unflatten.stdout, stdout=PIPE) + unflatten = Popen( + ["unflatten","-l", "3", MEDIA_ROOT + "/images/switchs.dot"], + stdout=PIPE + ) + image = Popen( + ["dot", "-Tpng", "-o", MEDIA_ROOT + "/images/switchs.png"], + stdin=unflatten.stdout, + stdout=PIPE + ) +def generate_image(data,template='topologie/graph_switch.dot'): + t = loader.get_template(template) + if not isinstance(t, Template) and not (hasattr(t, 'template') and isinstance(t.template, Template)): + raise Exception("Le template par défaut de Django n'est pas utilisé." + "Cela peut mener à des erreurs de rendu." + "Vérifiez les paramètres") + c = Context(data).flatten() + dot = t.render(c) + return(dot) -def recursive_switchs(port_start, switch_before, lignes,detected): +def recursive_switchs(port_start, switch_before, detected): """ Parcour récursivement le switchs auquel appartient port_start pour trouver les ports suivants liés """ - l_ports=port_start.switch.ports.filter(related__isnull=False) - for port in l_ports: - if port.related.switch!=switch_before and port.related.switch!=port.switch: - links=[] - for sw in [switch for switch in [port_start.switch,port.related.switch]]: - if(sw not in detected): - detected.append(sw) - if(sw.switchbay.building): - links.append("\"{}_{}\"".format(sw.switchbay.building.id,sw.id)) - else: - links.append("\"{}\"".format(sw.id)) - lignes.append(links[0]+" -> "+links[1]) - lignes, detected = recursive_switchs(port.related, port_start.switch, lignes, detected) - return (lignes, detected) + links_return=[]#Liste de dictionaires qui stockes les nouveaux liens trouvés + for port in port_start.switch.ports.filter(related__isnull=False):#Liste des ports dont le related est non null + if port.related.switch!=switch_before and port.related.switch != port.switch:#Pas le switch dont on descend, pas le switch actuel + links = {#dictionaire contenant le lien + 'depart':port_start.switch.id, + 'arrive':port.related.switch.id + } + if port.related.switch not in detected:#si le switch au bout de ce lien n'a pas été déjà exploré + links_down, detected = recursive_switchs(port.related, port_start.switch, detected)#On l'explore et on récupère le résultat + for link in links_down:#On ajoute les résultats du niveau inférieur à ceux du niveau actuel + if link: + links_return.append(link) + links_return.append(links)#Ajout des liens trouvés (niveau inférieur et actuel) à la liste à renvoyer + detected.append(port_start.switch)#Le switch est considéré comme parcouru + return (links_return, detected) + From 33d5df19a5144d78f8910661c4683c3cef0720d4 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Sat, 19 May 2018 21:20:55 +0200 Subject: [PATCH 2/9] Doc et commentaires --- topologie/views.py | 68 ++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 871b5bbc..570b9673 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -77,7 +77,7 @@ from .models import ( AccessPoint, SwitchBay, Building, - Server + Server, ) from .forms import ( EditPortForm, @@ -947,14 +947,14 @@ def del_constructor_switch(request, constructor_switch, **_kwargs): def make_machine_graph(): """ - Crée le fichier dot et l'image du graph des Switchs + Create the graph of switchs, machines and access points. """ dico = { 'subs': [], 'links' : [], 'alone': [], 'colors': { - 'head': "#7f0505", + 'head': "#7f0505",#Color parameters for the graph 'back': "#b5adad", 'texte': "#563d01", 'border_bornes': "#02078e", @@ -964,8 +964,7 @@ def make_machine_graph(): } missing = list(Switch.objects.all()) detected = [] - - #Parcours tous les batiments + #Visit all buildings for building in Building.objects.all(): dico['subs'].append( { @@ -976,7 +975,7 @@ def make_machine_graph(): 'machines': [] } ) - #Parcours tous les switchs de ce batiment + #Visit all switchs in this building for switch in Switch.objects.filter(switchbay__building=building): dico['subs'][-1]['switchs'].append({ 'name': switch.main_interface().domain.name, @@ -986,7 +985,7 @@ def make_machine_graph(): 'batiment': building, 'ports': [] }) - #Parcours tous les ports liés de ce switch, on ajoute les switchs relié à celui-ci + #visit all ports of this switch and add the switchs linked to it for port in switch.ports.filter(related__isnull=False): dico['subs'][-1]['switchs'][-1]['ports'].append({ 'numero': port.port, @@ -1008,41 +1007,43 @@ def make_machine_graph(): 'port': Port.objects.filter(machine_interface__machine=server)[0].port }) - #Tant que la liste des oubliés n'est pas vide i.e on les a pas tous passer + #While the list of forgotten ones is not empty while missing: - print("depart initial sur:{}".format(missing[0])) - if missing[0].ports.count():#le switch à des ports - links, new_detected = recursive_switchs(missing[0].ports.first(), None, [missing[0]]) + if missing[0].ports.count():#The switch is not empty + links, new_detected = recursive_switchs(missing[0], None, [missing[0]]) for link in links: dico['links'].append(link) - #On recrée la liste des oubliés et des detectés + #Update the lists of missings and already detected switchs missing=[i for i in missing if i not in new_detected] detected += new_detected - else:#Si le switch n'a pas de port, on explore pas et on passe au suivant + else:#If the switch have no ports, don't explore it and hop to the next one del missing[0] - #Tous ceux qui ne sont ni connectés ni dans un batiment + #Switchs that are not connected or not in a building for switch in Switch.objects.filter(switchbay__isnull=True).exclude(ports__related__isnull=False): dico['alone'].append({ 'id': switch.id, 'name': switch.main_interface().domain.name }) - #Exportation du dot et génération de l'image - dot_data=generate_image(dico) + + dot_data=generate_image(dico)#generate the dot file fichier = open(MEDIA_ROOT + "/images/switchs.dot","w", encoding='utf-8') fichier.write(dot_data) fichier.close() - unflatten = Popen( + unflatten = Popen(#unflatten the graph to make it look better ["unflatten","-l", "3", MEDIA_ROOT + "/images/switchs.dot"], stdout=PIPE ) - image = Popen( + image = Popen(#pipe the result of the first command into the second ["dot", "-Tpng", "-o", MEDIA_ROOT + "/images/switchs.png"], stdin=unflatten.stdout, stdout=PIPE ) -def generate_image(data,template='topologie/graph_switch.dot'): +def generate_dot(data,template='topologie/graph_switch.dot'): + """create the dot file + data: dictionary passed to the template + template: path to the dot template""" t = loader.get_template(template) if not isinstance(t, Template) and not (hasattr(t, 'template') and isinstance(t.template, Template)): raise Exception("Le template par défaut de Django n'est pas utilisé." @@ -1052,23 +1053,24 @@ def generate_image(data,template='topologie/graph_switch.dot'): dot = t.render(c) return(dot) -def recursive_switchs(port_start, switch_before, detected): - """ - Parcour récursivement le switchs auquel appartient port_start pour trouver les ports suivants liés - """ - links_return=[]#Liste de dictionaires qui stockes les nouveaux liens trouvés - for port in port_start.switch.ports.filter(related__isnull=False):#Liste des ports dont le related est non null - if port.related.switch!=switch_before and port.related.switch != port.switch:#Pas le switch dont on descend, pas le switch actuel - links = {#dictionaire contenant le lien - 'depart':port_start.switch.id, +def recursive_switchs(switch_start, switch_before, detected): + """Visit the switch and travel to the switchs linked to it. + switch_start: the switch to begin the visit on + switch_before: the switch that you come from. None if switch_start is the first one + detected: list of all switchs already visited. None if switch_start is the first one""" + links_return=[]#list of dictionaries of the links to be detected + for port in switch_start.ports.filter(related__isnull=False):#Ports that are related to another switch + if port.related.switch != switch_before and port.related.switch != port.switch:#Not the switch that we come from, not the current switch + links = {#Dictionary of a link + 'depart':switch_start.id, 'arrive':port.related.switch.id } - if port.related.switch not in detected:#si le switch au bout de ce lien n'a pas été déjà exploré - links_down, detected = recursive_switchs(port.related, port_start.switch, detected)#On l'explore et on récupère le résultat - for link in links_down:#On ajoute les résultats du niveau inférieur à ceux du niveau actuel + if port.related.switch not in detected:#The switch at the end of this link has not been visited + links_down, detected = recursive_switchs(port.related.switch, switch_start, detected)#explore it and get the results + for link in links_down:#Add the non empty links to the current list if link: links_return.append(link) - links_return.append(links)#Ajout des liens trouvés (niveau inférieur et actuel) à la liste à renvoyer - detected.append(port_start.switch)#Le switch est considéré comme parcouru + links_return.append(links)#Add current and below levels links + detected.append(switch_start)#This switch is considered detected return (links_return, detected) From 7e3e27be909c090106660c9f8eed92cdd0866feb Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Sun, 20 May 2018 11:42:35 +0200 Subject: [PATCH 3/9] Docstring reST style --- topologie/views.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 570b9673..9205b321 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -1042,8 +1042,9 @@ def make_machine_graph(): def generate_dot(data,template='topologie/graph_switch.dot'): """create the dot file - data: dictionary passed to the template - template: path to the dot template""" + :param data: dictionary passed to the template + :param template: path to the dot template + :return: all the lines of the dot file""" t = loader.get_template(template) if not isinstance(t, Template) and not (hasattr(t, 'template') and isinstance(t.template, Template)): raise Exception("Le template par défaut de Django n'est pas utilisé." @@ -1055,9 +1056,10 @@ def generate_dot(data,template='topologie/graph_switch.dot'): def recursive_switchs(switch_start, switch_before, detected): """Visit the switch and travel to the switchs linked to it. - switch_start: the switch to begin the visit on - switch_before: the switch that you come from. None if switch_start is the first one - detected: list of all switchs already visited. None if switch_start is the first one""" + :param switch_start: the switch to begin the visit on + :param switch_before: the switch that you come from. None if switch_start is the first one + :param detected: list of all switchs already visited. None if switch_start is the first one + :return: A list of all the links found and a list of all the switchs visited""" links_return=[]#list of dictionaries of the links to be detected for port in switch_start.ports.filter(related__isnull=False):#Ports that are related to another switch if port.related.switch != switch_before and port.related.switch != port.switch:#Not the switch that we come from, not the current switch From d8f139c2077c97d544255232fbac5cc57cb6a845 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Mon, 21 May 2018 21:13:44 +0200 Subject: [PATCH 4/9] =?UTF-8?q?R=C3=A9cup=C3=A9ration=20de=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/models.py | 108 +++++++++++++- .../templates/topologie/graph_switch.dot | 135 ++++++++++++++++++ topologie/templates/topologie/index.html | 13 +- topologie/views.py | 60 ++++---- 4 files changed, 287 insertions(+), 29 deletions(-) create mode 100644 topologie/templates/topologie/graph_switch.dot diff --git a/topologie/models.py b/topologie/models.py index 23b72b75..e1c945c7 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -40,7 +40,8 @@ from __future__ import unicode_literals import itertools from django.db import models -from django.db.models.signals import post_save, post_delete +from django.db.models.signals import pre_save, post_save, post_delete +from django.utils.functional import cached_property from django.dispatch import receiver from django.core.exceptions import ValidationError from django.db import IntegrityError @@ -50,6 +51,11 @@ from reversion import revisions as reversion from machines.models import Machine, regen from re2o.mixins import AclMixin, RevMixin +from os.path import isfile +from os import remove + + + class Stack(AclMixin, RevMixin, models.Model): """Un objet stack. Regrouppe des switchs en foreign key @@ -103,6 +109,70 @@ class AccessPoint(AclMixin, Machine): ("view_accesspoint", "Peut voir une borne"), ) + def port(self): + """Return the queryset of ports for this device""" + return Port.objects.filter( + machine_interface__machine=self + ) + + def switch(self): + """Return the switch where this is plugged""" + return Switch.objects.filter( + ports__machine_interface__machine=self + ) + + def building(self): + """Return the building of the AP/Server (building of the switchs connected to...)""" + return Building.objects.filter( + switchbay__switch=self.switch() + ) + + @cached_property + def short_name(self): + return str(self.interface_set.first().domain.name) + + @classmethod + def all_ap_in(cls, building_instance): + """Get a building as argument, returns all ap of a building""" + return cls.objects.filter(interface__port__switch__switchbay__building=building_instance) + + def __str__(self): + return str(self.interface_set.first()) + + +class Server(Machine): + """Dummy class, to retrieve servers of a building, or get switch of a server""" + + class Meta: + proxy = True + + def port(self): + """Return the queryset of ports for this device""" + return Port.objects.filter( + machine_interface__machine=self + ) + + def switch(self): + """Return the switch where this is plugged""" + return Switch.objects.filter( + ports__machine_interface__machine=self + ) + + def building(self): + """Return the building of the AP/Server (building of the switchs connected to...)""" + return Building.objects.filter( + switchbay__switch=self.switch() + ) + + @cached_property + def short_name(self): + return str(self.interface_set.first().domain.name) + + @classmethod + def all_server_in(cls, building_instance): + """Get a building as argument, returns all server of a building""" + return cls.objects.filter(interface__port__switch__switchbay__building=building_instance).exclude(accesspoint__isnull=False) + def __str__(self): return str(self.interface_set.first()) @@ -422,15 +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) +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") diff --git a/topologie/templates/topologie/graph_switch.dot b/topologie/templates/topologie/graph_switch.dot new file mode 100644 index 00000000..46c6a766 --- /dev/null +++ b/topologie/templates/topologie/graph_switch.dot @@ -0,0 +1,135 @@ +{% block graph_dot %} +strict digraph { +graph [label="TOPOLOGIE DU RÉSEAU", labelloc=t, fontsize=40]; +node [fontname=Helvetica fontsize=8 shape=plaintext]; +edge[arrowhead=none]; + + +{% block subgraphs %} +{% for sub in subs %} +subgraph cluster_{{ sub.bat_id }} { +fontsize=15; +label="Batiment {{ sub.bat_name }}"; + +{% if sub.bornes %} +{% block bornes %} +node [label=< + + + + + + +{% for borne in sub.bornes %} + + + + + +{% endfor %} +
+ Borne + Switch + Port
+ {{ borne.name }} + + {{ borne.switch }} + + {{ borne.port }} +
+>] {{sub.bat_name}}bornes; +{% endblock %} +{% endif %} + +{% if sub.machines %} +{% block machines %} +node [label=< + + + + + + + +{% for machine in sub.machines %} + + + + + +{% endfor %} +
+ Machine + Switch + Port
+ {{ machine.name }} + + {{ machine.switch }} + + {{ machine.port }} +
+>] {{sub.bat_name}}machines; +{% endblock %} +{% endif %} + + +{% block switchs %} +{% for switch in sub.switchs %} +node [label=< + + + + + + +{% block liens %} +{% for port in switch.ports %} + + +{% endfor %} +{% endblock %} +
+ +{{ switch.name }} +
+Modèle + +{{ switch.model }} +
+Taille + +{{ switch.nombre }} +
+{{ port.numero }} + +{{ port.related }} +
+>] "{{ switch.id }}" ; +{% endfor %} +{% endblock %} +} +{% endfor %} +{% endblock %} + + +{% block isoles %} +{% for switchs in alone %} +"{{switchs.id}}" [label=< + + +
+ +{{switchs.name}} +
+>] +{% endfor %} +{% endblock %} + + +{% block links %} +{% for link in links %} +"{{ link.depart }}" -> "{{ link.arrive }}"; +{% endfor %} +{% endblock %} +} +{% endblock %} \ No newline at end of file diff --git a/topologie/templates/topologie/index.html b/topologie/templates/topologie/index.html index 6e140251..b5df8a06 100644 --- a/topologie/templates/topologie/index.html +++ b/topologie/templates/topologie/index.html @@ -29,7 +29,18 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block title %}Switchs{% endblock %} {% block content %} - + + + + + + + +
+ +
diff --git a/topologie/views.py b/topologie/views.py index 9205b321..b66c8243 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -45,6 +45,8 @@ from django.core.exceptions import ValidationError from django.contrib.staticfiles.storage import staticfiles_storage from django.template.loader import get_template from django.template import Context, Template, loader +from django.db.models.signals import post_save +from django.dispatch import receiver import pprint @@ -95,7 +97,13 @@ from .forms import ( EditBuildingForm ) -from subprocess import Popen,PIPE +from subprocess import ( + Popen, + PIPE +) + +from os.path import isfile +from os import remove @login_required @@ -124,6 +132,8 @@ def index(request): for service_link in Service_link.objects.filter(service__service_type='graph_topo'): service_link.done_regen() + if not isfile("/var/www/re2o/media/images/switchs.png"): + make_machine_graph() return render( request, 'topologie/index.html', @@ -954,7 +964,7 @@ def make_machine_graph(): 'links' : [], 'alone': [], 'colors': { - 'head': "#7f0505",#Color parameters for the graph + 'head': "#7f0505", # Color parameters for the graph 'back': "#b5adad", 'texte': "#563d01", 'border_bornes': "#02078e", @@ -964,8 +974,8 @@ def make_machine_graph(): } missing = list(Switch.objects.all()) detected = [] - #Visit all buildings - for building in Building.objects.all(): + for building in Building.objects.all(): # Visit all buildings + dico['subs'].append( { 'bat_id': building.id, @@ -975,8 +985,8 @@ def make_machine_graph(): 'machines': [] } ) - #Visit all switchs in this building - for switch in Switch.objects.filter(switchbay__building=building): + # Visit all switchs in this building + for switch in Switch.objects.filter(switchbay__building=building): dico['subs'][-1]['switchs'].append({ 'name': switch.main_interface().domain.name, 'nombre': switch.number, @@ -985,7 +995,7 @@ def make_machine_graph(): 'batiment': building, 'ports': [] }) - #visit all ports of this switch and add the switchs linked to it + # visit all ports of this switch and add the switchs linked to it for port in switch.ports.filter(related__isnull=False): dico['subs'][-1]['switchs'][-1]['ports'].append({ 'numero': port.port, @@ -1007,18 +1017,18 @@ def make_machine_graph(): 'port': Port.objects.filter(machine_interface__machine=server)[0].port }) - #While the list of forgotten ones is not empty + # While the list of forgotten ones is not empty while missing: - if missing[0].ports.count():#The switch is not empty + if missing[0].ports.count(): # The switch is not empty links, new_detected = recursive_switchs(missing[0], None, [missing[0]]) for link in links: dico['links'].append(link) - #Update the lists of missings and already detected switchs + # Update the lists of missings and already detected switchs missing=[i for i in missing if i not in new_detected] detected += new_detected - else:#If the switch have no ports, don't explore it and hop to the next one + else: # If the switch have no ports, don't explore it and hop to the next one del missing[0] - #Switchs that are not connected or not in a building + # Switchs that are not connected or not in a building for switch in Switch.objects.filter(switchbay__isnull=True).exclude(ports__related__isnull=False): dico['alone'].append({ 'id': switch.id, @@ -1026,21 +1036,21 @@ def make_machine_graph(): }) - dot_data=generate_image(dico)#generate the dot file + dot_data=generate_dot(dico,'topologie/graph_switch.dot') # generate the dot file fichier = open(MEDIA_ROOT + "/images/switchs.dot","w", encoding='utf-8') fichier.write(dot_data) fichier.close() - unflatten = Popen(#unflatten the graph to make it look better + unflatten = Popen( # unflatten the graph to make it look better ["unflatten","-l", "3", MEDIA_ROOT + "/images/switchs.dot"], stdout=PIPE ) - image = Popen(#pipe the result of the first command into the second + image = Popen( # pipe the result of the first command into the second ["dot", "-Tpng", "-o", MEDIA_ROOT + "/images/switchs.png"], stdin=unflatten.stdout, stdout=PIPE ) -def generate_dot(data,template='topologie/graph_switch.dot'): +def generate_dot(data,template): """create the dot file :param data: dictionary passed to the template :param template: path to the dot template @@ -1060,19 +1070,19 @@ def recursive_switchs(switch_start, switch_before, detected): :param switch_before: the switch that you come from. None if switch_start is the first one :param detected: list of all switchs already visited. None if switch_start is the first one :return: A list of all the links found and a list of all the switchs visited""" - links_return=[]#list of dictionaries of the links to be detected - for port in switch_start.ports.filter(related__isnull=False):#Ports that are related to another switch - if port.related.switch != switch_before and port.related.switch != port.switch:#Not the switch that we come from, not the current switch - links = {#Dictionary of a link + links_return=[] # list of dictionaries of the links to be detected + for port in switch_start.ports.filter(related__isnull=False): # Ports that are related to another switch + if port.related.switch != switch_before and port.related.switch != port.switch: # Not the switch that we come from, not the current switch + links = { # Dictionary of a link 'depart':switch_start.id, 'arrive':port.related.switch.id } - if port.related.switch not in detected:#The switch at the end of this link has not been visited - links_down, detected = recursive_switchs(port.related.switch, switch_start, detected)#explore it and get the results - for link in links_down:#Add the non empty links to the current list + if port.related.switch not in detected: # The switch at the end of this link has not been visited + links_down, detected = recursive_switchs(port.related.switch, switch_start, detected) # explore it and get the results + for link in links_down: # Add the non empty links to the current list if link: links_return.append(link) - links_return.append(links)#Add current and below levels links - detected.append(switch_start)#This switch is considered detected + links_return.append(links) # Add current and below levels links + detected.append(switch_start) # This switch is considered detected return (links_return, detected) From 8cc1841f6958203f0c730d87fcd725c914ff95f4 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Mon, 21 May 2018 21:19:10 +0200 Subject: [PATCH 5/9] ajout des migrations --- .../migrations/0081_auto_20180521_1413.py | 21 ++++++++++++++++ topologie/migrations/0060_server.py | 25 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 machines/migrations/0081_auto_20180521_1413.py create mode 100644 topologie/migrations/0060_server.py diff --git a/machines/migrations/0081_auto_20180521_1413.py b/machines/migrations/0081_auto_20180521_1413.py new file mode 100644 index 00000000..7948796a --- /dev/null +++ b/machines/migrations/0081_auto_20180521_1413.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-05-21 19:13 +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='extension', + name='soa', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='machines.SOA'), + ), + ] diff --git a/topologie/migrations/0060_server.py b/topologie/migrations/0060_server.py new file mode 100644 index 00000000..af067e7b --- /dev/null +++ b/topologie/migrations/0060_server.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-05-21 19:13 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0081_auto_20180521_1413'), + ('topologie', '0059_auto_20180415_2249'), + ] + + operations = [ + migrations.CreateModel( + name='Server', + fields=[ + ], + options={ + 'proxy': True, + }, + bases=('machines.machine',), + ), + ] From 93a9ce630f11bcb6fa300ee85f7932c39135c858 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Mon, 21 May 2018 22:05:31 +0200 Subject: [PATCH 6/9] Fichier dot temporaire --- topologie/views.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index b66c8243..652ef475 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -48,7 +48,7 @@ from django.template import Context, Template, loader from django.db.models.signals import post_save from django.dispatch import receiver -import pprint +import tempfile from users.views import form from re2o.utils import re2o_paginator, SortTable @@ -1037,11 +1037,12 @@ def make_machine_graph(): dot_data=generate_dot(dico,'topologie/graph_switch.dot') # generate the dot file - fichier = open(MEDIA_ROOT + "/images/switchs.dot","w", encoding='utf-8') - fichier.write(dot_data) - fichier.close() + + f = tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8', delete=False) # Create a temporary file to store the dot data + with f: + f.write(dot_data) unflatten = Popen( # unflatten the graph to make it look better - ["unflatten","-l", "3", MEDIA_ROOT + "/images/switchs.dot"], + ["unflatten","-l", "3", f.name], stdout=PIPE ) image = Popen( # pipe the result of the first command into the second From f9af3fad8bed844f93c54d5713ed64c3d186fe9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Mon, 21 May 2018 21:50:11 +0000 Subject: [PATCH 7/9] Fix generate img when spaces in names --- topologie/templates/topologie/graph_switch.dot | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/topologie/templates/topologie/graph_switch.dot b/topologie/templates/topologie/graph_switch.dot index 46c6a766..a6a4c62d 100644 --- a/topologie/templates/topologie/graph_switch.dot +++ b/topologie/templates/topologie/graph_switch.dot @@ -37,7 +37,7 @@ node [label=< {% endfor %} ->] {{sub.bat_name}}bornes; +>] "{{sub.bat_name}}bornes"; {% endblock %} {% endif %} @@ -68,7 +68,7 @@ node [label=< {% endfor %} ->] {{sub.bat_name}}machines; +>] "{{sub.bat_name}}machines"; {% endblock %} {% endif %} From a8f3614e3012ec4e8c9cbafd063194d020f8f440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Mon, 21 May 2018 21:50:40 +0000 Subject: [PATCH 8/9] Hide graph by default --- topologie/templates/topologie/index.html | 34 +++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/topologie/templates/topologie/index.html b/topologie/templates/topologie/index.html index b5df8a06..a7c4bb51 100644 --- a/topologie/templates/topologie/index.html +++ b/topologie/templates/topologie/index.html @@ -30,7 +30,27 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block content %} - @@ -38,20 +58,10 @@ Topologie des Switchs -
+ - - - -

Switchs

{% can_create Switch %} Ajouter un switch From c06d710d3c5746ae01df6a985537aa6b959f526f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Mon, 21 May 2018 22:03:23 +0000 Subject: [PATCH 9/9] Typo --- topologie/templates/topologie/graph_switch.dot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/topologie/templates/topologie/graph_switch.dot b/topologie/templates/topologie/graph_switch.dot index a6a4c62d..cb9dfaf8 100644 --- a/topologie/templates/topologie/graph_switch.dot +++ b/topologie/templates/topologie/graph_switch.dot @@ -9,7 +9,7 @@ edge[arrowhead=none]; {% for sub in subs %} subgraph cluster_{{ sub.bat_id }} { fontsize=15; -label="Batiment {{ sub.bat_name }}"; +label="Bâtiment {{ sub.bat_name }}"; {% if sub.bornes %} {% block bornes %}