Commit 55014ddf authored by Jim Harris's avatar Jim Harris
Browse files

rpc: remove file parameters from save/load_config in rpc.py



User can pipe or redirect data as needed.  As features like
bdev crypto are added, storing config data in files needs to
be carefully scrutinized - don't make it easy by providing
ways to write data to files that may not be sufficiently
protected.

Signed-off-by: default avatarJim Harris <james.r.harris@intel.com>
Change-Id: I24dd32f81b965e275e1bac875f6d008d1b68007a

Reviewed-on: https://review.gerrithub.io/425165


Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
parent 5a69eb57
Loading
Loading
Loading
Loading
+12 −22
Original line number Diff line number Diff line
@@ -59,49 +59,39 @@ if __name__ == "__main__":
    @call_cmd
    def save_config(args):
        rpc.save_config(args.client,
                        filename=args.filename,
                        indent=args.indent)

    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 = subparsers.add_parser('save_config', help="""Write current (live) configuration of SPDK subsystems and targets to stdout.
    """)
    p.add_argument('-i', '--indent', help="""Indent level. Value less than 0 mean compact mode. Default indent level is 2.
    """, type=int, default=2)
    p.set_defaults(func=save_config)

    @call_cmd
    def load_config(args):
        rpc.load_config(args.client,
                        filename=args.filename)
        rpc.load_config(args.client)

    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('-f', '--filename', help="""JSON Configuration file.""")
    p = subparsers.add_parser('load_config', help="""Configure SPDK subsystems and targets using JSON RPC read from stdin.""")
    p.set_defaults(func=load_config)

    @call_cmd
    def save_subsystem_config(args):
        rpc.save_subsystem_config(args.client,
                                  filename=args.filename,
                                  indent=args.indent,
                                  name=args.name)

    p = subparsers.add_parser('save_subsystem_config', help="""Write current (live) configuration of SPDK subsystem.
    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 = subparsers.add_parser('save_subsystem_config', help="""Write current (live) configuration of SPDK subsystem to stdout.
    """)
    p.add_argument('-i', '--indent', help="""Indent level. Value less than 0 mean compact mode. Default indent level is 2.
    """, type=int, default=2)
    p.add_argument('-n', '--name', help='Name of subsystem', required=True)
    p.set_defaults(func=save_subsystem_config)

    @call_cmd
    def load_subsystem_config(args):
        rpc.load_subsystem_config(args.client,
                                  filename=args.filename)
        rpc.load_subsystem_config(args.client)

    p = subparsers.add_parser('load_subsystem_config', help="""Configure SPDK subsystem using JSON RPC. If no file is
    provided or file is '-' read configuration from stdin.""")
    p.add_argument('-f', '--filename', help="""JSON Configuration file.""")
    p = subparsers.add_parser('load_subsystem_config', help="""Configure SPDK subsystem using JSON RPC read from stdin.""")
    p.set_defaults(func=load_subsystem_config)

    # app
+25 −49
Original line number Diff line number Diff line
@@ -34,39 +34,20 @@ def get_rpc_methods(client, current=None):
    return client.call('get_rpc_methods', params)


def _json_dump(config, filename, indent):
    if filename is None:
def _json_dump(config, indent):
    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(filename, 'w') as file:
            json.dump(config, file, indent=indent)
            file.write('\n')


def _json_load(filename):
    if not filename or filename == '-':
        return json.load(sys.stdin)

    else:
        with open(filename, 'r') as file:
            return json.load(file)


def save_config(client, filename=None, indent=2):
    """Write current (live) configuration of SPDK subsystems and targets.
def save_config(client, indent=2):
    """Write current (live) configuration of SPDK subsystems and targets to stdout.
    Args:
        filename: File where to save JSON configuration to.
            Print to stdout if not provided.
        indent: Indent level. Value less than 0 mean compact mode.
            If filename is not given default then indent level is 2.
            If writing to file of filename is '-' then default is compact mode.
            Default indent level is 2.
    """
    config = {
        'subsystems': []
@@ -79,16 +60,15 @@ def save_config(client, filename=None, indent=2):
        }
        config['subsystems'].append(cfg)

    _json_dump(config, filename, indent)
    _json_dump(config, indent)


def load_config(client, filename=None):
    """Configure SPDK subsystems and tagets using JSON RPC.
def load_config(client):
    """Configure SPDK subsystems and targets using JSON RPC read from stdin.
    Args:
        filename: JSON Configuration file location.
            If no file path is provided or file is '-' then read configuration from stdin.
        none
    """
    json_config = _json_load(filename)
    json_config = json.load(sys.stdin)

    # remove subsystems with no config
    subsystems = json_config['subsystems']
@@ -132,30 +112,26 @@ def load_config(client, filename=None):
        print("Some configs were skipped because the RPC state that can call them passed over.")


def save_subsystem_config(client, filename=None, indent=2, name=None):
    """Write current (live) configuration of SPDK subsystem.
def save_subsystem_config(client, indent=2, name=None):
    """Write current (live) configuration of SPDK subsystem to stdout.
    Args:
        filename: File where to save JSON configuration to.
            Print to stdout if not provided.
        indent: Indent level. Value less than 0 mean compact mode.
            If filename is not given default then indent level is 2.
            If writing to file of filename is '-' then default is compact mode.
            Default is indent level 2.
    """
    cfg = {
        'subsystem': name,
        'config': client.call('get_subsystem_config', {"name": name})
    }

    _json_dump(cfg, filename, indent)
    _json_dump(cfg, indent)


def load_subsystem_config(client, filename=None):
    """Configure SPDK subsystem using JSON RPC.
def load_subsystem_config(client):
    """Configure SPDK subsystem using JSON RPC read from stdin.
    Args:
        filename: JSON Configuration file location.
            If no file path is provided or file is '-' then read configuration from stdin.
        none
    """
    subsystem = _json_load(filename)
    subsystem = json.load(sys.stdin)

    if not subsystem['config']:
        return
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ echo "Process pid: $pid"
trap "killprocess $pid; delete_tmp_conf_files; exit 1 " SIGINT SIGTERM EXIT

waitforlisten $pid
$rpc_py load_subsystem_config -f $testdir/iscsi.json
$rpc_py load_subsystem_config < $testdir/iscsi.json
$rpc_py start_subsystem_init
echo "iscsi_tgt is listening. Running tests..."

+3 −3
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ $rpc_py add_portal_group $PORTAL_TAG 127.0.0.1:$ISCSI_PORT
$rpc_py add_initiator_group $INITIATOR_TAG $INITIATOR_NAME $NETMASK
$rpc_py construct_malloc_bdev 64 4096 --name Malloc0
$rpc_py construct_target_node Target3 Target3_alias 'Malloc0:0' $PORTAL_TAG:$INITIATOR_TAG 64 -d
$rpc_py save_config -f $base_iscsi_config
$rpc_py save_config > $base_iscsi_config
timing_exit iscsi_json_config_create_setup

timing_enter iscsi_json_config_test
@@ -29,8 +29,8 @@ timing_enter iscsi_json_config_restart_spdk
$clear_config_py clear_config
kill_targets
run_spdk_tgt
$rpc_py load_config -f $base_iscsi_config
$rpc_py save_config -f $last_iscsi_config
$rpc_py load_config < $base_iscsi_config
$rpc_py save_config > $last_iscsi_config
timing_exit iscsi_json_config_restart_spdk

diff $base_iscsi_config $last_iscsi_config
+8 −8
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ function load_nvme() {
	echo '{"subsystems": [' > nvme_config.json
	$SPDK_BUILD_DIR/scripts/gen_nvme.sh --json >> nvme_config.json
	echo ']}' >> nvme_config.json
	$rpc_py load_config -f nvme_config.json
	$rpc_py load_config < nvme_config.json
	rm nvme_config.json
}

@@ -78,18 +78,18 @@ function kill_targets() {
# 11. Remove all files.
function test_json_config() {
	$rpc_py get_bdevs | jq '.|sort_by(.name)' > $base_bdevs
	$rpc_py save_config -f $full_config
	$rpc_py save_config > $full_config
	$JSON_DIR/config_filter.py -method "delete_global_parameters" -filename $full_config > $base_json_config
	$clear_config_py clear_config
	$rpc_py save_config -f $tmp_config
	$rpc_py save_config > $tmp_config
	$JSON_DIR/config_filter.py -method "delete_global_parameters" -filename $tmp_config > $null_json_config
	if [ "[]" != "$(jq '.subsystems | map(select(.config != null)) | map(select(.config != []))' $null_json_config)" ]; then
		echo "Config has not been cleared"
		return 1
	fi
	$rpc_py load_config -f $base_json_config
	$rpc_py load_config < $base_json_config
	$rpc_py get_bdevs | jq '.|sort_by(.name)' > $last_bdevs
	$rpc_py save_config -f $tmp_config
	$rpc_py save_config > $tmp_config
	$JSON_DIR/config_filter.py -method "delete_global_parameters" -filename $tmp_config > $last_json_config
	diff $base_json_config $last_json_config
	diff $base_bdevs $last_bdevs
@@ -184,7 +184,7 @@ function clear_bdev_subsystem_config() {
# 8. Delete all files.
function test_global_params() {
	target=$1
	$rpc_py save_config -f $full_config
	$rpc_py save_config > $full_config
	python $JSON_DIR/config_filter.py -method "delete_configs" -filename $full_config > $base_json_config
	if [ $target == "spdk_tgt" ]; then
		killprocess $spdk_tgt_pid
@@ -196,8 +196,8 @@ function test_global_params() {
		echo "Target is not specified for test_global_params"
		return 1
	fi
	$rpc_py load_config -f $full_config
	$rpc_py save_config -f $full_config
	$rpc_py load_config < $full_config
	$rpc_py save_config > $full_config
	python $JSON_DIR/config_filter.py -method "delete_configs" -filename $full_config > $last_json_config
	diff $base_json_config $last_json_config
	rm $base_json_config $last_json_config
Loading