Merge "Jira: DAISY-425 modify host ipmi check"
This commit is contained in:
commit
e749f7d039
|
@ -2448,54 +2448,67 @@ class Controller(controller.BaseController):
|
|||
|
||||
return {'host_meta': host_meta}
|
||||
|
||||
def _host_ipmi_check(self, host_id, host_meta):
|
||||
ipmi_check_result = {}
|
||||
if host_meta['os_status'] == 'active':
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'active host do not need ipmi check'
|
||||
LOG.info('active host %s do not need ipmi '
|
||||
'check' % host_id)
|
||||
else:
|
||||
ipmi_ip = host_meta.get('ipmi_addr', None)
|
||||
ipmi_user = host_meta.get('ipmi_user', None)
|
||||
ipmi_password = host_meta.get('ipmi_passwd', None)
|
||||
ipmi_config = [{'ipmi address': ipmi_ip},
|
||||
{'ipmi user': ipmi_user}
|
||||
]
|
||||
for i in ipmi_config:
|
||||
if not i.values()[0]:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
"No %s configed for host %s, please " \
|
||||
"check" % (i.keys()[0], host_id)
|
||||
LOG.info('No %s configed for host %s' %
|
||||
(i.keys()[0], host_id))
|
||||
return ipmi_check_result
|
||||
cmd = 'ipmitool -I lanplus -H %s -U %s -P %s chassis ' \
|
||||
'power status' % (ipmi_ip, ipmi_user, ipmi_password)
|
||||
obj = subprocess.Popen(cmd,
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
(stdoutput, erroutput) = obj.communicate()
|
||||
if 'Chassis Power is on' in stdoutput:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'ipmi check successfully'
|
||||
LOG.info('host %s ipmi check '
|
||||
'successfully' % host_id)
|
||||
elif 'Unable to get Chassis Power Status' in erroutput:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'ipmi check failed'
|
||||
LOG.info('host %s ipmi check failed' % host_id)
|
||||
return ipmi_check_result
|
||||
|
||||
@utils.mutating
|
||||
def host_check(self, req, host_meta):
|
||||
host_id = host_meta['id']
|
||||
orig_host_meta = self.get_host_meta_or_404(req, host_id)
|
||||
check_item = host_meta['check_item']
|
||||
if check_item == 'ipmi':
|
||||
ipmi_check_result = {}
|
||||
if orig_host_meta.get('hwm_id'):
|
||||
daisy_cmn.check_discover_state_with_hwm(req, orig_host_meta)
|
||||
else:
|
||||
daisy_cmn.check_discover_state_with_no_hwm(req, orig_host_meta)
|
||||
if orig_host_meta.get('discover_state') \
|
||||
and 'SSH' in orig_host_meta['discover_state']:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'host discovered by ssh do not need ipmi check'
|
||||
elif orig_host_meta.get('discover_state') \
|
||||
and 'PXE' in orig_host_meta['discover_state']:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'host discovered by hwm do not need ipmi check'
|
||||
elif orig_host_meta['os_status'] == 'active':
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'active host do not need ipmi check'
|
||||
else:
|
||||
ipmi_ip = orig_host_meta.get('ipmi_addr', None)
|
||||
ipmi_user = orig_host_meta.get('ipmi_user', None)
|
||||
ipmi_password = orig_host_meta.get('ipmi_passwd', None)
|
||||
ipmi_config = [{'ipmi address': ipmi_ip},
|
||||
{'ipmi user': ipmi_user}
|
||||
]
|
||||
for i in ipmi_config:
|
||||
if not i.values()[0]:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
"No %s configed for host %s, please " \
|
||||
"check" % (i.keys()[0], orig_host_meta['name'])
|
||||
return {'check_result': ipmi_check_result}
|
||||
cmd = 'ipmitool -I lanplus -H %s -U %s -P %s chassis ' \
|
||||
'power status' % (ipmi_ip, ipmi_user, ipmi_password)
|
||||
obj = subprocess.Popen(cmd,
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
(stdoutput, erroutput) = obj.communicate()
|
||||
if 'Chassis Power is on' in stdoutput:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'ipmi check successfully'
|
||||
elif 'Unable to get Chassis Power Status' in erroutput:
|
||||
ipmi_check_result['ipmi_check_result'] = \
|
||||
'ipmi check failed'
|
||||
path = os.path.join(os.path.abspath(os.path.dirname(
|
||||
os.path.realpath(__file__))), 'ext')
|
||||
for root, dirs, names in os.walk(path):
|
||||
filename = 'router.py'
|
||||
if filename in names:
|
||||
ext_name = root.split(path)[1].strip('/')
|
||||
ext_func = "%s.api.hosts" % ext_name
|
||||
extension = importutils.import_module(
|
||||
'daisy.api.v1.ext.%s' % ext_func)
|
||||
if 'check_hwm_host_with_ipmi' in dir(extension):
|
||||
ipmi_check_result = extension.check_hwm_host_with_ipmi(
|
||||
host_id, orig_host_meta)
|
||||
if ipmi_check_result:
|
||||
return {'check_result': ipmi_check_result}
|
||||
ipmi_check_result = self._host_ipmi_check(host_id, orig_host_meta)
|
||||
return {'check_result': ipmi_check_result}
|
||||
|
||||
|
||||
|
|
|
@ -1415,164 +1415,100 @@ class TestHostsApiConfig(test.TestCase):
|
|||
self.controller._verify_host_cluster, req,
|
||||
"840b92ab-7e79-4a7d-be0a-5e735e0a836e",
|
||||
orig_host_meta, host_meta)
|
||||
"""
|
||||
@mock.patch('logging.Logger')
|
||||
@mock.patch("daisy.api.v1.hosts.Controller.get_host_meta_or_404")
|
||||
def test_host_check_ipmi_with_hwm_discovered_host(self,
|
||||
mock_get_host,
|
||||
mock_log):
|
||||
req = webob.Request.blank('/')
|
||||
req.context = RequestContext(is_admin=True,
|
||||
user='fake user',
|
||||
tenant='fake tenant')
|
||||
host = {'hwm_id': '1',
|
||||
'id': '1',
|
||||
'name': 'host_1'}
|
||||
mock_get_host.return_value = host
|
||||
mock_log.side_effect = self._log_handler
|
||||
self.assertEqual({
|
||||
'check_result': {
|
||||
'ipmi_check_result':
|
||||
'host discovered by hwm do not need ipmi check'}},
|
||||
self.controller.host_check(req, self.host_meta))
|
||||
|
||||
@mock.patch('logging.Logger')
|
||||
@mock.patch("daisy.api.v1.hosts.Controller.get_host_meta_or_404")
|
||||
def test_host_check_ipmi_with_active_host(self, mock_get_host, mock_log):
|
||||
req = webob.Request.blank('/')
|
||||
req.context = RequestContext(is_admin=True,
|
||||
user='fake user',
|
||||
tenant='fake tenant')
|
||||
def test_host_check_ipmi_with_active_host(self, mock_log):
|
||||
host_id = '1'
|
||||
host = {'os_status': 'active',
|
||||
'id': '1',
|
||||
'name': 'host_1'}
|
||||
mock_get_host.return_value = host
|
||||
mock_log.side_effect = self._log_handler
|
||||
self.assertEqual({
|
||||
'check_result': {
|
||||
'ipmi_check_result':
|
||||
'active host do not need ipmi check'}},
|
||||
self.controller.host_check(req, self.host_meta))
|
||||
'ipmi_check_result': 'active host do not need ipmi check'},
|
||||
self.controller._host_ipmi_check(host_id, host))
|
||||
|
||||
@mock.patch('logging.Logger')
|
||||
@mock.patch("daisy.api.v1.hosts.Controller.get_host_meta_or_404")
|
||||
def test_host_check_ipmi_with_no_ipmi_addr(self, mock_get_host, mock_log):
|
||||
req = webob.Request.blank('/')
|
||||
req.context = RequestContext(is_admin=True,
|
||||
user='fake user',
|
||||
tenant='fake tenant')
|
||||
def test_host_check_ipmi_with_no_ipmi_addr(self, mock_log):
|
||||
host_id = '1'
|
||||
host = {'id': '1',
|
||||
'name':'test',
|
||||
'name': 'test',
|
||||
'os_status': 'init',
|
||||
'ipmi_addr': None,
|
||||
'ipmi_user': 'zteroot',
|
||||
'ipmi_passwd': 'superuser'}
|
||||
mock_get_host.return_value = host
|
||||
mock_log.side_effect = self._log_handler
|
||||
self.assertEqual({
|
||||
'check_result': {
|
||||
'ipmi_check_result': "No ipmi address configed for "
|
||||
"host 1, please check"}},
|
||||
self.controller.host_check(req, self.host_meta))
|
||||
self.assertEqual({'ipmi_check_result': "No ipmi address "
|
||||
"configed for host 1, "
|
||||
"please check"},
|
||||
self.controller._host_ipmi_check(host_id, host))
|
||||
|
||||
@mock.patch('logging.Logger')
|
||||
@mock.patch("daisy.api.v1.hosts.Controller.get_host_meta_or_404")
|
||||
def test_host_check_ipmi_with_no_ipmi_user(self, mock_get_host, mock_log):
|
||||
req = webob.Request.blank('/')
|
||||
req.context = RequestContext(is_admin=True,
|
||||
user='fake user',
|
||||
tenant='fake tenant')
|
||||
def test_host_check_ipmi_with_no_ipmi_user(self, mock_log):
|
||||
host_id = '1'
|
||||
host = {'id': '1',
|
||||
'name': 'test',
|
||||
'os_status': 'init',
|
||||
'ipmi_addr': '192.168.1.2',
|
||||
'ipmi_user': None,
|
||||
'ipmi_passwd': 'superuser'}
|
||||
mock_get_host.return_value = host
|
||||
mock_log.side_effect = self._log_handler
|
||||
self.assertEqual({
|
||||
'check_result': {
|
||||
'ipmi_check_result': "No ipmi user configed for host "
|
||||
"1, please check"}},
|
||||
self.controller.host_check(req, self.host_meta))
|
||||
self.assertEqual({'ipmi_check_result': "No ipmi user configed "
|
||||
"for host 1, please check"},
|
||||
self.controller._host_ipmi_check(host_id, host))
|
||||
|
||||
@mock.patch('logging.Logger')
|
||||
@mock.patch("daisy.api.v1.hosts.Controller.get_host_meta_or_404")
|
||||
@mock.patch('subprocess.Popen.communicate')
|
||||
def test_host_check_ipmi_with_no_ipmi_passwd(self,
|
||||
mock_communicate,
|
||||
mock_get_host,
|
||||
mock_log):
|
||||
req = webob.Request.blank('/')
|
||||
req.context = RequestContext(is_admin=True,
|
||||
user='fake user',
|
||||
tenant='fake tenant')
|
||||
host_id = '1'
|
||||
host = {'id': '1',
|
||||
'name': 'test',
|
||||
'os_status': 'init',
|
||||
'ipmi_addr': '192.168.1.2',
|
||||
'ipmi_user': 'zteroot',
|
||||
'ipmi_passwd': None}
|
||||
mock_get_host.return_value = host
|
||||
mock_log.side_effect = self._log_handler
|
||||
mock_communicate.return_value = \
|
||||
('', 'Unable to get Chassis Power Status')
|
||||
self.assertEqual({
|
||||
'check_result': {
|
||||
'ipmi_check_result': 'ipmi check failed'}},
|
||||
self.controller.host_check(req, self.host_meta))
|
||||
self.assertEqual({'ipmi_check_result': 'ipmi check failed'},
|
||||
self.controller._host_ipmi_check(host_id, host))
|
||||
|
||||
@mock.patch('logging.Logger')
|
||||
@mock.patch("daisy.api.v1.hosts.Controller.get_host_meta_or_404")
|
||||
@mock.patch('subprocess.Popen.communicate')
|
||||
def test_host_check_ipmi_with_correct_ipmi_parameters(self,
|
||||
mock_communicate,
|
||||
mock_get_host,
|
||||
mock_log):
|
||||
req = webob.Request.blank('/')
|
||||
req.context = RequestContext(is_admin=True,
|
||||
user='fake user',
|
||||
tenant='fake tenant')
|
||||
host_id = '1'
|
||||
host = {'id': '1',
|
||||
'name': 'host_1',
|
||||
'os_status': 'init',
|
||||
'ipmi_addr': '192.168.1.2',
|
||||
'ipmi_user': 'zteroot',
|
||||
'ipmi_passwd': 'superuser'}
|
||||
mock_get_host.return_value = host
|
||||
mock_log.side_effect = self._log_handler
|
||||
mock_communicate.return_value = ('Chassis Power is on', '')
|
||||
self.assertEqual({
|
||||
'check_result': {
|
||||
'ipmi_check_result': 'ipmi check successfully'}},
|
||||
self.controller.host_check(req, self.host_meta))
|
||||
self.assertEqual({'ipmi_check_result': 'ipmi check successfully'},
|
||||
self.controller._host_ipmi_check(host_id, host))
|
||||
|
||||
@mock.patch('logging.Logger')
|
||||
@mock.patch("daisy.api.v1.hosts.Controller.get_host_meta_or_404")
|
||||
@mock.patch('subprocess.Popen.communicate')
|
||||
def test_host_check_ipmi_with_error_ipmi_parameters(self,
|
||||
mock_communicate,
|
||||
mock_get_host,
|
||||
mock_log):
|
||||
req = webob.Request.blank('/')
|
||||
req.context = RequestContext(is_admin=True,
|
||||
user='fake user',
|
||||
tenant='fake tenant')
|
||||
host_id = '1'
|
||||
host = {'id': '1',
|
||||
'os_status': 'init',
|
||||
'name': 'host_1',
|
||||
'ipmi_addr': '192.168.1.2',
|
||||
'ipmi_user': 'zteroot',
|
||||
'ipmi_passwd': 'superuser'}
|
||||
mock_get_host.return_value = host
|
||||
mock_log.side_effect = self._log_handler
|
||||
mock_communicate.return_value = \
|
||||
('', 'Unable to get Chassis Power Status')
|
||||
self.assertEqual({
|
||||
'check_result': {
|
||||
'ipmi_check_result': 'ipmi check failed'}},
|
||||
self.controller.host_check(req, self.host_meta))
|
||||
"""
|
||||
self.assertEqual({'ipmi_check_result': 'ipmi check failed'},
|
||||
self.controller._host_ipmi_check(host_id, host))
|
||||
|
||||
@mock.patch('daisy.registry.client.v1.api.update_host_metadata')
|
||||
@mock.patch('daisy.registry.client.v1.api.get_roles_detail')
|
||||
@mock.patch('daisy.registry.client.v1.api.get_clusters_detail')
|
||||
|
|
|
@ -52,6 +52,8 @@ CREATE_PARAMS = ('id', 'name', 'description', 'resource_type', 'dmi_uuid',
|
|||
'memory', 'disks', 'devices', 'pci', 'version_patch_id',
|
||||
'tecs_version_id')
|
||||
|
||||
CHECK_PARAMS = ('id', 'check_item')
|
||||
|
||||
DEFAULT_PAGE_SIZE = 200
|
||||
|
||||
SORT_DIR_VALUES = ('asc', 'desc')
|
||||
|
@ -471,3 +473,15 @@ class HostManager(base.ManagerWithFind):
|
|||
return_request_id.append(resp.headers.get(OS_REQ_ID_HDR, None))
|
||||
# return Host(self, meta)
|
||||
return Host(self, self._format_host_meta_for_user(body['host']))
|
||||
|
||||
def host_check(self, **kwargs):
|
||||
hdrs = {}
|
||||
fields = {}
|
||||
for field in kwargs:
|
||||
if field in CHECK_PARAMS:
|
||||
fields[field] = kwargs[field]
|
||||
elif field == 'return_req_id':
|
||||
continue
|
||||
hdrs.update(self._host_meta_to_headers(fields))
|
||||
resp, body = self.client.post('/v1/check', headers=None, data=hdrs)
|
||||
return Host(self, self._format_host_meta_for_user(body['host']))
|
||||
|
|
Loading…
Reference in New Issue