rpc.py: Allow loading json config from different sources

This is relevant in context of rpc_cmd() which was introduced with the
following commit:

     "scripts/rpc.py: add daemon mode" 8b98cdb64a

When rpc.py runs in a server mode, its stdin is already attached to a
pipe connected to its parent Bash process. In such a setup, it's not
possible to use some of the rpc methods, e.g. load_config().

To make use of said methods possible, allow them to load the config
from different sources - a regular file or a string.

Change-Id: I6fa7d13fa1957b6449ce5d5c5a810bd58d0a5703
Signed-off-by: Michal Berger <michalx.berger@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/2106
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
This commit is contained in:
Michal Berger 2020-04-29 17:34:51 +02:00 committed by Tomasz Zawadzki
parent 7a2bf6fcb9
commit 871214e2c1
2 changed files with 22 additions and 6 deletions

View File

@ -91,11 +91,12 @@ if __name__ == "__main__":
p.set_defaults(func=save_config)
def load_config(args):
rpc.load_config(args.client, sys.stdin,
rpc.load_config(args.client, args.json_conf,
include_aliases=args.include_aliases)
p = subparsers.add_parser('load_config', help="""Configure SPDK subsystems and targets using JSON RPC read from stdin.""")
p = subparsers.add_parser('load_config', help="""Configure SPDK subsystems and targets using JSON RPC.""")
p.add_argument('-i', '--include-aliases', help='include RPC aliases', action='store_true')
p.add_argument('-j', '--json_conf', help='Valid JSON configuration', default=sys.stdin)
p.set_defaults(func=load_config)
def save_subsystem_config(args):
@ -113,9 +114,10 @@ if __name__ == "__main__":
def load_subsystem_config(args):
rpc.load_subsystem_config(args.client,
sys.stdin)
args.json_conf)
p = subparsers.add_parser('load_subsystem_config', help="""Configure SPDK subsystem using JSON RPC read from stdin.""")
p = subparsers.add_parser('load_subsystem_config', help="""Configure SPDK subsystem using JSON RPC.""")
p.add_argument('-j', '--json_conf', help='Valid JSON configuration', default=sys.stdin)
p.set_defaults(func=load_subsystem_config)
# app

View File

@ -1,6 +1,9 @@
import json
import os
import sys
from io import IOBase as io
from . import app
from . import bdev
from . import blobfs
@ -68,6 +71,17 @@ def _json_dump(config, fd, indent):
fd.write('\n')
def _json_load(j):
if j == sys.stdin or isinstance(j, io):
json_conf = json.load(j)
elif os.path.exists(j):
with open(j, "r") as j:
json_conf = json.load(j)
else:
json_conf = json.loads(j)
return json_conf
def save_config(client, fd, indent=2):
"""Write current (live) configuration of SPDK subsystems and targets to stdout.
Args:
@ -94,7 +108,7 @@ def load_config(client, fd, include_aliases=False):
Args:
fd: opened file descriptor where data will be taken from
"""
json_config = json.load(fd)
json_config = _json_load(fd)
# remove subsystems with no config
subsystems = json_config['subsystems']
@ -163,7 +177,7 @@ def load_subsystem_config(client, fd):
Args:
fd: opened file descriptor where data will be taken from
"""
subsystem = json.load(fd)
subsystem = _json_load(fd)
if not subsystem['config']:
return