Commit eb76d6cc authored by Alain Takoudjou's avatar Alain Takoudjou

Do not regenerate tap ip address it exists in slapos.xml

parent 8f6917f9
...@@ -176,7 +176,7 @@ def getIPv4SubnetAddressRange(ip_address, mask, size): ...@@ -176,7 +176,7 @@ def getIPv4SubnetAddressRange(ip_address, mask, size):
host ip address, then return list of ip address in the subnet""" host ip address, then return list of ip address in the subnet"""
ip = netaddr.IPNetwork('%s/%s' % (ip_address, mask)) ip = netaddr.IPNetwork('%s/%s' % (ip_address, mask))
# Delete network and default ip_address from the list # Delete network and default ip_address from the list
ip_list = [x for x in list(ip) ip_list = [x for x in sorted(list(ip))
if str(x) != ip_address and x.value != ip.cidr.network.value] if str(x) != ip_address and x.value != ip.cidr.network.value]
if len(ip_list) < size: if len(ip_list) < size:
raise ValueError('Could not create %s tap interfaces from address %s.' % ( raise ValueError('Could not create %s tap interfaces from address %s.' % (
...@@ -383,6 +383,10 @@ class Computer(object): ...@@ -383,6 +383,10 @@ class Computer(object):
if partition_dict['tap']: if partition_dict['tap']:
tap = Tap(partition_dict['tap']['name']) tap = Tap(partition_dict['tap']['name'])
tap.ipv4_addr = partition_dict['tap'].get('ipv4_addr', '')
tap.ipv4_netmask = partition_dict['tap'].get('ipv4_netmask', '')
tap.ipv4_gateway = partition_dict['tap'].get('ipv4_gateway', '')
tap.ipv4_network = partition_dict['tap'].get('ipv4_network', '')
else: else:
tap = Tap(partition_dict['reference']) tap = Tap(partition_dict['reference'])
...@@ -517,10 +521,13 @@ class Computer(object): ...@@ -517,10 +521,13 @@ class Computer(object):
self.interface.addTap(partition.tap) self.interface.addTap(partition.tap)
else: else:
next_ipv4_addr = '%s' % tap_address_list.pop(0) next_ipv4_addr = '%s' % tap_address_list.pop(0)
partition.tap.addRoutes(next_ipv4_addr, if not partition.tap.ipv4_addr:
gateway_addr_dict['netmask'], # define new ipv4 address for this tap
gateway_addr_dict['addr'], partition.tap.ipv4_addr = next_ipv4_addr
gateway_addr_dict['network']) partition.tap.ipv4_netmask = gateway_addr_dict['netmask']
partition.tap.ipv4_gateway = gateway_addr_dict['addr']
partition.tap.ipv4_network = gateway_addr_dict['network']
partition.tap.createRoutes()
# Reconstructing partition's directory # Reconstructing partition's directory
partition.createPath(alter_user) partition.createPath(alter_user)
...@@ -786,19 +793,19 @@ class Tap(object): ...@@ -786,19 +793,19 @@ class Tap(object):
if attach_to_tap: if attach_to_tap:
threading.Thread(target=self.attach).start() threading.Thread(target=self.attach).start()
def addRoutes(self, ipv4, netmask, gateway, network): def createRoutes(self):
""" """
Configure ipv4 route to reach this interface from local network Configure ipv4 route to reach this interface from local network
""" """
self.ipv4_addr = ipv4 if self.ipv4_addr:
self.ipv4_netmask = netmask # Check if this route exits
self.ipv4_gateway = gateway code, result = callAndRead(['ip', 'route', 'show', self.ipv4_addr])
self.ipv4_network = network if code == 0 and self.ipv4_addr in result and self.name in result:
# Check if this route exits return
code, result = callAndRead(['ip', 'route', 'show', ipv4]) callAndRead(['route', 'add', '-host', self.ipv4_addr, 'dev', self.name])
if code == 0 and ipv4 in result and self.name in result: else:
return raise ValueError("%s should not be empty. No ipv4 address assigned to %s" %
callAndRead(['route', 'add', '-host', ipv4, 'dev', self.name]) (self.ipv4_addr, self.name))
class Interface(object): class Interface(object):
"""Represent a network interface on the system""" """Represent a network interface on the system"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment