Change in vdsm[ovirt-3.5]: net: flatten ifcfg.ConfigWriter

ibarkan at redhat.com ibarkan at redhat.com
Thu Jul 9 13:06:10 UTC 2015


Ido Barkan has uploaded a new change for review.

Change subject: net: flatten ifcfg.ConfigWriter
......................................................................

net: flatten ifcfg.ConfigWriter

Make startDevices and stopDevices public for further use.
Also, promotoe them to be module level functions because they do not
need to know about the ConfigWriter state.
Also, sortDeviceIfcfgs is only used with them so make them call it
always. While at it, make them pep8 compatible.

Change-Id: I22676ee45dc7f4be7cd1b74f3f56305cc7e1a97a
Signed-off-by: Ido Barkan <ibarkan at redhat.com>
---
M vdsm/network/configurators/ifcfg.py
1 file changed, 50 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/81/43381/1

diff --git a/vdsm/network/configurators/ifcfg.py b/vdsm/network/configurators/ifcfg.py
index cf42256..3b3fba0 100644
--- a/vdsm/network/configurators/ifcfg.py
+++ b/vdsm/network/configurators/ifcfg.py
@@ -380,49 +380,6 @@
                     confFile.write(content)
             logging.info('Restored %s', confFilePath)
 
-    def _devType(self, content):
-        if re.search('^TYPE=Bridge$', content, re.MULTILINE):
-            return "Bridge"
-        elif re.search('^VLAN=yes$', content, re.MULTILINE):
-            return "Vlan"
-        elif re.search('^SLAVE=yes$', content, re.MULTILINE):
-            return "Slave"
-        else:
-            return "Other"
-
-    def _sortDeviceIfcfgs(self, deviceIfcfgs):
-        devdict = {'Bridge': [],
-                   'Vlan': [],
-                   'Slave': [],
-                   'Other': []}
-        for confFile in deviceIfcfgs:
-            if not confFile.startswith(netinfo.NET_CONF_PREF):
-                continue
-            try:
-                content = file(confFile).read()
-            except IOError as e:
-                if e.errno == os.errno.ENOENT:
-                    continue
-                else:
-                    raise
-            dev = confFile[len(netinfo.NET_CONF_PREF):]
-
-            devdict[self._devType(content)].append(dev)
-
-        return devdict['Other'] + devdict['Vlan'] + devdict['Bridge']
-
-    def _stopDevices(self, deviceIfcfgs):
-        for dev in reversed(deviceIfcfgs):
-            ifdown(dev)
-
-    def _startDevices(self, deviceIfcfgs):
-        for dev in deviceIfcfgs:
-            try:
-                ifup(dev)
-            except ConfigNetworkError:
-                logging.error('Failed to ifup device %s during rollback.', dev,
-                              exc_info=True)
-
     @classmethod
     def _persistentBackup(cls, filename):
         """ Persistently backup ifcfg-* config files """
@@ -491,12 +448,12 @@
         if not self._backups and not self._networksBackups:
             return
 
-        self._stopDevices(self._sortDeviceIfcfgs(self._backups.iterkeys()))
+        stop_devices(self._backups.iterkeys())
 
         self.restoreAtomicNetworkBackup()
         self.restoreAtomicBackup()
 
-        self._startDevices(self._sortDeviceIfcfgs(self._backups.iterkeys()))
+        start_devices((self._backups.iterkeys()))
 
     @classmethod
     def clearBackups(cls):
@@ -776,6 +733,54 @@
             self.setIfaceMtu(slave, newmtu)
 
 
+def stop_devices(device_ifcfgs):
+    for dev in reversed(_sort_device_ifcfgs(device_ifcfgs)):
+        ifdown(dev)
+
+
+def start_devices(device_ifcfgs):
+    for dev in _sort_device_ifcfgs(device_ifcfgs):
+        try:
+            ifup(dev)
+        except ConfigNetworkError:
+            logging.error('Failed to ifup device %s during rollback.', dev,
+                          exc_info=True)
+
+
+def _sort_device_ifcfgs(device_ifcfgs):
+    devices = {'Bridge': [],
+               'Vlan': [],
+               'Slave': [],
+               'Other': []}
+    for conf_file in device_ifcfgs:
+        if not conf_file.startswith(netinfo.NET_CONF_PREF):
+            continue
+        try:
+            with open(conf_file) as f:
+                content = f.read()
+        except IOError as e:
+            if e.errno == os.errno.ENOENT:
+                continue
+            else:
+                raise
+        dev = conf_file[len(netinfo.NET_CONF_PREF):]
+
+        devices[_dev_type(content)].append(dev)
+
+    return devices['Other'] + devices['Vlan'] + devices['Bridge']
+
+
+def _dev_type(content):
+    if re.search('^TYPE=Bridge$', content, re.MULTILINE):
+        return "Bridge"
+    elif re.search('^VLAN=yes$', content, re.MULTILINE):
+        return "Vlan"
+    elif re.search('^SLAVE=yes$', content, re.MULTILINE):
+        return "Slave"
+    else:
+        return "Other"
+
+
 def ifdown(iface):
     "Bring down an interface"
     rc, out, err = utils.execCmd([constants.EXT_IFDOWN, iface], raw=True)


-- 
To view, visit https://gerrit.ovirt.org/43381
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I22676ee45dc7f4be7cd1b74f3f56305cc7e1a97a
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: ovirt-3.5
Gerrit-Owner: Ido Barkan <ibarkan at redhat.com>


More information about the vdsm-patches mailing list