2018-09-11 13:26:14 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-03-23 10:40:31 +00:00
|
|
|
import sys
|
|
|
|
import argparse
|
2019-01-28 14:28:36 +00:00
|
|
|
from configshell_fb import ConfigShell, shell, ExecutionError
|
2018-09-19 08:23:44 +00:00
|
|
|
from pyparsing import (alphanums, Optional, Suppress, Word, Regex,
|
|
|
|
removeQuotes, dblQuotedString, OneOrMore)
|
2019-10-16 10:41:28 +00:00
|
|
|
from rpc.client import JSONRPCException, JSONRPCClient
|
|
|
|
from spdkcli import UIRoot
|
2018-09-19 08:23:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_quotes_to_shell(spdk_shell):
|
|
|
|
command = shell.locatedExpr(Word(alphanums + '_'))('command')
|
|
|
|
value = dblQuotedString.addParseAction(removeQuotes)
|
2019-02-08 17:50:49 +00:00
|
|
|
value_word = Word(alphanums + r';,=_\+/.<>()~@:-%[]')
|
|
|
|
keyword = Word(alphanums + r'_\-')
|
2018-09-19 08:23:44 +00:00
|
|
|
kparam = shell.locatedExpr(keyword + Suppress('=') +
|
|
|
|
Optional(value | value_word, default=''))('kparams*')
|
|
|
|
pparam = shell.locatedExpr(value | value_word)('pparams*')
|
|
|
|
parameters = OneOrMore(kparam | pparam)
|
2019-02-08 17:50:49 +00:00
|
|
|
bookmark = Regex(r'@([A-Za-z0-9:_.]|-)+')
|
|
|
|
pathstd = Regex(r'([A-Za-z0-9:_.\[\]]|-)*' + '/' + r'([A-Za-z0-9:_.\[\]/]|-)*') \
|
2018-09-19 08:23:44 +00:00
|
|
|
| '..' | '.'
|
|
|
|
path = shell.locatedExpr(bookmark | pathstd | '*')('path')
|
|
|
|
spdk_shell._parser = Optional(path) + Optional(command) + Optional(parameters)
|
2018-03-23 10:40:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Start SPDK CLI
|
|
|
|
:return:
|
|
|
|
"""
|
2018-09-19 08:23:44 +00:00
|
|
|
spdk_shell = ConfigShell("~/.scripts")
|
2019-02-16 22:45:52 +00:00
|
|
|
spdk_shell.interactive = True
|
2018-09-19 08:23:44 +00:00
|
|
|
add_quotes_to_shell(spdk_shell)
|
2018-03-23 10:40:31 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="SPDK command line interface")
|
2019-09-09 11:12:24 +00:00
|
|
|
parser.add_argument('-s', dest='server_addr',
|
|
|
|
help='RPC domain socket path or IP address', default='/var/tmp/spdk.sock')
|
|
|
|
parser.add_argument('-p', dest='port',
|
|
|
|
help='RPC port number (if server_addr is IP address)',
|
|
|
|
default=None, type=int)
|
2018-07-13 13:15:05 +00:00
|
|
|
parser.add_argument("-v", dest="verbose", help="Print request/response JSON for configuration calls",
|
|
|
|
default=False, action="store_true")
|
2018-03-23 10:40:31 +00:00
|
|
|
parser.add_argument("commands", metavar="command", type=str, nargs="*", default="",
|
|
|
|
help="commands to execute by SPDKCli as one-line command")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
spdkcli: handle intialiazation error
Catch exceptions during RPC client initialization
to display meaningful error message.
Withouth this change, user gets stacktrace
when e.g. SPDK application is not running.
Before:
```
Traceback (most recent call last):
File "scripts/rpc/client.py", line 53, in __init__
raise socket.error("Unix socket '%s' does not exist" % addr)
OSError: Unix socket '/var/tmp/spdk.sock' does not exist
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "scripts/spdkcli.py", line 74, in <module>
main()
File "scripts/spdkcli.py", line 47, in main
with rpc.client.JSONRPCClient(args.socket) as client:
File "scripts/rpc/client.py", line 56, in __init__
"Error details: %s" % (addr, ex))
rpc.client.JSONRPCException: Error while connecting to /var/tmp/spdk.sock
Error details: Unix socket '/var/tmp/spdk.sock' does not exist
```
After:
```
Error while connecting to /var/tmp/spdk.sock
Error details: Unix socket '/var/tmp/spdk.sock' does not exist. SPDK not running?
```
Change-Id: I65862965b68acf3bd4709de598f04de49da27de2
Signed-off-by: Vitaliy Mysak <vitaliy.mysak@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/462020
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Karol Latecki <karol.latecki@intel.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
2019-07-15 17:03:10 +00:00
|
|
|
try:
|
2019-10-16 10:41:28 +00:00
|
|
|
client = JSONRPCClient(args.server_addr, port=args.port)
|
spdkcli: handle intialiazation error
Catch exceptions during RPC client initialization
to display meaningful error message.
Withouth this change, user gets stacktrace
when e.g. SPDK application is not running.
Before:
```
Traceback (most recent call last):
File "scripts/rpc/client.py", line 53, in __init__
raise socket.error("Unix socket '%s' does not exist" % addr)
OSError: Unix socket '/var/tmp/spdk.sock' does not exist
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "scripts/spdkcli.py", line 74, in <module>
main()
File "scripts/spdkcli.py", line 47, in main
with rpc.client.JSONRPCClient(args.socket) as client:
File "scripts/rpc/client.py", line 56, in __init__
"Error details: %s" % (addr, ex))
rpc.client.JSONRPCException: Error while connecting to /var/tmp/spdk.sock
Error details: Unix socket '/var/tmp/spdk.sock' does not exist
```
After:
```
Error while connecting to /var/tmp/spdk.sock
Error details: Unix socket '/var/tmp/spdk.sock' does not exist. SPDK not running?
```
Change-Id: I65862965b68acf3bd4709de598f04de49da27de2
Signed-off-by: Vitaliy Mysak <vitaliy.mysak@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/462020
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Karol Latecki <karol.latecki@intel.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
2019-07-15 17:03:10 +00:00
|
|
|
except JSONRPCException as e:
|
|
|
|
spdk_shell.log.error("%s. SPDK not running?" % e)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
with client:
|
2019-02-14 15:47:31 +00:00
|
|
|
root_node = UIRoot(client, spdk_shell)
|
|
|
|
root_node.verbose = args.verbose
|
2019-01-28 14:28:36 +00:00
|
|
|
try:
|
2019-02-14 15:47:31 +00:00
|
|
|
root_node.refresh()
|
|
|
|
except BaseException:
|
|
|
|
pass
|
2018-03-23 10:40:31 +00:00
|
|
|
|
2019-10-16 10:41:28 +00:00
|
|
|
if args.commands:
|
2019-02-14 15:47:31 +00:00
|
|
|
try:
|
|
|
|
spdk_shell.interactive = False
|
|
|
|
spdk_shell.run_cmdline(" ".join(args.commands))
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("%s\n" % e)
|
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
spdk_shell.con.display("SPDK CLI v0.1")
|
|
|
|
spdk_shell.con.display("")
|
2019-10-16 10:41:28 +00:00
|
|
|
|
2020-06-15 10:11:51 +00:00
|
|
|
while not spdk_shell._exit:
|
|
|
|
try:
|
|
|
|
spdk_shell.run_interactive()
|
|
|
|
except (JSONRPCException, ExecutionError) as e:
|
|
|
|
spdk_shell.log.error("%s" % e)
|
|
|
|
except BrokenPipeError as e:
|
|
|
|
spdk_shell.log.error("Lost connection with SPDK: %s" % e)
|
2018-03-23 10:40:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|