Async qemu monitor reading

This commit is contained in:
Julien Duponchelle committed 2015-02-21 00:15:56 +01:00
1 parent 45a48cfcc1
commit cecf2f5014
5 files changed
+74 -16

No files matched your search

+21 -1
View File
@@ -205,7 +205,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Reload a Qemu.instance")
description="Suspend a Qemu.instance")
def suspend(request, response):
qemu_manager = Qemu.instance()
@@ -213,6 +213,26 @@ class QEMUHandler:
yield from vm.suspend()
response.set_status(204)
@classmethod
@Route.post(
r"/projects/{project_id}/qemu/vms/{vm_id}/resume",
parameters={
"project_id": "UUID for the project",
"vm_id": "UUID for the instance",
},
status_codes={
204: "Instance resumed",
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Resume a Qemu.instance")
def resume(request, response):
qemu_manager = Qemu.instance()
vm = qemu_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
yield from vm.resume()
response.set_status(204)
@Route.post(
r"/projects/{project_id}/qemu/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio",
parameters={
+14 -15
View File
@@ -612,13 +612,12 @@ class QemuVM(BaseVM):
self._stop_cpulimit()
@asyncio.coroutine
def _control_vm(self, command, expected=None, timeout=30):
def _control_vm(self, command, expected=None):
"""
Executes a command with QEMU monitor when this VM is running.
:param command: QEMU monitor command (e.g. info status, stop etc.)
:params expected: An array with the string attended (Default None)
:param timeout: how long to wait for QEMU monitor
:returns: result of the command (Match object or None)
"""
@@ -627,25 +626,29 @@ class QemuVM(BaseVM):
if self.is_running() and self._monitor:
log.debug("Execute QEMU monitor command: {}".format(command))
try:
tn = telnetlib.Telnet(self._monitor_host, self._monitor, timeout=timeout)
reader, writer = yield from asyncio.open_connection("127.0.0.1", self._monitor)
except OSError as e:
log.warn("Could not connect to QEMU monitor: {}".format(e))
return result
try:
tn.write(command.encode('ascii') + b"\n")
time.sleep(0.1)
writer.write(command.encode('ascii') + b"\n")
except OSError as e:
log.warn("Could not write to QEMU monitor: {}".format(e))
tn.close()
writer.close()
return result
if expected:
try:
ind, match, dat = tn.expect(list=expected, timeout=timeout)
if match:
result = match
while result is None:
line = yield from reader.readline()
if not line:
break
for expect in expected:
if expect in line:
result = line
break
except EOFError as e:
log.warn("Could not read from QEMU monitor: {}".format(e))
tn.close()
writer.close()
return result
@asyncio.coroutine
@@ -667,11 +670,7 @@ class QemuVM(BaseVM):
:returns: status (string)
"""
result = None
match = yield from self._control_vm("info status", [b"running", b"paused"])
if match:
result = match.group(0).decode('ascii')
result = yield from self._control_vm("info status", [b"running", b"paused"])
return result
@asyncio.coroutine
+2
View File
@@ -142,6 +142,8 @@ class Server:
@asyncio.coroutine
def start_shell(self):
from ptpython.repl import embed
from gns3server.modules import Qemu
yield from embed(globals(), locals(), return_asyncio_coroutine=True, patch_stdout=True)
def run(self):