Store the set of supported QMP commands in
QmpConnection.supported_commands. This allows selective error handling
to give the caller a change to downgrade to HMP when unsupported
commands are encountered.

The `query-commands` QMP command used to implement this, appeared in
QEMU 0.14.0 together with QMP itself, so it is supported by all
instances featuring a QMP monitor.

Signed-off-by: Apollon Oikonomopoulos <[email protected]>
---
 lib/hypervisor/hv_kvm/monitor.py | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lib/hypervisor/hv_kvm/monitor.py b/lib/hypervisor/hv_kvm/monitor.py
index 6a7ff3a..fc6fac9 100644
--- a/lib/hypervisor/hv_kvm/monitor.py
+++ b/lib/hypervisor/hv_kvm/monitor.py
@@ -183,11 +183,13 @@ class QmpConnection(MonitorSocket):
   _EXECUTE_KEY = "execute"
   _ARGUMENTS_KEY = "arguments"
   _CAPABILITIES_COMMAND = "qmp_capabilities"
+  _QUERY_COMMANDS = "query-commands"
   _MESSAGE_END_TOKEN = "\r\n"
 
   def __init__(self, monitor_filename):
     super(QmpConnection, self).__init__(monitor_filename)
     self._buf = ""
+    self.supported_commands = frozenset()
 
   def __enter__(self):
     self.connect()
@@ -222,6 +224,7 @@ class QmpConnection(MonitorSocket):
     # command, or else no command will be executable.
     # (As per the QEMU Protocol Specification 0.1 - section 4)
     self.Execute(self._CAPABILITIES_COMMAND)
+    self.supported_commands = self._GetSupportedCommands()
 
   def _ParseMessage(self, buf):
     """Extract and parse a QMP message from the given buffer.
@@ -306,6 +309,13 @@ class QmpConnection(MonitorSocket):
       raise errors.HypervisorError("Unable to send data from KVM using the"
                                    " QMP protocol: %s" % err)
 
+  def _GetSupportedCommands(self):
+    """Update the list of supported commands.
+
+    """
+    result = self.Execute(self._QUERY_COMMANDS)
+    return frozenset(com["name"] for com in result[self._RETURN_KEY])
+
   def Execute(self, command, arguments=None):
     """Executes a QMP command and returns the response of the server.
 
-- 
1.9.1

Reply via email to