Kolla-on-XenServer: Add dom0's ip addresses into xenapi facts

This commit is to add dom0's IPs in the xenapi facts. So that
kolla-ansible can use the proper IP address for vxlan tunnel or
other places when require dom0's IPs.

Change-Id: Iede11b4a529ff1885b588bf01d7ff15dc6185092
This commit is contained in:
Jianghua Wang
2018-01-09 06:20:12 +00:00
parent 12065324bc
commit ebefaa7d0a
4 changed files with 75 additions and 2 deletions

View File

@@ -27,3 +27,32 @@ class CommonUtilFuncTestCase(base.TestCase):
self.assertEqual(hostname, 'Fake_host_name') self.assertEqual(hostname, 'Fake_host_name')
mock_client.ssh.assert_called_with('hostname') mock_client.ssh.assert_called_with('hostname')
def test_get_host_ipv4s(self):
mock_client = mock.Mock()
out = u'xenbr0 10.71.64.118/20\n'
out += 'xenapi 169.254.0.1/16\n'
err = ''
mock_client.ssh.return_value = (out, err)
ipv4s = common_function.get_host_ipv4s(mock_client)
expect = [
{
"address": "10.71.64.118",
"broadcast": "10.71.79.255",
"interface": "xenbr0",
"netmask": "255.255.240.0",
"network": "10.71.64.0"
},
{
"address": "169.254.0.1",
"broadcast": "169.254.255.255",
"interface": "xenapi",
"netmask": "255.255.0.0",
"network": "169.254.0.0"
}
]
self.assertEqual(ipv4s, expect)
mock_client.ssh.assert_called()

View File

@@ -20,21 +20,40 @@ from os_xenapi.utils import xenapi_facts
class XenapiFactsTestCase(base.TestCase): class XenapiFactsTestCase(base.TestCase):
@mock.patch.object(common_function, 'get_host_ipv4s')
@mock.patch.object(common_function, 'get_remote_hostname') @mock.patch.object(common_function, 'get_remote_hostname')
@mock.patch.object(himn, 'get_local_himn_eth') @mock.patch.object(himn, 'get_local_himn_eth')
@mock.patch.object(netifaces, 'ifaddresses') @mock.patch.object(netifaces, 'ifaddresses')
def test_get_facts(self, mock_ifaddr, mock_eth, mock_hostname): def test_get_facts(self, mock_ifaddr, mock_eth, mock_hostname, mock_ip):
mock_client = mock.Mock() mock_client = mock.Mock()
mock_client.ip = mock.sentinel.dom0_himn_ip mock_client.ip = mock.sentinel.dom0_himn_ip
mock_eth.return_value = 'eth3' mock_eth.return_value = 'eth3'
mock_ifaddr.return_value = {2: [{'netmask': u'255.255.0.0', mock_ifaddr.return_value = {2: [{'netmask': u'255.255.0.0',
'addr': u'169.254.0.2'}]} 'addr': u'169.254.0.2'}]}
mock_hostname.return_value = 'traya' mock_hostname.return_value = 'traya'
fake_ipv4s = [
{
"address": "10.71.64.118",
"broadcast": "10.71.79.255",
"interface": "xenbr0",
"netmask": "255.255.240.0",
"network": "10.71.64.0"
},
{
"address": "169.254.0.1",
"broadcast": "169.254.255.255",
"interface": "xenapi",
"netmask": "255.255.0.0",
"network": "169.254.0.0"
}
]
mock_ip.return_value = fake_ipv4s
ret_facts = xenapi_facts.get_xenapi_facts(mock_client) ret_facts = xenapi_facts.get_xenapi_facts(mock_client)
expect_facts = {"domu_himn_ip": "169.254.0.2", expect_facts = {"domu_himn_ip": "169.254.0.2",
"domu_himn_eth": "eth3", "domu_himn_eth": "eth3",
"dom0_hostname": "traya"} "dom0_hostname": "traya",
"dom0_ipv4s": fake_ipv4s}
self.assertEqual(ret_facts, expect_facts) self.assertEqual(ret_facts, expect_facts)
mock_eth.assert_called_with(mock.sentinel.dom0_himn_ip) mock_eth.assert_called_with(mock.sentinel.dom0_himn_ip)

View File

@@ -16,6 +16,7 @@
It contains the common functions used by XenAPI utils.""" It contains the common functions used by XenAPI utils."""
import ipaddress
import logging import logging
import netifaces import netifaces
import os import os
@@ -87,3 +88,25 @@ def get_remote_hostname(host_client):
out, _ = host_client.ssh('hostname') out, _ = host_client.ssh('hostname')
hostname = out.strip() hostname = out.strip()
return hostname return hostname
def get_host_ipv4s(host_client):
# Get host's IPs (v4 only) via the host_client connected to the host.
ipv4s = []
command = "ip -4 -o addr show scope global | awk '{print $2, $4}'"
out, _ = host_client.ssh(command)
for line in out.split('\n'):
line = line.strip()
if line:
interface, ipv4_address = line.split()
net_if = ipaddress.IPv4Interface(ipv4_address)
network = net_if.network
ipv4 = {}
ipv4['interface'] = interface
ipv4['address'], _ = ipv4_address.split('/')
ipv4['broadcast'] = str(network.broadcast_address)
ipv4['network'] = str(network.network_address)
ipv4['netmask'] = str(network.netmask)
ipv4s.append(ipv4)
return ipv4s

View File

@@ -41,6 +41,8 @@ def get_xenapi_facts(dom0_client):
# get dom0's hostname # get dom0's hostname
facts['dom0_hostname'] = common_function.get_remote_hostname(dom0_client) facts['dom0_hostname'] = common_function.get_remote_hostname(dom0_client)
# get dom0's IPs
facts['dom0_ipv4s'] = common_function.get_host_ipv4s(dom0_client)
# get domU's eth and ip which are connected to HIMN. # get domU's eth and ip which are connected to HIMN.
eth = himn.get_local_himn_eth(dom0_client.ip) eth = himn.get_local_himn_eth(dom0_client.ip)