Skip to content

Commit

Permalink
Dev: ui_node: refactor do_clearstate
Browse files Browse the repository at this point in the history
Parse the cibadmin output depending on the pacemaker version.
(ref: ClusterLabs/pacemaker#3031,
cibadmin has little changed it's format at Version-2.1.7).
  • Loading branch information
Aleksei Burlakov committed Feb 12, 2024
1 parent e3d1170 commit d8e380c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
23 changes: 17 additions & 6 deletions crmsh/ui_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,23 @@ def do_clearstate(self, context, node=None):
cib_elem = xmlutil.cibdump2elem()
if cib_elem is None:
return False
if cib_elem.xpath("//node_state[@uname=\"%s\"]/@crmd" % node) == ["online"]:
return utils.ext_cmd(self.node_cleanup_resources % node) == 0
elif cib_elem.xpath("//node_state[@uname=\"%s\"]/@in_ccm" % node) == ["true"]:
logger.warning("Node is offline according to Pacemaker, but online according to corosync. First shut down node '%s'", node)
return False
else:
if utils.is_pcmk_version_between("1.1.8", "2.1.6"):
if cib_elem.xpath("//node_state[@uname=\"%s\"]/@crmd" % node) == ["online"]:
return utils.ext_cmd(self.node_cleanup_resources % node) == 0
elif cib_elem.xpath("//node_state[@uname=\"%s\"]/@in_ccm" % node) == ["true"]:
logger.warning("Node is offline according to Pacemaker, but online according to corosync. First shut down node '%s'", node)
return False

Check warning on line 509 in crmsh/ui_node.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_node.py#L504-L509

Added lines #L504 - L509 were not covered by tests
else:
return utils.ext_cmd(self.node_clear_state_118 % node) == 0

Check warning on line 511 in crmsh/ui_node.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_node.py#L511

Added line #L511 was not covered by tests
# TODO: Add a test with an newer pacemaker version
if utils.is_min_pcmk_ver("2.1.7"):
crmd = cib_elem.xpath("//node_state[@uname=\"%s\"]/@crmd" % node)
if crmd and crmd[0].isdigit() and int(crmd[0]) != 0:
return utils.ext_cmd(self.node_cleanup_resources % node) == 0
in_ccm = cib_elem.xpath("//node_state[@uname=\"%s\"]/@in_ccm" % node)
if in_ccm and in_ccm[0].isdigit() and int(in_ccm[0]) != 0:
logger.warning("Node is offline according to Pacemaker, but online according to corosync. First shut down node '%s'", node)
return False

Check warning on line 520 in crmsh/ui_node.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_node.py#L513-L520

Added lines #L513 - L520 were not covered by tests
return utils.ext_cmd(self.node_clear_state_118 % node) == 0
else:
return utils.ext_cmd(self.node_clear_state % ("-M -c", node, node)) == 0 and \
Expand Down
22 changes: 20 additions & 2 deletions crmsh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1828,8 +1828,7 @@ def get_cib_attributes(cib_f, tag, attr_l, dflt_l):
def is_larger_than_min_version(version, min_version):
return LooseVersion(version) >= LooseVersion(min_version)


def is_min_pcmk_ver(min_ver, cib_f=None):
def set_pcmk_version(cib_f=None):
if not constants.pcmk_version:
if cib_f:
constants.pcmk_version = get_cib_property(cib_f, "dc-version")
Expand All @@ -1839,12 +1838,31 @@ def is_min_pcmk_ver(min_ver, cib_f=None):
fatal(f"Failed to get 'dc-version' from {cib_f}")
else:
constants.pcmk_version = get_pcmk_version()

def is_min_pcmk_ver(min_ver, cib_f=None):
set_pcmk_version(cib_f)
return is_larger_than_min_version(constants.pcmk_version, min_ver)


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

def is_pcmk_version_between(min_version, max_version, exclude_versions=None, cib_f=None):
"""
Check if the pacemaker version is between min_ and max_version,
and not in the exclude_versions list.
"""
set_pcmk_version(cib_f)
if exclude_versions:
for v in exclude_versions:
if LooseVersion(constants.pcmk_version) == LooseVersion(v):
return False
result = True
if min_version:
result = result and ( LooseVersion(constants.pcmk_version) >= LooseVersion(min_version) )
if max_version:
result = result and ( LooseVersion(constants.pcmk_version) <= LooseVersion(max_version) )
return result

Check warning on line 1865 in crmsh/utils.py

View check run for this annotation

Codecov / codecov/patch

crmsh/utils.py#L1855-L1865

Added lines #L1855 - L1865 were not covered by tests

@memoize
def cibadmin_features():
Expand Down

0 comments on commit d8e380c

Please sign in to comment.