Spdk/scripts/sma.py
Konrad Sztyber 93a20e79ec sma: loading out-of-tree plugins
It's now possible to register SMA device managers defined in a module
outside of the regular directory.  To do that, a global variable called
`devices` containing a list of device manager classes needs to be defined
in a module that's added to SMA_PLUGINS environment variable.

For example:
```
$ cat /path/to/plugins/external_devices/__init__.py
import spdk.sma as sma

class MyDeviceManager1(sma.DeviceManager):
	pass

class MyDeviceManager2(sma.DeviceManager):
	pass

devices = [MyDeviceManager1, MyDeviceManager2]

$ SMA_PLUGINS=external_devices scripts/sma.py
```

Multiple plugins can be specified by separating them with a colon, e.g.:
```
SMA_PLUGINS=plugin1:plugin2
```
Of course, the location at which these modules reside must be in
PYTHONPATH.

Signed-off-by: Konrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I2bb4cfea5191710f97d66abe3a21e4deacc6338a
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11412
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
2022-05-18 07:24:06 +00:00

54 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
from argparse import ArgumentParser
import importlib
import logging
import os
import sys
sys.path.append(os.path.dirname(__file__) + '/../python')
import spdk.sma as sma # noqa
from spdk.rpc.client import JSONRPCClient # noqa
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()
def get_build_client(sock):
def build_client():
return JSONRPCClient(sock)
return build_client
def register_device(agent, device):
device.init(None)
agent.register_device(device)
def load_plugins(agent, client, plugins):
for plugin in plugins:
module = importlib.import_module(plugin)
for device in getattr(module, 'devices', []):
logging.debug(f'Loading external device: {plugin}.{device.__name__}')
register_device(agent, device(client))
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),
filter(None, os.environ.get('SMA_PLUGINS', '').split(':')))
agent.run()