diff --git a/scripts/rpc.py b/scripts/rpc.py index d75bebafe..130d060ab 100755 --- a/scripts/rpc.py +++ b/scripts/rpc.py @@ -21,6 +21,18 @@ if __name__ == "__main__": p = subparsers.add_parser('get_rpc_methods', help='Get list of supported RPC methods') p.set_defaults(func=rpc.get_rpc_methods) + p = subparsers.add_parser('save_config', help="""Write current (live) configuration of SPDK subsystems and targets. + If no filename is given write configuration to stdout.""") + p.add_argument('-f', '--filename', help="""File where to save JSON configuration to.""") + p.add_argument('-i', '--indent', help="""Indent level. Value less than 0 mean compact mode. If filename is not given default + indent level is 2. If writing to file of filename is '-' then default is compact mode.""", type=int, default=2) + p.set_defaults(func=rpc.save_config) + + p = subparsers.add_parser('load_config', help="""Configure SPDK subsystems and tagets using JSON RPC. If no file is + provided or file is '-' read configuration from stdin.""") + p.add_argument('--filename', help="""JSON Configuration file.""") + p.set_defaults(func=rpc.load_config) + # app p = subparsers.add_parser('kill_instance', help='Send signal to instance') p.add_argument('sig_name', help='signal will be sent to server.') diff --git a/scripts/rpc/__init__.py b/scripts/rpc/__init__.py index da8b97faf..8254fbe17 100755 --- a/scripts/rpc/__init__.py +++ b/scripts/rpc/__init__.py @@ -10,7 +10,53 @@ import nvmf import pmem import subsystem import vhost +import json +import sys def get_rpc_methods(args): print_dict(args.client.call('get_rpc_methods')) + + +def save_config(args): + config = { + 'subsystems': [] + } + + for elem in args.client.call('get_subsystems'): + cfg = { + 'subsystem': elem['subsystem'], + 'config': args.client.call('get_subsystem_config', {"name": elem['subsystem']}) + } + config['subsystems'].append(cfg) + + indent = args.indent + if args.filename is None: + if indent is None: + indent = 2 + elif indent < 0: + indent = None + json.dump(config, sys.stdout, indent=indent) + sys.stdout.write('\n') + else: + if indent is None or indent < 0: + indent = None + with open(args.filename, 'w') as file: + json.dump(config, file, indent=indent) + file.write('\n') + + +def load_config(args): + if not args.filename or args.filename == '-': + config = json.load(sys.stdin) + else: + with open(args.filename, 'r') as file: + config = json.load(file) + + for subsystem in config['subsystems']: + name = subsystem['subsystem'] + config = subsystem['config'] + if not config: + continue + for elem in subsystem['config']: + args.client.call(elem['method'], elem['params'])