From d2db395987c114b3b11d4f6fac93a7436b2876eb Mon Sep 17 00:00:00 2001 From: Konrad Sztyber Date: Tue, 22 Feb 2022 14:53:06 +0100 Subject: [PATCH] sma: load configuration from file This patch adds support for loading configuration from a yaml-formatted file. The command-line options takes precedence over those from the config file. Signed-off-by: Konrad Sztyber Change-Id: Iafb5b4aec0a64afe358fce2d6c55361fc4093223 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11712 Community-CI: Broadcom CI Tested-by: SPDK CI Jenkins Reviewed-by: Karol Latecki Reviewed-by: Tomasz Zawadzki Reviewed-by: Ben Walker Reviewed-by: Jim Harris Reviewed-by: Monica Kenguva --- scripts/pkgdep/arch.sh | 1 + scripts/pkgdep/debian.sh | 1 + scripts/pkgdep/rhel.sh | 1 + scripts/sma.py | 44 ++++++++++++++++++++++++++++++---------- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/scripts/pkgdep/arch.sh b/scripts/pkgdep/arch.sh index 252302e2b..9e3a3c252 100755 --- a/scripts/pkgdep/arch.sh +++ b/scripts/pkgdep/arch.sh @@ -11,6 +11,7 @@ pip install ijson pip install python-magic pip install grpcio pip install grpcio-tools +pip install pyyaml # Additional dependencies for DPDK pacman -Sy --needed --noconfirm numactl nasm # Additional dependencies for ISA-L used in compression diff --git a/scripts/pkgdep/debian.sh b/scripts/pkgdep/debian.sh index 62550194c..9df697a98 100755 --- a/scripts/pkgdep/debian.sh +++ b/scripts/pkgdep/debian.sh @@ -17,6 +17,7 @@ pip3 install ijson pip3 install python-magic pip3 install grpcio pip3 install grpcio-tools +pip3 install pyyaml # Additional dependencies for SPDK CLI - not available on older Ubuntus apt-get install -y python3-configshell-fb python3-pexpect || echo \ "Note: Some SPDK CLI dependencies could not be installed." diff --git a/scripts/pkgdep/rhel.sh b/scripts/pkgdep/rhel.sh index dbd2ac8e9..ba53eeeef 100755 --- a/scripts/pkgdep/rhel.sh +++ b/scripts/pkgdep/rhel.sh @@ -105,6 +105,7 @@ pip3 install ijson pip3 install python-magic pip3 install grpcio pip3 install grpcio-tools +pip3 install pyyaml # Additional dependencies for SPDK CLI - not available in rhel and centos if ! echo "$ID $VERSION_ID" | grep -E -q 'rhel 7|centos 7'; then diff --git a/scripts/sma.py b/scripts/sma.py index 555b19a3f..303181f8c 100755 --- a/scripts/sma.py +++ b/scripts/sma.py @@ -5,6 +5,7 @@ import importlib import logging import os import sys +import yaml sys.path.append(os.path.dirname(__file__) + '/../python') @@ -12,15 +13,34 @@ import spdk.sma as sma # noqa from spdk.rpc.client import JSONRPCClient # noqa +def parse_config(path): + if path is None: + return {} + with open(path, 'r') as cfgfile: + config = yaml.load(cfgfile, Loader=yaml.FullLoader) + return {**config} if config is not None else {} + + def parse_argv(): parser = ArgumentParser(description='Storage Management Agent command line interface') - parser.add_argument('--address', '-a', default='localhost', - help='IP address to listen on') - parser.add_argument('--socket', '-s', default='/var/tmp/spdk.sock', - help='SPDK RPC socket') - parser.add_argument('--port', '-p', default=8080, type=int, - help='IP port to listen on') - return parser.parse_args() + parser.add_argument('--address', '-a', help='IP address to listen on') + parser.add_argument('--socket', '-s', help='SPDK RPC socket') + parser.add_argument('--port', '-p', type=int, help='IP port to listen on') + parser.add_argument('--config', '-c', help='Path to config file') + defaults = {'address': 'localhost', + 'socket': '/var/tmp/spdk.sock', + 'port': 8080} + # Merge the default values, config file, and the command-line + args = vars(parser.parse_args()) + config = parse_config(args.get('config')) + for argname, argvalue in defaults.items(): + if args.get(argname) is not None: + if config.get(argname) is not None: + logging.info(f'Overriding "{argname}" value from command-line') + config[argname] = args[argname] + if config.get(argname) is None: + config[argname] = argvalue + return config def get_build_client(sock): @@ -44,10 +64,12 @@ def load_plugins(agent, client, plugins): if __name__ == '__main__': - argv = parse_argv() logging.basicConfig(level=os.environ.get('SMA_LOGLEVEL', 'WARNING').upper()) - agent = sma.StorageManagementAgent(argv.address, argv.port) - register_device(agent, sma.NvmfTcpDeviceManager(get_build_client(argv.socket))) - load_plugins(agent, get_build_client(argv.socket), + + config = parse_argv() + agent = sma.StorageManagementAgent(config['address'], config['port']) + register_device(agent, sma.NvmfTcpDeviceManager(get_build_client(config['socket']))) + load_plugins(agent, get_build_client(config['socket']), config.get('plugins') or []) + load_plugins(agent, get_build_client(config['socket']), filter(None, os.environ.get('SMA_PLUGINS', '').split(':'))) agent.run()