2018-03-09 20:19:34 +00:00
|
|
|
import json
|
|
|
|
import sys
|
2017-06-06 21:22:03 +00:00
|
|
|
|
2018-03-20 00:37:58 +00:00
|
|
|
from . import app
|
|
|
|
from . import bdev
|
2018-06-12 23:31:20 +00:00
|
|
|
from . import ioat
|
2018-03-20 00:37:58 +00:00
|
|
|
from . import iscsi
|
|
|
|
from . import log
|
|
|
|
from . import lvol
|
|
|
|
from . import nbd
|
|
|
|
from . import net
|
|
|
|
from . import nvmf
|
|
|
|
from . import pmem
|
|
|
|
from . import subsystem
|
|
|
|
from . import vhost
|
2018-05-31 02:49:45 +00:00
|
|
|
from . import client as rpc_client
|
2018-03-20 00:37:58 +00:00
|
|
|
|
2017-06-06 21:22:03 +00:00
|
|
|
|
2018-05-02 04:50:39 +00:00
|
|
|
def start_subsystem_init(client):
|
|
|
|
return client.call('start_subsystem_init')
|
|
|
|
|
|
|
|
|
2018-05-04 01:39:27 +00:00
|
|
|
def get_rpc_methods(client, args):
|
|
|
|
params = {}
|
|
|
|
|
|
|
|
if args.current:
|
|
|
|
params['current'] = args.current
|
|
|
|
|
|
|
|
return client.call('get_rpc_methods', params)
|
2018-03-09 20:19:34 +00:00
|
|
|
|
|
|
|
|
2018-06-21 01:45:00 +00:00
|
|
|
def _json_dump(config, filename, indent):
|
|
|
|
if 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(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)
|
|
|
|
|
|
|
|
|
2018-03-27 21:31:52 +00:00
|
|
|
def save_config(client, args):
|
2018-03-09 20:19:34 +00:00
|
|
|
config = {
|
|
|
|
'subsystems': []
|
|
|
|
}
|
|
|
|
|
2018-03-27 21:31:52 +00:00
|
|
|
for elem in client.call('get_subsystems'):
|
2018-03-09 20:19:34 +00:00
|
|
|
cfg = {
|
|
|
|
'subsystem': elem['subsystem'],
|
2018-03-27 21:31:52 +00:00
|
|
|
'config': client.call('get_subsystem_config', {"name": elem['subsystem']})
|
2018-03-09 20:19:34 +00:00
|
|
|
}
|
|
|
|
config['subsystems'].append(cfg)
|
|
|
|
|
2018-06-21 01:45:00 +00:00
|
|
|
_json_dump(config, args.filename, args.indent)
|
2018-03-09 20:19:34 +00:00
|
|
|
|
|
|
|
|
2018-04-03 06:12:36 +00:00
|
|
|
def load_config(client, args):
|
2018-06-21 01:45:00 +00:00
|
|
|
json_config = _json_load(args.filename)
|
2018-05-14 14:58:01 +00:00
|
|
|
|
2018-06-21 01:53:46 +00:00
|
|
|
# remove subsystems with no config
|
2018-05-14 14:58:01 +00:00
|
|
|
subsystems = json_config['subsystems']
|
2018-06-21 01:53:46 +00:00
|
|
|
for subsystem in list(subsystems):
|
|
|
|
if not subsystem['config']:
|
|
|
|
subsystems.remove(subsystem)
|
|
|
|
|
|
|
|
# check if methods in the config file are known
|
|
|
|
allowed_methods = client.call('get_rpc_methods')
|
|
|
|
for subsystem in list(subsystems):
|
|
|
|
config = subsystem['config']
|
|
|
|
for elem in list(config):
|
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
|
|
|
raise rpc_client.JSONRPCException("Unknown method was included in the config file")
|
|
|
|
|
2018-05-14 14:58:01 +00:00
|
|
|
while subsystems:
|
|
|
|
allowed_methods = client.call('get_rpc_methods', {'current': True})
|
|
|
|
allowed_found = False
|
|
|
|
|
|
|
|
for subsystem in list(subsystems):
|
|
|
|
config = subsystem['config']
|
|
|
|
for elem in list(config):
|
2018-06-21 01:53:46 +00:00
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
2018-05-14 14:58:01 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
client.call(elem['method'], elem['params'])
|
|
|
|
config.remove(elem)
|
|
|
|
allowed_found = True
|
|
|
|
|
|
|
|
if not config:
|
|
|
|
subsystems.remove(subsystem)
|
|
|
|
|
|
|
|
if 'start_subsystem_init' in allowed_methods:
|
|
|
|
client.call('start_subsystem_init')
|
|
|
|
allowed_found = True
|
|
|
|
|
2018-06-21 01:53:46 +00:00
|
|
|
if not allowed_found:
|
|
|
|
break
|
|
|
|
|
|
|
|
if subsystems:
|
|
|
|
print("Some configs were skipped because the RPC state that can call them passed over.")
|
2018-05-16 19:45:39 +00:00
|
|
|
|
|
|
|
|
2018-06-21 23:44:54 +00:00
|
|
|
def save_subsystem_config(client, args):
|
|
|
|
cfg = {
|
|
|
|
'subsystem': args.name,
|
|
|
|
'config': client.call('get_subsystem_config', {"name": args.name})
|
|
|
|
}
|
|
|
|
|
|
|
|
_json_dump(cfg, args.filename, args.indent)
|
|
|
|
|
|
|
|
|
2018-05-16 19:45:39 +00:00
|
|
|
def load_subsystem_config(client, args):
|
2018-06-24 23:06:09 +00:00
|
|
|
subsystem = _json_load(args.filename)
|
2018-05-16 19:45:39 +00:00
|
|
|
|
2018-06-24 23:06:09 +00:00
|
|
|
if not subsystem['config']:
|
|
|
|
return
|
|
|
|
|
|
|
|
allowed_methods = client.call('get_rpc_methods')
|
|
|
|
config = subsystem['config']
|
|
|
|
for elem in list(config):
|
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
|
|
|
raise rpc_client.JSONRPCException("Unknown method was included in the config file")
|
|
|
|
|
|
|
|
allowed_methods = client.call('get_rpc_methods', {'current': True})
|
|
|
|
for elem in list(config):
|
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
2018-05-16 19:45:39 +00:00
|
|
|
continue
|
2018-06-24 23:06:09 +00:00
|
|
|
|
2018-05-16 19:45:39 +00:00
|
|
|
client.call(elem['method'], elem['params'])
|
2018-06-24 23:06:09 +00:00
|
|
|
config.remove(elem)
|
|
|
|
|
|
|
|
if config:
|
|
|
|
print("Some configs were skipped because they cannot be called in the current RPC state.")
|