Log warning if CONF.my_ip is not found on system

Explicitly check if CONF.my_ip is present in any of the network
interfaces then log a warning to alert the operators.

Code borrowed from swift:
http://git.openstack.org/cgit/openstack/swift/tree/swift/common/utils.py#n1545

Closes-Bug: #1419002
Change-Id: I9db5a2f7c5395d1d34ab2eb6d169c62fc4753f73
This commit is contained in:
Davanum Srinivas 2015-02-08 21:06:57 -05:00 committed by Dan Smith
parent 6c90c96d7c
commit b838ca28e0
3 changed files with 43 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import itertools
import string
import traceback
import netifaces
from oslo_config import cfg
from oslo_utils import encodeutils
@ -459,6 +460,31 @@ def get_reboot_type(task_state, current_power_state):
return reboot_type
def get_machine_ips():
"""Get the machine's ip addresses
:returns: list of Strings of ip addresses
"""
addresses = []
for interface in netifaces.interfaces():
iface_data = netifaces.ifaddresses(interface)
for family in iface_data:
if family not in (netifaces.AF_INET, netifaces.AF_INET6):
continue
try:
for address in iface_data[family]:
addr = address['addr']
# If we have an ipv6 address remove the
# %ether_interface at the end
if family == netifaces.AF_INET6:
addr = addr.split('%')[0]
addresses.append(addr)
except ValueError:
pass
return addresses
class EventReporter(object):
"""Context manager to report instance action events."""

View File

@ -8105,6 +8105,18 @@ class LibvirtConnTestCase(test.TestCase):
ip = drvr.get_host_ip_addr()
self.assertEqual(ip, CONF.my_ip)
@mock.patch.object(libvirt_driver.LOG, 'warn')
@mock.patch('nova.compute.utils.get_machine_ips')
def test_get_host_ip_addr_failure(self, mock_ips, mock_log):
mock_ips.return_value = ['8.8.8.8', '75.75.75.75']
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
drvr.get_host_ip_addr()
mock_log.assert_called_once_with(u'my_ip address (%(my_ip)s) was '
u'not found on any of the '
u'interfaces: %(ifaces)s',
{'ifaces': '8.8.8.8, 75.75.75.75',
'my_ip': mock.ANY})
def test_conn_event_handler(self):
self.mox.UnsetStubs()
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)

View File

@ -2439,6 +2439,11 @@ class LibvirtDriver(driver.ComputeDriver):
@staticmethod
def get_host_ip_addr():
ips = compute_utils.get_machine_ips()
if CONF.my_ip not in ips:
LOG.warn(_LW('my_ip address (%(my_ip)s) was not found on '
'any of the interfaces: %(ifaces)s'),
{'my_ip': CONF.my_ip, 'ifaces': ", ".join(ips)})
return CONF.my_ip
def get_vnc_console(self, context, instance):