Skip to content

Commit c603419

Browse files
committed
Dev: ui_utils: Move ui_node.parse_option_for_nodes to ui_utils.parse_and_validate_node_args
As that function is more generic and should be relocated and renamed accordingly.
1 parent eb1fcfc commit c603419

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

crmsh/ui_cluster.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from .prun import prun
2727
from .service_manager import ServiceManager
2828
from .sh import ShellUtils
29-
from .ui_node import parse_option_for_nodes
29+
from . import ui_utils
3030
from . import constants
3131

3232

@@ -168,7 +168,7 @@ def do_start(self, context, *args):
168168
Starts the cluster stack on all nodes or specific node(s)
169169
'''
170170
try:
171-
node_list = parse_option_for_nodes(context, *args)
171+
node_list = ui_utils.parse_and_validate_node_args("start", args)
172172
except utils.NoSSHError as msg:
173173
logger.error('%s', msg)
174174
logger.info("Please try 'crm cluster start' on each node")
@@ -250,7 +250,7 @@ def do_stop(self, context, *args):
250250
Stops the cluster stack on all nodes or specific node(s)
251251
'''
252252
try:
253-
node_list = parse_option_for_nodes(context, *args)
253+
node_list = ui_utils.parse_and_validate_node_args("stop", args)
254254
except utils.NoSSHError as msg:
255255
logger.error('%s', msg)
256256
logger.info("Please try 'crm cluster stop' on each node")
@@ -299,7 +299,7 @@ def do_enable(self, context, *args):
299299
'''
300300
Enable the cluster services on this node
301301
'''
302-
node_list = parse_option_for_nodes(context, *args)
302+
node_list = ui_utils.parse_and_validate_node_args("enable", args)
303303
service_manager = ServiceManager()
304304
node_list = service_manager.enable_service("pacemaker.service", node_list=node_list)
305305
if service_manager.service_is_available("corosync-qdevice.service") and corosync.is_qdevice_configured():
@@ -312,7 +312,7 @@ def do_disable(self, context, *args):
312312
'''
313313
Disable the cluster services on this node
314314
'''
315-
node_list = parse_option_for_nodes(context, *args)
315+
node_list = ui_utils.parse_and_validate_node_args("disable", args)
316316
service_manager = ServiceManager()
317317
node_list = service_manager.disable_service("pacemaker.service", node_list=node_list)
318318
service_manager.disable_service("corosync-qdevice.service", node_list=node_list)

crmsh/ui_node.py

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import copy
77
import subprocess
88
from lxml import etree
9-
from argparse import ArgumentParser, RawDescriptionHelpFormatter
109

1110
from . import config
1211
from . import command
@@ -219,47 +218,6 @@ def print_node(uname, ident, node_type, other, inst_attr, offline):
219218
print(term.render("\t%s" % (s)))
220219

221220

222-
def parse_option_for_nodes(context, *args):
223-
"""
224-
Parse option for nodes
225-
Return a node list
226-
"""
227-
action_type = context.get_command_name()
228-
action_target = "node" if action_type in ["standby", "online"] else "cluster service"
229-
action = "{} {}".format(action_type, action_target)
230-
usage_template = """
231-
Specify node(s) on which to {action}.
232-
If no nodes are specified, {action} on the local node.
233-
If --all is specified, {action} on all nodes."""
234-
addtion_usage = ""
235-
if action_type == "standby":
236-
usage_template += """
237-
\n\nAdditionally, you may specify a lifetime for the standby---if set to
238-
"reboot", the node will be back online once it reboots. "forever" will
239-
keep the node in standby after reboot. The life time defaults to
240-
"forever"."""
241-
addtion_usage = " [lifetime]"
242-
243-
parser = ArgumentParser(description=usage_template.format(action=action),
244-
usage="{} [--all | <node>... ]{}".format(action_type, addtion_usage),
245-
add_help=False,
246-
formatter_class=RawDescriptionHelpFormatter)
247-
parser.add_argument("-h", "--help", action="store_true", dest="help", help="Show this help message")
248-
parser.add_argument("--all", help="To {} on all nodes".format(action), action="store_true", dest="all")
249-
250-
options, args = parser.parse_known_args(args)
251-
if options.help:
252-
parser.print_help()
253-
raise utils.TerminateSubCommand(success=True)
254-
if options is None or args is None:
255-
raise utils.TerminateSubCommand
256-
if options.all and args:
257-
context.fatal_error("Should either use --all or specific node(s)")
258-
259-
include_remote = action_type in ["standby", "online"]
260-
return utils.validate_and_get_reachable_nodes(args, options.all, include_remote)
261-
262-
263221
class NodeMgmt(command.UI):
264222
'''
265223
Nodes management class
@@ -343,7 +301,7 @@ def do_standby(self, context, *args):
343301
args = args[:-1]
344302

345303
# Parse node option
346-
node_list = parse_option_for_nodes(context, *args)
304+
node_list = ui_utils.parse_and_validate_node_args("standby", args)
347305
if not node_list:
348306
return
349307

@@ -431,7 +389,7 @@ def do_online(self, context, *args):
431389
To avoid race condition for --all option, melt all online values into one cib replace session
432390
"""
433391
# Parse node option
434-
node_list = parse_option_for_nodes(context, *args)
392+
node_list = ui_utils.parse_and_validate_node_args("online", args)
435393
if not node_list:
436394
return
437395

crmsh/ui_utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import inspect
77
from . import utils
88
from . import log
9+
from argparse import ArgumentParser, RawDescriptionHelpFormatter
910

1011

1112
logger = log.setup_logger(__name__)
@@ -162,3 +163,45 @@ def mknamed():
162163
if max_args >= 0 and len(args) > max_args:
163164
raise ValueError("Expected (%s), takes at most %d arguments (%d given)" %
164165
(mknamed(), max_args-nskip, len(args)-nskip))
166+
167+
168+
def parse_and_validate_node_args(command_name, *args) -> list:
169+
'''
170+
Parses option for node-related commands
171+
Then validates and returns the reachable node list
172+
'''
173+
action_target = "node" if command_name in ["standby", "online"] else "cluster service"
174+
action = f"{command_name} {action_target}"
175+
usage_template = """
176+
Specify node(s) on which to {action}.
177+
If no nodes are specified, {action} on the local node.
178+
If --all is specified, {action} on all nodes."""
179+
addtion_usage = ""
180+
if command_name == "standby":
181+
usage_template += """
182+
\n\nAdditionally, you may specify a lifetime for the standby---if set to
183+
"reboot", the node will be back online once it reboots. "forever" will
184+
keep the node in standby after reboot. The life time defaults to
185+
"forever"."""
186+
addtion_usage = " [lifetime]"
187+
188+
parser = ArgumentParser(
189+
description=usage_template.format(action=action),
190+
usage=f"{command_name} [--all | <node>... ]{addtion_usage}",
191+
add_help=False,
192+
formatter_class=RawDescriptionHelpFormatter
193+
)
194+
parser.add_argument("-h", "--help", action="store_true", dest="help", help="Show this help message")
195+
parser.add_argument("--all", help=f"To {action} on all nodes", action="store_true", dest="all")
196+
197+
options, args = parser.parse_known_args(args)
198+
if options.help:
199+
parser.print_help()
200+
raise utils.TerminateSubCommand(success=True)
201+
if options is None or args is None:
202+
raise utils.TerminateSubCommand
203+
if options.all and args:
204+
raise ValueError("Should either use --all or specific node(s)")
205+
206+
include_remote = command_name in ["standby", "online"]
207+
return utils.validate_and_get_reachable_nodes(args, options.all, include_remote)

0 commit comments

Comments
 (0)