def editNetwork(oldBridge, newBridge, vlan=None, bonding=None, nics=None, **options): configWriter = ConfigWriter() try: delNetwork(oldBridge, configWriter=configWriter, **options) addNetwork(newBridge, vlan=vlan, bonding=bonding, nics=nics, configWriter=configWriter, **options) except: configWriter.restoreAtomicBackup() raise if utils.tobool(options.get('connectivityCheck', False)): if not clientSeen( int( options.get('connectivityTimeout', CONNECTIVITY_TIMEOUT_DEFAULT))): delNetwork(newBridge, force=True) configWriter.restoreAtomicBackup() return define.errCode['noConPeer']['status']['code']
def editNetwork(oldBridge, newBridge, vlan=None, bonding=None, nics=None, **options): configWriter = ConfigWriter() try: delNetwork(oldBridge, configWriter=configWriter, **options) addNetwork(newBridge, vlan=vlan, bonding=bonding, nics=nics, configWriter=configWriter, **options) except: configWriter.restoreAtomicBackup() raise if utils.tobool(options.get("connectivityCheck", False)): if not clientSeen(int(options.get("connectivityTimeout", CONNECTIVITY_TIMEOUT_DEFAULT))): delNetwork(newBridge, force=True) configWriter.restoreAtomicBackup() 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> explicitBonding=0|1 Notes: Bondings are removed when they change state from 'used' to 'unused'. By default, if 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 attachement in the network's attributes). Similarly, if you edit a bonding, it's not necessary to specify its networks. However, if you specify the 'explicitBonding' option as true, the function will expect you to specify all networks that are attached to a specified bonding, and vice-versa, the bonding attached to a specified network. """ logger = logging.getLogger("setupNetworks") try: _netinfo = NetInfo() configWriter = ConfigWriter() networksAdded = [] # bondingNetworks = {} # Reminder TODO logger.info("Setting up network") logger.debug( "Setting up network according to configuration: networks:%r, bondings:%r, options:%r" % (networks, bondings, options) ) force = options.get("force", False) if not _isTrue(force): logging.debug("Validating configuration") _validateNetworkSetup(dict(networks), dict(bondings), explicitBonding=options.get("explicitBonding", False)) logger.debug("Applying...") try: for network, networkAttrs in networks.items(): if networkAttrs.pop("remove", False): assert not networkAttrs logger.debug("Removing network %r" % network) delNetwork(network, force=force) continue if network in _netinfo.networks: delNetwork(network, force=force) else: networksAdded.append(network) d = dict(networkAttrs) if "bonding" in d: d["nics"] = bondings[d["bonding"]]["nics"] d["bondingOptions"] = bondings[d["bonding"]].get("options", None) else: d["nics"] = [d.pop("nic")] d["force"] = force logger.debug("Adding network %r" % network) addNetwork(network, **d) except: configWriter.restoreAtomicBackup() raise 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 bridge in networksAdded: delNetwork(bridge, force=True) configWriter.restoreAtomicBackup() raise ConfigNetworkError(ne.ERR_LOST_CONNECTION, "connectivity check failed") except Exception, e: # SuperVdsm eats the error, so let's print it ourselves logger.error(e, exc_info=True) raise
def addNetwork( bridge, vlan=None, bonding=None, nics=None, ipaddr=None, netmask=None, gateway=None, force=False, configWriter=None, bondingOptions=None, **options ): nics = nics or () _netinfo = NetInfo() # Validation if not _isTrue(force): logging.debug("validating bridge...") _addNetworkValidation( _netinfo, bridge, vlan=vlan, bonding=bonding, nics=nics, ipaddr=ipaddr, netmask=netmask, gateway=gateway, bondingOptions=bondingOptions, ) logging.info( "Adding bridge %s with vlan=%s, bonding=%s, nics=%s. bondingOptions=%s, options=%s" % (bridge, vlan, bonding, nics, bondingOptions, options) ) if configWriter is None: configWriter = ConfigWriter() configWriter.addBridge(bridge, ipaddr=ipaddr, netmask=netmask, gateway=gateway, **options) ifaceBridge = bridge if vlan: configWriter.addVlan(vlan, bonding or nics[0], bridge) # since we have vlan device, it is connected to the bridge. other # interfaces should be connected to the bridge through vlan, and not # directly. ifaceBridge = None if bonding: configWriter.addBonding(bonding, ifaceBridge, bondingOptions=bondingOptions) for nic in nics: configWriter.addNic(nic, bonding=bonding) else: for nic in nics: configWriter.addNic(nic, bridge=ifaceBridge) # take down nics that need to be changed vlanedIfaces = [v["iface"] for v in _netinfo.vlans.values()] if bonding not in vlanedIfaces: for nic in nics: if nic not in vlanedIfaces: ifdown(nic) ifdown(bridge) # nics must be activated in the same order of boot time to expose the correct # MAC address. for nic in nicSort(nics): ifup(nic) if bonding: ifup(bonding) if vlan: ifup((bonding or nics[0]) + "." + vlan) if options.get("bootproto") == "dhcp" and not utils.tobool(options.get("blockingdhcp")): # wait for dhcp in another thread, so vdsm won't get stuck (BZ#498940) t = threading.Thread(target=ifup, name="ifup-waiting-on-dhcp", args=(bridge,)) t.daemon = True t.start() else: ifup(bridge)
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> explicitBonding=0|1 Notes: Bondings are removed when they change state from 'used' to 'unused'. By default, if 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 attachement in the network's attributes). Similarly, if you edit a bonding, it's not necessary to specify its networks. However, if you specify the 'explicitBonding' option as true, the function will expect you to specify all networks that are attached to a specified bonding, and vice-versa, the bonding attached to a specified network. """ logger = logging.getLogger("setupNetworks") try: _netinfo = NetInfo() configWriter = ConfigWriter() networksAdded = [] #bondingNetworks = {} # Reminder TODO logger.info("Setting up network") logger.debug( "Setting up network according to configuration: networks:%r, bondings:%r, options:%r" % (networks, bondings, options)) force = options.get('force', False) if not _isTrue(force): logging.debug("Validating configuration") _validateNetworkSetup(dict(networks), dict(bondings), explicitBonding=options.get( 'explicitBonding', False)) logger.debug("Applying...") try: for network, networkAttrs in networks.items(): if networkAttrs.pop('remove', False): assert not networkAttrs logger.debug('Removing network %r' % network) delNetwork(network, force=force) continue if network in _netinfo.networks: delNetwork(network, force=force) else: networksAdded.append(network) d = dict(networkAttrs) if 'bonding' in d: d['nics'] = bondings[d['bonding']]['nics'] d['bondingOptions'] = bondings[d['bonding']].get( 'options', None) else: d['nics'] = [d.pop('nic')] d['force'] = force logger.debug('Adding network %r' % network) addNetwork(network, **d) except: configWriter.restoreAtomicBackup() raise 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 bridge in networksAdded: delNetwork(bridge, force=True) configWriter.restoreAtomicBackup() raise ConfigNetworkError(ne.ERR_LOST_CONNECTION, 'connectivity check failed') except Exception, e: # SuperVdsm eats the error, so let's print it ourselves logger.error(e, exc_info=True) raise
def addNetwork(bridge, vlan=None, bonding=None, nics=None, ipaddr=None, netmask=None, gateway=None, force=False, configWriter=None, bondingOptions=None, **options): nics = nics or () _netinfo = NetInfo() # Validation if not _isTrue(force): logging.debug('validating bridge...') _addNetworkValidation(_netinfo, bridge, vlan=vlan, bonding=bonding, nics=nics, ipaddr=ipaddr, netmask=netmask, gateway=gateway, bondingOptions=bondingOptions) logging.info( "Adding bridge %s with vlan=%s, bonding=%s, nics=%s. bondingOptions=%s, options=%s" % (bridge, vlan, bonding, nics, bondingOptions, options)) if configWriter is None: configWriter = ConfigWriter() configWriter.addBridge(bridge, ipaddr=ipaddr, netmask=netmask, gateway=gateway, **options) ifaceBridge = bridge if vlan: configWriter.addVlan(vlan, bonding or nics[0], bridge) # since we have vlan device, it is connected to the bridge. other # interfaces should be connected to the bridge through vlan, and not # directly. ifaceBridge = None if bonding: configWriter.addBonding(bonding, ifaceBridge, bondingOptions=bondingOptions) for nic in nics: configWriter.addNic(nic, bonding=bonding) else: for nic in nics: configWriter.addNic(nic, bridge=ifaceBridge) # take down nics that need to be changed vlanedIfaces = [v['iface'] for v in _netinfo.vlans.values()] if bonding not in vlanedIfaces: for nic in nics: if nic not in vlanedIfaces: ifdown(nic) ifdown(bridge) # nics must be activated in the same order of boot time to expose the correct # MAC address. for nic in nicSort(nics): ifup(nic) if bonding: ifup(bonding) if vlan: ifup((bonding or nics[0]) + '.' + vlan) if options.get('bootproto') == 'dhcp' and not utils.tobool( options.get('blockingdhcp')): # wait for dhcp in another thread, so vdsm won't get stuck (BZ#498940) t = threading.Thread(target=ifup, name='ifup-waiting-on-dhcp', args=(bridge, )) t.daemon = True t.start() else: ifup(bridge)