Hello Petr Horáček, Dan Kenigsberg,

I'd like you to do a code review.  Please visit

    https://gerrit.ovirt.org/64393

to review the following change.

Change subject: net: Relocating wait-for-event under its own module.
......................................................................

net: Relocating wait-for-event under its own module.

Creating waitfor module under the netlink package.
To be used as a context manager that waits for a specific event to
arrive (using monitor module) before proceeding with the context body.

Change-Id: Ie99c93083fd4db7a2d20518c06e70da5bc333a71
Signed-off-by: Edward Haas <edwa...@redhat.com>
Bug-Url: https://bugzilla.redhat.com/1379115
Reviewed-on: https://gerrit.ovirt.org/62876
Continuous-Integration: Jenkins CI
Reviewed-by: Petr Horáček <phora...@redhat.com>
Reviewed-by: Dan Kenigsberg <dan...@redhat.com>
---
M lib/vdsm/network/configurators/ifcfg.py
M lib/vdsm/network/link/iface.py
M lib/vdsm/network/netlink/Makefile.am
A lib/vdsm/network/netlink/waitfor.py
M vdsm.spec.in
5 files changed, 102 insertions(+), 47 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/93/64393/1

diff --git a/lib/vdsm/network/configurators/ifcfg.py 
b/lib/vdsm/network/configurators/ifcfg.py
index ea09dc3..1d4d6fc 100644
--- a/lib/vdsm/network/configurators/ifcfg.py
+++ b/lib/vdsm/network/configurators/ifcfg.py
@@ -51,7 +51,7 @@
 from vdsm.network.netinfo import (bonding as netinfo_bonding, mtus, nics,
                                   vlans, misc, NET_PATH)
 from vdsm.network.netinfo.cache import ifaceUsed
-from vdsm.network.netlink import monitor
+from vdsm.network.netlink import waitfor
 
 if utils.isOvirtNode():
     from ovirt.node.utils import fs as node_fs
@@ -854,12 +854,11 @@
     else:
         if not iface.master and (iface.ipv4 or iface.ipv6):
             if iface.ipv4:
-                expected_event = {'label': iface.name, 'family': 'inet',
-                                  'scope': 'global'}
+                wait_for_ip = waitfor.waitfor_ipv4_addr
             elif iface.ipv6:
-                expected_event = {'label': iface.name, 'family': 'inet6',
-                                  'scope': 'global'}
-            with _wait_for_event(iface, expected_event):
+                wait_for_ip = waitfor.waitfor_ipv6_addr
+
+            with wait_for_ip(iface.name):
                 _exec_ifup(iface, cgroup)
         else:
             _exec_ifup(iface, cgroup)
@@ -986,31 +985,3 @@
         ifcfgs.add(ROUTE_PATH % top_level_device)
 
     return ifcfgs
-
-
-def _is_subdict(subdict, superdict):
-    return all(item in frozenset(superdict.items())
-               for item in frozenset(subdict.items()))
-
-
-@contextmanager
-def _wait_for_event(iface, expected_event, timeout=10):
-    with monitor.Monitor(groups=('ipv4-ifaddr', 'ipv6-ifaddr'),
-                         timeout=timeout) as mon:
-        try:
-            yield
-        finally:
-            caught_events = []
-            try:
-                for event in mon:
-                    caught_events.append(event)
-                    if _is_subdict(expected_event, event):
-                        return
-            except monitor.MonitorError as e:
-                if e[0] == monitor.E_TIMEOUT:
-                    logging.warning('Expected event "%s" of interface "%s" '
-                                    'was not caught within the given timeout. '
-                                    'Caught events: %s', expected_event, iface,
-                                    caught_events)
-                else:
-                    raise
diff --git a/lib/vdsm/network/link/iface.py b/lib/vdsm/network/link/iface.py
index e2c4d3c..011d7de 100644
--- a/lib/vdsm/network/link/iface.py
+++ b/lib/vdsm/network/link/iface.py
@@ -18,12 +18,10 @@
 #
 from __future__ import absolute_import
 
-import logging
-
 from vdsm.network import ipwrapper
 from vdsm.network.netlink import link
 from vdsm.network.netlink.link import get_link, is_link_up
-from vdsm.network.netlink.monitor import Monitor
+from vdsm.network.netlink.waitfor import waitfor_linkup
 
 
 STATE_UP = 'up'
@@ -64,14 +62,6 @@
     return bool(get_link(dev)['flags'] & link.IFF_PROMISC)
 
 
-def _up_blocking(dev, oper_blocking):
-    iface_up_check = is_oper_up if oper_blocking else is_up
-    with Monitor(groups=('link',), timeout=2, silent_timeout=True) as mon:
+def _up_blocking(dev, link_blocking):
+    with waitfor_linkup(dev, link_blocking):
         ipwrapper.linkSet(dev, [STATE_UP])
-        if iface_up_check(dev):
-            return
-        mon_device = (e for e in mon if e.get('name') == dev)
-        for event in mon_device:
-            logging.info('Monitor event: %s', event)
-            if is_link_up(event.get('flags', 0), oper_blocking):
-                return
diff --git a/lib/vdsm/network/netlink/Makefile.am 
b/lib/vdsm/network/netlink/Makefile.am
index 5cf018a..cc4ca26 100644
--- a/lib/vdsm/network/netlink/Makefile.am
+++ b/lib/vdsm/network/netlink/Makefile.am
@@ -25,4 +25,5 @@
        link.py \
        monitor.py \
        route.py \
+       waitfor.py \
        $(NULL)
diff --git a/lib/vdsm/network/netlink/waitfor.py 
b/lib/vdsm/network/netlink/waitfor.py
new file mode 100644
index 0000000..170e00f
--- /dev/null
+++ b/lib/vdsm/network/netlink/waitfor.py
@@ -0,0 +1,92 @@
+#
+# Copyright 2016 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 __future__ import absolute_import
+
+from contextlib import contextmanager
+import logging
+
+from . import monitor
+from .link import get_link, is_link_up
+
+
+@contextmanager
+def waitfor_linkup(iface, oper_blocking=True, timeout=10):
+    iface_up_check = _is_oper_up if oper_blocking else _is_admin_up
+    with monitor.Monitor(groups=('link',), timeout=timeout,
+                         silent_timeout=True) as mon:
+        try:
+            yield
+        finally:
+            if iface_up_check(iface):
+                return
+            for event in (e for e in mon if e.get('name') == iface):
+                if is_link_up(event.get('flags', 0), oper_blocking):
+                    return
+
+
+@contextmanager
+def waitfor_ipv4_addr(iface, timeout=10):
+    expected_event = {'label': iface, 'family': 'inet', 'scope': 'global'}
+    with _wait_for_event(iface, expected_event, timeout):
+        yield
+
+
+@contextmanager
+def waitfor_ipv6_addr(iface, timeout=10):
+    expected_event = {'label': iface, 'family': 'inet6', 'scope': 'global'}
+    with _wait_for_event(iface, expected_event, timeout):
+        yield
+
+
+@contextmanager
+def _wait_for_event(iface, expected_event, timeout):
+    with monitor.Monitor(groups=('ipv4-ifaddr', 'ipv6-ifaddr'),
+                         timeout=timeout) as mon:
+        try:
+            yield
+        finally:
+            caught_events = []
+            try:
+                for event in mon:
+                    caught_events.append(event)
+                    if _is_subdict(expected_event, event):
+                        return
+            except monitor.MonitorError as e:
+                if e[0] == monitor.E_TIMEOUT:
+                    logging.warning('Expected event "%s" of interface "%s" '
+                                    'was not caught within %ssec. '
+                                    'Caught events: %s',
+                                    expected_event, iface, timeout,
+                                    caught_events)
+                else:
+                    raise
+
+
+def _is_subdict(subdict, superdict):
+    return all(item in frozenset(superdict.items())
+               for item in frozenset(subdict.items()))
+
+
+def _is_admin_up(iface):
+    return is_link_up(get_link(iface)['flags'], check_oper_status=False)
+
+
+def _is_oper_up(iface):
+    return is_link_up(get_link(iface)['flags'], check_oper_status=True)
diff --git a/vdsm.spec.in b/vdsm.spec.in
index dead32b..726b998 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -1247,6 +1247,7 @@
 %{python_sitelib}/%{vdsm_name}/network/netlink/link.py*
 %{python_sitelib}/%{vdsm_name}/network/netlink/monitor.py*
 %{python_sitelib}/%{vdsm_name}/network/netlink/route.py*
+%{python_sitelib}/%{vdsm_name}/network/netlink/waitfor.py*
 %{python_sitelib}/%{vdsm_name}/network/ovs/__init__.py*
 %{python_sitelib}/%{vdsm_name}/network/ovs/info.py*
 %{python_sitelib}/%{vdsm_name}/network/ovs/switch.py*


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie99c93083fd4db7a2d20518c06e70da5bc333a71
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: ovirt-4.0
Gerrit-Owner: Edward Haas <edwa...@redhat.com>
Gerrit-Reviewer: Dan Kenigsberg <dan...@redhat.com>
Gerrit-Reviewer: Petr Horáček <phora...@redhat.com>
_______________________________________________
vdsm-patches mailing list -- vdsm-patches@lists.fedorahosted.org
To unsubscribe send an email to vdsm-patches-le...@lists.fedorahosted.org

Reply via email to