Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[crmsh-4.4] Dev: utils: Change the way to get pacemaker's version #1156

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions crmsh/cibconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .ra import get_ra, get_properties_list, get_pe_meta, get_properties_meta, RAInfo
from .utils import ext_cmd, safe_open_w, pipe_string, safe_close_w, crm_msec
from .utils import ask, lines2cli, olist
from .utils import page_string, cibadmin_can_patch, str2tmp, ensure_sudo_readable
from .utils import page_string, str2tmp, ensure_sudo_readable
from .utils import run_ptest, is_id_valid, edit_file, get_boolean, filter_string
from .xmlutil import is_child_rsc, rsc_constraint, sanitize_cib, rename_id, get_interesting_nodes
from .xmlutil import is_pref_location, get_topnode, new_cib, get_rscop_defaults_meta_node
Expand Down Expand Up @@ -2619,7 +2619,7 @@ def commit(self, force=False, replace=False):
'Commit the configuration to the CIB.'
if not self.is_cib_sane():
return False
if not replace and cibadmin_can_patch():
if not replace:
rc = self._patch_cib(force)
else:
rc = self._replace_cib(force)
Expand Down Expand Up @@ -2783,9 +2783,8 @@ def initialize(self, cib=None):
cib = text2elem(cib)
if not self._import_cib(cib):
return False
if cibadmin_can_patch():
self.cib_orig = copy.deepcopy(self.cib_elem)
sanitize_cib_for_patching(self.cib_orig)
self.cib_orig = copy.deepcopy(self.cib_elem)
sanitize_cib_for_patching(self.cib_orig)
sanitize_cib(self.cib_elem)
show_unrecognized_elems(self.cib_elem)
self._populate()
Expand Down
1 change: 0 additions & 1 deletion crmsh/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@
REJOIN_COUNT = 60
REJOIN_INTERVAL = 10
DC_DEADTIME_DEFAULT = 20

ADVISED_ACTION_LIST = ['monitor', 'start', 'stop', 'promote', 'demote']
ADVISED_KEY_LIST = ['timeout', 'interval', 'role']
DEFAULT_INTERVAL_IN_ACTION = "20s"
Expand Down
1 change: 0 additions & 1 deletion crmsh/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,6 @@ def set_node_colors(self):
i = (i+1) % len(self.nodecolors)

def _report_setup_source(self):
constants.pcmk_version = None
# is this an crm report or a crm_report?
for descname in ("description.txt", "report.summary"):
self.desc = os.path.join(self.loc, descname)
Expand Down
2 changes: 1 addition & 1 deletion crmsh/log_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@


def patterns(cib_f=None):
is118 = utils.is_pcmk_118(cib_f=cib_f)
is118 = utils.is_larger_than_pcmk_118(cib_f=cib_f)
if is118:
return _patterns_118
else:
Expand Down
7 changes: 0 additions & 7 deletions crmsh/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,6 @@ def set_interactive():
options.interactive = True


def compatibility_setup():
if not utils.is_pcmk_118():
del constants.attr_defaults["node"]
constants.cib_no_section_rc = 22


def add_quotes(args):
'''
Add quotes if there's whitespace in one of the
Expand Down Expand Up @@ -250,7 +244,6 @@ def main_input_loop(context, user_args):
Main input loop for crmsh. Parses input
line by line.
"""
compatibility_setup()
rc = handle_noninteractive_use(context, user_args)
if rc is not None:
return rc
Expand Down
10 changes: 9 additions & 1 deletion crmsh/ui_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,18 @@ def do_stop(self, context, *args):
else:
logger.info("Cluster services already stopped on {}".format(node))
node_list.remove(node)
elif not utils.service_is_active("pacemaker.service", remote_addr=node):
utils.stop_service("corosync", remote_addr=node)
logger.info("Cluster services stopped on {}".format(node))
node_list.remove(node)
if not node_list:
return

if not utils.get_dc(timeout=5):
dc_deadtime = utils.get_property("dc-deadtime") or constants.DC_DEADTIME_DEFAULT
dc_timeout = int(dc_deadtime.strip('s')) + 5
try:
utils.check_function_with_timeout(utils.get_dc, wait_timeout=dc_timeout)
except TimeoutError:
logger.error("No DC found currently, please wait if the cluster is still starting")
return False

Expand Down
1 change: 0 additions & 1 deletion crmsh/ui_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,6 @@ def _commit(self, force=False, replace=False):
logger.info("apparently there is nothing to commit")
logger.info("try changing something first")
return True
replace = replace or not utils.cibadmin_can_patch()
rc1 = True
if replace and not force:
rc1 = cib_factory.is_current_cib_equal()
Expand Down
2 changes: 1 addition & 1 deletion crmsh/ui_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def do_clearstate(self, context, node=None):
if not config.core.force and \
not utils.ask("Do you really want to drop state for node %s?" % node):
return False
if utils.is_pcmk_118():
if utils.is_larger_than_pcmk_118():
cib_elem = xmlutil.cibdump2elem()
if cib_elem is None:
return False
Expand Down
71 changes: 33 additions & 38 deletions crmsh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,8 @@ def append_file(dest, src):
return False


def get_dc(timeout=None):
cmd = "crmadmin -D"
if timeout:
cmd += " -t {}".format(timeout)
def get_dc():
cmd = "crmadmin -D -t 1"
rc, s, _ = get_stdout_stderr(add_sudo(cmd))
if rc != 0:
return None
Expand Down Expand Up @@ -1468,32 +1466,15 @@ def load_graphviz_file(ini_f):
return True, _graph_d


def get_pcmk_version(dflt):
version = dflt

crmd = pacemaker_controld()
if crmd:
cmd = crmd
else:
return version

try:
rc, s, err = get_stdout_stderr("%s version" % (cmd))
if rc != 0:
logger.error("%s exited with %d [err: %s][out: %s]", cmd, rc, err, s)
else:
if err.startswith("CRM Version:"):
version = s.split()[0]
else:
version = s.split()[2]
logger.debug("found pacemaker version: %s", version)
except Exception as msg:
logger.warning("could not get the pacemaker version, bad installation?")
logger.warning(msg)
def get_pcmk_version():
cmd = "/usr/sbin/pacemakerd --version"
out = get_stdout_or_raise_error(cmd)
version = out.split()[1]
logger.debug("Found pacemaker version: %s", version)
return version


def get_cib_property(cib_f, attr, dflt):
def get_cib_property(cib_f, attr, dflt=None):
"""A poor man's get attribute procedure.
We don't want heavy parsing, this needs to be relatively
fast.
Expand Down Expand Up @@ -1561,14 +1542,17 @@ def is_larger_than_min_version(version, min_version):
def is_min_pcmk_ver(min_ver, cib_f=None):
if not constants.pcmk_version:
if cib_f:
constants.pcmk_version = get_cib_property(cib_f, "dc-version", "1.1.11")
logger.debug("found pacemaker version: %s in cib: %s", constants.pcmk_version, cib_f)
constants.pcmk_version = get_cib_property(cib_f, "dc-version")
if constants.pcmk_version:
logger.debug("Found pacemaker version: %s in cib: %s", constants.pcmk_version, cib_f)
else:
fatal(f"Failed to get 'dc-version' from {cib_f}")
else:
constants.pcmk_version = get_pcmk_version("1.1.11")
constants.pcmk_version = get_pcmk_version()
return is_larger_than_min_version(constants.pcmk_version, min_ver)


def is_pcmk_118(cib_f=None):
def is_larger_than_pcmk_118(cib_f=None):
return is_min_pcmk_ver("1.1.8", cib_f=cib_f)


Expand All @@ -1586,12 +1570,6 @@ def cibadmin_features():
return []


@memoize
def cibadmin_can_patch():
# cibadmin -P doesn't handle comments in <1.1.11 (unless patched)
return is_min_pcmk_ver("1.1.11")


# quote function from python module shlex.py in python 3.3

_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search
Expand Down Expand Up @@ -3005,7 +2983,8 @@ def get_property(name):
"""
Get cluster properties
"""
cmd = "crm configure get_property " + name
cib_path = os.getenv('CIB_file', constants.CIB_RAW_FILE)
cmd = "CIB_file={} crm configure get_property {}".format(cib_path, name)
rc, stdout, _ = get_stdout_stderr(cmd)
return stdout if rc == 0 else None

Expand Down Expand Up @@ -3131,4 +3110,20 @@ def read_from_file(infile):
with open(infile, 'rt', encoding='utf-8', errors='replace') as f:
data = f.read()
return to_ascii(data)


def check_function_with_timeout(check_function, wait_timeout=30, interval=1):
"""
Run check_function in a loop
Return when check_function is true
Raise TimeoutError when timeout
"""
current_time = int(time.time())
timeout = current_time + wait_timeout
while current_time <= timeout:
if check_function():
return
time.sleep(interval)
current_time = int(time.time())
raise TimeoutError
# vim:ts=4:sw=4:et:
2 changes: 1 addition & 1 deletion crmsh/xmlutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def cibdump2elem(section=None):
rc, outp, errp = sudocall(cmd)
if rc == 0:
return text2elem(outp)
elif rc != constants.cib_no_section_rc:
else:
logger.error("running %s: %s", cmd, errp)
return None

Expand Down
19 changes: 19 additions & 0 deletions test/features/bootstrap_bugs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,22 @@ Feature: Regression test for bootstrap bugs
When Run "crm cluster remove HANODE2 -y" on "hanode1"
Then Cluster service is "stopped" on "hanode2"
And Online nodes are "hanode1"

@clean
Scenario: Stop service quickly(bsc#1203601)
Given Cluster service is "stopped" on "hanode1"
And Cluster service is "stopped" on "hanode2"
When Run "crm cluster init -y" on "hanode1"
Then Cluster service is "started" on "hanode1"
When Run "crm cluster join -c hanode1 -y" on "hanode2"
Then Cluster service is "started" on "hanode2"
When Run "crm cluster stop --all" on "hanode1"
Then Cluster service is "stopped" on "hanode1"
And Cluster service is "stopped" on "hanode2"
When Run "crm cluster start --all;crm cluster stop --all" on "hanode1"
Then Cluster service is "stopped" on "hanode1"
And Cluster service is "stopped" on "hanode2"
When Run "systemctl start corosync" on "hanode1"
Then Service "corosync" is "started" on "hanode1"
When Run "crm cluster stop" on "hanode1"
Then Service "corosync" is "stopped" on "hanode1"
10 changes: 7 additions & 3 deletions test/unittests/test_ui_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,23 @@ def test_do_stop_already_stopped(self, mock_parse_nodes, mock_active, mock_info)
@mock.patch('crmsh.utils.is_quorate')
@mock.patch('crmsh.utils.is_dlm_running')
@mock.patch('crmsh.utils.get_dc')
@mock.patch('crmsh.utils.check_function_with_timeout')
@mock.patch('crmsh.utils.get_property')
@mock.patch('crmsh.utils.service_is_active')
@mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
def test_do_stop(self, mock_parse_nodes, mock_active, mock_get_dc, mock_dlm_running, mock_is_quorate, mock_set_dlm, mock_stop, mock_info, mock_debug):
def test_do_stop(self, mock_parse_nodes, mock_active, mock_get_property, mock_check, mock_get_dc, mock_dlm_running, mock_is_quorate, mock_set_dlm, mock_stop, mock_info, mock_debug):
context_inst = mock.Mock()
mock_parse_nodes.return_value = ["node1"]
mock_active.side_effect = [True, True]
mock_active.side_effect = [True, True, True]
mock_dlm_running.return_value = True
mock_is_quorate.return_value = False
mock_get_dc.return_value = "node1"
mock_get_property.return_value = "20s"

self.ui_cluster_inst.do_stop(context_inst, "node1")

mock_active.assert_has_calls([
mock.call("corosync.service", remote_addr="node1"),
mock.call("pacemaker.service", remote_addr="node1"),
mock.call("corosync-qdevice.service")
])
mock_stop.assert_has_calls([
Expand All @@ -123,3 +126,4 @@ def test_do_stop(self, mock_parse_nodes, mock_active, mock_get_dc, mock_dlm_runn
])
mock_info.assert_called_once_with("Cluster services stopped on node1")
mock_debug.assert_called_once_with("Quorum is lost; Set enable_quorum_fencing=0 and enable_quorum_lockspace=0 for dlm")
mock_check.assert_called_once_with(mock_get_dc, wait_timeout=25)
6 changes: 4 additions & 2 deletions test/unittests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1616,11 +1616,13 @@ def test_list_cluster_nodes(mock_run, mock_env, mock_isfile, mock_file2elem):
])


@mock.patch('os.getenv')
@mock.patch('crmsh.utils.get_stdout_stderr')
def test_get_property(mock_run):
def test_get_property(mock_run, mock_env):
mock_run.return_value = (0, "data", None)
mock_env.return_value = "cib.xml"
assert utils.get_property("no-quorum-policy") == "data"
mock_run.assert_called_once_with("crm configure get_property no-quorum-policy")
mock_run.assert_called_once_with("CIB_file=cib.xml crm configure get_property no-quorum-policy")


@mock.patch('crmsh.utils.get_stdout_or_raise_error')
Expand Down