Change in vdsm[master]: NetReload: Moved addNetwork logic to the configurator

asegurap at redhat.com asegurap at redhat.com
Fri Mar 22 01:52:45 UTC 2013


Antoni Segura Puimedon has uploaded a new change for review.

Change subject: NetReload: Moved addNetwork logic to the configurator
......................................................................

NetReload: Moved addNetwork logic to the configurator

This change starts moving the logic of configNetwork API endpoints
towards the configurators.

configureBridge, configureVlan, configureBond and configureNic
after this patch have to take care of the underlying devices
explicitly in the configurator module. However, the underlying
device handling will be abstracted to the network models in a
future patch. Thus, the code of configureBond for IfcfgConfigurator
is gonna be something like:

self.configWriter.addBonding(params)
for slave in bond.slaves:
    slave.configure(params)

Change-Id: I877eddc07a17f0bdd376df7bab8b19ffe52f72ba
Signed-off-by: Antoni S. Puimedon <asegurap at redhat.com>
---
M vdsm/configNetwork.py
M vdsm/ifcfgConfigurator.py
2 files changed, 120 insertions(+), 73 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/68/13268/1

diff --git a/vdsm/configNetwork.py b/vdsm/configNetwork.py
index ff18629..0defdd4 100755
--- a/vdsm/configNetwork.py
+++ b/vdsm/configNetwork.py
@@ -38,6 +38,7 @@
 from ifcfgConfigurator import ifup
 from ifcfgConfigurator import ifdown
 from ifcfgConfigurator import ConfigWriter
+from ifcfgConfigurator import IfcfgConfigurator
 
 CONNECTIVITY_TIMEOUT_DEFAULT = 4
 MAX_VLAN_ID = 4094
@@ -275,7 +276,6 @@
             raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                      'Both PREFIX and NETMASK supplied')
 
-    # Validation
     if not utils.tobool(force):
         logging.debug('validating network...')
         _addNetworkValidation(_netinfo, network=network, vlan=vlan,
@@ -291,14 +291,10 @@
 
     if configWriter is None:
         configWriter = ConfigWriter()
+    configurator = IfcfgConfigurator(configWriter)
 
-    prevmtu = None
-    if mtu:
-        prevmtu = configWriter.getMaxMtu(nics, mtu)
-
-    nic = nics[0] if nics else None
-    iface = bonding or nic
-    blockingDhcp = utils.tobool(options.get('blockingdhcp'))
+    async = utils.tobool(options.get('blockingdhcp')) and \
+        options.get('bootproto') == 'dhcp'
 
     # take down nics that need to be changed
     vlanedIfaces = [v['iface'] for v in _netinfo.vlans.values()]
@@ -308,70 +304,20 @@
                 ifdown(nic)
 
     if bridged:
-        configWriter.addBridge(network, ipaddr=ipaddr, netmask=netmask,
-                               mtu=mtu, gateway=gateway, **options)
-        ifdown(network)
-        # We need to define (if requested) ip, mask & gateway on ifcfg-*
-        # only on most top device according to following order:
-        # bridge -> vlan -> bond -> nic
-        # For lower level devices we should ignore it.
-        # reset ip, netmask, gateway and bootproto for lower level devices
-        bridgeBootproto = options.get('bootproto')
-        ipaddr = netmask = gateway = options['bootproto'] = None
-
-    # For VLAN we should attach bridge only to the VLAN device
-    # rather than to underlying NICs or bond
-    brName = network if bridged else None
-    bridgeForNic = None if vlan else brName
-
-    # We want to create config files (ifcfg-*) in top-down order
-    # (bridge->vlan->bond->nic) to be able to handle IP/NETMASK
-    # correctly for bridgeless networks
-    if vlan:
-        # don't ifup VLAN interface here, it should be done last,
-        # after the bond and nic up
-        configWriter.addVlan(vlan, iface, network=brName,
-                             mtu=mtu, bridged=bridged,
-                             ipaddr=ipaddr, netmask=netmask,
-                             gateway=gateway, **options)
-        iface += '.' + vlan
-        vlanBootproto = options.get('bootproto')
-        # reset ip, netmask, gateway and bootproto for lower level devices
-        ipaddr = netmask = gateway = options['bootproto'] = None
-
-    # First we need to prepare all conf files
-    if bonding:
-        configWriter.addBonding(bonding, bridge=bridgeForNic,
-                                bondingOptions=bondingOptions,
-                                mtu=max(prevmtu, mtu),
-                                ipaddr=ipaddr, netmask=netmask,
-                                gateway=gateway, **options)
-        bondBootproto = options.get('bootproto')
-        # reset ip, netmask, gateway and bootproto for lower level devices
-        ipaddr = netmask = gateway = options['bootproto'] = None
-
-    for nic in nics:
-        configWriter.addNic(nic, bonding=bonding,
-                            bridge=bridgeForNic if not bonding else None,
-                            mtu=max(prevmtu, mtu), ipaddr=ipaddr,
-                            netmask=netmask, gateway=gateway, **options)
-
-    # Now we can run ifup for all interfaces
-    if bonding:
-        ifup(bonding, bondBootproto == 'dhcp' and not blockingDhcp)
-    else:
-        for nic in nics:
-            ifup(nic, options.get('bootproto') == 'dhcp' and not blockingDhcp)
-
-    # Now we can ifup VLAN interface, because bond and nic already up
-    if vlan:
-        ifup(iface, vlanBootproto == 'dhcp' and not blockingDhcp)
-
-    if bridged:
-        ifup(network, bridgeBootproto == 'dhcp' and not blockingDhcp)
-
-    # add libvirt network
-    configWriter.createLibvirtNetwork(network, bridged, iface)
+        configurator.configureBridge(network, vlan, bonding, nics, ipaddr,
+                                     netmask, mtu, gateway, bondingOptions,
+                                     async, **options)
+    elif vlan:
+        configurator.configureVlan(network, vlan, None, bonding, nics,
+                                   ipaddr, netmask, mtu, gateway,
+                                   bondingOptions, async, **options)
+    elif bonding:
+        configurator.configureBond(network, bonding, nics, ipaddr, netmask,
+                                   mtu, gateway, None, bondingOptions, async,
+                                   **options)
+    elif nics:
+        configurator.configureNic(network, nics[0], None, bonding, ipaddr,
+                                  netmask, mtu, gateway, async, **options)
 
 
 def assertBridgeClean(bridge, vlan, bonding, nics):
diff --git a/vdsm/ifcfgConfigurator.py b/vdsm/ifcfgConfigurator.py
index 5723185..288730c 100644
--- a/vdsm/ifcfgConfigurator.py
+++ b/vdsm/ifcfgConfigurator.py
@@ -43,7 +43,108 @@
 
 class IfcfgConfigurator(object):
     # TODO: Do all the configWriter interaction from here.
-    pass
+    def __init__(self, configWriter=None):
+        self.configWriter = configWriter
+        self.libvirtAdded = []
+
+    def begin(self):
+        if self.configWriter is None:
+            self.configWriter = ConfigWriter()
+            self.libvirtAdded = []
+
+    def rollback(self):
+        if self.configWriter:
+            self.configWriter.restoreBackups()
+            # TODO: Add a try except with logging, but not raising.
+            for network in self.libvirtAdded:
+                self.configWriter.removeLibvirtNetwork(network)
+
+            self.configWriter = None
+
+    def commit(self):
+        if self.configWriter:
+            self.configWriter = None
+            self.libvirtAdded = []
+
+    def configureBridge(self, network, vlan=None, bonding=None, nics=None,
+                        ipaddr=None, netmask=None, mtu=None, gateway=None,
+                        bondingOptions=None, async=False, **options):
+        self.configWriter.addBridge(network, ipaddr=ipaddr, netmask=netmask,
+                                    mtu=mtu, gateway=gateway, **options)
+        ifdown(network)
+        options['bootproto'] = None
+        if vlan:
+            iface = self.configureVlan(None, vlan, bridge=network,
+                                       bonding=bonding, nics=nics, mtu=mtu,
+                                       bondingOptions=bondingOptions,
+                                       **options)
+        elif bonding:
+            iface = self.configureBond(None, bonding, nics, bridge=network,
+                                       bondingOptions=bondingOptions, mtu=mtu,
+                                       **options)
+        elif nics:
+            mtu = self.configWriter.getMaxMtu(nics, mtu)
+            for nic in nics:
+                iface = self.configureNic(None, nic, bridge=network, mtu=mtu,
+                                          **options)
+        else:
+            iface = None
+        ifup(network, async)
+        self.configWriter.createLibvirtNetwork(network, True, iface)
+
+    def configureVlan(self, network, vlan, bridge=None, bonding=None,
+                      nics=None, ipaddr=None, netmask=None, mtu=None,
+                      gateway=None, bondingOptions=None, async=False,
+                      **options):
+        iface = (bonding or nics[0])
+        self.configWriter.addVlan(vlan, iface, network=bridge, mtu=mtu,
+                                  bridged=bridge is not None, ipaddr=ipaddr,
+                                  netmask=netmask, gateway=gateway, **options)
+        iface += '.' + vlan
+        options['bootproto'] = None
+        if bonding:
+            self.configureBond(None, bonding, nics, bridge=None,
+                               bondingOptions=bondingOptions, mtu=mtu,
+                               **options)
+        elif nics:
+            mtu = self.configWriter.getMaxMtu(nics, mtu)
+            for nic in nics:
+                self.configureNic(None, nic, bridge=None, mtu=mtu, **options)
+        ifup(iface, async)
+        if network:
+            self.configWriter.createLibvirtNetwork(network, False, iface)
+        return iface
+
+    def configureBond(self, network, bonding=None, nics=None, ipaddr=None,
+                      netmask=None, mtu=None, gateway=None, bridge=None,
+                      bondingOptions=None, async=False, **options):
+        mtu = self.configWriter.getMaxMtu(nics, mtu)
+        self.configWriter.addBonding(bonding, bridge=bridge,
+                                     bondingOptions=bondingOptions,
+                                     mtu=mtu, ipaddr=ipaddr,
+                                     netmask=netmask, gateway=gateway,
+                                     **options)
+        options['bootproto'] = None
+        for nic in nics:
+            self.configureNic(None, nic, bonding=bonding, mtu=mtu, **options)
+
+        ifup(bonding, async)
+        if network:
+            self.configWriter.createLibvirtNetwork(network, False, bonding)
+        return bonding
+
+    def configureNic(self, network, nic, bridge=None, bonding=None,
+                     ipaddr=None, netmask=None, mtu=None, gateway=None,
+                     async=False, **options):
+        self.configWriter.addNic(nic, bonding=bonding, bridge=bridge, mtu=mtu,
+                                 ipaddr=ipaddr, netmask=netmask,
+                                 gateway=gateway, **options)
+        if not bonding:
+            ifup(nic, async)
+
+        if network:
+            self.configWriter.createLibvirtNetwork(network, False, nic)
+        return nic
 
 
 class ConfigWriter(object):


--
To view, visit http://gerrit.ovirt.org/13268
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I877eddc07a17f0bdd376df7bab8b19ffe52f72ba
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <asegurap at redhat.com>


More information about the vdsm-patches mailing list