]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Add Remote.{ip_address, interface, cidr}
authorZack Cerza <zack@redhat.com>
Wed, 24 Feb 2016 17:30:06 +0000 (10:30 -0700)
committerZack Cerza <zack@redhat.com>
Fri, 26 Feb 2016 17:01:45 +0000 (10:01 -0700)
Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/orchestra/remote.py

index 8e561f166e1fb9d89f6e69526d0f708a02d6af39..668ba56ff8493931ab8cf30fd8a11b25f3fe566a 100644 (file)
@@ -14,6 +14,7 @@ from teuthology import lockstatus as ls
 import os
 import pwd
 import tempfile
+import netaddr
 
 try:
     import libvirt
@@ -92,6 +93,45 @@ class Remote(object):
             log.debug(e)
             return False
 
+    @property
+    def ip_address(self):
+        return self.ssh.get_transport().getpeername()[0]
+
+    @property
+    def interface(self):
+        """
+        The interface used by the current SSH connection
+        """
+        if not hasattr(self, '_interface'):
+            self._set_iface_and_cidr()
+        return self._interface
+
+    @property
+    def cidr(self):
+        """
+        The network (in CIDR notation) used by the remote's SSH connection
+        """
+        if not hasattr(self, '_cidr'):
+            self._set_iface_and_cidr()
+        return self._cidr
+
+    def _set_iface_and_cidr(self):
+        proc = self.run(
+            args=['PATH=/sbin:/usr/sbin', 'ip', 'addr', 'show'],
+            stdout=StringIO(),
+        )
+        proc.wait()
+        regexp = 'inet.? %s' % self.ip_address
+        proc.stdout.seek(0)
+        for line in proc.stdout.readlines():
+            line = line.strip()
+            if re.match(regexp, line):
+                items = line.split()
+                self._interface = items[-1]
+                self._cidr = str(netaddr.IPNetwork(items[1]).cidr)
+                return
+        raise RuntimeError("Could not determine interface/CIDR!")
+
     @property
     def hostname(self):
         if not hasattr(self, '_hostname'):