Merge "Drop the use of 'crm node status' on jammy."
This commit is contained in:
commit
cf1c3eeeb3
|
@ -29,6 +29,10 @@ from charmhelpers.core.hookenv import (
|
|||
DEBUG,
|
||||
WARNING,
|
||||
)
|
||||
from charmhelpers.core.host import (
|
||||
get_distrib_codename,
|
||||
CompareHostReleases,
|
||||
)
|
||||
|
||||
|
||||
class ServicesNotUp(Exception):
|
||||
|
@ -183,10 +187,18 @@ def crm_res_running_on_node(resource, node):
|
|||
|
||||
def list_nodes():
|
||||
"""List member nodes."""
|
||||
cmd = ['crm', 'node', 'status']
|
||||
out = subprocess.check_output(cmd).decode('utf-8')
|
||||
tree = etree.fromstring(out)
|
||||
nodes = [n.attrib['uname'] for n in tree.iter('node')]
|
||||
nodes = []
|
||||
if CompareHostReleases(get_distrib_codename()) >= 'jammy':
|
||||
cmd = ['crm', 'node', 'show']
|
||||
out = subprocess.check_output(cmd).decode('utf-8')
|
||||
for line in out.strip().split('\n'):
|
||||
nodes.append(line.split('(')[0])
|
||||
else:
|
||||
cmd = ['crm', 'node', 'status']
|
||||
out = subprocess.check_output(cmd).decode('utf-8')
|
||||
tree = etree.fromstring(out)
|
||||
nodes = [n.attrib['uname'] for n in tree.iter('node')]
|
||||
|
||||
return sorted(nodes)
|
||||
|
||||
|
||||
|
|
|
@ -72,6 +72,7 @@ from charmhelpers.core.host import (
|
|||
file_hash,
|
||||
lsb_release,
|
||||
init_is_systemd,
|
||||
get_distrib_codename,
|
||||
CompareHostReleases,
|
||||
)
|
||||
from charmhelpers.fetch import (
|
||||
|
@ -1167,16 +1168,26 @@ def is_in_standby_mode(node_name):
|
|||
@param node_name: The name of the node to check
|
||||
@returns boolean - True if node_name is in standby mode
|
||||
"""
|
||||
out = (subprocess
|
||||
.check_output(['crm', 'node', 'status', node_name])
|
||||
.decode('utf-8'))
|
||||
root = ET.fromstring(out)
|
||||
if CompareHostReleases(get_distrib_codename()) >= 'jammy':
|
||||
out = (subprocess.check_output(['crm', 'node', 'attribute',
|
||||
node_name, 'show', 'standby'])
|
||||
.decode('utf-8'))
|
||||
attrs = {}
|
||||
for item in re.split('[ ]+', out.strip()):
|
||||
tokens = item.split('=')
|
||||
attrs[tokens[0]] = tokens[1]
|
||||
standby_mode = attrs['name'] == 'standby' and attrs['value'] == 'on'
|
||||
else:
|
||||
out = (subprocess
|
||||
.check_output(['crm', 'node', 'status', node_name])
|
||||
.decode('utf-8'))
|
||||
root = ET.fromstring(out)
|
||||
|
||||
standby_mode = False
|
||||
for nvpair in root.iter('nvpair'):
|
||||
if (nvpair.attrib.get('name') == 'standby' and
|
||||
nvpair.attrib.get('value') == 'on'):
|
||||
standby_mode = True
|
||||
standby_mode = False
|
||||
for nvpair in root.iter('nvpair'):
|
||||
if (nvpair.attrib.get('name') == 'standby' and
|
||||
nvpair.attrib.get('value') == 'on'):
|
||||
standby_mode = True
|
||||
return standby_mode
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,15 @@ import utils
|
|||
import pcmk
|
||||
|
||||
|
||||
FOCAL_NODE_STATUS_STANDBY = '''<node id="1000" uname="juju-3ff82c-focal-1">
|
||||
<instance_attributes id="nodes-1000">
|
||||
<nvpair id="nodes-1000-standby" name="standby" value="on"/>
|
||||
</instance_attributes>
|
||||
</node>
|
||||
'''
|
||||
JAMMY_NODE_STATUS_STANDBY = 'scope=nodes name=standby value=on\n'
|
||||
|
||||
|
||||
def write_file(path, content, *args, **kwargs):
|
||||
with open(path, 'wt') as f:
|
||||
f.write(content)
|
||||
|
@ -1367,3 +1376,23 @@ class UtilsTestCase(unittest.TestCase):
|
|||
127, "fake crm command")
|
||||
with self.assertRaises(utils.RemoveCorosyncNodeFailed):
|
||||
utils.update_node_list()
|
||||
|
||||
@mock.patch('subprocess.check_output', autospec=True)
|
||||
@mock.patch.object(utils, 'get_distrib_codename', autospec=True)
|
||||
def test_is_in_standby_mode(self, get_distrib_codename, check_output):
|
||||
node_name = 'juju-3ff82c-focal-1'
|
||||
get_distrib_codename.return_value = 'focal'
|
||||
check_output.return_value = FOCAL_NODE_STATUS_STANDBY.encode()
|
||||
self.assertTrue(utils.is_in_standby_mode(node_name))
|
||||
|
||||
check_output.assert_called_with(['crm', 'node', 'status', node_name])
|
||||
|
||||
get_distrib_codename.reset_mock()
|
||||
check_output.reset_mock()
|
||||
|
||||
get_distrib_codename.return_value = 'jammy'
|
||||
check_output.return_value = JAMMY_NODE_STATUS_STANDBY.encode()
|
||||
self.assertTrue(utils.is_in_standby_mode(node_name))
|
||||
|
||||
check_output.assert_called_with(['crm', 'node', 'attribute', node_name,
|
||||
'show', 'standby'])
|
||||
|
|
Loading…
Reference in New Issue