Commit 3a41efec authored by Alain Takoudjou's avatar Alain Takoudjou

updates

parent f3aa60e4
......@@ -69,13 +69,20 @@ def getInitialQemuResourceDict(pid_file):
return None
with open(pid_file) as f:
pid = int(f.read())
try:
pid = int(f.read())
except ValueError, e:
raise ValueError("%r is empty or doesn't contain a valid pid number: %s" % (
pid_file, str(e)))
try:
process = psutil.Process(pid)
except psutil.NoSuchProcess:
print 'No process with pid %s' % pid
return None
raise ValueError('No process with pid %s' % pid)
if not process.name().startswith('qemu-system'):
raise ValueError("The pid %s is used by %r and not a qemu process." % (
pid, process.name()))
resource_dict = {'cpu': None, 'ram': None}
cmd_list = process.cmdline()
cpu_index = cmd_list.index('-smp')
......@@ -137,7 +144,10 @@ class QemuQMPWrapper(object):
data = self.sockf.readline()
if not data:
return
response = json.loads(data)
try:
response = json.loads(data)
except ValueError:
raise ValueError('Wrong data: %s' % data)
if 'error' in response:
raise QmpCommandError(response["error"]["desc"])
if 'event' in response:
......@@ -148,18 +158,17 @@ class QemuQMPWrapper(object):
return response
def _send(self, message):
def _send(self, message, retry=0, sleep=0.5):
self.socket.sendall(json.dumps(message))
return self._readResponse()
def _sendRetry(self, message):
"""
Send Qmp message and retry once if the result is None
"""
result = self._send(message)
if result is None:
return self._send(message)
return result
response = self._readResponse()
for i in range(0, retry):
if response is not None:
break
print "Retrying send command after %s second(s)..." % sleep
time.sleep(sleep)
self.socket.sendall(json.dumps(message))
response = self._readResponse()
return response
def _getVMStatus(self):
response = self._send({'execute': 'query-status'})
......@@ -317,9 +326,9 @@ class QemuQMPWrapper(object):
return some info about VM CPUs
"""
cpu_info_dict = {'hotplugged': [], 'base': []}
cpu_list = self._sendRetry({
cpu_list = self._send({
'execute': 'query-cpus'
})['return']
}, retry=5)['return']
for cpu in cpu_list:
if 'unattached' in cpu['qom_path']:
index = 'base'
......@@ -337,9 +346,9 @@ class QemuQMPWrapper(object):
return some info about VM Memory. Can only say info about hotplugged RAM
"""
mem_info_dict = {'hotplugged': [], 'base': []}
memory_list = self._sendRetry({
memory_list = self._send({
'execute': 'query-memory-devices'
})['return']
}, retry=5)['return']
for mem in memory_list:
if mem['data']['hotplugged'] == True:
mem_info_dict['hotplugged'].append(mem['data'])
......@@ -386,6 +395,7 @@ class QemuQMPWrapper(object):
raise ValueError("Cannot remove device %s" % dev_id)
# try soft reboot of the VM
print "Powering down the VM..."
self.powerdown()
system_exited = False
# wait for ~10 seconds if the system exit, else quit Qemu
......@@ -418,7 +428,7 @@ class QemuQMPWrapper(object):
unremovable_cpu = 0
cpu_hotplugable_list = self._send({
'execute': 'query-hotpluggable-cpus'
})['return']
}, retry=5)['return']
cpu_hotplugable_list.reverse()
for cpu in cpu_hotplugable_list:
if cpu.get('qom-path', '') == '':
......@@ -508,8 +518,8 @@ class QemuQMPWrapper(object):
num_slot_used = 0
memory_id_list = [] # current hotplugged memory
cleanup_memdev_id_dict = {}
current_dimm_list = self._send({ "execute": "query-memory-devices" })
current_memdev_list = self._send({ "execute": "query-memdev" })
current_dimm_list = self._send({ "execute": "query-memory-devices" }, retry=5)
current_memdev_list = self._send({ "execute": "query-memdev" }, retry=5)
for memdev in current_memdev_list['return']:
cleanup_memdev_id_dict[memdev['id']] = ''
......
......@@ -76,7 +76,7 @@ class TestQemuQMPWrapper(unittest.TestCase):
if message['arguments']['id'].startswith('cpu'):
self.setChange('cpu', -1)
if self.fail:
return {"error": {"class": "CommandFailed", "message": ""}}
return {"error": {"class": "CommandFailed", "desc": ""}}
return {"return": {}}
def fake_getEventList(self, timeout=0, cleanup=False):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment