Ondřej Svoboda has uploaded a new change for review.

Change subject: netinfo: Modernize functions recognizing the usage of DHCP from 
lease files
......................................................................

netinfo: Modernize functions recognizing the usage of DHCP from lease files

Change-Id: I5b95d0223b35c3bbbcd736c28ed1dd7c2f9bd2bb
Signed-off-by: Ondřej Svoboda <osvob...@redhat.com>
---
M lib/vdsm/netinfo.py
M tests/netinfoTests.py
2 files changed, 33 insertions(+), 39 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/38/36038/1

diff --git a/lib/vdsm/netinfo.py b/lib/vdsm/netinfo.py
index f54a8bd..3dadca9 100644
--- a/lib/vdsm/netinfo.py
+++ b/lib/vdsm/netinfo.py
@@ -497,73 +497,67 @@
     return info
 
 
-def _parseExpiryTime(expiryTime):
+def _parse_expiry_time(expiry_time):
     EPOCH = 'epoch '
 
-    if expiryTime.startswith(EPOCH):
-        since_epoch = expiryTime[len(EPOCH):]
+    if expiry_time.startswith(EPOCH):
+        since_epoch = expiry_time[len(EPOCH):]
         return datetime.utcfromtimestamp(float(since_epoch))
 
     else:
-        return datetime.strptime(expiryTime, '%w %Y/%m/%d %H:%M:%S')
+        return datetime.strptime(expiry_time, '%w %Y/%m/%d %H:%M:%S')
 
 
-def _parseLeaseFile(leaseFile, ipv6):
-    LEASE = 'lease{0} {{\n'.format('6' if ipv6 else '')
+def _parse_lease_file(lease_file):
+    LEASE = 'lease {\n'
     IFACE = '  interface "'
     IFACE_END = '";\n'
     EXPIRE = '  expire '
 
-    interfaces = set()
-    insideLease = False
+    dhcpv4 = set()
+    family = None
 
-    for line in leaseFile:
-        if insideLease:
+    for line in lease_file:
+        if family:
             if line.startswith(IFACE) and line.endswith(IFACE_END):
-                name = line[len(IFACE):-len(IFACE_END)]
+                iface = line[len(IFACE):-len(IFACE_END)]
 
             elif line.startswith(EXPIRE):
                 end = line.find(';')
                 if end == -1:
                     continue  # the line should always contain a ;
 
-                expiryTime = _parseExpiryTime(line[len(EXPIRE):end])
-                if datetime.utcnow() > expiryTime:
-                    insideLease = False
+                expiry_time = _parse_expiry_time(line[len(EXPIRE):end])
+                if datetime.utcnow() > expiry_time:
+                    family = None
                     continue
 
             elif line == '}\n':
-                insideLease = False
-                if name:
-                    interfaces.add(name)
+                family = None
+                if iface:
+                    dhcpv4.add(iface)
 
         elif line == LEASE:
-            insideLease = True
-            name = ''
+            family = 4
+            iface = ''
 
-    return interfaces
+    return dhcpv4
 
 
-def getDhclientIfaces(leaseFilesGlobs, ipv6=False):
-    """Returns a set of interfaces configured using dhclient.
+def _get_dhclient_ifaces(lease_files_globs):
+    """Return a set of interfaces configured using dhclient.
 
     dhclient stores DHCP leases to file(s) whose names can be specified
-    by the leaseFilesGlobs parameter (an iterable of glob strings).
+    by the lease_files_globs parameter (an iterable of glob strings).
+    """
+    dhcpv4 = set()
 
-        TODO: dhclient6 does not use an 'expire' line, create a test to see
-        if a line reading 'released;' is an unambiguous sign of an invalid
-        DHCPv6 lease.
+    for lease_files_glob in lease_files_globs:
+        for lease_path in iglob(lease_files_glob):
+            with open(lease_path) as lease_file:
+                dhcpv4.update(_parse_lease_file(lease_file))
 
-    To discover DHCPv6 leases set the ipv6 parameter to True."""
-
-    interfaces = set()
-
-    for leaseFilesGlob in leaseFilesGlobs:
-        for leaseFile in iglob(leaseFilesGlob):
-            with open(leaseFile) as leaseFile:
-                interfaces.update(_parseLeaseFile(leaseFile, ipv6))
-
-    return interfaces
+    return dhcpv4
 
 
 def _getIpAddrs():
@@ -612,7 +606,7 @@
     if ipAddrs is None:
         ipAddrs = _getIpAddrs()
     if dhcpv4 is None:
-        dhcpv4 = getDhclientIfaces(_DHCLIENT_LEASES_GLOBS)
+        dhcpv4 = _get_dhclient_ifaces(_DHCLIENT_LEASES_GLOBS)
     d = {}
     for net, netAttr in nets.iteritems():
         try:
@@ -635,7 +629,7 @@
          'vlans': {}}
     paddr = permAddr()
     ipaddrs = _getIpAddrs()
-    dhcpv4 = getDhclientIfaces(_DHCLIENT_LEASES_GLOBS)
+    dhcpv4 = _get_dhclient_ifaces(_DHCLIENT_LEASES_GLOBS)
     routes = _get_routes()
 
     if vdsmnets is None:
diff --git a/tests/netinfoTests.py b/tests/netinfoTests.py
index e1bf294..b44559d 100644
--- a/tests/netinfoTests.py
+++ b/tests/netinfoTests.py
@@ -27,7 +27,7 @@
 
 from vdsm import ipwrapper
 from vdsm import netinfo
-from vdsm.netinfo import (getDhclientIfaces, BONDING_MASTERS, BONDING_OPT,
+from vdsm.netinfo import (_get_dhclient_ifaces, BONDING_MASTERS, BONDING_OPT,
                           _getBondingOptions, OPERSTATE_UP)
 from vdsm.netlink import addr as nl_addr
 from vdsm.tool.dump_bonding_defaults import _random_iface_name


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5b95d0223b35c3bbbcd736c28ed1dd7c2f9bd2bb
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ondřej Svoboda <osvob...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to