Change in vdsm[master]: Separate libvirt network configuration from ifcfg

wudxw at linux.vnet.ibm.com wudxw at linux.vnet.ibm.com
Thu May 30 07:31:10 UTC 2013


Mark Wu has uploaded a new change for review.

Change subject: Separate libvirt network configuration from ifcfg
......................................................................

Separate libvirt network configuration from ifcfg

The libvirt network configuration code could be reused by non persistent
configurator, so this patch separates it from the ifcfg configurator.

Change-Id: I798cca309f54c3497b18d83de4ff378f0bb6582b
Signed-off-by: Mark Wu <wudxw at linux.vnet.ibm.com>
---
M tests/netconfTests.py
M vdsm.spec.in
M vdsm/netconf/Makefile.am
M vdsm/netconf/ifcfg.py
A vdsm/netconf/libvirtCfg.py
5 files changed, 84 insertions(+), 50 deletions(-)


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

diff --git a/tests/netconfTests.py b/tests/netconfTests.py
index 31ecfb9..69f7764 100644
--- a/tests/netconfTests.py
+++ b/tests/netconfTests.py
@@ -34,6 +34,7 @@
 from configNetwork import objectivizeNetwork
 from netconf import ifcfg
 from netmodels import Nic, Bond, Vlan, Bridge
+from netconf import libvirtCfg
 
 from monkeypatch import MonkeyPatch
 from monkeypatch import MonkeyPatchScope
@@ -100,9 +101,9 @@
              os.path.join(self._tempdir, 'ifcfg-')),
             (ifcfg, 'ifdown', lambda x: 0),
             (ifcfg, 'ifup', lambda *x: 0),
-            (ifcfg.ConfigWriter, '_createNetwork',
+            (libvirtCfg.LibvirtNetwork, 'create',
              lambda *x: None),
-            (ifcfg.ConfigWriter, '_removeNetwork',
+            (libvirtCfg.LibvirtNetwork, 'remove',
              lambda *x: None),
         ]):
             #after vdsm package is installed, the 'vdsm' account will be
diff --git a/vdsm.spec.in b/vdsm.spec.in
index 6aac0ce..039278a 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -892,6 +892,7 @@
 %{_datadir}/%{vdsm_name}/momIF.py*
 %{_datadir}/%{vdsm_name}/netconf/__init__.py*
 %{_datadir}/%{vdsm_name}/netconf/ifcfg.py*
+%{_datadir}/%{vdsm_name}/netconf/libvirtCfg.py*
 %{_datadir}/%{vdsm_name}/neterrors.py*
 %{_datadir}/%{vdsm_name}/netmodels.py*
 %{_datadir}/%{vdsm_name}/respawn
diff --git a/vdsm/netconf/Makefile.am b/vdsm/netconf/Makefile.am
index a871543..57be550 100644
--- a/vdsm/netconf/Makefile.am
+++ b/vdsm/netconf/Makefile.am
@@ -23,4 +23,5 @@
 
 dist_vdsmnetconf_PYTHON = \
 	__init__.py \
-	ifcfg.py
+	ifcfg.py \
+	libvirtCfg.py
diff --git a/vdsm/netconf/ifcfg.py b/vdsm/netconf/ifcfg.py
index 09da70f..0bdaff3 100644
--- a/vdsm/netconf/ifcfg.py
+++ b/vdsm/netconf/ifcfg.py
@@ -17,7 +17,6 @@
 # Refer to the README and COPYING files for full details of the license
 #
 
-from xml.sax.saxutils import escape
 import glob
 import libvirt
 import logging
@@ -31,9 +30,9 @@
 from contextlib import contextmanager
 
 from neterrors import ConfigNetworkError
+from libvirtCfg import LibvirtCfg
 from storage.misc import execCmd
 from vdsm import constants
-from vdsm import libvirtconnection
 from vdsm import netinfo
 from vdsm import utils
 from netmodels import Bond, Vlan, Bridge
@@ -226,12 +225,6 @@
         utils.rmFile(filename)
         logging.debug("Removed file %s", filename)
 
-    def _createNetwork(self, netXml):
-        conn = libvirtconnection.get()
-        net = conn.networkDefineXML(netXml)
-        net.create()
-        net.setAutostart(1)
-
     def createLibvirtNetwork(self, network, iface=None, skipBackup=False):
         """
         Generate libvirt network's XML definition and create the network via
@@ -245,47 +238,15 @@
         :type iface: string
 
         """
-        netName = netinfo.LIBVIRT_NET_PREFIX + network
-        if not iface:
-            netXml = '''<network><name>%s</name><forward mode='bridge'/>
-                        <bridge name='%s'/></network>''' % (escape(netName),
-                                                            escape(network))
-        else:
-            netXml = (
-                '''<network><name>%s</name><forward mode='passthrough'>'''
-                '''<interface dev='%s'/></forward></network>''' %
-                (escape(netName), escape(iface)))
+        netXml = LibvirtCfg.createNetworkDef(network, iface)
         if not skipBackup:
             self._networkBackup(network)
-        self._createNetwork(netXml)
-
-    def _removeNetwork(self, network):
-        netName = netinfo.LIBVIRT_NET_PREFIX + network
-        conn = libvirtconnection.get()
-
-        net = conn.networkLookupByName(netName)
-        if net.isActive():
-            net.destroy()
-        if net.isPersistent():
-            net.undefine()
+        LibvirtCfg.createNetwork(netXml)
 
     def removeLibvirtNetwork(self, network, skipBackup=False):
         if not skipBackup:
             self._networkBackup(network)
-        self._removeNetwork(network)
-
-    @classmethod
-    def getLibvirtNetwork(cls, network):
-        netName = netinfo.LIBVIRT_NET_PREFIX + network
-        conn = libvirtconnection.get()
-        try:
-            net = conn.networkLookupByName(netName)
-            return net.XMLDesc(0)
-        except libvirt.libvirtError as e:
-            if e.get_error_code() == libvirt.VIR_ERR_NO_NETWORK:
-                return
-
-            raise
+        LibvirtCfg.removeNetwork(network)
 
     @classmethod
     def writeBackupFile(cls, dirName, fileName, content):
@@ -313,13 +274,13 @@
     def _atomicNetworkBackup(self, network):
         """ In-memory backup libvirt networks """
         if network not in self._networksBackups:
-            self._networksBackups[network] = self.getLibvirtNetwork(network)
+            self._networksBackups[network] = LibvirtCfg.getNetworkDef(network)
             logging.debug("Backed up %s", network)
 
     @classmethod
     def _persistentNetworkBackup(cls, network):
         """ Persistently backup libvirt networks """
-        content = cls.getLibvirtNetwork(network)
+        content = LibvirtCfg.getNetworkDef(network)
         if not content:
             # For non-exists networks use predefined header
             content = cls.DELETED_HEADER + '\n'
@@ -337,13 +298,13 @@
             # To avoid libvirt errors during recreation we need
             # to remove the old network first
             try:
-                self._removeNetwork(network)
+                LibvirtCfg.removeNetwork(network)
             except libvirt.libvirtError as e:
                 if e.get_error_code() == libvirt.VIR_ERR_NO_NETWORK:
                     pass
 
             if content:
-                self._createNetwork(content)
+                LibvirtCfg.createNetwork(content)
 
             logging.info('Restored %s', network)
 
diff --git a/vdsm/netconf/libvirtCfg.py b/vdsm/netconf/libvirtCfg.py
new file mode 100644
index 0000000..13f749c
--- /dev/null
+++ b/vdsm/netconf/libvirtCfg.py
@@ -0,0 +1,70 @@
+# Copyright 2011-2013 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+from xml.sax.saxutils import escape
+import libvirt
+
+from vdsm import libvirtconnection
+from vdsm import netinfo
+
+
+class LibvirtCfg(object):
+
+    @classmethod
+    def getNetworkDef(cls, network):
+        netName = netinfo.LIBVIRT_NET_PREFIX + network
+        conn = libvirtconnection.get()
+        try:
+            net = conn.networkLookupByName(netName)
+            return net.XMLDesc(0)
+        except libvirt.libvirtError as e:
+            if e.get_error_code() == libvirt.VIR_ERR_NO_NETWORK:
+                return
+
+            raise
+
+    @classmethod
+    def createNetworkDef(cls, network, iface=None):
+        netName = netinfo.LIBVIRT_NET_PREFIX + network
+        if not iface:
+            return '''<network><name>%s</name><forward mode='bridge'/>
+                        <bridge name='%s'/></network>''' % (escape(netName),
+                                                            escape(network))
+        else:
+            return (
+                '''<network><name>%s</name><forward mode='passthrough'>'''
+                '''<interface dev='%s'/></forward></network>''' %
+                (escape(netName), escape(iface)))
+
+    @classmethod
+    def createNetwork(cls, netXml):
+        conn = libvirtconnection.get()
+        net = conn.networkDefineXML(netXml)
+        net.create()
+        net.setAutostart(1)
+
+    @classmethod
+    def removeNetwork(cls, network):
+        netName = netinfo.LIBVIRT_NET_PREFIX + network
+        conn = libvirtconnection.get()
+
+        net = conn.networkLookupByName(netName)
+        if net.isActive():
+            net.destroy()
+        if net.isPersistent():
+            net.undefine()


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I798cca309f54c3497b18d83de4ff378f0bb6582b
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Mark Wu <wudxw at linux.vnet.ibm.com>


More information about the vdsm-patches mailing list