Merge "Remove hash from stonith resource name"

This commit is contained in:
Zuul 2020-06-19 13:49:22 +00:00 committed by Gerrit Code Review
commit cdef15c156
3 changed files with 46 additions and 10 deletions

View File

@ -104,6 +104,20 @@ def crm_opt_exists(opt_name):
return False
def crm_maas_stonith_resource_list():
"""Returns list of resources of the type stonith:external/maas.
:returns: List of resource names.
:rtype: [str,]
"""
resource_names = []
output = subprocess.check_output(['crm_resource', '-L']).decode()
for line in output.split('\n'):
if 'stonith:external/maas' in line:
resource_names.append(line.split()[0])
return [n for n in resource_names if n.startswith('st-maas-')]
def crm_res_running(opt_name):
(_, output) = subprocess.getstatusoutput(
"crm resource status %s" % opt_name)

View File

@ -613,6 +613,16 @@ def configure_cluster_global(failure_timeout):
pcmk.commit(cmd)
def remove_legacy_maas_stonith_resources():
"""Remove maas stoniths resources using the old name."""
stonith_resources = pcmk.crm_maas_stonith_resource_list()
for resource_name in stonith_resources:
pcmk.commit(
'crm -w -F resource stop {}'.format(resource_name))
pcmk.commit(
'crm -w -F configure delete {}'.format(resource_name))
def configure_maas_stonith_resource(stonith_hostnames):
"""Create stonith resource for the given hostname.
@ -631,12 +641,8 @@ def configure_maas_stonith_resource(stonith_hostnames):
'apikey': config('maas_credentials'),
'hostnames': ' '.join(sorted(hostnames))}
if all(ctxt.values()):
maas_login_params = "url='{url}' apikey='{apikey}'".format(**ctxt)
maas_rsc_hash = pcmk.resource_checksum(
'st',
'stonith:external/maas',
res_params=maas_login_params)[:7]
ctxt['stonith_resource_name'] = 'st-maas-{}'.format(maas_rsc_hash)
ctxt['stonith_resource_name'] = 'st-maas'
remove_legacy_maas_stonith_resources()
ctxt['resource_params'] = (
"params url='{url}' apikey='{apikey}' hostnames='{hostnames}' "
"op monitor interval=25 start-delay=25 "

View File

@ -833,10 +833,11 @@ class UtilsTestCase(unittest.TestCase):
['res-node1', 'res-node2'])
@mock.patch.object(utils, 'config')
@mock.patch.object(utils, 'remove_legacy_maas_stonith_resources')
@mock.patch('pcmk.commit')
@mock.patch('pcmk.is_resource_present')
def test_configure_maas_stonith_resource(self, is_resource_present,
commit, config):
commit, remove_legacy, config):
cfg = {
'maas_url': 'http://maas/2.0',
'maas_credentials': 'apikey'}
@ -844,7 +845,7 @@ class UtilsTestCase(unittest.TestCase):
config.side_effect = lambda x: cfg.get(x)
utils.configure_maas_stonith_resource(['node1'])
cmd = (
"crm configure primitive st-maas-3975c9d "
"crm configure primitive st-maas "
"stonith:external/maas "
"params url='http://maas/2.0' apikey='apikey' "
"hostnames='node1' "
@ -859,13 +860,15 @@ class UtilsTestCase(unittest.TestCase):
commit.assert_has_calls(commit_calls)
@mock.patch.object(utils, 'config')
@mock.patch.object(utils, 'remove_legacy_maas_stonith_resources')
@mock.patch('pcmk.commit')
@mock.patch('pcmk.is_resource_present')
@mock.patch('pcmk.crm_update_resource')
def test_configure_maas_stonith_resource_duplicate(self,
crm_update_resource,
is_resource_present,
commit, config):
commit, remove_legacy,
config):
cfg = {
'maas_url': 'http://maas/2.0',
'maas_credentials': 'apikey'}
@ -873,7 +876,7 @@ class UtilsTestCase(unittest.TestCase):
config.side_effect = lambda x: cfg.get(x)
utils.configure_maas_stonith_resource(['node1'])
crm_update_resource.assert_called_once_with(
'st-maas-3975c9d',
'st-maas',
'stonith:external/maas',
("params url='http://maas/2.0' apikey='apikey' hostnames='node1' "
"op monitor interval=25 start-delay=25 timeout=25"))
@ -1174,3 +1177,16 @@ class UtilsTestCase(unittest.TestCase):
relation_set.assert_called_once_with(
relation_id='rid1',
relation_settings={'series_upgrade_of_nova_compute_2': None})
@mock.patch('pcmk.crm_maas_stonith_resource_list')
@mock.patch('pcmk.commit')
def test_remove_legacy_maas_stonith_resources(self, mock_commit,
mock_resource_list):
mock_resource_list.return_value = ['st-maas-abcd', 'st-maas-1234']
utils.remove_legacy_maas_stonith_resources()
commit_calls = [
mock.call('crm -w -F resource stop st-maas-abcd'),
mock.call('crm -w -F configure delete st-maas-abcd'),
mock.call('crm -w -F resource stop st-maas-1234'),
mock.call('crm -w -F configure delete st-maas-1234')]
mock_commit.assert_has_calls(commit_calls)