summaryrefslogtreecommitdiffstats
path: root/extras/cliutils
diff options
context:
space:
mode:
Diffstat (limited to 'extras/cliutils')
-rw-r--r--extras/cliutils/README.md6
-rw-r--r--extras/cliutils/__init__.py28
-rw-r--r--extras/cliutils/cliutils.py33
3 files changed, 47 insertions, 20 deletions
diff --git a/extras/cliutils/README.md b/extras/cliutils/README.md
index ccb60802c3d..309beb1ca25 100644
--- a/extras/cliutils/README.md
+++ b/extras/cliutils/README.md
@@ -81,7 +81,7 @@ to address the following issues
Create a file in `$LIBEXEC/glusterfs/peer_message.py` with following
content.
- #!/usr/bin/env python
+ #!/usr/bin/python3
from gluster.cliutils import Cmd, runcli, execute_in_peers, node_output_ok
class NodeHello(Cmd):
@@ -149,7 +149,7 @@ Now users can use `gluster-message` instead of calling
Following example uses prettytable library, which can be installed
using `pip install prettytable` or `dnf install python-prettytable`
- #!/usr/bin/env python
+ #!/usr/bin/python3
from prettytable import PrettyTable
from gluster.cliutils import Cmd, runcli, execute_in_peers, node_output_ok
@@ -221,7 +221,7 @@ required.(Under `%files` section)
- gluster-mountbroker http://review.gluster.org/14544
- gluster-eventsapi http://review.gluster.org/14248
- gluster-georep-sshkey http://review.gluster.org/14732
-- gluster-restapi https://github.com/aravindavk/glusterfs-restapi
+- gluster-restapi https://github.com/gluster/restapi
## Limitations/TODOs
- Not yet possible to create CLI without any subcommand, For example
diff --git a/extras/cliutils/__init__.py b/extras/cliutils/__init__.py
index 4bb8395bb46..8765cc85099 100644
--- a/extras/cliutils/__init__.py
+++ b/extras/cliutils/__init__.py
@@ -1,17 +1,18 @@
# -*- coding: utf-8 -*-
# Reexporting the utility funcs and classes
-from cliutils import (runcli,
- sync_file_to_peers,
- execute_in_peers,
- execute,
- node_output_ok,
- node_output_notok,
- output_error,
- oknotok,
- yesno,
- get_node_uuid,
- Cmd,
- GlusterCmdException)
+from .cliutils import (runcli,
+ sync_file_to_peers,
+ execute_in_peers,
+ execute,
+ node_output_ok,
+ node_output_notok,
+ output_error,
+ oknotok,
+ yesno,
+ get_node_uuid,
+ Cmd,
+ GlusterCmdException,
+ set_common_args_func)
# This will be useful when `from cliutils import *`
@@ -26,4 +27,5 @@ __all__ = ["runcli",
"yesno",
"get_node_uuid",
"Cmd",
- "GlusterCmdException"]
+ "GlusterCmdException",
+ "set_common_args_func"]
diff --git a/extras/cliutils/cliutils.py b/extras/cliutils/cliutils.py
index 4e035d7ff5c..55fbaf56704 100644
--- a/extras/cliutils/cliutils.py
+++ b/extras/cliutils/cliutils.py
@@ -16,10 +16,18 @@ subparsers = parser.add_subparsers(dest="mode")
subcommands = {}
cache_data = {}
ParseError = etree.ParseError if hasattr(etree, 'ParseError') else SyntaxError
+_common_args_func = lambda p: True
class GlusterCmdException(Exception):
- pass
+ def __init__(self, message):
+ self.message = message
+ try:
+ # Python 3
+ super().__init__(message)
+ except TypeError:
+ # Python 2
+ super(GlusterCmdException, self).__init__(message)
def get_node_uuid():
@@ -50,9 +58,9 @@ def oknotok(flag):
return "OK" if flag else "NOT OK"
-def output_error(message):
+def output_error(message, errcode=1):
print (message, file=sys.stderr)
- sys.exit(1)
+ sys.exit(errcode)
def node_output_ok(message=""):
@@ -70,7 +78,8 @@ def node_output_notok(message):
def execute(cmd):
- p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ universal_newlines=True)
out, err = p.communicate()
return p.returncode, out, err
@@ -186,6 +195,7 @@ def runcli():
# a subcommand as specified in the Class name. Call the args
# method by passing subcommand parser, Derived class can add
# arguments to the subcommand parser.
+ metavar_data = []
for c in Cmd.__subclasses__():
cls = c()
if getattr(cls, "name", "") == "":
@@ -193,14 +203,24 @@ def runcli():
"to \"{0}\"".format(
cls.__class__.__name__))
+ # Do not show in help message if subcommand starts with node-
+ if not cls.name.startswith("node-"):
+ metavar_data.append(cls.name)
+
p = subparsers.add_parser(cls.name)
args_func = getattr(cls, "args", None)
if args_func is not None:
args_func(p)
+ # Apply common args if any
+ _common_args_func(p)
+
# A dict to save subcommands, key is name of the subcommand
subcommands[cls.name] = cls
+ # Hide node commands in Help message
+ subparsers.metavar = "{" + ",".join(metavar_data) + "}"
+
# Get all parsed arguments
args = parser.parse_args()
@@ -210,3 +230,8 @@ def runcli():
# Run
if cls is not None:
cls.run(args)
+
+
+def set_common_args_func(func):
+ global _common_args_func
+ _common_args_func = func