Commit 871214e2 authored by Michal Berger's avatar Michal Berger Committed by Tomasz Zawadzki
Browse files

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" 8b98cdb6

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: default avatarMichal Berger <michalx.berger@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/2106


Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
parent 7a2bf6fc
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -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
+16 −2
Original line number Diff line number Diff line
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