Refactor to use 'interface' name where 'bridge' was used

parent 4be336e6
...@@ -9,7 +9,7 @@ buildout = /path/to/buildout/binary ...@@ -9,7 +9,7 @@ buildout = /path/to/buildout/binary
computer_xml = /opt/slapos/slapos.xml computer_xml = /opt/slapos/slapos.xml
log_file = /opt/slapos/slapformat.log log_file = /opt/slapos/slapformat.log
partition_amount = 200 partition_amount = 200
bridge_name = br0 interface_name = br0
partition_base_name = slappart partition_base_name = slappart
user_base_name = slapuser user_base_name = slapuser
tap_base_name = slaptap tap_base_name = slaptap
......
...@@ -144,22 +144,22 @@ class Error(Exception): ...@@ -144,22 +144,22 @@ class Error(Exception):
def __str__(self): def __str__(self):
return self.message return self.message
class NoAddressOnBridge(Error): class NoAddressOnInterface(Error):
""" """
Exception raised if there's not address on the bridge to construct IPv6 Exception raised if there's not address on the interface to construct IPv6
address with. address with.
Attributes: Attributes:
brige: String, the name of the bridge. brige: String, the name of the interface.
""" """
def __init__(self, bridge): def __init__(self, interface):
self.message = 'No IPv6 found on bridge %s to construct IPv6 with.' % bridge self.message = 'No IPv6 found on interface %s to construct IPv6 with.' % interface
class AddressGenerationError(Error): class AddressGenerationError(Error):
""" """
Exception raised if the generation of an IPv6 based on the prefix obtained Exception raised if the generation of an IPv6 based on the prefix obtained
from the bridge failed. from the interface failed.
Attributes: Attributes:
addr: String, the invalid address the exception is raised for. addr: String, the invalid address the exception is raised for.
...@@ -170,32 +170,32 @@ class AddressGenerationError(Error): ...@@ -170,32 +170,32 @@ class AddressGenerationError(Error):
class Computer: class Computer:
"Object representing the computer" "Object representing the computer"
def __init__(self, reference, bridge=None, addr = None, netmask = None, def __init__(self, reference, interface=None, addr = None, netmask = None,
ipv6_interface=None): ipv6_interface=None):
""" """
Attributes: Attributes:
reference: String, the reference of the computer. reference: String, the reference of the computer.
bridge: String, if it has one, the name of the computer's bridge. interface: String, if it has one, the name of the interface to be used.
""" """
self.reference = str(reference) self.reference = str(reference)
self.bridge = bridge self.interface = interface
self.partition_list = [] self.partition_list = []
self.address = addr self.address = addr
self.netmask = netmask self.netmask = netmask
self.ipv6_interface = ipv6_interface self.ipv6_interface = ipv6_interface
def __getinitargs__(self): def __getinitargs__(self):
return (self.reference, self.bridge) return (self.reference, self.interface)
def getAddress(self): def getAddress(self):
""" """
Return a list of the bridge address not attributed to any partition, (which Return a list of the interface address not attributed to any partition, (which
are therefore free for the computer itself). are therefore free for the computer itself).
Returns: Returns:
False if the bridge isn't available, else the list of the free addresses. False if the interface isn't available, else the list of the free addresses.
""" """
if self.bridge is None: if self.interface is None:
return dict(addr=self.address, netmask=self.netmask) return dict(addr=self.address, netmask=self.netmask)
computer_partition_address_list = [] computer_partition_address_list = []
...@@ -203,8 +203,8 @@ class Computer: ...@@ -203,8 +203,8 @@ class Computer:
for address in partition.address_list: for address in partition.address_list:
if netaddr.valid_ipv6(address['addr']): if netaddr.valid_ipv6(address['addr']):
computer_partition_address_list.append(address['addr']) computer_partition_address_list.append(address['addr'])
# Going through addresses of the computer's bridge interface # Going through addresses of the interface
for address_dict in self.bridge.getGlobalScopeAddressList(): for address_dict in self.interface.getGlobalScopeAddressList():
# Comparing with computer's partition addresses # Comparing with computer's partition addresses
if address_dict['addr'] not in computer_partition_address_list: if address_dict['addr'] not in computer_partition_address_list:
return address_dict return address_dict
...@@ -212,8 +212,8 @@ class Computer: ...@@ -212,8 +212,8 @@ class Computer:
# all addresses on interface are for partition, so lets add new one # all addresses on interface are for partition, so lets add new one
computer_tap = Tap('compdummy') computer_tap = Tap('compdummy')
computer_tap.createWithOwner(User('root'), attach_to_tap=True) computer_tap.createWithOwner(User('root'), attach_to_tap=True)
self.bridge.addTap(computer_tap) self.interface.addTap(computer_tap)
return self.bridge.addAddr() return self.interface.addAddr()
def send(self, config): def send(self, config):
""" """
...@@ -304,7 +304,7 @@ class Computer: ...@@ -304,7 +304,7 @@ class Computer:
Construct the computer object as it is. Construct the computer object as it is.
""" """
if alter_network and self.address is not None: if alter_network and self.address is not None:
self.bridge.addAddr(self.address, self.netmask) self.interface.addAddr(self.address, self.netmask)
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):
...@@ -338,13 +338,13 @@ class Computer: ...@@ -338,13 +338,13 @@ class Computer:
if alter_network: if alter_network:
# In case it has to be attached to the TAP network device, only one # In case it has to be attached to the TAP network device, only one
# is necessary for the bridge to assert carrier # is necessary for the interface to assert carrier
if self.bridge.attach_to_tap and partition_index == 0: if self.interface.attach_to_tap and partition_index == 0:
partition.tap.createWithOwner(owner, attach_to_tap=True) partition.tap.createWithOwner(owner, attach_to_tap=True)
else: else:
partition.tap.createWithOwner(owner) partition.tap.createWithOwner(owner)
self.bridge.addTap(partition.tap) self.interface.addTap(partition.tap)
# Reconstructing partition's directory # Reconstructing partition's directory
partition.createPath(alter_user) partition.createPath(alter_user)
...@@ -355,8 +355,8 @@ class Computer: ...@@ -355,8 +355,8 @@ class Computer:
# * local IPv4, took from slapformat:ipv4_local_network # * local IPv4, took from slapformat:ipv4_local_network
if len(partition.address_list) == 0: if len(partition.address_list) == 0:
# regenerate # regenerate
partition.address_list.append(self.bridge.addIPv4LocalAddress()) partition.address_list.append(self.interface.addIPv4LocalAddress())
partition.address_list.append(self.bridge.addAddr()) partition.address_list.append(self.interface.addAddr())
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
...@@ -369,14 +369,14 @@ class Computer: ...@@ -369,14 +369,14 @@ class Computer:
raise ValueError('Not valid ipv6 addresses loaded') raise ValueError('Not valid ipv6 addresses loaded')
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.bridge.addAddr(address['addr'], partition.address_list.append(self.interface.addAddr(address['addr'],
address['netmask'])) address['netmask']))
elif netaddr.valid_ipv4(address['addr']): elif netaddr.valid_ipv4(address['addr']):
partition.address_list.append(self.bridge.addIPv4LocalAddress(address['addr'])) partition.address_list.append(self.interface.addIPv4LocalAddress(address['addr']))
else: else:
raise ValueError('Address %r is incorrect' % address['addr']) raise ValueError('Address %r is incorrect' % address['addr'])
finally: finally:
if alter_network and self.bridge.attach_to_tap: if alter_network and self.interface.attach_to_tap:
try: try:
self.partition_list[0].tap.detach() self.partition_list[0].tap.detach()
except IndexError: except IndexError:
...@@ -516,6 +516,7 @@ class Tap: ...@@ -516,6 +516,7 @@ class Tap:
there is carrier, e.g. the network cable is plugged into a switch for there is carrier, e.g. the network cable is plugged into a switch for
example). example).
In case of bridge :
In order to be able to check the uniqueness of IPv6 address assigned to In order to be able to check the uniqueness of IPv6 address assigned to
the bridge, the network interface must be up from an administrative *and* the bridge, the network interface must be up from an administrative *and*
operational point of view. operational point of view.
...@@ -581,13 +582,13 @@ class Tap: ...@@ -581,13 +582,13 @@ class Tap:
return True return True
class Bridge: class Interface:
"Bridge represent a bridge on the system" "Interface represent a interface on the system"
def __init__(self, name, ipv4_local_network, ipv6_interface=None): def __init__(self, name, ipv4_local_network, ipv6_interface=None):
""" """
Attributes: Attributes:
name: String, the name of the bridge name: String, the name of the interface
""" """
self.name = str(name) self.name = str(name)
...@@ -633,19 +634,19 @@ class Bridge: ...@@ -633,19 +634,19 @@ class Bridge:
return address_list return address_list
def getInterfaceList(self): def getInterfaceList(self):
"""Returns list of interfaces already present on bridge""" """Returns list of interfaces already present on interface"""
interface_list = [] interface_list = []
returncode, result = callAndRead(['brctl', 'show']) returncode, result = callAndRead(['brctl', 'show'])
in_bridge = False in_interface = False
for line in result.split('\n'): for line in result.split('\n'):
if len(line.split()) > 1: if len(line.split()) > 1:
if self.name in line: if self.name in line:
interface_list.append(line.split()[-1]) interface_list.append(line.split()[-1])
in_bridge = True in_interface = True
continue continue
if in_bridge: if in_interface:
break break
elif in_bridge: elif in_interface:
if line.strip(): if line.strip():
interface_list.append(line.strip()) interface_list.append(line.strip())
...@@ -662,7 +663,7 @@ class Bridge: ...@@ -662,7 +663,7 @@ class Bridge:
callAndRead(['brctl', 'addif', self.name, tap.name]) callAndRead(['brctl', 'addif', self.name, tap.name])
def _addSystemAddress(self, address, netmask, ipv6=True): def _addSystemAddress(self, address, netmask, ipv6=True):
"""Adds system address to bridge """Adds system address to interface
Returns True if address was added successfully. Returns True if address was added successfully.
...@@ -739,15 +740,15 @@ class Bridge: ...@@ -739,15 +740,15 @@ class Bridge:
def addAddr(self, addr = None, netmask = None): def addAddr(self, addr = None, netmask = None):
""" """
Adds IP address to bridge. Adds IP address to interface.
If addr is specified and exists already on bridge does nothing. If addr is specified and exists already on interface does nothing.
If addr is specified and does not exists on bridge, tries to add given address. If addr is specified and does not exists on interface, tries to add given address.
In case if it is not possible (ex. because network changed) calculates new address. In case if it is not possible (ex. because network changed) calculates new address.
Args: Args:
addr: Wished address to be added to bridge. addr: Wished address to be added to interface.
netmask: Wished netmask to be used. netmask: Wished netmask to be used.
Returns: Returns:
...@@ -755,32 +756,32 @@ class Bridge: ...@@ -755,32 +756,32 @@ class Bridge:
Raises: Raises:
AddressGenerationError: Couldn't construct valid address with existing AddressGenerationError: Couldn't construct valid address with existing
one's on the bridge. one's on the interface.
NoAddressOnBridge: There's no address on the bridge to construct NoAddressOnInterface: There's no address on the interface to construct
an address with. an address with.
""" """
# Getting one address of the bridge as base of the next addresses # Getting one address of the interface as base of the next addresses
if self.ipv6_interface: if self.ipv6_interface:
interface_name = self.ipv6_interface interface_name = self.ipv6_interface
else: else:
interface_name = self.name interface_name = self.name
bridge_addr_list = self.getGlobalScopeAddressList() interface_addr_list = self.getGlobalScopeAddressList()
# No address found # No address found
if len(bridge_addr_list) == 0: if len(interface_addr_list) == 0:
raise NoAddressOnBridge(interface_name) raise NoAddressOnInterface(interface_name)
address_dict = bridge_addr_list[0] address_dict = interface_addr_list[0]
if addr is not None: if addr is not None:
if dict(addr=addr, netmask=netmask) in bridge_addr_list: if dict(addr=addr, netmask=netmask) in interface_addr_list:
# confirmed to be configured # confirmed to be configured
return dict(addr=addr, netmask=netmask) return dict(addr=addr, netmask=netmask)
if netmask == address_dict['netmask']: if netmask == address_dict['netmask']:
# same netmask, so there is a chance to add good one # same netmask, so there is a chance to add good one
bridge_network = netaddr.ip.IPNetwork('%s/%s' % (address_dict['addr'], interface_network = netaddr.ip.IPNetwork('%s/%s' % (address_dict['addr'],
netmaskToPrefixIPv6(address_dict['netmask']))) netmaskToPrefixIPv6(address_dict['netmask'])))
requested_network = netaddr.ip.IPNetwork('%s/%s' % (addr, netmaskToPrefixIPv6(netmask))) requested_network = netaddr.ip.IPNetwork('%s/%s' % (addr, netmaskToPrefixIPv6(netmask)))
if bridge_network.network == requested_network.network: if interface_network.network == requested_network.network:
# same network, try to add # same network, try to add
if self._addSystemAddress(addr, netmask): if self._addSystemAddress(addr, netmask):
# succeed, return it # succeed, return it
...@@ -866,18 +867,18 @@ def run(config): ...@@ -866,18 +867,18 @@ def run(config):
config.logger.info('Using definition file %r' % filepath) config.logger.info('Using definition file %r' % filepath)
computer_definition = ConfigParser.RawConfigParser() computer_definition = ConfigParser.RawConfigParser()
computer_definition.read(filepath) computer_definition.read(filepath)
bridge = None interface = None
address = None address = None
netmask = None netmask = None
if computer_definition.has_option('computer', 'address'): if computer_definition.has_option('computer', 'address'):
address, netmask = computer_definition.get('computer', 'address').split('/') address, netmask = computer_definition.get('computer', 'address').split('/')
if config.alter_network and config.bridge_name is not None \ if config.alter_network and config.interface_name is not None \
and config.ipv4_local_network is not None: and config.ipv4_local_network is not None:
bridge = Bridge(config.bridge_name, config.ipv4_local_network, interface = Interface(config.interface_name, config.ipv4_local_network,
config.ipv6_interface) config.ipv6_interface)
computer = Computer( computer = Computer(
reference=config.computer_id, reference=config.computer_id,
bridge=bridge, interface=interface,
addr=address, addr=address,
netmask=netmask, netmask=netmask,
ipv6_interface=config.ipv6_interface ipv6_interface=config.ipv6_interface
...@@ -903,15 +904,15 @@ def run(config): ...@@ -903,15 +904,15 @@ def run(config):
if os.path.exists(config.computer_xml): if os.path.exists(config.computer_xml):
config.logger.info('Loading previous computer data from %r' % config.computer_xml) config.logger.info('Loading previous computer data from %r' % config.computer_xml)
computer = Computer.load(config.computer_xml, reference=config.computer_id, ipv6_interface=config.ipv6_interface) computer = Computer.load(config.computer_xml, reference=config.computer_id, ipv6_interface=config.ipv6_interface)
# Connect to the bridge interface defined by the configuration # Connect to the interface interface defined by the configuration
computer.bridge = Bridge(config.bridge_name, config.ipv4_local_network, computer.interface = Interface(config.interface_name, config.ipv4_local_network,
config.ipv6_interface) config.ipv6_interface)
else: else:
# If no pre-existent configuration found, creating a new computer object # If no pre-existent configuration found, creating a new computer object
config.logger.warning('Creating new data computer with id %r' % config.computer_id) config.logger.warning('Creating new data computer with id %r' % config.computer_id)
computer = Computer( computer = Computer(
reference=config.computer_id, reference=config.computer_id,
bridge=Bridge(config.bridge_name, config.ipv4_local_network, interface=Interface(config.interface_name, config.ipv4_local_network,
config.ipv6_interface), config.ipv6_interface),
addr=None, addr=None,
netmask=None, netmask=None,
...@@ -1019,7 +1020,7 @@ class Config: ...@@ -1019,7 +1020,7 @@ class Config:
setattr(self, key, configuration_dict[key]) setattr(self, key, configuration_dict[key])
# setup some nones # setup some nones
for parameter in ['bridge_name', 'partition_base_name', 'user_base_name', for parameter in ['interface_name', 'partition_base_name', 'user_base_name',
'tap_base_name', 'ipv4_local_network', 'ipv6_interface']: 'tap_base_name', 'ipv4_local_network', 'ipv6_interface']:
if getattr(self, parameter, None) is None: if getattr(self, parameter, None) is None:
setattr(self, parameter, None) setattr(self, parameter, None)
...@@ -1117,12 +1118,12 @@ def main(*args): ...@@ -1117,12 +1118,12 @@ def main(*args):
else: else:
return 0, '' return 0, ''
callAndRead = dry_callAndRead callAndRead = dry_callAndRead
real_addSystemAddress = Bridge._addSystemAddress real_addSystemAddress = Interface._addSystemAddress
def fake_addSystemAddress(*args, **kw): def fake_addSystemAddress(*args, **kw):
real_addSystemAddress(*args, **kw) real_addSystemAddress(*args, **kw)
# Fake success # Fake success
return True return True
Bridge._addSystemAddress = fake_addSystemAddress Interface._addSystemAddress = fake_addSystemAddress
def fake_getpwnam(user): def fake_getpwnam(user):
class result: class result:
pw_uid = 12345 pw_uid = 12345
......
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