Irit Goihman has uploaded a new change for review.
Change subject: wip: jsonrpc: introduce new cli
......................................................................
wip: jsonrpc: introduce new cli
Change-Id: Idd45d7e88bf2246beaf30550b12201917f32c354
Signed-off-by: Irit goihman <igoihman(a)redhat.com>
---
M lib/vdsm/Makefile.am
A lib/vdsm/jsonrpccli.py
M vdsm.spec.in
3 files changed, 200 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/02/64502/1
diff --git a/lib/vdsm/Makefile.am b/lib/vdsm/Makefile.am
index d248d30..1022380 100644
--- a/lib/vdsm/Makefile.am
+++ b/lib/vdsm/Makefile.am
@@ -35,6 +35,7 @@
hooks.py \
hostdev.py \
jobs.py \
+ jsonrpccli.py \
jsonrpcvdscli.py \
kaxmlrpclib.py \
libvirtconnection.py \
diff --git a/lib/vdsm/jsonrpccli.py b/lib/vdsm/jsonrpccli.py
new file mode 100644
index 0000000..13d4d55
--- /dev/null
+++ b/lib/vdsm/jsonrpccli.py
@@ -0,0 +1,198 @@
+#
+# Copyright 2016 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+from __future__ import absolute_import
+
+from functools import partial
+from uuid import uuid4
+
+from yajsonrpc import stompreactor
+from yajsonrpc import \
+ JsonRpcRequest, \
+ JsonRpcNoResponseError, \
+ CALL_TIMEOUT
+
+from api import vdsmapi
+from .config import config
+
+
+_COMMAND_CONVERTER = {
+ 'activateStorageDomain': 'StorageDomain.activate',
+ 'connectStoragePool': 'StoragePool.connect',
+ 'connectStorageServer': 'StoragePool.connectStorageServer',
+ 'cont': 'VM.cont',
+ 'clearTask': 'Task.clear',
+ 'create': 'VM.create',
+ 'createStorageDomain': 'StorageDomain.create',
+ 'createStoragePool': 'StoragePool.create',
+ 'createVG': 'LVMVolumeGroup.create',
+ 'createVolume': 'Volume.create',
+ 'destroy': 'VM.destroy',
+ 'destroyStoragePool': 'StoragePool.destroy',
+ 'detachStorageDomain': 'StorageDomain.detach',
+ 'disconnectStoragePool': 'StoragePool.disconnect',
+ 'disconnectStorageServer': 'StoragePool.disconnectStorageServer',
+ 'discoverSendTargets': 'ISCSIConnection.discoverSendTargets',
+ 'extendVolumeSize': 'Volume.extendSize',
+ 'formatStorageDomain': 'StorageDomain.format',
+ 'fullList': 'Host.getVMFullList',
+ 'getAllTasksInfo': 'Host.getAllTasksInfo',
+ 'getAllTasksStatuses': 'Host.getAllTasksStatuses',
+ 'getAllVmStats': 'Host.getAllVmStats',
+ 'getConnectedStoragePoolsList': 'Host.getConnectedStoragePools',
+ 'getDeviceList': 'Host.getDeviceList',
+ 'getImagesList': 'StorageDomain.getImages',
+ 'getIoTunePolicy': 'VM.getIoTunePolicy',
+ 'getIoTune': 'VM.getIoTune',
+ 'getSpmStatus': 'StoragePool.getSpmStatus',
+ 'getStorageDomainInfo': 'StorageDomain.getInfo',
+ 'getStorageDomainsList': 'Host.getStorageDomains',
+ 'getStorageDomainStats': 'StorageDomain.getStats',
+ 'getStoragePoolInfo': 'StoragePool.getInfo',
+ 'getTaskInfo': 'Task.getInfo',
+ 'getTaskStatus': 'Task.getStatus',
+ 'getVdsCapabilities': 'Host.getCapabilities',
+ 'getVdsHardwareInfo': 'Host.getHardwareInfo',
+ 'getVdsStats': 'Host.getStats',
+ 'getVGInfo': 'LVMVolumeGroup.getInfo',
+ 'getVolumeInfo': 'Volume.getInfo',
+ 'getVmStats': 'VM.getStats',
+ 'getVolumeSize': 'Volume.getSize',
+ 'getVolumesList': 'StorageDomain.getVolumes',
+ 'glusterTasksList': 'GlusterTask.list',
+ 'glusterVolumeCreate': 'GlusterVolume.create',
+ 'glusterVolumeSet': 'GlusterVolume.set',
+ 'glusterVolumesList': 'GlusterVolume.list',
+ 'glusterVolumeStart': 'GlusterVolume.start',
+ 'glusterTasksList': 'GlusterTask.list',
+ 'hotplugDisk': 'VM.hotplugDisk',
+ 'hotplugNic': 'VM.hotplugNic',
+ 'hotunplugDisk': 'VM.hotunplugDisk',
+ 'hotunplugNic': 'VM.hotunplugNic',
+ 'list': 'Host.getVMList',
+ 'migrate': 'VM.migrate',
+ 'migrateStatus': 'VM.getMigrationStatus',
+ 'migrationCreate': 'VM.migrationCreate',
+ 'ping': 'Host.ping',
+ 'prepareImage': 'Image.prepare',
+ 'repoStats': 'Host.getStorageRepoStats',
+ 'setBalloonTarget': 'VM.setBalloonTarget',
+ 'setCpuTunePeriod': 'VM.setCpuTunePeriod',
+ 'setCpuTuneQuota': 'VM.setCpuTuneQuota',
+ 'setKsmTune': 'Host.setKsmTune',
+ 'setMOMPolicy': 'Host.setMOMPolicy',
+ 'setSafeNetworkConfig': 'Host.setSafeNetworkConfig',
+ 'setupNetworks': 'Host.setupNetworks',
+ 'setVmTicket': 'VM.setTicket',
+ 'setIoTune': 'VM.setIoTune',
+ 'setVolumeDescription': 'Volume.setDescription',
+ 'shutdown': 'VM.shutdown',
+ 'spmStart': 'StoragePool.spmStart',
+ 'spmStop': 'StoragePool.spmStop',
+ 'startMonitoringDomain': 'Host.startMonitoringDomain',
+ 'stopMonitoringDomain': 'Host.stopMonitoringDomain',
+ 'updateVmPolicy': 'VM.updateVmPolicy',
+ 'validateStorageDomain': 'StorageDomain.validate',
+}
+
+
+class _Server(object):
+
+ def __init__(self, client):
+ api_strict_mode = config.getboolean('devel', 'api_strict_mode')
+ self._schema = vdsmapi.Schema([vdsmapi.find_schema()],
+ api_strict_mode)
+ self._client = client
+ self._default_timeout = CALL_TIMEOUT
+ self._timeouts = {
+ 'migrationCreate': config.getint(
+ 'vars', 'migration_create_timeout'),
+ }
+
+ def set_default_timeout(self, timeout):
+ self._default_timeout = timeout
+
+ def _prepare_args(self, className, methodName, args, kwargs):
+ allargs = self._schema.get_arg_names(vdsmapi.MethodRep(className,
+ methodName))
+ params = dict(zip(allargs, args))
+ params.update(kwargs)
+ return params
+
+ def _callMethod(self, methodName, *args, **kwargs):
+ try:
+ # TODO: method+params should be extracted from schema
+ method = _COMMAND_CONVERTER[methodName]
+ except KeyError as e:
+ raise Exception("Attempt to call function: %s with "
+ "arguments: %s error: %s" %
+ (methodName, args, e))
+
+ class_name, method_name = method.split('.')
+ params = self._prepare_args(class_name, method_name, args, kwargs)
+
+ req = JsonRpcRequest(method, params, reqId=str(uuid4()))
+
+ responses = self._client.call(
+ req, timeout=self._timeouts.get(
+ method_name,
+ kwargs.pop('_transport_timeout', self._default_timeout)))
+ if responses:
+ resp = responses[0]
+ else:
+ raise JsonRpcNoResponseError(method)
+ # TODO: raise exception on error
+ return resp.result
+
+ def migrationCreate(self, params, incomingLimit=None):
+ args = [params]
+ if incomingLimit is not None:
+ args.append(incomingLimit)
+ return self._callMethod('migrationCreate',
+ params['vmId'],
+ *args)
+
+ def create(self, params):
+ return self._callMethod('create',
+ params['vmId'],
+ params)
+
+ def __getattr__(self, methodName):
+ return partial(self._callMethod, methodName)
+
+ def close(self):
+ self._client.close()
+
+ def __del__(self):
+ self._client.close()
+
+
+def connect(host=None, port=None, useSSL=None):
+ if host is None:
+ host = 'localhost'
+ if port is None:
+ port = int(config.getint('addresses', 'management_port'))
+
+ if useSSL is None:
+ useSSL = config.getboolean('vars', 'ssl')
+
+ client = stompreactor.SimpleClient(host, port, useSSL)
+
+ return _Server(client)
diff --git a/vdsm.spec.in b/vdsm.spec.in
index 80360d0..914e3bf 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -1181,6 +1181,7 @@
%{python_sitelib}/%{vdsm_name}/hooks.py*
%{python_sitelib}/%{vdsm_name}/hostdev.py*
%{python_sitelib}/%{vdsm_name}/jobs.py*
+%{python_sitelib}/%{vdsm_name}/jsonrpccli.py*
%{python_sitelib}/%{vdsm_name}/jsonrpcvdscli.py*
%{python_sitelib}/%{vdsm_name}/kaxmlrpclib.py*
%{python_sitelib}/%{vdsm_name}/libvirtconnection.py*
--
To view, visit
https://gerrit.ovirt.org/64502
To unsubscribe, visit
https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Idd45d7e88bf2246beaf30550b12201917f32c354
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Irit Goihman <igoihman(a)redhat.com>