Revision a94d2a99 lib/hypervisor/hv_kvm.py
b/lib/hypervisor/hv_kvm.py | ||
---|---|---|
360 | 360 |
return self.data == other.data |
361 | 361 |
|
362 | 362 |
|
363 |
class QmpConnection: |
|
364 |
"""Connection to the QEMU Monitor using the QEMU Monitor Protocol (QMP). |
|
365 |
|
|
366 |
""" |
|
367 |
_FIRST_MESSAGE_KEY = "QMP" |
|
368 |
_EVENT_KEY = "event" |
|
369 |
_ERROR_KEY = "error" |
|
370 |
_RETURN_KEY = RETURN_KEY = "return" |
|
371 |
_ACTUAL_KEY = ACTUAL_KEY = "actual" |
|
372 |
_ERROR_CLASS_KEY = "class" |
|
373 |
_ERROR_DATA_KEY = "data" |
|
374 |
_ERROR_DESC_KEY = "desc" |
|
375 |
_EXECUTE_KEY = "execute" |
|
376 |
_ARGUMENTS_KEY = "arguments" |
|
377 |
_CAPABILITIES_COMMAND = "qmp_capabilities" |
|
378 |
_MESSAGE_END_TOKEN = "\r\n" |
|
363 |
class MonitorSocket(object): |
|
379 | 364 |
_SOCKET_TIMEOUT = 5 |
380 | 365 |
|
381 | 366 |
def __init__(self, monitor_filename): |
382 |
"""Instantiates the QmpConnection object.
|
|
367 |
"""Instantiates the MonitorSocket object.
|
|
383 | 368 |
|
384 | 369 |
@type monitor_filename: string |
385 | 370 |
@param monitor_filename: the filename of the UNIX raw socket on which the |
386 |
QMP monitor is listening
|
|
371 |
monitor (QMP or simple one) is listening
|
|
387 | 372 |
|
388 | 373 |
""" |
389 | 374 |
self.monitor_filename = monitor_filename |
... | ... | |
392 | 377 |
# in a reasonable amount of time |
393 | 378 |
self.sock.settimeout(self._SOCKET_TIMEOUT) |
394 | 379 |
self._connected = False |
395 |
self._buf = "" |
|
396 | 380 |
|
397 | 381 |
def _check_socket(self): |
398 | 382 |
sock_stat = None |
... | ... | |
400 | 384 |
sock_stat = os.stat(self.monitor_filename) |
401 | 385 |
except EnvironmentError, err: |
402 | 386 |
if err.errno == errno.ENOENT: |
403 |
raise errors.HypervisorError("No qmp socket found")
|
|
387 |
raise errors.HypervisorError("No monitor socket found")
|
|
404 | 388 |
else: |
405 |
raise errors.HypervisorError("Error checking qmp socket: %s",
|
|
389 |
raise errors.HypervisorError("Error checking monitor socket: %s",
|
|
406 | 390 |
utils.ErrnoOrStr(err)) |
407 | 391 |
if not stat.S_ISSOCK(sock_stat.st_mode): |
408 |
raise errors.HypervisorError("Qmp socket is not a socket")
|
|
392 |
raise errors.HypervisorError("Monitor socket is not a socket")
|
|
409 | 393 |
|
410 | 394 |
def _check_connection(self): |
411 | 395 |
"""Make sure that the connection is established. |
412 | 396 |
|
413 | 397 |
""" |
414 | 398 |
if not self._connected: |
415 |
raise errors.ProgrammerError("To use a QmpConnection you need to first"
|
|
399 |
raise errors.ProgrammerError("To use a MonitorSocket you need to first"
|
|
416 | 400 |
" invoke connect() on it") |
417 | 401 |
|
418 | 402 |
def connect(self): |
419 |
"""Connects to the QMP monitor.
|
|
403 |
"""Connects to the monitor. |
|
420 | 404 |
|
421 |
Connects to the UNIX socket and makes sure that we can actually send and |
|
422 |
receive data to the kvm instance via QMP. |
|
405 |
Connects to the UNIX socket |
|
423 | 406 |
|
424 | 407 |
@raise errors.HypervisorError: when there are communication errors |
425 |
@raise errors.ProgrammerError: when there are data serialization errors |
|
426 | 408 |
|
427 | 409 |
""" |
428 | 410 |
if self._connected: |
... | ... | |
437 | 419 |
raise errors.HypervisorError("Can't connect to qmp socket") |
438 | 420 |
self._connected = True |
439 | 421 |
|
422 |
def close(self): |
|
423 |
"""Closes the socket |
|
424 |
|
|
425 |
It cannot be used after this call. |
|
426 |
|
|
427 |
""" |
|
428 |
self.sock.close() |
|
429 |
|
|
430 |
|
|
431 |
class QmpConnection(MonitorSocket): |
|
432 |
"""Connection to the QEMU Monitor using the QEMU Monitor Protocol (QMP). |
|
433 |
|
|
434 |
""" |
|
435 |
_FIRST_MESSAGE_KEY = "QMP" |
|
436 |
_EVENT_KEY = "event" |
|
437 |
_ERROR_KEY = "error" |
|
438 |
_RETURN_KEY = RETURN_KEY = "return" |
|
439 |
_ACTUAL_KEY = ACTUAL_KEY = "actual" |
|
440 |
_ERROR_CLASS_KEY = "class" |
|
441 |
_ERROR_DATA_KEY = "data" |
|
442 |
_ERROR_DESC_KEY = "desc" |
|
443 |
_EXECUTE_KEY = "execute" |
|
444 |
_ARGUMENTS_KEY = "arguments" |
|
445 |
_CAPABILITIES_COMMAND = "qmp_capabilities" |
|
446 |
_MESSAGE_END_TOKEN = "\r\n" |
|
447 |
|
|
448 |
def __init__(self, monitor_filename): |
|
449 |
super(QmpConnection, self).__init__(monitor_filename) |
|
450 |
self._buf = "" |
|
451 |
|
|
452 |
def connect(self): |
|
453 |
"""Connects to the QMP monitor. |
|
454 |
|
|
455 |
Connects to the UNIX socket and makes sure that we can actually send and |
|
456 |
receive data to the kvm instance via QMP. |
|
457 |
|
|
458 |
@raise errors.HypervisorError: when there are communication errors |
|
459 |
@raise errors.ProgrammerError: when there are data serialization errors |
|
460 |
|
|
461 |
""" |
|
462 |
super(QmpConnection, self).connect() |
|
440 | 463 |
# Check if we receive a correct greeting message from the server |
441 | 464 |
# (As per the QEMU Protocol Specification 0.1 - section 2.2) |
442 | 465 |
greeting = self._Recv() |
Also available in: Unified diff