From: Zack Cerza Date: Wed, 24 Feb 2016 17:30:06 +0000 (-0700) Subject: Add Remote.{ip_address, interface, cidr} X-Git-Tag: 1.1.0~671^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c68ae249f304b83e4d4223baf2ca233b1c936beb;p=teuthology.git Add Remote.{ip_address, interface, cidr} Signed-off-by: Zack Cerza --- diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index 8e561f166..668ba56ff 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -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'):