def editNetwork(oldBridge, newBridge, vlan=None, bonding=None, nics=None, **options): configurator = Ifcfg() try: delNetwork(oldBridge, configurator=configurator, **options) addNetwork(newBridge, vlan=vlan, bonding=bonding, nics=nics, configurator=configurator, **options) except: configurator.rollback() raise if utils.tobool(options.get('connectivityCheck', False)): if not clientSeen( int( options.get('connectivityTimeout', CONNECTIVITY_TIMEOUT_DEFAULT))): delNetwork(newBridge, force=True) configurator.rollback() return define.errCode['noConPeer']['status']['code']
def editNetwork(oldBridge, newBridge, vlan=None, bonding=None, nics=None, **options): configurator = Ifcfg() try: delNetwork(oldBridge, configurator=configurator, **options) addNetwork(newBridge, vlan=vlan, bonding=bonding, nics=nics, configurator=configurator, **options) except: configurator.rollback() raise if utils.tobool(options.get('connectivityCheck', False)): if not clientSeen(int(options.get('connectivityTimeout', CONNECTIVITY_TIMEOUT_DEFAULT))): delNetwork(newBridge, force=True) configurator.rollback() return define.errCode['noConPeer']['status']['code']
def setupNetworks(networks, bondings, **options): """Add/Edit/Remove configuration for networks and bondings. Params: networks - dict of key=network, value=attributes where 'attributes' is a dict with the following optional items: vlan=<id> bonding="<name>" | nic="<name>" (bonding and nics are mutually exclusive) ipaddr="<ip>" netmask="<ip>" gateway="<ip>" bootproto="..." delay="..." onboot="yes"|"no" (other options will be passed to the config file AS-IS) -- OR -- remove=True (other attributes can't be specified) bondings - dict of key=bonding, value=attributes where 'attributes' is a dict with the following optional items: nics=["<nic1>" , "<nic2>", ...] options="<bonding-options>" -- OR -- remove=True (other attributes can't be specified) options - dict of options, such as: force=0|1 connectivityCheck=0|1 connectivityTimeout=<int> Notes: When you edit a network that is attached to a bonding, it's not necessary to re-specify the bonding (you need only to note the attachment in the network's attributes). Similarly, if you edit a bonding, it's not necessary to specify its networks. """ logger = logging.getLogger("setupNetworks") _netinfo = netinfo.NetInfo() configurator = Ifcfg() networksAdded = set() logger.debug("Setting up network according to configuration: " "networks:%r, bondings:%r, options:%r" % (networks, bondings, options)) force = options.get('force', False) if not utils.tobool(force): logging.debug("Validating configuration") _validateNetworkSetup(dict(networks), dict(bondings)) logger.debug("Applying...") try: libvirt_nets = netinfo.networks() # Remove edited networks and networks with 'remove' attribute for network, networkAttrs in networks.items(): if network in _netinfo.networks: logger.debug("Removing network %r" % network) delNetwork(network, configurator=configurator, force=force, implicitBonding=False) if 'remove' in networkAttrs: del networks[network] del libvirt_nets[network] elif network in libvirt_nets: # If the network was not in _netinfo but is in the networks # returned by libvirt, it means that we are dealing with # a broken network. logger.debug('Removing broken network %r' % network) _delBrokenNetwork(network, libvirt_nets[network], configurator=configurator) if 'remove' in networkAttrs: del networks[network] del libvirt_nets[network] elif 'remove' in networkAttrs: raise ConfigNetworkError( ne.ERR_BAD_BRIDGE, "Cannot delete " "network %r: It doesn't exist in the " "system" % network) else: networksAdded.add(network) _handleBondings(bondings, configurator) # We need to use the newest host info _ni = netinfo.NetInfo() for network, networkAttrs in networks.iteritems(): d = dict(networkAttrs) if 'bonding' in d: d.update(_buildBondOptions(d['bonding'], bondings, _ni)) else: d['nics'] = [d.pop('nic')] d['force'] = force logger.debug("Adding network %r" % network) addNetwork(network, configurator=configurator, implicitBonding=True, **d) if utils.tobool(options.get('connectivityCheck', True)): logger.debug('Checking connectivity...') if not clientSeen( int( options.get('connectivityTimeout', CONNECTIVITY_TIMEOUT_DEFAULT))): logger.info('Connectivity check failed, rolling back') for network in networksAdded: # If the new added network was created on top of # existing bond, we need to keep the bond on rollback # flow, else we will break the new created bond. delNetwork(network, force=True, implicitBonding=networks[network].get('bonding') in bondings) raise ConfigNetworkError(ne.ERR_LOST_CONNECTION, 'connectivity check failed') except: configurator.rollback() raise
def setupNetworks(networks, bondings, **options): """Add/Edit/Remove configuration for networks and bondings. Params: networks - dict of key=network, value=attributes where 'attributes' is a dict with the following optional items: vlan=<id> bonding="<name>" | nic="<name>" (bonding and nics are mutually exclusive) ipaddr="<ip>" netmask="<ip>" gateway="<ip>" bootproto="..." delay="..." onboot="yes"|"no" (other options will be passed to the config file AS-IS) -- OR -- remove=True (other attributes can't be specified) bondings - dict of key=bonding, value=attributes where 'attributes' is a dict with the following optional items: nics=["<nic1>" , "<nic2>", ...] options="<bonding-options>" -- OR -- remove=True (other attributes can't be specified) options - dict of options, such as: force=0|1 connectivityCheck=0|1 connectivityTimeout=<int> Notes: When you edit a network that is attached to a bonding, it's not necessary to re-specify the bonding (you need only to note the attachment in the network's attributes). Similarly, if you edit a bonding, it's not necessary to specify its networks. """ logger = logging.getLogger("setupNetworks") _netinfo = netinfo.NetInfo() configurator = Ifcfg() networksAdded = set() logger.debug("Setting up network according to configuration: " "networks:%r, bondings:%r, options:%r" % (networks, bondings, options)) force = options.get('force', False) if not utils.tobool(force): logging.debug("Validating configuration") _validateNetworkSetup(dict(networks), dict(bondings)) logger.debug("Applying...") try: libvirt_nets = netinfo.networks() # Remove edited networks and networks with 'remove' attribute for network, networkAttrs in networks.items(): if network in _netinfo.networks: logger.debug("Removing network %r" % network) delNetwork(network, configurator=configurator, force=force, implicitBonding=False) if 'remove' in networkAttrs: del networks[network] del libvirt_nets[network] elif network in libvirt_nets: # If the network was not in _netinfo but is in the networks # returned by libvirt, it means that we are dealing with # a broken network. logger.debug('Removing broken network %r' % network) _delBrokenNetwork(network, libvirt_nets[network], configurator=configurator) if 'remove' in networkAttrs: del networks[network] del libvirt_nets[network] elif 'remove' in networkAttrs: raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete " "network %r: It doesn't exist in the " "system" % network) else: networksAdded.add(network) _handleBondings(bondings, configurator) # We need to use the newest host info _ni = netinfo.NetInfo() for network, networkAttrs in networks.iteritems(): d = dict(networkAttrs) if 'bonding' in d: d.update(_buildBondOptions(d['bonding'], bondings, _ni)) else: d['nics'] = [d.pop('nic')] d['force'] = force logger.debug("Adding network %r" % network) addNetwork(network, configurator=configurator, implicitBonding=True, **d) if utils.tobool(options.get('connectivityCheck', True)): logger.debug('Checking connectivity...') if not clientSeen(int(options.get('connectivityTimeout', CONNECTIVITY_TIMEOUT_DEFAULT))): logger.info('Connectivity check failed, rolling back') for network in networksAdded: # If the new added network was created on top of # existing bond, we need to keep the bond on rollback # flow, else we will break the new created bond. delNetwork(network, force=True, implicitBonding=networks[network]. get('bonding') in bondings) raise ConfigNetworkError(ne.ERR_LOST_CONNECTION, 'connectivity check failed') except: configurator.rollback() raise