architecture.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.core.management.base import BaseCommand, CommandParser
  2. from services.models import IPResource, IPPrefix, Service, ServiceType, ServiceAllocation, Switch
  3. from djadhere.utils import get_active_filter
  4. class Command(BaseCommand):
  5. def add_arguments(self, parser):
  6. parser.add_argument('--switch', action='store_true')
  7. parser.add_argument('--ip', action='store_true')
  8. def handle(self, *args, **options):
  9. if options['switch']:
  10. self.export_switch()
  11. if options['ip']:
  12. self.export_ip()
  13. def export_switch(self):
  14. for switch in Switch.objects.all():
  15. print()
  16. print("=== Switch %s ===" % switch.name)
  17. print(switch.notes)
  18. for port in switch.ports.all():
  19. line = "Port %d" % port.port
  20. if port.service:
  21. line += ' - (%s %s) %s' % (port.service.adhesion, port.service.adhesion.adherent, port.service)
  22. if port.notes:
  23. line += ' - %s' % port.notes
  24. print(line)
  25. def export_ip(self):
  26. public_ips = IPResource.objects.filter(category=IPResource.CATEGORY_PUBLIC).values_list('pk', flat=True)
  27. for prefix in IPPrefix.objects.filter(ipresource__in=public_ips).distinct():
  28. print("\n=== %s ===\n" % prefix)
  29. active_ips = prefix.ipresource_set.filter(in_use=True)
  30. if not active_ips.exists():
  31. print("empty")
  32. continue
  33. elif active_ips.count() < 20:
  34. ip_set = active_ips
  35. else:
  36. ip_set = prefix.ipresource_set
  37. for ip in ip_set.all():
  38. line = str(ip)
  39. if ip.reserved:
  40. line += ' - réservé'
  41. else:
  42. try:
  43. allocation = ip.allocations.get(active=True)
  44. except ServiceAllocation.DoesNotExist:
  45. line += ' - disponible'
  46. allocation = ip.allocations.order_by('-end').first()
  47. if allocation:
  48. service = allocation.service
  49. end = allocation.end.strftime('%Y%m%d')
  50. adhesion = service.adhesion
  51. adherent = adhesion.adherent
  52. line += ' depuis %s (dernière utilisation : %s %s %s)' % (end, adhesion, adherent, service)
  53. else:
  54. service = allocation.service
  55. start = allocation.start.strftime('%Y%m%d')
  56. adhesion = service.adhesion
  57. adherent = adhesion.adherent
  58. line += ' - %s %s %s (depuis %s)' % (adhesion, adherent, service, start)
  59. print(line)