Use XML to get node list

The current list_nodes command tries to parse the output of
"crm node list" to get a list of nodes. But the output is messy if
remote nodes are present so use "crm node status" to get properly
formatted XML which can be reliably parsed.

Change-Id: Iea7ef3dca194e7440dc2cde2624d07e990006685
This commit is contained in:
Liam Young 2019-03-27 18:25:44 +00:00
parent f3873fe67f
commit 6e3cec799f
2 changed files with 23 additions and 7 deletions

View File

@ -103,14 +103,12 @@ def crm_res_running(opt_name):
def list_nodes():
cmd = ['crm', 'node', 'list']
"""List member nodes."""
cmd = ['crm', 'node', 'status']
out = subprocess.check_output(cmd).decode('utf-8')
nodes = []
for line in str(out).split('\n'):
if line != '':
nodes.append(line.split(':')[0])
return nodes
tree = etree.fromstring(out)
nodes = [n.attrib['uname'] for n in tree.iter('node')]
return sorted(nodes)
def _maas_ipmi_stonith_resource(node, power_params):

View File

@ -74,6 +74,14 @@ CRM_CONFIGURE_SHOW_XML_MAINT_MODE_TRUE = '''<?xml version="1.0" ?>
''' # noqa
CRM_NODE_STATUS_XML = b'''
<nodes>
<node id="1000" uname="juju-982848-zaza-ce47c58f6c88-10"/>
<node id="1001" uname="juju-982848-zaza-ce47c58f6c88-9"/>
<node id="1002" uname="juju-982848-zaza-ce47c58f6c88-11"/>
</nodes>
'''
class TestPcmk(unittest.TestCase):
def setUp(self):
@ -226,3 +234,13 @@ class TestPcmk(unittest.TestCase):
r = pcmk.resource_checksum('res_test', 'IPaddr2',
'params ip=1.2.3.4 cidr_netmask=255.0.0.0')
self.assertEqual(r, 'ef395293b1b7c29c5bf1c99774f75cf4')
@mock.patch('subprocess.check_output', return_value=CRM_NODE_STATUS_XML)
def test_list_nodes(self, mock_check_output):
self.assertSequenceEqual(
pcmk.list_nodes(),
[
'juju-982848-zaza-ce47c58f6c88-10',
'juju-982848-zaza-ce47c58f6c88-11',
'juju-982848-zaza-ce47c58f6c88-9'])
mock_check_output.assert_called_once_with(['crm', 'node', 'status'])