[Kimchi-devel] [PATCH 5/7] Verify all networks set as Template defaults prior to server start up

Aline Manera alinefm at linux.vnet.ibm.com
Thu Apr 23 19:51:06 UTC 2015


Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/model/networks.py | 61 +++++++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 35 deletions(-)

diff --git a/src/kimchi/model/networks.py b/src/kimchi/model/networks.py
index 92aed92..9550584 100644
--- a/src/kimchi/model/networks.py
+++ b/src/kimchi/model/networks.py
@@ -29,6 +29,7 @@ from kimchi import netinfo
 from kimchi import network as knetwork
 from kimchi.exception import InvalidOperation, InvalidParameter
 from kimchi.exception import MissingParameter, NotFoundError, OperationFailed
+from kimchi.osinfo import defaults as tmpl_defaults
 from kimchi.rollbackcontext import RollbackContext
 from kimchi.utils import kimchi_log, run_command
 from kimchi.xmlutils.network import create_vlan_tagged_bridge_xml
@@ -43,42 +44,32 @@ class NetworksModel(object):
     def __init__(self, **kargs):
         self.conn = kargs['conn']
         if self.conn.isQemuURI():
-            self._default_network_check()
-
-    def _default_network_check(self):
-        def create_default_network():
-            try:
-                subnet = self._get_available_address(knetwork.DefaultNetsPool)
-                params = {"name": "default", "connection": "nat",
-                          "subnet": subnet}
-                self.create(params)
-                return conn.networkLookupByName("default")
-            except Exception as e:
-                kimchi_log.error("Fatal: Cannot create default network "
-                                 "because of %s, exit kimchid", e.message)
-                sys.exit(1)
+            self._check_default_networks()
 
+    def _check_default_networks(self):
+        networks = list(set(tmpl_defaults['networks']))
         conn = self.conn.get()
-        try:
-            net = conn.networkLookupByName("default")
-        except libvirt.libvirtError:
-            net = create_default_network()
 
-        if net.isActive() == 0:
+        error_msg = "Please, check the configuration in \
+                     /etc/kimchi/template.conf to ensure it lists only valid \
+                     networks."
+
+        for net in networks:
             try:
-                net.create()
-            except libvirt.libvirtError as e:
-                # FIXME we can not distinguish this error from other internal
-                # error by error code.
-                if ("network is already in use by interface"
-                        in e.message.lower()):
-                    # libvirt do not support update IP element, so delete the
-                    # the network and create new one.
-                    net.undefine()
-                    create_default_network()
-                else:
-                    kimchi_log.error("Fatal: Cannot activate default network "
-                                     "because of %s, exit kimchid", e.message)
+                net = conn.networkLookupByName(net)
+            except libvirt.libvirtError, e:
+                msg = "Fatal: Unable to find network %s. " + error_msg
+                kimchi_log.error(msg % net)
+                kimchi_log.error("Details: %s", e.message)
+                sys.exit(1)
+
+            if net.isActive() == 0:
+                try:
+                    net.create()
+                except libvirt.libvirtError as e:
+                    msg = "Fatal: Unable to activate network %s. " + error_msg
+                    kimchi_log.error(msg % net)
+                    kimchi_log.error("Details: %s", e.message)
                     sys.exit(1)
 
     def create(self, params):
@@ -274,10 +265,10 @@ class NetworkModel(object):
                 'persistent': True if network.isPersistent() else False}
 
     def _is_network_in_use(self, name):
-        # The network "default" is used for Kimchi proposal and should not be
-        # deactivate or deleted. Otherwise, we will allow user create
+        # All the networks listed as default in template.conf file should not
+        # be deactivate or deleted. Otherwise, we will allow user create
         # inconsistent templates from scratch
-        if name == 'default':
+        if name in tmpl_defaults['networks']:
             return True
 
         vms = self._get_vms_attach_to_a_network(name)
-- 
2.1.0




More information about the Kimchi-devel mailing list