Skip to content

Commit

Permalink
Merge pull request #893 from liangxin1300/20211130_standby_other_value
Browse files Browse the repository at this point in the history
Dev: ui_node: Improve node standby/online methods
  • Loading branch information
liangxin1300 authored Dec 3, 2021
2 parents 9b8a991 + 9e287a4 commit 5b4e12b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 45 deletions.
17 changes: 2 additions & 15 deletions crmsh/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,20 +508,7 @@
CIB_RAW_FILE = "/var/lib/pacemaker/cib/cib.xml"
XML_NODE_PATH = "/cib/configuration/nodes/node"
XML_STATUS_PATH = "/cib/status/node_state"
XML_NODE_QUERY_STANDBY_PATH = "//nodes/node[@id='{node_id}']/instance_attributes/nvpair[@name='standby']/@value"
XML_STATUS_QUERY_STANDBY_PATH = "//status/node_state[@id='{node_id}']/transient_attributes/instance_attributes/nvpair[@name='standby']/@value"
STANDBY_TEMPLATE = """
<instance_attributes id="nodes-{node_id}">
<nvpair id="nodes-{node_id}-standby" name="standby" value="{value}"/>
</instance_attributes>
"""
STANDBY_TEMPLATE_REBOOT = """
<transient_attributes id="{node_id}">
<instance_attributes id="status-{node_id}">
<nvpair id="status-{node_id}-standby" name="standby" value="{value}"/>
</instance_attributes>
</transient_attributes>
"""
STANDBY_NV_RE = r'(<nvpair.*{node_id}.*name="standby".*)value="{value}"(.*)'
XML_NODE_QUERY_STANDBY_PATH = "//nodes/node[@id='{node_id}']/instance_attributes/nvpair[@name='standby']"
XML_STATUS_QUERY_STANDBY_PATH = "//status/node_state[@id='{node_id}']/transient_attributes/instance_attributes/nvpair[@name='standby']"
CRM_MON_ONE_SHOT = "crm_mon -1"
# vim:ts=4:sw=4:et:
3 changes: 3 additions & 0 deletions crmsh/idmgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
# Make sure that ids are unique.

import re
import copy
from . import constants
from . import xmlutil
Expand Down Expand Up @@ -45,6 +46,8 @@ def new(node, pfx):
'''
Create a unique id for the xml node.
'''
if re.search(r'^\d+$', pfx) and node.tag != "node":
pfx = "num-{}".format(pfx)
name = node.get("name")
if node.tag == "nvpair":
node_id = "%s-%s" % (pfx, name)
Expand Down
55 changes: 25 additions & 30 deletions crmsh/ui_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# See COPYING for license information.

import re
from lxml import etree
from . import config
from . import command
from . import completers as compl
Expand Down Expand Up @@ -303,47 +302,45 @@ def do_standby(self, context, *args):

# Parse node option
node_list = parse_option_for_nodes(context, *args)
for node in node_list[:]:
if utils.is_standby(node):
logger.info("Node %s already standby", node)
node_list.remove(node)
if not node_list:
return

# For default "forever" lifetime, under "nodes" section
xml_path = constants.XML_NODE_PATH
xml_query_path = constants.XML_NODE_QUERY_STANDBY_PATH
standby_template = constants.STANDBY_TEMPLATE
xml_query_path_oppsite = constants.XML_STATUS_QUERY_STANDBY_PATH
# For "reboot" lifetime, under "status" section
if lifetime_opt == "reboot":
xml_path = constants.XML_STATUS_PATH
xml_query_path = constants.XML_STATUS_QUERY_STANDBY_PATH
standby_template = constants.STANDBY_TEMPLATE_REBOOT
xml_query_path_oppsite = constants.XML_NODE_QUERY_STANDBY_PATH

cib = xmlutil.cibdump2elem()
xml_item_list = cib.xpath(xml_path)
for xml_item in xml_item_list:
if xml_item.get("uname") in node_list:
node_id = xml_item.get('id')
# If the standby nvpair already exists, continue
if cib.xpath(xml_query_path.format(node_id=node_id)):
# Remove possible oppsite lifetime standby nvpair
item_to_del = cib.xpath(xml_query_path_oppsite.format(node_id=node_id))
if item_to_del:
xmlutil.rmnodes(item_to_del)
# If the standby nvpair already exists, set and continue
item = cib.xpath(xml_query_path.format(node_id=node_id))
if item and item[0].get("value") != "on":
item[0].set("value", "on")
continue
# Create standby nvpair
standby_template_str = standby_template.format(node_id=node_id, value="on")
xml_item.append(etree.fromstring(standby_template_str))

cib_str = xmlutil.xml_tostring(cib)
# Consider both "nodes" and "status" section might contain different standby value at the same time
# Should replace all possible "off" values here
for node in node_list:
node_id = utils.get_nodeid_from_name(node)
cib_str = re.sub(constants.STANDBY_NV_RE.format(node_id=node_id, value="off"), r'\1value="on"\2', cib_str)

cmd = constants.CIB_REPLACE.format(xmlstr=cib_str)
interface_item = xml_item
if lifetime_opt == "reboot":
res_item = xmlutil.get_set_nodes(xml_item, "transient_attributes", create=True)
interface_item = res_item[0]
res_item = xmlutil.get_set_nodes(interface_item, "instance_attributes", create=True)
xmlutil.set_attr(res_item[0], "standby", "on")

cmd = constants.CIB_REPLACE.format(xmlstr=xmlutil.xml_tostring(cib))
utils.get_stdout_or_raise_error(cmd)
for node in node_list:
logger.info("Standby node %s", node)

logger.info("standby node %s", node)

@command.wait
@command.completers(compl.nodes)
Expand All @@ -354,23 +351,21 @@ def do_online(self, context, *args):
"""
# Parse node option
node_list = parse_option_for_nodes(context, *args)
for node in node_list[:]:
if not utils.is_standby(node):
logger.info("Node %s already online", node)
node_list.remove(node)
if not node_list:
return

cib = xmlutil.cibdump2elem()
cib_str = xmlutil.xml_tostring(cib)
for node in node_list:
node_id = utils.get_nodeid_from_name(node)
cib_str = re.sub(constants.STANDBY_NV_RE.format(node_id=node_id, value="on"), r'\1value="off"\2', cib_str)
for query_path in [constants.XML_NODE_QUERY_STANDBY_PATH, constants.XML_STATUS_QUERY_STANDBY_PATH]:
item = cib.xpath(query_path.format(node_id=node_id))
if item and item[0].get("value") != "off":
item[0].set("value", "off")

cmd = constants.CIB_REPLACE.format(xmlstr=cib_str)
cmd = constants.CIB_REPLACE.format(xmlstr=xmlutil.xml_tostring(cib))
utils.get_stdout_or_raise_error(cmd)
for node in node_list:
logger.info("Online node %s", node)
logger.info("online node %s", node)

@command.wait
@command.completers(compl.nodes)
Expand Down

0 comments on commit 5b4e12b

Please sign in to comment.