Add a new command to get facts for XenAPI
This commit is to add a new command - get_xenapi_facts which will gather facts for XenAPI. When deploy OpenStack on XenServer, the facts can be used by deploy approach(e.g. kolla-ansible). Change-Id: Ia606d6ddc651cfa0b75fa34d8f14bc156d9dbe27
This commit is contained in:
parent
7b6aa0ad9b
commit
a69cf17f67
0
os_xenapi/cmd/__init__.py
Normal file
0
os_xenapi/cmd/__init__.py
Normal file
75
os_xenapi/cmd/get_xenapi_facts.py
Normal file
75
os_xenapi/cmd/get_xenapi_facts.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
# Copyright 2017 Citrix Systems
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
"""Command for getting facts about XenAPI.
|
||||||
|
|
||||||
|
This command will return the facts about XenAPI in a json formatted dict.
|
||||||
|
e.g.
|
||||||
|
'
|
||||||
|
{
|
||||||
|
"DOM0_HOST_NAME": u"traya",
|
||||||
|
"HIMN_LOCAL_IP": u"169.254.0.2",
|
||||||
|
"HIMN_LOCAL_ETH": u"eth3"
|
||||||
|
...}
|
||||||
|
'
|
||||||
|
"""
|
||||||
|
|
||||||
|
import getopt
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import os_xenapi.utils.xenapi_facts as xenapi_facts
|
||||||
|
|
||||||
|
|
||||||
|
USAGE_MSG = "Run the following command to get facts for XenAPI:\n"
|
||||||
|
USAGE_MSG += sys.argv[0]
|
||||||
|
USAGE_MSG += " [-i|--himn-ip=] <XenServer's HIMN IP>"
|
||||||
|
USAGE_MSG += " [-u|--user-name=] <user-name>"
|
||||||
|
USAGE_MSG += " [-p|--passwd=] <passwd>\n\n"
|
||||||
|
|
||||||
|
VALID_OPS_SHORT_STR = "i:p:u:"
|
||||||
|
VALID_OPS_LONG_LST = ["himn-ip=", "passwd=", "user-name="]
|
||||||
|
|
||||||
|
|
||||||
|
def exit_with_usage():
|
||||||
|
sys.stderr.write(USAGE_MSG)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
try:
|
||||||
|
opts, args = getopt.getopt(argv,
|
||||||
|
VALID_OPS_SHORT_STR,
|
||||||
|
VALID_OPS_LONG_LST)
|
||||||
|
except getopt.GetoptError:
|
||||||
|
return exit_with_usage()
|
||||||
|
|
||||||
|
if len(opts) != len(VALID_OPS_LONG_LST):
|
||||||
|
return exit_with_usage()
|
||||||
|
|
||||||
|
# Get the values from input parameters.
|
||||||
|
for opt, arg in opts:
|
||||||
|
if opt in ("-i", "--himn-ip"):
|
||||||
|
himn_ip = arg
|
||||||
|
elif opt in ("-p", "--passwd"):
|
||||||
|
passwd = arg
|
||||||
|
elif opt in ("-u", "--user-name"):
|
||||||
|
user_name = arg
|
||||||
|
|
||||||
|
return xenapi_facts.get_facts(himn_ip, user_name, passwd)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
exit_with_usage()
|
||||||
|
|
||||||
|
sys.exit(main(sys.argv[1:]))
|
0
os_xenapi/tests/cmd/__init__.py
Normal file
0
os_xenapi/tests/cmd/__init__.py
Normal file
51
os_xenapi/tests/cmd/test_xenapi_facts.py
Normal file
51
os_xenapi/tests/cmd/test_xenapi_facts.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
import mock
|
||||||
|
|
||||||
|
from os_xenapi.cmd import get_xenapi_facts
|
||||||
|
from os_xenapi.tests import base
|
||||||
|
from os_xenapi.utils import xenapi_facts
|
||||||
|
|
||||||
|
|
||||||
|
class GetXenapiFactsTestCase(base.TestCase):
|
||||||
|
@mock.patch.object(xenapi_facts, 'get_facts')
|
||||||
|
def test_get_xenapi_facts(self, mock_get):
|
||||||
|
argv = ['-i', '169.254.0.1', '-u', 'root', '-p', 'passwd']
|
||||||
|
|
||||||
|
get_xenapi_facts.main(argv)
|
||||||
|
|
||||||
|
mock_get.assert_called_with('169.254.0.1', 'root', 'passwd')
|
||||||
|
|
||||||
|
@mock.patch.object(get_xenapi_facts, 'exit_with_usage')
|
||||||
|
@mock.patch.object(xenapi_facts, 'get_facts')
|
||||||
|
def test_get_xenapi_facts_wrong_opt(self, mock_get, mock_usage):
|
||||||
|
# Verify if it will exit with prompting usage if pass in
|
||||||
|
# wrong opts.
|
||||||
|
argv = ['-i', '169.254.0.1', '-u', 'root', '-v', 'invalid_opt']
|
||||||
|
|
||||||
|
get_xenapi_facts.main(argv)
|
||||||
|
|
||||||
|
mock_usage.assert_called_with()
|
||||||
|
mock_get.assert_not_called()
|
||||||
|
|
||||||
|
@mock.patch.object(get_xenapi_facts, 'exit_with_usage')
|
||||||
|
@mock.patch.object(xenapi_facts, 'get_facts')
|
||||||
|
def test_get_xenapi_facts_lack_opts(self, mock_get, mock_usage):
|
||||||
|
# Verify if it will exit with prompting usage if not giving
|
||||||
|
# all required opts.
|
||||||
|
argv = ['-i', '169.254.0.1']
|
||||||
|
|
||||||
|
get_xenapi_facts.main(argv)
|
||||||
|
|
||||||
|
mock_usage.assert_called_with()
|
||||||
|
mock_get.assert_not_called()
|
49
os_xenapi/tests/utils/test_xenapi_facts.py
Normal file
49
os_xenapi/tests/utils/test_xenapi_facts.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
import json
|
||||||
|
import mock
|
||||||
|
import netifaces
|
||||||
|
|
||||||
|
from os_xenapi.tests import base
|
||||||
|
from os_xenapi.utils import himn
|
||||||
|
from os_xenapi.utils import xenapi_facts
|
||||||
|
|
||||||
|
|
||||||
|
class XenapiFactsTestCase(base.TestCase):
|
||||||
|
def test_hostname(self):
|
||||||
|
mock_client = mock.Mock()
|
||||||
|
out = 'fake_hostname\r\n'
|
||||||
|
err = ''
|
||||||
|
mock_client.ssh.return_value = (out, err)
|
||||||
|
|
||||||
|
hostname = xenapi_facts.get_hostname(mock_client)
|
||||||
|
|
||||||
|
mock_client.ssh.assert_called_with('hostname')
|
||||||
|
self.assertEqual(hostname, 'fake_hostname')
|
||||||
|
|
||||||
|
@mock.patch.object(xenapi_facts, 'get_hostname')
|
||||||
|
@mock.patch.object(himn, 'get_local_himn_eth')
|
||||||
|
@mock.patch.object(netifaces, 'ifaddresses')
|
||||||
|
def test_get_facts(self, mock_ifaddr, mock_eth, mock_hostname):
|
||||||
|
xenapi_facts.sshclient.SSHClient = mock.Mock
|
||||||
|
mock_eth.return_value = 'eth3'
|
||||||
|
mock_ifaddr.return_value = {2: [{'netmask': u'255.255.0.0',
|
||||||
|
'addr': u'169.254.0.2'}]}
|
||||||
|
mock_hostname.return_value = 'traya'
|
||||||
|
|
||||||
|
facts_json = xenapi_facts.get_facts('169.254.0.1', 'root', 'passwd')
|
||||||
|
|
||||||
|
expect_facts = {"local_himn_ip": "169.254.0.2",
|
||||||
|
"local_himn_eth": "eth3",
|
||||||
|
"hostname": "traya"}
|
||||||
|
self.assertEqual(json.loads(facts_json), expect_facts)
|
@ -72,6 +72,11 @@ def execute(*cmd, **kwargs):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def get_eth_ipaddr(eth):
|
||||||
|
# return eth's IP address.
|
||||||
|
return netifaces.ifaddresses(eth).get(netifaces.AF_INET)[0]['addr']
|
||||||
|
|
||||||
|
|
||||||
def get_eth_mac(eth):
|
def get_eth_mac(eth):
|
||||||
# Get eth's mac address.
|
# Get eth's mac address.
|
||||||
return netifaces.ifaddresses(eth).get(netifaces.AF_LINK)[0]['addr']
|
return netifaces.ifaddresses(eth).get(netifaces.AF_LINK)[0]['addr']
|
||||||
|
52
os_xenapi/utils/xenapi_facts.py
Normal file
52
os_xenapi/utils/xenapi_facts.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# Copyright 2017 Citrix Systems
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
"""Utilies for XenAPI facts gathering
|
||||||
|
|
||||||
|
It contains utilies to gather XenAPI relative facts."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from os_xenapi.utils import common_function
|
||||||
|
from os_xenapi.utils import himn
|
||||||
|
from os_xenapi.utils import sshclient
|
||||||
|
|
||||||
|
|
||||||
|
def get_hostname(host_client):
|
||||||
|
out, _ = host_client.ssh('hostname')
|
||||||
|
hostname = out.strip()
|
||||||
|
return hostname
|
||||||
|
|
||||||
|
|
||||||
|
def get_facts(dom0_himn_ip, user_name, passwd):
|
||||||
|
facts = {}
|
||||||
|
|
||||||
|
dom0_client = sshclient.SSHClient(dom0_himn_ip, user_name, passwd)
|
||||||
|
|
||||||
|
facts['hostname'] = get_hostname(dom0_client)
|
||||||
|
|
||||||
|
# get local HIMN info
|
||||||
|
eth = himn.get_local_himn_eth(dom0_himn_ip)
|
||||||
|
ip_addr = common_function.get_eth_ipaddr(eth)
|
||||||
|
facts['local_himn_eth'] = eth
|
||||||
|
facts['local_himn_ip'] = ip_addr
|
||||||
|
|
||||||
|
return json.dumps(facts)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
dom0_himn_ip, user_name, passwd = sys.argv[1:]
|
||||||
|
facts_json = get_facts(dom0_himn_ip, user_name, passwd)
|
||||||
|
print('get_facts returns:\n %s' % facts_json)
|
Loading…
Reference in New Issue
Block a user