root / snf-cyclades-app / synnefo / logic / management / commands / reconcile-pools.py @ d7841399
History | View | Annotate | Download (6.7 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
#
|
30 |
from django.core.management.base import BaseCommand |
31 |
|
32 |
from synnefo.db.models import (Network, BackendNetwork, |
33 |
BridgePoolTable, MacPrefixPoolTable) |
34 |
from synnefo.db.pools import EmptyPool |
35 |
from synnefo.settings import MAC_POOL_BASE |
36 |
|
37 |
|
38 |
class Command(BaseCommand): |
39 |
help = 'Check consistency of unique resources.'
|
40 |
|
41 |
def handle(self, **options): |
42 |
self.detect_bridges()
|
43 |
self.detect_mac_prefixes()
|
44 |
self.detect_unique_mac_prefixes()
|
45 |
|
46 |
def detect_bridges(self): |
47 |
write = self.stdout.write
|
48 |
|
49 |
write("---------------------------------------\n")
|
50 |
write("Checking consistency of the Bridge Pool\n")
|
51 |
write("---------------------------------------\n")
|
52 |
|
53 |
try:
|
54 |
bridge_pool = BridgePoolTable.get_pool() |
55 |
except EmptyPool:
|
56 |
write("No Bridge Pool\n")
|
57 |
return
|
58 |
bridges = [] |
59 |
for i in xrange(0, bridge_pool.size()): |
60 |
if not bridge_pool.is_available(i, index=True) and \ |
61 |
not bridge_pool.is_reserved(i, index=True): |
62 |
bridges.append(bridge_pool.index_to_value(i)) |
63 |
|
64 |
write("Used bridges from Pool: %d\n" % len(bridges)) |
65 |
|
66 |
network_bridges = Network.objects.filter(type='PRIVATE_PHYSICAL_VLAN',
|
67 |
deleted=False)\
|
68 |
.values_list('link', flat=True) |
69 |
|
70 |
write("Used bridges from Networks: %d\n" %
|
71 |
len(network_bridges))
|
72 |
|
73 |
set_network_bridges = set(network_bridges)
|
74 |
if len(network_bridges) > len(set_network_bridges): |
75 |
write("Found duplicated bridges:\n")
|
76 |
duplicates = list(network_bridges)
|
77 |
for bridge in set_network_bridges: |
78 |
duplicates.remove(bridge) |
79 |
for bridge in set(duplicates): |
80 |
write("Duplicated bridge: %s. " % bridge)
|
81 |
write("Used by the following Networks:\n")
|
82 |
nets = Network.objects.filter(deleted=False, link=bridge)
|
83 |
write(" " + "\n ".join([str(net.id) for net in nets])\ |
84 |
+ "\n")
|
85 |
|
86 |
def detect_mac_prefixes(self): |
87 |
write = self.stdout.write
|
88 |
|
89 |
write("---------------------------------------\n")
|
90 |
write("Checking consistency of the MAC Prefix Pool\n")
|
91 |
write("---------------------------------------\n")
|
92 |
|
93 |
try:
|
94 |
macp_pool = MacPrefixPoolTable.get_pool() |
95 |
except EmptyPool:
|
96 |
write("No mac-prefix pool\n")
|
97 |
return
|
98 |
|
99 |
macs = [] |
100 |
for i in xrange(0, macp_pool.size()): |
101 |
if not macp_pool.is_available(i, index=True) and \ |
102 |
not macp_pool.is_reserved(i, index=True): |
103 |
value = macp_pool.index_to_value(i) |
104 |
if value != MAC_POOL_BASE:
|
105 |
macs.append(macp_pool.index_to_value(i)) |
106 |
|
107 |
write("Used MAC prefixes from Pool: %d\n" % len(macs)) |
108 |
|
109 |
network_mac_prefixes = \ |
110 |
Network.objects.filter(deleted=False)\
|
111 |
.exclude(mac_prefix=MAC_POOL_BASE) \ |
112 |
.values_list('mac_prefix', flat=True) |
113 |
write("Used MAC prefixes from Networks: %d\n" %
|
114 |
len(network_mac_prefixes))
|
115 |
|
116 |
set_network_mac_prefixes = set(network_mac_prefixes)
|
117 |
if len(network_mac_prefixes) > len(set_network_mac_prefixes): |
118 |
write("Found duplicated mac_prefixes:\n")
|
119 |
duplicates = list(network_mac_prefixes)
|
120 |
for mac_prefix in set_network_mac_prefixes: |
121 |
duplicates.remove(mac_prefix) |
122 |
for mac_prefix in set(duplicates): |
123 |
write("Duplicated mac_prefix: %s. " % mac_prefix)
|
124 |
write("Used by the following Networks:\n")
|
125 |
nets = Network.objects.filter(deleted=False,
|
126 |
mac_prefix=mac_prefix) |
127 |
write(" " + "\n ".join([str(net.id) for net in nets])\ |
128 |
+ "\n")
|
129 |
|
130 |
def detect_unique_mac_prefixes(self): |
131 |
write = self.stdout.write
|
132 |
|
133 |
write("---------------------------------------\n")
|
134 |
write("Checking uniqueness of BackendNetwork prefixes.\n")
|
135 |
write("---------------------------------------\n")
|
136 |
|
137 |
mac_prefixes = BackendNetwork.objects.filter(deleted=False)\
|
138 |
.values_list('mac_prefix', flat=True) |
139 |
mac_prefixes = filter(lambda x: not x.startswith(MAC_POOL_BASE), |
140 |
mac_prefixes) |
141 |
set_mac_prefixes = set(mac_prefixes)
|
142 |
if len(mac_prefixes) > len(set_mac_prefixes): |
143 |
write("Found duplicated mac_prefixes:\n")
|
144 |
duplicates = list(mac_prefixes)
|
145 |
for mac_prefix in set_mac_prefixes: |
146 |
duplicates.remove(mac_prefix) |
147 |
for mac_prefix in set(duplicates): |
148 |
write("Duplicated mac_prefix: %s. " % mac_prefix)
|
149 |
write("Used by the following BackendNetworks:\n")
|
150 |
nets = BackendNetwork.objects.filter(deleted=False,
|
151 |
mac_prefix=mac_prefix) |
152 |
write(" " + "\n ".join([str(net.id) for net in nets])\ |
153 |
+ "\n")
|