Change in vdsm[master]: NetDevice: Lookup IP attributes using __getattr__

osvoboda at redhat.com osvoboda at redhat.com
Fri Jan 9 00:01:10 UTC 2015


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

Change subject: NetDevice: Lookup IP attributes using __getattr__
......................................................................

NetDevice: Lookup IP attributes using __getattr__

Change-Id: Iecf268d557d642cb12a4b25d157ec059293a49c4
Signed-off-by: Ondřej Svoboda <osvoboda at redhat.com>
---
M vdsm/network/configurators/__init__.py
M vdsm/network/configurators/ifcfg.py
M vdsm/network/configurators/iproute2.py
M vdsm/network/configurators/pyroute_two.py
M vdsm/network/models.py
M vdsm/network/sourceroute.py
6 files changed, 64 insertions(+), 54 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/15/36715/1

diff --git a/vdsm/network/configurators/__init__.py b/vdsm/network/configurators/__init__.py
index a3c7567..5ada3b6 100644
--- a/vdsm/network/configurators/__init__.py
+++ b/vdsm/network/configurators/__init__.py
@@ -123,24 +123,23 @@
         qos.remove_outbound(top_device)
 
     def _addSourceRoute(self, netEnt):
-        ip = netEnt.ipConfig
         # bootproto is None for both static and no bootproto
-        if ip.bootproto != 'dhcp' and netEnt.master is None:
+        if netEnt.bootproto != 'dhcp' and netEnt.master is None:
             logging.debug("Adding source route: name=%s, addr=%s, netmask=%s, "
-                          "gateway=%s" % (netEnt.name, ip.ipaddr, ip.netmask,
-                                          ip.gateway))
-            if (ip.gateway in (None, '0.0.0.0')
-               or not ip.ipaddr or not ip.netmask):
+                          "gateway=%s" % (netEnt.name, netEnt.ipaddr,
+                                          netEnt.netmask, netEnt.gateway))
+            if (netEnt.gateway in (None, '0.0.0.0')
+               or not netEnt.ipaddr or not netEnt.netmask):
                     logging.error('invalid input for source routing: name=%s, '
                                   'addr=%s, netmask=%s, gateway=%s',
-                                  netEnt.name, ip.ipaddr, ip.netmask,
-                                  ip.gateway)
+                                  netEnt.name, netEnt.ipaddr, netEnt.netmask,
+                                  netEnt.gateway)
             else:
-                StaticSourceRoute(netEnt.name, self, ip.ipaddr, ip.netmask,
-                                  ip.gateway).configure()
+                StaticSourceRoute(netEnt.name, self, netEnt.ipaddr,
+                                  netEnt.netmask, netEnt.gateway).configure()
 
     def _removeSourceRoute(self, netEnt, sourceRouteClass):
-        if netEnt.ipConfig.bootproto != 'dhcp' and netEnt.master is None:
+        if netEnt.bootproto != 'dhcp' and netEnt.master is None:
             logging.debug("Removing source route for device %s", netEnt.name)
             sourceRouteClass(netEnt.name, self, None, None, None).remove()
 
@@ -176,7 +175,7 @@
 
 def runDhclient(iface, family=4):
     dhclient = DhcpClient(iface.name, family)
-    rc = dhclient.start(iface.ipConfig.async)
-    if not iface.ipConfig.async and rc:
+    rc = dhclient.start(iface.async)
+    if not iface.async and rc:
         raise ConfigNetworkError(ERR_FAILED_IFUP, 'dhclient%s failed',
                                  family)
diff --git a/vdsm/network/configurators/ifcfg.py b/vdsm/network/configurators/ifcfg.py
index 4f7cc07..a85b25c 100644
--- a/vdsm/network/configurators/ifcfg.py
+++ b/vdsm/network/configurators/ifcfg.py
@@ -89,13 +89,13 @@
         if bridge.port:
             bridge.port.configure(**opts)
         self._addSourceRoute(bridge)
-        ifup(bridge.name, bridge.ipConfig.async)
+        ifup(bridge.name, bridge.async)
 
     def configureVlan(self, vlan, **opts):
         self.configApplier.addVlan(vlan, **opts)
         vlan.device.configure(**opts)
         self._addSourceRoute(vlan)
-        ifup(vlan.name, vlan.ipConfig.async)
+        ifup(vlan.name, vlan.async)
 
     def configureBond(self, bond, **opts):
         self.configApplier.addBonding(bond, **opts)
@@ -105,7 +105,7 @@
         for slave in bond.slaves:
             slave.configure(**opts)
         self._addSourceRoute(bond)
-        ifup(bond.name, bond.ipConfig.async)
+        ifup(bond.name, bond.async)
         if self.unifiedPersistence:
             self.runningConfig.setBonding(
                 bond.name, {'options': bond.options,
@@ -158,7 +158,7 @@
         if nic.bond is None:
             if not netinfo.isVlanned(nic.name):
                 ifdown(nic.name)
-            ifup(nic.name, nic.ipConfig.async)
+            ifup(nic.name, nic.async)
 
     def removeBridge(self, bridge):
         DynamicSourceRoute.addInterfaceTracking(bridge)
@@ -608,13 +608,12 @@
         opts['hotplug'] = 'no'  # So that udev doesn't trigger an ifup
         if bridge.stp is not None:
             conf += 'STP=%s\n' % ('on' if bridge.stp else 'off')
-        ipconfig = bridge.ipConfig
         if not self.unifiedPersistence or ipconfig.defaultRoute:
             conf += 'ONBOOT=%s\n' % 'yes'
         else:
             conf += 'ONBOOT=%s\n' % 'no'
-        defaultRoute = ConfigWriter._toIfcfgFormat(ipconfig.defaultRoute)
-        ipconfig = ipconfig._replace(defaultRoute=defaultRoute)
+        defaultRoute = ConfigWriter._toIfcfgFormat(bridge.defaultRoute)
+        ipconfig = bridge.ipConfig._replace(defaultRoute=defaultRoute)
 
         if 'custom' in opts and 'bridge_opts' in opts['custom']:
             opts['bridging_opts'] = opts['custom']['bridge_opts']
@@ -630,9 +629,8 @@
             conf += 'ONBOOT=%s\n' % 'yes'
         else:
             conf += 'ONBOOT=%s\n' % 'no'
-        ipconfig = vlan.ipConfig
-        defaultRoute = ConfigWriter._toIfcfgFormat(ipconfig.defaultRoute)
-        ipconfig = ipconfig._replace(defaultRoute=defaultRoute)
+        defaultRoute = ConfigWriter._toIfcfgFormat(vlan.defaultRoute)
+        ipconfig = vlan.ipConfig._replace(defaultRoute=defaultRoute)
         self._createConfFile(conf, vlan.name, ipconfig, vlan.mtu, **opts)
 
     def addBonding(self, bond, **opts):
diff --git a/vdsm/network/configurators/iproute2.py b/vdsm/network/configurators/iproute2.py
index ed2dab5..860fc00 100644
--- a/vdsm/network/configurators/iproute2.py
+++ b/vdsm/network/configurators/iproute2.py
@@ -222,24 +222,23 @@
 class ConfigApplier(object):
 
     def _setIpConfig(self, iface):
-        ipConfig = iface.ipConfig
-        if ipConfig.ipaddr or ipConfig.ipv6addr:
+        if iface.ipaddr or iface.ipv6addr:
             self.removeIpConfig(iface)
-        if ipConfig.ipaddr:
-            ipwrapper.addrAdd(iface.name, ipConfig.ipaddr,
-                              ipConfig.netmask)
-            if ipConfig.gateway and ipConfig.defaultRoute:
-                ipwrapper.routeAdd(['default', 'via', ipConfig.gateway])
-        if ipConfig.ipv6addr:
-            ipv6addr, ipv6netmask = ipConfig.ipv6addr.split('/')
+        if iface.ipaddr:
+            ipwrapper.addrAdd(iface.name, iface.ipaddr,
+                              iface.netmask)
+            if iface.gateway and iface.defaultRoute:
+                ipwrapper.routeAdd(['default', 'via', iface.gateway])
+        if iface.ipv6addr:
+            ipv6addr, ipv6netmask = iface.ipv6addr.split('/')
             ipwrapper.addrAdd(iface.name, ipv6addr, ipv6netmask, family=6)
-            if ipConfig.ipv6gateway:
-                ipwrapper.routeAdd(['default', 'via', ipConfig.ipv6gateway],
+            if iface.ipv6gateway:
+                ipwrapper.routeAdd(['default', 'via', iface.ipv6gateway],
                                    dev=iface.name, family=6)
-        if ipConfig.ipv6autoconf is not None:
+        if iface.ipv6autoconf is not None:
             with open('/proc/sys/net/ipv6/conf/%s/autoconf' % iface.name,
                       'w') as ipv6_autoconf:
-                ipv6_autoconf.write('1' if ipConfig.ipv6autoconf else '0')
+                ipv6_autoconf.write('1' if iface.ipv6autoconf else '0')
 
     def removeIpConfig(self, iface):
         ipwrapper.addrFlush(iface.name)
@@ -252,9 +251,9 @@
 
     def ifup(self, iface):
         ipwrapper.linkSet(iface.name, ['up'])
-        if iface.ipConfig.bootproto == 'dhcp':
+        if iface.bootproto == 'dhcp':
             runDhclient(iface)
-        if iface.ipConfig.dhcpv6:
+        if iface.dhcpv6:
             runDhclient(iface, 6)
 
     def ifdown(self, iface):
diff --git a/vdsm/network/configurators/pyroute_two.py b/vdsm/network/configurators/pyroute_two.py
index e195714..318bc5b 100644
--- a/vdsm/network/configurators/pyroute_two.py
+++ b/vdsm/network/configurators/pyroute_two.py
@@ -56,25 +56,24 @@
         self.ip = IPDB()
 
     def _setIpConfig(self, iface):
-        ipConfig = iface.ipConfig
-        if ipConfig.ipaddr or ipConfig.ipv6addr:
+        if iface.ipaddr or iface.ipv6addr:
             self.removeIpConfig(iface)
-        if ipConfig.ipaddr:
+        if iface.ipaddr:
             with self.ip.interfaces[iface.name] as i:
-                i.add_ip('%s/%s' % (ipConfig.ipaddr, ipConfig.netmask))
-            if ipConfig.gateway and ipConfig.defaultRoute:
+                i.add_ip('%s/%s' % (iface.ipaddr, iface.netmask))
+            if iface.gateway and iface.defaultRoute:
                 self.ip.routes.add({'dst': 'default',
-                                    'gateway': ipConfig.gateway}).commit()
-        if ipConfig.ipv6addr:
+                                    'gateway': iface.gateway}).commit()
+        if iface.ipv6addr:
             with self.ip.interfaces[iface.name] as i:
-                i.add_ip(ipConfig.ipv6addr)
-            if ipConfig.ipv6gateway:
+                i.add_ip(iface.ipv6addr)
+            if iface.ipv6gateway:
                 self.ip.routes.add({'dst': 'default',
-                                    'gateway': ipConfig.ipv6gateway}).commit()
-        if ipConfig.ipv6autoconf is not None:
+                                    'gateway': iface.ipv6gateway}).commit()
+        if iface.ipv6autoconf is not None:
             with open('/proc/sys/net/ipv6/conf/%s/autoconf' % iface.name,
                       'w') as ipv6_autoconf:
-                ipv6_autoconf.write('1' if ipConfig.ipv6autoconf else '0')
+                ipv6_autoconf.write('1' if iface.ipv6autoconf else '0')
 
     def removeIpConfig(self, iface):
         ipwrapper.addrFlush(iface.name)
@@ -89,9 +88,9 @@
     def ifup(self, iface):
         with self.ip.interfaces[iface.name] as i:
             i.up()
-        if iface.ipConfig.bootproto == 'dhcp':
+        if iface.bootproto == 'dhcp':
             runDhclient(iface)
-        if iface.ipConfig.dhcpv6:
+        if iface.dhcpv6:
             runDhclient(iface, 6)
 
     def ifdown(self, iface):
diff --git a/vdsm/network/models.py b/vdsm/network/models.py
index 8567713..47cabc7 100644
--- a/vdsm/network/models.py
+++ b/vdsm/network/models.py
@@ -47,6 +47,9 @@
     def configure(self, **opts):
         raise NotImplementedError
 
+    def __getattr__(self, name):
+        return getattr(self.ipconfig, name)
+
     @property
     def ipConfig(self):
         try:
@@ -78,7 +81,7 @@
     def serving_default_route(self):
         device = self
         while device:
-            if device.ipConfig.defaultRoute:
+            if device.defaultRoute:
                 return True
             device = device.master
         return False
@@ -489,6 +492,18 @@
         self.ipv6autoconf = ipv6autoconf
         self.dhcpv6 = dhcpv6
 
+    def __getattr__(self, name):
+        if name not in self.ipConfig._fields:
+            raise AttributeError(name)
+        if name.startswith('ipv6'):
+            name = name[4:]
+            ip = self.inet6
+        else:
+            ip = self.inet4
+        if 'addr' in name:
+            return getattr(ip, 'address')
+        return getattr(ip, name)
+
     def __repr__(self):
         return 'IpConfig(%r, %r, %s, %s, %s)' % (self.inet4, self.inet6,
                                                  self.bootproto,
diff --git a/vdsm/network/sourceroute.py b/vdsm/network/sourceroute.py
index bf11162..88d0c87 100644
--- a/vdsm/network/sourceroute.py
+++ b/vdsm/network/sourceroute.py
@@ -93,7 +93,7 @@
 
     @staticmethod
     def addInterfaceTracking(device):
-        if device.ipConfig.bootproto == 'dhcp':
+        if device.bootproto == 'dhcp':
             open(DynamicSourceRoute.getTrackingFilePath(device.name), 'a').\
                 close()
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iecf268d557d642cb12a4b25d157ec059293a49c4
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ondřej Svoboda <osvoboda at redhat.com>


More information about the vdsm-patches mailing list