Commit 6272d68e authored by Xavier Thompson's avatar Xavier Thompson

Slapformat: IPv6 range for partitions and tun

See merge request nexedi/slapos.core!455
parents 7e2f8eec c4e86b91
Pipeline #27042 failed with stage
in 0 seconds
...@@ -125,7 +125,7 @@ class FormatCommand(ConfigCommand): ...@@ -125,7 +125,7 @@ class FormatCommand(ConfigCommand):
try: try:
conf.setConfig() conf.setConfig()
except UsageError as err: except UsageError as err:
sys.stderr.write(err.message + '\n') sys.stderr.write(str(err) + '\n')
sys.stderr.write("For help use --help\n") sys.stderr.write("For help use --help\n")
sys.exit(1) sys.exit(1)
......
This diff is collapsed.
...@@ -301,7 +301,7 @@ class SlapformatMixin(unittest.TestCase): ...@@ -301,7 +301,7 @@ class SlapformatMixin(unittest.TestCase):
raise ValueError("{} already has logger attached".format(self.__class__.__name__)) raise ValueError("{} already has logger attached".format(self.__class__.__name__))
self.logger = logger self.logger = logger
self.partition = slapos.format.Partition('partition', '/part_path', self.partition = slapos.format.Partition('partition', '/part_path',
slapos.format.User('testuser'), [], None) slapos.format.User('testuser'), [], None, None)
global USER_LIST global USER_LIST
USER_LIST = [] USER_LIST = []
global GROUP_LIST global GROUP_LIST
...@@ -423,7 +423,8 @@ class TestComputer(SlapformatMixin): ...@@ -423,7 +423,8 @@ class TestComputer(SlapformatMixin):
logger=self.logger, name='myinterface', 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'), [],
ipv6_range=None, tap=slapos.format.Tap('tap')),
]) ])
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['myinterface'] = { INTERFACE_DICT['myinterface'] = {
...@@ -466,7 +467,8 @@ class TestComputer(SlapformatMixin): ...@@ -466,7 +467,8 @@ class TestComputer(SlapformatMixin):
logger=self.logger, name='myinterface', 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'), [],
ipv6_range=None, tap=slapos.format.Tap('tap')),
]) ])
global USER_LIST global USER_LIST
USER_LIST = ['testuser'] USER_LIST = ['testuser']
...@@ -517,7 +519,7 @@ class TestComputer(SlapformatMixin): ...@@ -517,7 +519,7 @@ class TestComputer(SlapformatMixin):
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')), ipv6_range=None, tap=slapos.format.Tap('tap')),
]) ])
global USER_LIST global USER_LIST
USER_LIST = ['testuser'] USER_LIST = ['testuser']
...@@ -577,7 +579,7 @@ class TestComputer(SlapformatMixin): ...@@ -577,7 +579,7 @@ class TestComputer(SlapformatMixin):
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')), ipv6_range=None, tap=slapos.format.Tap('tap')),
]) ])
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['myinterface'] = { INTERFACE_DICT['myinterface'] = {
...@@ -618,7 +620,7 @@ class TestComputer(SlapformatMixin): ...@@ -618,7 +620,7 @@ class TestComputer(SlapformatMixin):
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')), ipv6_range=None, tap=slapos.format.Tap('tap')),
]) ])
global INTERFACE_DICT global INTERFACE_DICT
INTERFACE_DICT['myinterface'] = { INTERFACE_DICT['myinterface'] = {
...@@ -636,12 +638,7 @@ class TestComputer(SlapformatMixin): ...@@ -636,12 +638,7 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)" "chmod('/instance_root/partition', 488)"
], ],
self.test_result.bucket) self.test_result.bucket)
self.assertEqual([ self.assertEqual([],
'ip addr add ip/255.255.255.255 dev myinterface',
# 'ip addr list myinterface',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface',
'ip -6 addr list myinterface',
],
self.fakeCallAndRead.external_command_list) self.fakeCallAndRead.external_command_list)
...@@ -660,7 +657,8 @@ class TestFormatDump(SlapformatMixin): ...@@ -660,7 +657,8 @@ class TestFormatDump(SlapformatMixin):
logger=self.logger, name='myinterface', 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'), [],
ipv6_range=None, tap=slapos.format.Tap('tap')),
]) ])
global USER_LIST global USER_LIST
USER_LIST = ['testuser'] USER_LIST = ['testuser']
...@@ -745,7 +743,8 @@ class TestComputerWithCPUSet(SlapformatMixin): ...@@ -745,7 +743,8 @@ class TestComputerWithCPUSet(SlapformatMixin):
logger=self.logger, name='lo', 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'), [],
ipv6_range=None, tap=None),
], ],
config={ config={
"manager_list": "cpuset", "manager_list": "cpuset",
......
...@@ -205,6 +205,59 @@ def ipv6FromBin(ip, suffix=''): ...@@ -205,6 +205,59 @@ def ipv6FromBin(ip, suffix=''):
return socket.inet_ntop(socket.AF_INET6, return socket.inet_ntop(socket.AF_INET6,
struct.pack('>QQ', int(ip[:64], 2), int(ip[64:], 2))) struct.pack('>QQ', int(ip[:64], 2), int(ip[64:], 2)))
def getPartitionIpv6Addr(ipv6_range, partition_index):
"""
from a IPv6 range in the form
{
'addr' : addr,
'prefixlen' : CIDR
}
returns the IPv6 addr
addr::(partition_index+2) (address 1 is is used by re6st)
"""
addr = ipv6_range['addr']
prefixlen = ipv6_range['prefixlen']
prefix = binFromIpv6(addr)[:prefixlen]
return dict(addr=ipv6FromBin(prefix + bin(partition_index+2)[2:].zfill(128 - prefixlen)), prefixlen=prefixlen)
def getIpv6RangeFactory(k, s):
def getIpv6Range(ipv6_range, partition_index):
"""
from a IPv6 range in the form
{
'addr' : addr,
'prefixlen' : CIDR
}
returns the IPv6 range
{
'addr' : addr:(k*(2^14) + partition_index+1)
'prefixlen' : CIDR+16
}
"""
addr = ipv6_range['addr']
prefixlen = ipv6_range['prefixlen']
prefix = binFromIpv6(addr)[:prefixlen]
# we generate a subnetwork for the partition
# the subnetwork has 16 bits more than our IPv6 range
# make sure we have at least 2 IPs in the subnetwork
prefixlen += 16
if prefixlen >= 128:
raise ValueError('The IPv6 range has prefixlen {} which is too big for generating IPv6 range for partitions.'.format(prefixlen))
return dict(addr=ipv6FromBin(prefix + bin((k << 14) + partition_index+1)[2:].zfill(16) + s * (128 - prefixlen)), prefixlen=prefixlen)
return getIpv6Range
getPartitionIpv6Range = getIpv6RangeFactory(1, '0')
getTapIpv6Range = getIpv6RangeFactory(2, '1')
getTunIpv6Range = getIpv6RangeFactory(3, '0')
def getIpv6RangeFirstAddr(addr, prefixlen):
addr_1 = "%s1" % ipv6FromBin(binFromIpv6(addr)[:prefixlen])
return ipv6FromBin(binFromIpv6(addr_1)) # correctly format the IPv6
def lenNetmaskIpv6(netmask): def lenNetmaskIpv6(netmask):
"""Convert string represented netmask to its integer prefix""" """Convert string represented netmask to its integer prefix"""
# Since version 0.10.7 of netifaces, the netmask is something like "ffff::/16", # Since version 0.10.7 of netifaces, the netmask is something like "ffff::/16",
...@@ -215,6 +268,10 @@ def lenNetmaskIpv6(netmask): ...@@ -215,6 +268,10 @@ def lenNetmaskIpv6(netmask):
except ValueError: except ValueError:
return netaddr.IPNetwork(netmask).prefixlen return netaddr.IPNetwork(netmask).prefixlen
def netmaskFromLenIPv6(netmask_len):
""" opposite of lenNetmaskIpv6"""
return ipv6FromBin('1' * netmask_len)
# Used for Python 2-3 compatibility # Used for Python 2-3 compatibility
if str is bytes: if str is bytes:
bytes2str = str2bytes = lambda s: s bytes2str = str2bytes = lambda s: s
......
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