Changeset 6458
- Timestamp:
- Dec 15, 2008, 1:27:43 AM (16 years ago)
- Location:
- trunk/exodus
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/exodus/tests.py
r6457 r6458 47 47 ip_size = self.ipcalc(netmask)._ip_size(25) 48 48 self.failUnless(ip_size, 128) 49 50 def test_ipcalc_get_used_ips(self): 51 self.fail("Test not implemented") 52 53 def test_ipcalc_get_free_network_addr(self): 54 self.fail("Test not implemented") 49 55 50 56 def test_ipcalc_out_of_bounds(self): -
trunk/exodus/wlipcalc.py
r6457 r6458 13 13 raise ValueError, 'Netmask out of bounds.' 14 14 15 master_ip = node.masterip 15 self.node = node 16 self.master_ip = node.masterip 16 17 master_netmask = MASTERIP_NETMASK 17 master_network = wl.get_network(master_ip, master_netmask)18 self.master_network = wl.get_network(self.master_ip, master_netmask) 18 19 19 20 # calculate the number of ipaddresses in the subnetmask 20 master_ip_size = self._ip_size(master_netmask)21 new_ip_size = self._ip_size(free_netmask)21 self.master_ip_size = self._ip_size(master_netmask) 22 self.new_ip_size = self._ip_size(free_netmask) 22 23 23 # generate a list of all used_ipaddresses 24 # we take only the ip addresses of the interfaces that are 25 # linked to itself, because they are 'master' and the slaves 26 # on other nodes fall in their subnet. 27 used_list = [master_ip] 28 for i in node.interface_set.all(): 29 #XXX: rewrite to if i.ip in masterip?? 30 if i.id == i.link_id: 31 ip_size = self._ip_size(i.netmask) 32 network_addr = wl.network(i.ip, i.netmask) 33 for j in range(ip_size): 34 used_list.append(wl.show_addr(network_addr + j)) 24 used_list = self._get_used_ips() 25 network = self._get_free_network_addr(used_list) 35 26 36 for i in range(master_ip_size/new_ip_size): 37 network_addr = wl.parse_addr(master_network) + (new_ip_size * i) 38 k = 0 39 for j in range(new_ip_size): 40 new_ip = wl.show_addr(network_addr + j) 41 if new_ip not in used_list: 42 k += 1 43 if k == new_ip_size and \ 44 wl.show_addr(network_addr) != master_network: 45 self.network = wl.show_addr(network_addr) 46 break 47 48 if not self.network: 27 if not network: 49 28 raise Exception, "Not enough space for a /%d subnet" % \ 50 29 (free_netmask) 30 else: 31 self.network = wl.show_addr(network) 51 32 52 33 # variables for extenal calling … … 59 40 # broadcast addresses. 60 41 self.ips = [] 61 for i in range(1, new_ip_size-1):62 self.ips.append(wl.show_addr(network _addr+ i))42 for i in range(1, self.new_ip_size-1): 43 self.ips.append(wl.show_addr(network + i)) 63 44 def _ip_size(self, netmask): 64 45 return int(pow(2,32 - netmask)) 46 47 def _get_used_ips(self): 48 """ Generate a list of all used_ipaddresses. 49 50 we take only the ip addresses of the interfaces that are 51 linked to itself, because they are 'master' and the slaves 52 on other nodes fall in their subnet. 53 """ 54 used_list = [self.master_ip] 55 for i in self.node.interface_set.all(): 56 #XXX: rewrite to if i.ip in masterip?? 57 if i.id == i.link_id: 58 ip_size = self._ip_size(i.netmask) 59 network_addr = wl.network(i.ip, i.netmask) 60 for j in range(ip_size): 61 used_list.append(wl.show_addr(network_addr + j)) 62 return used_list 63 64 def _get_free_network_addr(self, used_list): 65 """Calculate a new network address with a used_list and given netmask. 66 """ 67 for i in range(self.master_ip_size/self.new_ip_size): 68 network_addr = wl.parse_addr(self.master_network) + \ 69 (self.new_ip_size * i) 70 k = 0 71 for j in range(self.new_ip_size): 72 new_ip = wl.show_addr(network_addr + j) 73 if new_ip not in used_list: 74 k += 1 75 if k == self.new_ip_size and \ 76 wl.show_addr(network_addr) != self.master_network: 77 return network_addr 78 return None 79
Note:
See TracChangeset
for help on using the changeset viewer.