Commit 06c91260 authored by Xavier Thompson's avatar Xavier Thompson

format: Support partition capabilities in computer definition file

See merge request nexedi/slapos.core!585
parents fa1a40dd 266ebf80
...@@ -144,6 +144,19 @@ class AddressGenerationError(Exception): ...@@ -144,6 +144,19 @@ class AddressGenerationError(Exception):
) )
def getNormalizedIfaddresses(iface, inet):
"""
Keep a single representation for netmasks, to avoid netmasks like
ffff:ffff:ffff:ffff:ffff:ffff::/96
"""
address_list = netifaces.ifaddresses(iface)[inet]
for q in address_list:
try:
q['netmask'] = q['netmask'].split('/')[0]
except KeyError:
pass
return address_list
def getPublicIPv4Address(): def getPublicIPv4Address():
test_list = [ test_list = [
{ "url": 'https://api.ipify.org/?format=json' , "json_key": "ip"}, { "url": 'https://api.ipify.org/?format=json' , "json_key": "ip"},
...@@ -194,7 +207,7 @@ def getIfaceAddressIPv4(iface): ...@@ -194,7 +207,7 @@ def getIfaceAddressIPv4(iface):
raise ValueError('Could not find interface called %s to use as gateway ' \ raise ValueError('Could not find interface called %s to use as gateway ' \
'for tap network' % iface) 'for tap network' % iface)
try: try:
addresses_list = netifaces.ifaddresses(iface)[socket.AF_INET] addresses_list = getNormalizedIfaddresses(iface, socket.AF_INET)
if len (addresses_list) > 0: if len (addresses_list) > 0:
addresses = addresses_list[0].copy() addresses = addresses_list[0].copy()
...@@ -269,7 +282,8 @@ class Computer(object): ...@@ -269,7 +282,8 @@ class Computer(object):
self.interface = interface self.interface = interface
self.partition_list = partition_list or [] self.partition_list = partition_list or []
self.address = addr self.address = addr
self.netmask = netmask # Normalize netmask due to netiface netmasks like ffff::/16
self.netmask = netmask and netmask.split('/')[0]
self.ipv6_interface = ipv6_interface self.ipv6_interface = ipv6_interface
self.partition_has_ipv6_range = partition_has_ipv6_range self.partition_has_ipv6_range = partition_has_ipv6_range
self.software_user = software_user self.software_user = software_user
...@@ -1056,14 +1070,16 @@ class Interface(object): ...@@ -1056,14 +1070,16 @@ class Interface(object):
Returns currently configured local IPv4 addresses which are in Returns currently configured local IPv4 addresses which are in
ipv4_local_network ipv4_local_network
""" """
if not socket.AF_INET in netifaces.ifaddresses(self.name): try:
address_list = getNormalizedIfaddresses(self.name, socket.AF_INET)
except KeyError: # No entry for socket.AF_INET
return [] return []
return [ return [
{ {
'addr': q['addr'], 'addr': q['addr'],
'netmask': q['netmask'] 'netmask': q['netmask']
} }
for q in netifaces.ifaddresses(self.name)[socket.AF_INET] for q in address_list
if netaddr.IPAddress(q['addr'], 4) in netaddr.glob_to_iprange( if netaddr.IPAddress(q['addr'], 4) in netaddr.glob_to_iprange(
netaddr.cidr_to_glob(self.ipv4_local_network)) netaddr.cidr_to_glob(self.ipv4_local_network))
] ]
...@@ -1074,7 +1090,7 @@ class Interface(object): ...@@ -1074,7 +1090,7 @@ class Interface(object):
try: try:
address_list = [ address_list = [
q q
for q in netifaces.ifaddresses(interface_name)[socket.AF_INET6] for q in getNormalizedIfaddresses(interface_name, socket.AF_INET6)
if isGlobalScopeAddress(q['addr'].split('%')[0]) if isGlobalScopeAddress(q['addr'].split('%')[0])
] ]
except KeyError: except KeyError:
...@@ -1087,7 +1103,7 @@ class Interface(object): ...@@ -1087,7 +1103,7 @@ class Interface(object):
try: try:
address_list += [ address_list += [
q q
for q in netifaces.ifaddresses(tap.name)[socket.AF_INET6] for q in getNormalizedIfaddresses(tap.name, socket.AF_INET6)
if isGlobalScopeAddress(q['addr'].split('%')[0]) if isGlobalScopeAddress(q['addr'].split('%')[0])
] ]
except KeyError: except KeyError:
...@@ -1486,6 +1502,15 @@ def parse_computer_definition(conf, definition_path): ...@@ -1486,6 +1502,15 @@ def parse_computer_definition(conf, definition_path):
address_list=address_list, address_list=address_list,
ipv6_range=ipv6_range, ipv6_range=ipv6_range,
tap=tap, tun=tun) tap=tap, tun=tun)
if computer_definition.has_option(section, 'capability_list'):
# Attribute .capability_list exists only when capabilities are defined
capability_string = computer_definition.get(section, 'capability_list')
capability_list = []
for c in capability_string.splitlines():
c = c.strip()
if c:
capability_list.append(c)
partition.capability_list = capability_list
partition_list.append(partition) partition_list.append(partition)
computer.partition_list = partition_list computer.partition_list = partition_list
return computer return computer
...@@ -1604,7 +1629,8 @@ def do_format(conf): ...@@ -1604,7 +1629,8 @@ def do_format(conf):
conf.logger.info('Updating computer') conf.logger.info('Updating computer')
address = computer.getAddress() address = computer.getAddress()
computer.address = address['addr'] computer.address = address['addr']
computer.netmask = address['netmask'] # Normalize netmask due to netiface netmasks like ffff::/16
computer.netmask = address['netmask'] and address['netmask'].split('/')[0]
if conf.output_definition_file: if conf.output_definition_file:
write_computer_definition(conf, computer) write_computer_definition(conf, computer)
......
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