Mark Wu has uploaded a new change for review.

Change subject: netconf: Add a base configurator class
......................................................................

netconf: Add a base configurator class

This patch add a base configurator class to make the code not related
to specific configurator implementation shared by configurator ifcfg
and iproute2.

It renames ifcfg's member configWriter to configApplier, a more generic
name and also applicable to iproute2.

Change-Id: I6a1291409bf96cd54971791aebe9a9e74af325bf
Signed-off-by: Mark Wu <[email protected]>
---
M vdsm/netconf/__init__.py
M vdsm/netconf/ifcfg.py
2 files changed, 126 insertions(+), 79 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/78/16178/1

diff --git a/vdsm/netconf/__init__.py b/vdsm/netconf/__init__.py
index 8e88115..128c4ce 100644
--- a/vdsm/netconf/__init__.py
+++ b/vdsm/netconf/__init__.py
@@ -16,3 +16,89 @@
 #
 # Refer to the README and COPYING files for full details of the license
 #
+
+import logging
+
+from netmodels import Bond, Bridge
+from sourceRoute import StaticSourceRoute
+from vdsm import netinfo
+
+
+class Configurator(object):
+    # TODO: Do all the configWriter interaction from here.
+    def __init__(self, configApplier=None):
+        self.configApplier = configApplier
+        self._libvirtAdded = set()
+
+    def configureBridge(self, bridge, **opts):
+        raise NotImplementedError
+
+    def configureVlan(self, vlan, bridge=None, **opts):
+        raise NotImplementedError
+
+    def configureBond(self, bond, bridge=None, **opts):
+        raise NotImplementedError
+
+    def editBonding(self, bond, _netinfo):
+        raise NotImplementedError
+
+    def configureNic(self, nic, bridge=None, bonding=None, **opts):
+        raise NotImplementedError
+
+    def removeBridge(self, bridge):
+        raise NotImplementedError
+
+    def removeVlan(self, vlan):
+        raise NotImplementedError
+
+    def removeBond(self, bonding):
+        raise NotImplementedError
+
+    def removeNic(self, nic):
+        raise NotImplementedError
+
+    def configureSourceRoute(self, routes, rules, device):
+        raise NotImplementedError
+
+    def removeSourceRoute(self, routes, rules, device):
+        raise NotImplementedError
+
+    def configureLibvirtNetwork(self, network, iface):
+        self.configApplier.createLibvirtNetwork(network,
+                                                isinstance(iface, Bridge),
+                                                iface.name)
+        self._libvirtAdded.add(network)
+
+    def removeLibvirtNetwork(self, network):
+        self.configApplier.removeLibvirtNetwork(network)
+
+    def _addSourceRoute(self, netEnt, ipaddr, netmask, gateway, bootproto):
+        # bootproto is None for both static and no bootproto
+        if bootproto != 'dhcp' and netEnt.master is None:
+            logging.debug("Adding source route %s, %s, %s, %s" %
+                          (netEnt.name, ipaddr, netmask, gateway))
+            StaticSourceRoute(netEnt.name, self).\
+                configure(ipaddr, netmask, gateway)
+
+    def _removeSourceRoute(self, netEnt):
+        _, _, _, bootproto, _ = netEnt.getIpConfig()
+        if bootproto != 'dhcp' and netEnt.master is None:
+            logging.debug("Removing source route for device %s" % netEnt.name)
+            StaticSourceRoute(netEnt.name, self).remove()
+
+    def _setNewMtu(self, iface, _netinfo):
+        """
+        Update an interface's MTU when one of its users is removed.
+
+        :param iface: interface object (bond or nic device)
+        :type iface: NetDevice instance
+
+        """
+        ifaceMtu = netinfo.getMtu(iface.name)
+        maxMtu = netinfo.getMaxMtu(_netinfo.getVlanDevsForIface(iface.name),
+                                   None)
+        if maxMtu and maxMtu < ifaceMtu:
+            if isinstance(iface, Bond):
+                self.configApplier.setBondingMtu(iface.name, maxMtu)
+            else:
+                self.configApplier.setIfaceMtu(iface.name, maxMtu)
diff --git a/vdsm/netconf/ifcfg.py b/vdsm/netconf/ifcfg.py
index 4ccc15d..69248da 100644
--- a/vdsm/netconf/ifcfg.py
+++ b/vdsm/netconf/ifcfg.py
@@ -28,9 +28,9 @@
 import shutil
 import threading
 
+from netconf import Configurator
 from neterrors import ConfigNetworkError
-from netmodels import Nic, Bond, Bridge
-from sourceRoute import StaticSourceRoute
+from netmodels import Nic
 from storage.misc import execCmd
 from vdsm import constants
 from vdsm import netinfo
@@ -39,38 +39,38 @@
 import neterrors as ne
 
 
-class Ifcfg(object):
+class Ifcfg(Configurator):
     # TODO: Do all the configWriter interaction from here.
-    def __init__(self, configWriter=None):
-        self.configWriter = configWriter
+    def __init__(self, configApplier=None):
+        self.configApplier = configApplier
         self._libvirtAdded = set()
 
     def begin(self):
-        if self.configWriter is None:
-            self.configWriter = ConfigWriter()
+        if self.configApplier is None:
+            self.configApplier = ConfigWriter()
             self._libvirtAdded = set()
 
     def rollback(self):
-        if self.configWriter:
-            self.configWriter.restoreBackups()
+        if self.configApplier:
+            self.configApplier.restoreBackups()
             for network in self._libvirtAdded:
                 # TODO: Add meaningful logging for failure to remove the added
                 # networks.
-                self.configWriter.removeLibvirtNetwork(network)
+                self.configApplier.removeLibvirtNetwork(network)
 
-            self.configWriter = None
+            self.configApplier = None
 
     def commit(self):
-        if self.configWriter:
-            self.configWriter = None
+        if self.configApplier:
+            self.configApplier = None
             self._libvirtAdded = set()
 
     def configureBridge(self, bridge, **opts):
         ipaddr, netmask, gateway, bootproto, async = bridge.getIpConfig()
-        self.configWriter.addBridge(bridge.name, ipaddr=ipaddr,
-                                    netmask=netmask, mtu=bridge.mtu,
-                                    gateway=gateway, bootproto=bootproto,
-                                    **opts)
+        self.configApplier.addBridge(bridge.name, ipaddr=ipaddr,
+                                     netmask=netmask, mtu=bridge.mtu,
+                                     gateway=gateway, bootproto=bootproto,
+                                     **opts)
         ifdown(bridge.name)
         if bridge.port:
             bridge.port.configure(bridge=bridge.name, **opts)
@@ -79,20 +79,21 @@
 
     def configureVlan(self, vlan, bridge=None, **opts):
         ipaddr, netmask, gateway, bootproto, async = vlan.getIpConfig()
-        self.configWriter.addVlan(vlan.name, bridge=bridge, mtu=vlan.mtu,
-                                  ipaddr=ipaddr, netmask=netmask,
-                                  gateway=gateway, bootproto=bootproto, **opts)
+        self.configApplier.addVlan(vlan.name, bridge=bridge, mtu=vlan.mtu,
+                                   ipaddr=ipaddr, netmask=netmask,
+                                   gateway=gateway, bootproto=bootproto,
+                                   **opts)
         vlan.device.configure(**opts)
         self._addSourceRoute(vlan, ipaddr, netmask, gateway, bootproto)
         ifup(vlan.name, async)
 
     def configureBond(self, bond, bridge=None, **opts):
         ipaddr, netmask, gateway, bootproto, async = bond.getIpConfig()
-        self.configWriter.addBonding(bond.name, bridge=bridge,
-                                     bondingOptions=bond.options,
-                                     mtu=bond.mtu, ipaddr=ipaddr,
-                                     netmask=netmask, gateway=gateway,
-                                     bootproto=bootproto, **opts)
+        self.configApplier.addBonding(bond.name, bridge=bridge,
+                                      bondingOptions=bond.options,
+                                      mtu=bond.mtu, ipaddr=ipaddr,
+                                      netmask=netmask, gateway=gateway,
+                                      bootproto=bootproto, **opts)
         if not netinfo.isVlanned(bond.name):
             for slave in bond.slaves:
                 ifdown(slave.name)
@@ -110,44 +111,35 @@
 
     def configureNic(self, nic, bridge=None, bonding=None, **opts):
         ipaddr, netmask, gateway, bootproto, async = nic.getIpConfig()
-        self.configWriter.addNic(nic.name, bonding=bonding, bridge=bridge,
-                                 mtu=nic.mtu, ipaddr=ipaddr,
-                                 netmask=netmask, gateway=gateway,
-                                 bootproto=bootproto, **opts)
+        self.configApplier.addNic(nic.name, bonding=bonding, bridge=bridge,
+                                  mtu=nic.mtu, ipaddr=ipaddr,
+                                  netmask=netmask, gateway=gateway,
+                                  bootproto=bootproto, **opts)
         self._addSourceRoute(nic, ipaddr, netmask, gateway, bootproto)
         if not bonding:
             if not netinfo.isVlanned(nic.name):
                 ifdown(nic.name)
             ifup(nic.name, async)
 
-    def configureLibvirtNetwork(self, network, iface):
-        self.configWriter.createLibvirtNetwork(network,
-                                               isinstance(iface, Bridge),
-                                               iface.name)
-        self._libvirtAdded.add(network)
-
-    def removeLibvirtNetwork(self, network):
-        self.configWriter.removeLibvirtNetwork(network)
-
     def removeBridge(self, bridge):
         ifdown(bridge.name)
         self._removeSourceRoute(bridge)
         execCmd([constants.EXT_BRCTL, 'delbr', bridge.name])
-        self.configWriter.removeBridge(bridge.name)
+        self.configApplier.removeBridge(bridge.name)
         if bridge.port:
             bridge.port.remove()
 
     def removeVlan(self, vlan):
         ifdown(vlan.name)
         self._removeSourceRoute(vlan)
-        self.configWriter.removeVlan(vlan.name)
+        self.configApplier.removeVlan(vlan.name)
         vlan.device.remove()
 
     def _ifaceDownAndCleanup(self, iface, _netinfo):
         """Returns True iff the iface is to be removed."""
         ifdown(iface.name)
         self._removeSourceRoute(iface)
-        self.configWriter.removeIfaceCleanup(iface.name)
+        self.configApplier.removeIfaceCleanup(iface.name)
         return not _netinfo.ifaceUsers(iface.name)
 
     def removeBond(self, bonding):
@@ -155,12 +147,12 @@
         to_be_removed = self._ifaceDownAndCleanup(bonding, _netinfo)
         if to_be_removed:
             if bonding.destroyOnMasterRemoval:
-                self.configWriter.removeBonding(bonding.name)
+                self.configApplier.removeBonding(bonding.name)
                 for slave in bonding.slaves:
                     slave.remove()
             else:
-                self.configWriter.setBondingMtu(bonding.name,
-                                                netinfo.DEFAULT_MTU)
+                self.configApplier.setBondingMtu(bonding.name,
+                                                 netinfo.DEFAULT_MTU)
                 ifup(bonding.name)
         else:
             self._setNewMtu(bonding, _netinfo)
@@ -170,49 +162,18 @@
         _netinfo = netinfo.NetInfo()
         to_be_removed = self._ifaceDownAndCleanup(nic, _netinfo)
         if to_be_removed:
-            self.configWriter.removeNic(nic.name)
+            self.configApplier.removeNic(nic.name)
         else:
             self._setNewMtu(nic, _netinfo)
         ifup(nic.name)
-
-    def _addSourceRoute(self, netEnt, ipaddr, netmask, gateway, bootproto):
-        # bootproto is None for both static and no bootproto
-        if bootproto != 'dhcp' and netEnt.master is None:
-            logging.debug("Adding source route %s, %s, %s, %s" %
-                          (netEnt.name, ipaddr, netmask, gateway))
-            StaticSourceRoute(netEnt.name, self).\
-                configure(ipaddr, netmask, gateway)
-
-    def _removeSourceRoute(self, netEnt):
-        _, _, _, bootproto, _ = netEnt.getIpConfig()
-        if bootproto != 'dhcp' and netEnt.master is None:
-            logging.debug("Removing source route for device %s" % netEnt.name)
-            StaticSourceRoute(netEnt.name, self).remove()
-
-    def _setNewMtu(self, iface, _netinfo):
-        """
-        Update an interface's MTU when one of its users is removed.
-
-        :param iface: interface object (bond or nic device)
-        :type iface: NetDevice instance
-
-        """
-        ifaceMtu = netinfo.getMtu(iface.name)
-        maxMtu = netinfo.getMaxMtu(_netinfo.getVlanDevsForIface(iface.name),
-                                   None)
-        if maxMtu and maxMtu < ifaceMtu:
-            if isinstance(iface, Bond):
-                self.configWriter.setBondingMtu(iface.name, maxMtu)
-            else:
-                self.configWriter.setIfaceMtu(iface.name, maxMtu)
 
     def _getFilePath(self, fileType, device):
         return os.path.join(netinfo.NET_CONF_DIR, '%s-%s' % (fileType, device))
 
     def _removeSourceRouteFile(self, fileType, device):
         filePath = self._getFilePath(fileType, device)
-        self.configWriter._backup(filePath)
-        self.configWriter._removeFile(filePath)
+        self.configApplier._backup(filePath)
+        self.configApplier._removeFile(filePath)
 
     def _writeConfFile(self, contents, fileType, device):
         filePath = self._getFilePath(fileType, device)
@@ -221,7 +182,7 @@
         for entry in contents:
             configuration += str(entry) + '\n'
 
-        self.configWriter.writeConfFile(filePath, configuration)
+        self.configApplier.writeConfFile(filePath, configuration)
 
     def configureSourceRoute(self, routes, rules, device):
         self._writeConfFile(routes, 'route', device)


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6a1291409bf96cd54971791aebe9a9e74af325bf
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Mark Wu <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to