summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-06-28 05:35:48 +0000
committerGerrit Code Review <review@openstack.org>2013-06-28 05:35:48 +0000
commit35029ec8d584be6eadfbe4ccfb179ac9e99acf03 (patch)
tree9da07f8667bcd4dfe8c916669f046928223d1cbe /nova/utils.py
parenta11a723ee16e325505851d32466e4615c7d60017 (diff)
parent7b97d363524fbd471289070557d1b8ffe9c06758 (diff)
downloadnova-35029ec8d584be6eadfbe4ccfb179ac9e99acf03.tar.gz
nova-35029ec8d584be6eadfbe4ccfb179ac9e99acf03.tar.xz
nova-35029ec8d584be6eadfbe4ccfb179ac9e99acf03.zip
Merge "Better default for my_ip if 8.8.8.8 is unreachable"
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py
index c2580271d..9976e2858 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -329,6 +329,62 @@ def last_octet(address):
return int(address.split('.')[-1])
+def get_my_ipv4_address():
+ """Run ip route/addr commands to figure out the best ipv4
+ """
+ LOCALHOST = '127.0.0.1'
+ try:
+ out = execute('ip', '-f', 'inet', '-o', 'route', 'show',
+ run_as_root=True)
+
+ # Find the default route
+ regex_default = ('default\s*via\s*'
+ '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
+ '\s*dev\s*(\w*)\s*')
+ default_routes = re.findall(regex_default, out[0])
+ if not default_routes:
+ return LOCALHOST
+ gateway, iface = default_routes[0]
+
+ # Find the right subnet for the gateway/interface for
+ # the default route
+ route = ('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})'
+ '\s*dev\s*(\w*)\s*')
+ for match in re.finditer(route, out[0]):
+ subnet = netaddr.IPNetwork(match.group(1) + "/" + match.group(2))
+ if (match.group(3) == iface and
+ netaddr.IPAddress(gateway) in subnet):
+ try:
+ return _get_ipv4_address_for_interface(iface)
+ except exception.NovaException:
+ pass
+ except Exception as ex:
+ LOG.error(_("Couldn't get IPv4 : %(ex)s") % {'ex': ex})
+ return LOCALHOST
+
+
+def _get_ipv4_address_for_interface(iface):
+ """Run ip addr show for an interface and grab its ipv4 addresses
+ """
+ try:
+ out = execute('ip', '-f', 'inet', '-o', 'addr', 'show', iface,
+ run_as_root=True)
+ regexp_address = re.compile('inet\s*'
+ '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
+ address = [m.group(1) for m in regexp_address.finditer(out[0])
+ if m.group(1) != '127.0.0.1']
+ if address:
+ return address[0]
+ else:
+ msg = _('IPv4 address is not found.: %s') % out[0]
+ raise exception.NovaException(msg)
+ except Exception as ex:
+ msg = _("Couldn't get IPv4 of %(interface)s"
+ " : %(ex)s") % {'interface': iface, 'ex': ex}
+ LOG.error(msg)
+ raise exception.NovaException(msg)
+
+
def get_my_linklocal(interface):
try:
if_str = execute('ip', '-f', 'inet6', '-o', 'addr', 'show', interface)