checkvm.py 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from django.core.management.base import BaseCommand, CommandError
  2. from django.contrib.contenttypes.models import ContentType
  3. import re, sys, os, json
  4. from adhesions.models import Adhesion
  5. from services.models import Service, ServiceType, IPResource, Route
  6. from accounts.models import Profile
  7. regex = re.compile('^\| (?P<uuid>[0-9a-f-]*) \| (?P<nom>[a-z0-9.-]+)[ ]+\| vlan-routed=(?P<ip>.*)[ ]+\| (?P<created>[0-9TZ:-]+) |$')
  8. ipv4 = re.compile('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')
  9. # nova list --all-t --fields name,networks,created
  10. class Command(BaseCommand):
  11. help = 'Comparaison des VM dans Djadhere avec OpenStack'
  12. def add_arguments(self, parser):
  13. parser.add_argument('file', help='Fichier OpenStack')
  14. parser.add_argument('--absent-ok', help='Ne pas afficher de message d’erreur si la VM est absente d’OpenStack', action='append', default=[])
  15. def handle(self, *args, **options):
  16. vm_st = ServiceType.objects.get(name='VM')
  17. djadhere_list = Service.objects.filter(service_type=vm_st)
  18. try:
  19. f = open(options['file'])
  20. except FileNotFoundError:
  21. raise CommandError('Le fichier "%s" n’a pas été trouvé.' % options['file'])
  22. else:
  23. with f:
  24. openstack_list = json.load(f)
  25. openstack_list = { vm['name']: vm for vm in openstack_list }
  26. self.compare(djadhere_list, openstack_list, absent_ok=options['absent_ok'])
  27. def compare(self, djadhere_list, openstack_list, absent_ok):
  28. for vm in djadhere_list.filter(label__exact=''):
  29. if not vm.is_active():
  30. continue
  31. self.stdout.write(self.style.WARNING("> La VM n°%d n’a pas de label.\n"
  32. "Celui-ci est obligatoire pour les VM afin de les retrouver dans OpenStack.\n"
  33. "Merci d’éditer le service pour lui rajouter un label." % vm.id))
  34. djadhere_list = djadhere_list.exclude(label__exact='')
  35. djadhere_name_set = set(djadhere_list.values_list('label', flat=True))
  36. openstack_name_set = set(openstack_list.keys())
  37. for vm in djadhere_name_set & openstack_name_set: # In both
  38. djadhere = djadhere_list.get(label=vm)
  39. if not djadhere.is_active():
  40. self.stdout.write(self.style.WARNING("> La VM '%s' est marqué inactive dans Djadhere or elle est présente dans OpenStack.\n"
  41. "Soit c’est une erreur et elle doit être marqué active, soit elle doit être supprimé d’OpenStack suite à une demande de son propriétaire." % vm))
  42. openstack_ip_set = set(map(lambda vlan_routed: vlan_routed['addr'], openstack_list[vm]['addresses']['vlan-routed']))
  43. djadhere_ip_set = set(djadhere.allocations.filter(active=True).values_list('resource__ip', flat=True))
  44. for ip in openstack_ip_set & djadhere_ip_set: # In both
  45. alloc = djadhere.allocations.get(resource__ip=ip)
  46. if alloc.route.name != 'openstack':
  47. self.stdout.write(self.style.WARNING("> L’IP '%s' allouée à la VM '%s' a pour route '%s' dans Djadhere ; merci de modifier la route pour 'openstack'." % (ip, vm, alloc.route)))
  48. for ip in djadhere_ip_set - openstack_ip_set: # Only in djadhere
  49. self.stdout.write(self.style.WARNING("> L’IP '%s' est allouée à la VM '%s' dans Djadhere mais pas dans OpenStack.\n"
  50. "L’IP est soit à dés-allouer dans Djadhere, soit à rajouter à la VM dans OpenStack." % (ip, vm)))
  51. for ip in openstack_ip_set - djadhere_ip_set: # Only in OpenStack
  52. if not ip.startswith('fe80::'):
  53. self.stdout.write(self.style.WARNING("> L’IP '%s' est utilisée par la VM '%s' d’après OpenStack.\n"
  54. "Il faut renseigner cette allocation dans Djadhere ou dés-allouer cette IP dans OpenStack." % (ip, vm)))
  55. for vm in djadhere_name_set - openstack_name_set: # Only in djadhere
  56. djadhere = djadhere_list.get(label=vm)
  57. if djadhere.is_active() and vm not in absent_ok:
  58. self.stdout.write(self.style.WARNING("> La VM '%s' présente dans Djadhere est absente d’OpenStack.\n"
  59. "Soit il s’agit d’une VM à créer dans OpenStack, soit elle a été supprimée d’OpenStack et l’IP doit être déallouée dans Djadhere." % vm))
  60. for vm in openstack_name_set - djadhere_name_set: # Only in openstack
  61. self.stdout.write(self.style.WARNING("> La VM '%s' présente sur OpenStack n’a pas été trouvée dans Djadhere." % vm))
  62. djadhere = djadhere_list.filter(label__icontains=vm[:vm.find('.')])
  63. if djadhere.exists():
  64. self.stdout.write(self.style.WARNING("Elle a peut-être été mal nommée, est-ce une des VM suivantes ?"))
  65. for d in djadhere.all():
  66. self.stdout.write(self.style.WARNING("\t[%d] %s" % (d.pk, d.label)))