Commit 086504fa authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼 Committed by Rafael Monnerat

CLEANUP: reformat a bit the code (no functional change)

parent 1368d75d
...@@ -522,16 +522,6 @@ class Computer(object): ...@@ -522,16 +522,6 @@ class Computer(object):
- add groups and users - add groups and users
- construct partitions inside slapgrid - construct partitions inside slapgrid
""" """
if alter_network and self.address is not None:
self.interface.addAddr(self.address, self.netmask)
if use_unique_local_address_block and alter_network:
if self.ipv6_interface:
network_interface_name = self.ipv6_interface
else:
network_interface_name = self.interface.name
self._addUniqueLocalAddressIpv6(network_interface_name)
for path in self.instance_root, self.software_root: for path in self.instance_root, self.software_root:
if not os.path.exists(path): if not os.path.exists(path):
os.makedirs(path, 0o755) os.makedirs(path, 0o755)
...@@ -562,8 +552,21 @@ class Computer(object): ...@@ -562,8 +552,21 @@ class Computer(object):
if the_digit.isdigit(): if the_digit.isdigit():
instance_external_list.append(data_path) instance_external_list.append(data_path)
tap_address_list = [] ####################
if alter_network and create_tap: ### Network part ###
####################
if alter_network:
if self.address is not None:
self.interface.addIPv6Address(self.address, self.netmask)
if use_unique_local_address_block:
if self.ipv6_interface:
network_interface_name = self.ipv6_interface
else:
network_interface_name = self.interface.name
self._addUniqueLocalAddressIpv6(network_interface_name)
if create_tap:
if self.tap_gateway_interface: if self.tap_gateway_interface:
gateway_addr_dict = getIfaceAddressIPv4(self.tap_gateway_interface) gateway_addr_dict = getIfaceAddressIPv4(self.tap_gateway_interface)
tap_address_list = getIPv4SubnetAddressRange(gateway_addr_dict['addr'], tap_address_list = getIPv4SubnetAddressRange(gateway_addr_dict['addr'],
...@@ -573,12 +576,10 @@ class Computer(object): ...@@ -573,12 +576,10 @@ class Computer(object):
else: else:
gateway_addr_dict = {'peer': '10.0.0.1', 'netmask': '255.255.0.0', gateway_addr_dict = {'peer': '10.0.0.1', 'netmask': '255.255.0.0',
'addr': '10.0.0.1', 'network': '10.0.0.0'} 'addr': '10.0.0.1', 'network': '10.0.0.0'}
tap_address_list = getIPv4SubnetAddressRange(gateway_addr_dict['addr'], tap_address_list = getIPv4SubnetAddressRange(gateway_addr_dict['addr'],
gateway_addr_dict['netmask'], gateway_addr_dict['netmask'],
len(self.partition_list)) len(self.partition_list))
if alter_network:
self._speedHackAddAllOldIpsToInterface() self._speedHackAddAllOldIpsToInterface()
try: try:
...@@ -592,26 +593,27 @@ class Computer(object): ...@@ -592,26 +593,27 @@ class Computer(object):
if alter_user: if alter_user:
partition.user.create() partition.user.create()
# Reconstructing Tap # Reconstructing Tap and Tun
if alter_network:
if partition.user and partition.user.isAvailable(): if partition.user and partition.user.isAvailable():
owner = partition.user owner = partition.user
else: else:
owner = User('root') owner = User('root')
if alter_network and create_tap: if create_tap:
partition.tap.createWithOwner(owner) partition.tap.createWithOwner(owner)
# add addresses and create route for this tap # add addresses and create route for this tap
next_ipv4_addr = '%s' % tap_address_list.pop(0)
if not partition.tap.ipv4_addr: if not partition.tap.ipv4_addr:
# define new ipv4 address for this tap # define new ipv4 address for this tap
next_ipv4_addr = '%s' % tap_address_list.pop(0)
partition.tap.ipv4_addr = next_ipv4_addr partition.tap.ipv4_addr = next_ipv4_addr
partition.tap.ipv4_netmask = gateway_addr_dict['netmask'] partition.tap.ipv4_netmask = gateway_addr_dict['netmask']
partition.tap.ipv4_gateway = gateway_addr_dict['addr'] partition.tap.ipv4_gateway = gateway_addr_dict['addr']
partition.tap.ipv4_network = gateway_addr_dict['network'] partition.tap.ipv4_network = gateway_addr_dict['network']
if not partition.tap.ipv6_addr: if not partition.tap.ipv6_addr:
ipv6_addr = self.interface.addAddr(tap=partition.tap) ipv6_addr = self.interface.addIPv6Address(tap=partition.tap)
partition.tap.ipv6_addr = "" partition.tap.ipv6_addr = ""
partition.tap.ipv6_netmask = "" partition.tap.ipv6_netmask = ""
partition.tap.ipv6_gateway = "" partition.tap.ipv6_gateway = ""
...@@ -619,7 +621,7 @@ class Computer(object): ...@@ -619,7 +621,7 @@ class Computer(object):
partition.tap.createRoutes() partition.tap.createRoutes()
if alter_network and partition.tun is not None: if partition.tun is not None:
# create TUN interface per partition as well # create TUN interface per partition as well
partition.tun.createWithOwner(owner) partition.tun.createWithOwner(owner)
partition.tun.createRoutes() partition.tun.createRoutes()
...@@ -630,12 +632,12 @@ class Computer(object): ...@@ -630,12 +632,12 @@ class Computer(object):
# Reconstructing partition's address # Reconstructing partition's address
# There should be two addresses on each Computer Partition: # There should be two addresses on each Computer Partition:
# * global IPv6
# * local IPv4, took from slapformat:ipv4_local_network # * local IPv4, took from slapformat:ipv4_local_network
# * global IPv6
if not partition.address_list: if not partition.address_list:
# regenerate # regenerate
partition.address_list.append(self.interface.addIPv4LocalAddress()) partition.address_list.append(self.interface.addIPv4LocalAddress())
partition.address_list.append(self.interface.addAddr()) partition.address_list.append(self.interface.addIPv6Address())
elif alter_network: elif alter_network:
# regenerate list of addresses # regenerate list of addresses
old_partition_address_list = partition.address_list old_partition_address_list = partition.address_list
...@@ -653,7 +655,7 @@ class Computer(object): ...@@ -653,7 +655,7 @@ class Computer(object):
for address in old_partition_address_list: for address in old_partition_address_list:
if netaddr.valid_ipv6(address['addr']): if netaddr.valid_ipv6(address['addr']):
partition.address_list.append(self.interface.addAddr( partition.address_list.append(self.interface.addIPv6Address(
address['addr'], address['addr'],
address['netmask'])) address['netmask']))
elif netaddr.valid_ipv4(address['addr']): elif netaddr.valid_ipv4(address['addr']):
...@@ -1097,14 +1099,14 @@ class Interface(object): ...@@ -1097,14 +1099,14 @@ class Interface(object):
# confirmed to be configured # confirmed to be configured
return dict(addr=addr, netmask=netmask) return dict(addr=addr, netmask=netmask)
def addAddr(self, addr=None, netmask=None, tap=None): def addIPv6Address(self, addr=None, netmask=None, tap=None):
""" """
Adds IP address to interface. Adds IPv6 address to interface.
If addr is specified and exists already on interface does nothing. If addr is specified and exists already on interface, do nothing.
If addr is specified and does not exists on interface, tries to add given If addr is specified and does not exist on interface, try to add given
address. If it is not possible (ex. because network changed) calculates new address. If it is not possible (ex. because network changed), calculate new
address. address.
Args: Args:
......
...@@ -317,7 +317,7 @@ class TestComputer(SlapformatMixin): ...@@ -317,7 +317,7 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='lo', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
computer.format() computer.format()
self.assertEqual([ self.assertEqual([
...@@ -327,7 +327,7 @@ class TestComputer(SlapformatMixin): ...@@ -327,7 +327,7 @@ class TestComputer(SlapformatMixin):
"chmod('/software_root', 493)"], "chmod('/software_root', 493)"],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqual([
'ip addr list bridge', 'ip addr list lo',
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r' 'useradd -d /software_root -g slapsoft slapsoft -r'
], ],
...@@ -338,7 +338,7 @@ class TestComputer(SlapformatMixin): ...@@ -338,7 +338,7 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='lo', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
computer.format(alter_user=False) computer.format(alter_user=False)
self.assertEqual([ self.assertEqual([
...@@ -355,7 +355,7 @@ class TestComputer(SlapformatMixin): ...@@ -355,7 +355,7 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='lo', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
self.assertEqual([ self.assertEqual([
"makedirs('/instance_root', 493)", "makedirs('/instance_root', 493)",
...@@ -364,7 +364,7 @@ class TestComputer(SlapformatMixin): ...@@ -364,7 +364,7 @@ class TestComputer(SlapformatMixin):
"chmod('/software_root', 493)"], "chmod('/software_root', 493)"],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqual([
'ip addr list bridge', 'ip addr list lo',
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r' 'useradd -d /software_root -g slapsoft slapsoft -r'
], ],
...@@ -375,7 +375,7 @@ class TestComputer(SlapformatMixin): ...@@ -375,7 +375,7 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='lo', ipv4_local_network='127.0.0.1/16'),
partition_list=[]) partition_list=[])
computer.format(alter_network=False, alter_user=False) computer.format(alter_network=False, alter_user=False)
self.assertEqual([ self.assertEqual([
...@@ -392,13 +392,13 @@ class TestComputer(SlapformatMixin): ...@@ -392,13 +392,13 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='myinterface', ipv4_local_network='127.0.0.1/16'),
partition_list=[ partition_list=[
slapos.format.Partition( slapos.format.Partition(
'partition', '/part_path', slapos.format.User('testuser'), [], tap=slapos.format.Tap('tap')), 'partition', '/part_path', slapos.format.User('testuser'), [], tap=slapos.format.Tap('tap')),
]) ])
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['bridge'] = { INTERFACE_DICT['myinterface'] = {
socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1', socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1',
'netmask': '255.255.255.0'}], 'netmask': '255.255.255.0'}],
socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}] socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}]
...@@ -415,17 +415,17 @@ class TestComputer(SlapformatMixin): ...@@ -415,17 +415,17 @@ class TestComputer(SlapformatMixin):
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqual([
'ip addr list bridge', 'ip addr list myinterface',
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r', 'useradd -d /software_root -g slapsoft slapsoft -r',
'groupadd testuser', 'groupadd testuser',
'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r', 'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r',
'ip tuntap add dev tap mode tap user testuser', 'ip tuntap add dev tap mode tap user testuser',
'ip link set tap up', 'ip link set tap up',
'ip addr add ip/255.255.255.255 dev bridge', 'ip addr add ip/255.255.255.255 dev myinterface',
'ip addr list bridge', 'ip addr list myinterface',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface',
'ip addr list bridge', 'ip addr list myinterface',
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -434,7 +434,7 @@ class TestComputer(SlapformatMixin): ...@@ -434,7 +434,7 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='myinterface', ipv4_local_network='127.0.0.1/16'),
partition_list=[ partition_list=[
slapos.format.Partition( slapos.format.Partition(
'partition', '/part_path', slapos.format.User('testuser'), [], tap=slapos.format.Tap('tap')), 'partition', '/part_path', slapos.format.User('testuser'), [], tap=slapos.format.Tap('tap')),
...@@ -442,7 +442,7 @@ class TestComputer(SlapformatMixin): ...@@ -442,7 +442,7 @@ class TestComputer(SlapformatMixin):
global USER_LIST global USER_LIST
USER_LIST = ['testuser'] USER_LIST = ['testuser']
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['bridge'] = { INTERFACE_DICT['myinterface'] = {
socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1', socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1',
'netmask': '255.255.255.0'}], 'netmask': '255.255.255.0'}],
socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}] socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}]
...@@ -468,10 +468,10 @@ class TestComputer(SlapformatMixin): ...@@ -468,10 +468,10 @@ class TestComputer(SlapformatMixin):
'ip -6 addr list tap', 'ip -6 addr list tap',
'ip route show 10.0.0.2', 'ip route show 10.0.0.2',
'ip route add 10.0.0.2 dev tap', 'ip route add 10.0.0.2 dev tap',
'ip addr add ip/255.255.255.255 dev bridge', 'ip addr add ip/255.255.255.255 dev myinterface',
# 'ip addr list bridge', # 'ip addr list myinterface',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface',
'ip -6 addr list bridge', 'ip -6 addr list myinterface',
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -537,14 +537,14 @@ class TestComputer(SlapformatMixin): ...@@ -537,14 +537,14 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='myinterface', ipv4_local_network='127.0.0.1/16'),
partition_list=[ partition_list=[
slapos.format.Partition( slapos.format.Partition(
'partition', '/part_path', slapos.format.User('testuser'), [], 'partition', '/part_path', slapos.format.User('testuser'), [],
tap=slapos.format.Tap('tap')), tap=slapos.format.Tap('tap')),
]) ])
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['bridge'] = { INTERFACE_DICT['myinterface'] = {
socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1', socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1',
'netmask': '255.255.255.0'}], 'netmask': '255.255.255.0'}],
socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}] socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}]
...@@ -561,15 +561,15 @@ class TestComputer(SlapformatMixin): ...@@ -561,15 +561,15 @@ class TestComputer(SlapformatMixin):
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqual([
# 'ip addr list bridge', # 'ip addr list myinterface',
'groupadd slapsoft', 'groupadd slapsoft',
'useradd -d /software_root -g slapsoft slapsoft -r', 'useradd -d /software_root -g slapsoft slapsoft -r',
'groupadd testuser', 'groupadd testuser',
'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r', 'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r',
# 'ip addr add ip/255.255.255.255 dev bridge', # 'ip addr add ip/255.255.255.255 dev myinterface',
# 'ip addr list bridge', # 'ip addr list myinterface',
# 'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge', # 'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface',
# 'ip addr list bridge', # 'ip addr list myinterface',
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -578,14 +578,14 @@ class TestComputer(SlapformatMixin): ...@@ -578,14 +578,14 @@ class TestComputer(SlapformatMixin):
instance_root='/instance_root', instance_root='/instance_root',
software_root='/software_root', software_root='/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='myinterface', ipv4_local_network='127.0.0.1/16'),
partition_list=[ partition_list=[
slapos.format.Partition( slapos.format.Partition(
'partition', '/part_path', slapos.format.User('testuser'), [], 'partition', '/part_path', slapos.format.User('testuser'), [],
tap=slapos.format.Tap('tap')), tap=slapos.format.Tap('tap')),
]) ])
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['bridge'] = { INTERFACE_DICT['myinterface'] = {
socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1', socket.AF_INET: [{'addr': '192.168.242.77', 'broadcast': '127.0.0.1',
'netmask': '255.255.255.0'}], 'netmask': '255.255.255.0'}],
socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}] socket.AF_INET6: [{'addr': '2a01:e35:2e27::e59c', 'netmask': 'ffff:ffff:ffff:ffff::'}]
...@@ -601,10 +601,10 @@ class TestComputer(SlapformatMixin): ...@@ -601,10 +601,10 @@ class TestComputer(SlapformatMixin):
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqual([
'ip addr add ip/255.255.255.255 dev bridge', 'ip addr add ip/255.255.255.255 dev myinterface',
# 'ip addr list bridge', # 'ip addr list myinterface',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev bridge', 'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface',
'ip -6 addr list bridge', 'ip -6 addr list myinterface',
], ],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -686,7 +686,7 @@ class TestComputerWithCPUSet(SlapformatMixin): ...@@ -686,7 +686,7 @@ class TestComputerWithCPUSet(SlapformatMixin):
global USER_LIST, INTERFACE_DICT global USER_LIST, INTERFACE_DICT
USER_LIST = ['testuser'] USER_LIST = ['testuser']
INTERFACE_DICT['bridge'] = { INTERFACE_DICT['lo'] = {
socket.AF_INET: [ socket.AF_INET: [
{'addr': '127.0.0.1', 'broadcast': '127.0.255.255', 'netmask': '255.255.0.0'}], {'addr': '127.0.0.1', 'broadcast': '127.0.255.255', 'netmask': '255.255.0.0'}],
socket.AF_INET6: [ socket.AF_INET6: [
...@@ -704,7 +704,7 @@ class TestComputerWithCPUSet(SlapformatMixin): ...@@ -704,7 +704,7 @@ class TestComputerWithCPUSet(SlapformatMixin):
instance_root='/tmp/slapgrid/instance_root', instance_root='/tmp/slapgrid/instance_root',
software_root='/tmp/slapgrid/software_root', software_root='/tmp/slapgrid/software_root',
interface=slapos.format.Interface( interface=slapos.format.Interface(
logger=self.logger, name='bridge', ipv4_local_network='127.0.0.1/16'), logger=self.logger, name='lo', ipv4_local_network='127.0.0.1/16'),
partition_list=[ partition_list=[
slapos.format.Partition( slapos.format.Partition(
'partition', '/tmp/slapgrid/instance_root/part1', slapos.format.User('testuser'), [], tap=None), 'partition', '/tmp/slapgrid/instance_root/part1', slapos.format.User('testuser'), [], tap=None),
......
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