Francesco Romani has uploaded a new change for review.
Change subject: client: vm params from JSON
......................................................................
client: vm params from JSON
the 'create' parameter accepts a configuration
file as source to define the parameters of the VM
being created.
This patch add support to
1. use JSON for this configuration file, so there is
little to no need to reformat data from Engine,
for example took from VDSM logs
2. use stdin as input, avoiding the need to write
(temporary) files.
Change-Id: Ic6a73bc0e5fe87eb1a48c708e7ea316c6d9df7e5
Signed-off-by: Francesco Romani <fromani(a)redhat.com>
---
M client/vdsClient.py
1 file changed, 95 insertions(+), 68 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/92/35292/1
diff --git a/client/vdsClient.py b/client/vdsClient.py
index aabf7a1..583a0a2 100644
--- a/client/vdsClient.py
+++ b/client/vdsClient.py
@@ -19,6 +19,7 @@
import sys
import ast
+import json
import getopt
import traceback
import xmlrpclib
@@ -204,74 +205,13 @@
sys.exit(response['status']['code'])
def do_create(self, args):
- params = {}
- drives = []
- devices = []
- cpuPinning = {}
- numaTune = {}
- guestNumaNodes = []
- confLines = []
- confFile = open(args[0])
- for line in confFile.readlines():
- line = re.sub("\s+", '', line)
- line = re.sub("\#.*", '', line)
- if line:
- confLines.append(line)
- if len(args) > 1:
- confLines.extend(args[1:])
- for line in confLines:
- if '=' in line:
- param, value = line.split("=", 1)
- if param == 'devices':
- devices.append(self._parseDriveSpec(value))
- elif param == 'drive':
- drives.append(self._parseDriveSpec(value))
- elif param == 'cpuPinning':
- cpuPinning, rStr = self._parseNestedSpec(value)
- elif param == 'numaTune':
- numaTune, rStr = self._parseNestedSpec(value)
- elif param == 'guestNumaNodes':
- guestNumaNodes.append(self._parseDriveSpec(value))
- elif param.startswith('custom_'):
- if 'custom' not in params:
- params['custom'] = {}
- params['custom'][param[7:]] = value
- else:
- if param in ('cdrom', 'floppy'):
- value = self._parseDriveSpec(value)
- params[param] = value
- else:
- params[line.strip()] = ''
- if cpuPinning:
- params['cpuPinning'] = cpuPinning
- if numaTune:
- params['numaTune'] = numaTune
- if guestNumaNodes:
- params['guestNumaNodes'] = guestNumaNodes
- if drives:
- params['drives'] = drives
- if devices:
- params['devices'] = devices
- # Backward compatibility for vdsClient users
- if 'vt' in params:
- params['kvmEnable'] = params['vt']
-
- if 'imageFile' in params:
- params['hda'] = params['imageFile']
-
- drives = ['hdd', 'hdc', 'hdb']
- if 'moreImages' in params:
- for image in params['moreImages'].split(','):
- params[drives.pop()] = image
-
- if 'sysprepInf' in params:
- infFile = open(params['sysprepInf'], 'rb')
- try:
- params['sysprepInf'] = xmlrpclib.Binary(infFile.read())
- finally:
- infFile.close()
-
- return self.ExecAndExit(self.s.create(params))
+ if args[0] == '-' or args[0].endswith('.json'):
+ params = self._readVmParamsFromJson(args)
+ else:
+ params = self._readVmParamsFromConf(args)
+ if params is not None:
+ return self.ExecAndExit(self.s.create(params))
+ return sys.exit(1)
def vmUpdateDevice(self, args):
params = self._eqSplit(args[1:])
@@ -1884,6 +1824,93 @@
return status['status']['code'],
status['status']['message']
+ def _readVmParamsFromJson(self, args):
+ if args[0] == "-":
+ data = sys.stdin.read()
+ else:
+ try:
+ with open(args[0]) as f:
+ data = f.read()
+ except IOError as e:
+ print "failed to read VM config: %s" % str(e)
+ return None
+ try:
+ return json.loads(data)
+ except ValueError as e:
+ print "failed to decode VM config: %s" % str(e)
+ return None
+
+ def _readVmParamsFromConf(self, args):
+ params = {}
+ drives = []
+ devices = []
+ cpuPinning = {}
+ numaTune = {}
+ guestNumaNodes = []
+ confLines = []
+ confFile = open(args[0])
+ for line in confFile.readlines():
+ line = re.sub("\s+", '', line)
+ line = re.sub("\#.*", '', line)
+ if line:
+ confLines.append(line)
+ if len(args) > 1:
+ confLines.extend(args[1:])
+ for line in confLines:
+ if '=' in line:
+ param, value = line.split("=", 1)
+ if param == 'devices':
+ devices.append(self._parseDriveSpec(value))
+ elif param == 'drive':
+ drives.append(self._parseDriveSpec(value))
+ elif param == 'cpuPinning':
+ cpuPinning, rStr = self._parseNestedSpec(value)
+ elif param == 'numaTune':
+ numaTune, rStr = self._parseNestedSpec(value)
+ elif param == 'guestNumaNodes':
+ guestNumaNodes.append(self._parseDriveSpec(value))
+ elif param.startswith('custom_'):
+ if 'custom' not in params:
+ params['custom'] = {}
+ params['custom'][param[7:]] = value
+ else:
+ if param in ('cdrom', 'floppy'):
+ value = self._parseDriveSpec(value)
+ params[param] = value
+ else:
+ params[line.strip()] = ''
+ if cpuPinning:
+ params['cpuPinning'] = cpuPinning
+ if numaTune:
+ params['numaTune'] = numaTune
+ if guestNumaNodes:
+ params['guestNumaNodes'] = guestNumaNodes
+ if drives:
+ params['drives'] = drives
+ if devices:
+ params['devices'] = devices
+ # Backward compatibility for vdsClient users
+ if 'vt' in params:
+ params['kvmEnable'] = params['vt']
+
+ if 'imageFile' in params:
+ params['hda'] = params['imageFile']
+
+ drives = ['hdd', 'hdc', 'hdb']
+ if 'moreImages' in params:
+ for image in params['moreImages'].split(','):
+ params[drives.pop()] = image
+
+ if 'sysprepInf' in params:
+ infFile = open(params['sysprepInf'], 'rb')
+ try:
+ params['sysprepInf'] = xmlrpclib.Binary(infFile.read())
+ finally:
+ infFile.close()
+
+ return params
+
+
if __name__ == '__main__':
if _glusterEnabled:
serv = ge.GlusterService()
--
To view, visit
http://gerrit.ovirt.org/35292
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic6a73bc0e5fe87eb1a48c708e7ea316c6d9df7e5
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Francesco Romani <fromani(a)redhat.com>