sjpp / rpms / libvirt-python

Forked from rpms/libvirt-python 3 years ago
Clone

Blame SOURCES/libvirt-python-event-Add-bindings-for-agent-lifecycle-event.patch

b79911
From 863a09898c215294d9e54ce7daf5f17e7956f7fd Mon Sep 17 00:00:00 2001
b79911
Message-Id: <863a09898c215294d9e54ce7daf5f17e7956f7fd@dist-git>
b79911
From: Peter Krempa <pkrempa@redhat.com>
b79911
Date: Mon, 24 Nov 2014 17:59:41 +0100
b79911
Subject: [PATCH] event: Add bindings for agent lifecycle event
b79911
b79911
https://bugzilla.redhat.com/show_bug.cgi?id=1167336
b79911
b79911
Also add the example.
b79911
b79911
(cherry picked from commit acc47bcb71dd523264ba7e626624746017a052bc)
b79911
b79911
Conflicts:
b79911
	libvirt-override.c
b79911
b79911
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
b79911
---
b79911
 examples/event-test.py         | 11 ++++++++
b79911
 libvirt-override-virConnect.py | 10 +++++++
b79911
 libvirt-override.c             | 60 ++++++++++++++++++++++++++++++++++++++++++
b79911
 3 files changed, 81 insertions(+)
b79911
 mode change 100644 => 100755 examples/event-test.py
b79911
b79911
diff --git a/examples/event-test.py b/examples/event-test.py
b79911
old mode 100644
b79911
new mode 100755
b79911
index be7a7d4..6cc33ce
b79911
--- a/examples/event-test.py
b79911
+++ b/examples/event-test.py
b79911
@@ -464,6 +464,14 @@ def blockJobStatusToString(status):
b79911
     blockJobStatus = ( "Completed", "Failed", "Canceled", "Ready", )
b79911
     return blockJobStatus[status]
b79911
 
b79911
+def agentLifecycleStateToString(state):
b79911
+    agentStates = ( "unknown", "connected", "disconnected", )
b79911
+    return agentStates[state]
b79911
+
b79911
+def agentLifecycleReasonToString(reason):
b79911
+    agentReasons = ( "unknown", "domain started", "channel event", )
b79911
+    return agentReasons[reason]
b79911
+
b79911
 def myDomainEventCallback1 (conn, dom, event, detail, opaque):
b79911
     print("myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(),
b79911
                                                                  domEventToString(event),
b79911
@@ -517,6 +525,8 @@ def myDomainEventBlockJob2Callback(conn, dom, disk, type, status, opaque):
b79911
     print("myDomainEventBlockJob2Callback: Domain %s(%s) %s on disk %s %s" % (dom.name(), dom.ID(), blockJobTypeToString(type), disk, blockJobStatusToString(status)))
b79911
 def myDomainEventTunableCallback(conn, dom, params, opaque):
b79911
     print("myDomainEventTunableCallback: Domain %s(%s) %s" % (dom.name(), dom.ID(), params))
b79911
+def myDomainEventAgentLifecycleCallback(conn, dom, state, reason, opaque):
b79911
+    print("myDomainEventAgentLifecycleCallback: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), agentLifecycleStateToString(state), agentLifecycleReasonToString(reason)))
b79911
 
b79911
 ##########################################################################
b79911
 # Network events
b79911
@@ -627,6 +637,7 @@ def main():
b79911
     vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED, myDomainEventDeviceRemovedCallback, None)
b79911
     vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2, myDomainEventBlockJob2Callback, None)
b79911
     vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_TUNABLE, myDomainEventTunableCallback, None)
b79911
+    vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE, myDomainEventAgentLifecycleCallback, None)
b79911
 
b79911
     vc.networkEventRegisterAny(None, libvirt.VIR_NETWORK_EVENT_ID_LIFECYCLE, myNetworkEventLifecycleCallback, None)
b79911
 
b79911
diff --git a/libvirt-override-virConnect.py b/libvirt-override-virConnect.py
b79911
index ffb6d6b..88c864c 100644
b79911
--- a/libvirt-override-virConnect.py
b79911
+++ b/libvirt-override-virConnect.py
b79911
@@ -197,6 +197,16 @@
b79911
         cb(self, virDomain(self, _obj=dom), params, opaque)
b79911
         return 0
b79911
 
b79911
+    def _dispatchDomainEventAgentLifecycleCallback(self, dom, state, reason, cbData):
b79911
+        """Dispatches event to python user domain agent lifecycle event callback
b79911
+        """
b79911
+
b79911
+        cb = cbData["cb"]
b79911
+        opaque = cbData["opaque"]
b79911
+
b79911
+        cb(self, virDomain(self, _obj=dom), state, reason, opaque)
b79911
+        return 0
b79911
+
b79911
     def domainEventDeregisterAny(self, callbackID):
b79911
         """Removes a Domain Event Callback. De-registering for a
b79911
            domain callback will disable delivery of this event type """
b79911
diff --git a/libvirt-override.c b/libvirt-override.c
b79911
index 1a91c7f..c5d1478 100644
b79911
--- a/libvirt-override.c
b79911
+++ b/libvirt-override.c
b79911
@@ -6566,6 +6566,61 @@ libvirt_virConnectDomainEventTunableCallback(virConnectPtr conn ATTRIBUTE_UNUSED
b79911
 }
b79911
 #endif /* LIBVIR_CHECK_VERSION(1, 2, 8) */
b79911
 
b79911
+#if LIBVIR_CHECK_VERSION(1, 2, 11)
b79911
+static int
b79911
+libvirt_virConnectDomainEventAgentLifecycleCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
b79911
+                                                    virDomainPtr dom,
b79911
+                                                    int state,
b79911
+                                                    int reason,
b79911
+                                                    void *opaque)
b79911
+{
b79911
+    PyObject *pyobj_cbData = (PyObject*)opaque;
b79911
+    PyObject *pyobj_dom;
b79911
+    PyObject *pyobj_ret = NULL;
b79911
+    PyObject *pyobj_conn;
b79911
+    PyObject *dictKey;
b79911
+    int ret = -1;
b79911
+
b79911
+    LIBVIRT_ENSURE_THREAD_STATE;
b79911
+
b79911
+    if (!(dictKey = libvirt_constcharPtrWrap("conn")))
b79911
+        goto cleanup;
b79911
+    pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
b79911
+    Py_DECREF(dictKey);
b79911
+
b79911
+    /* Create a python instance of this virDomainPtr */
b79911
+    virDomainRef(dom);
b79911
+    if (!(pyobj_dom = libvirt_virDomainPtrWrap(dom))) {
b79911
+        virDomainFree(dom);
b79911
+        goto cleanup;
b79911
+    }
b79911
+    Py_INCREF(pyobj_cbData);
b79911
+
b79911
+    /* Call the Callback Dispatcher */
b79911
+    pyobj_ret = PyObject_CallMethod(pyobj_conn,
b79911
+                                    (char*)"_dispatchDomainEventAgentLifecycleCallback",
b79911
+                                    (char*)"OiiO",
b79911
+                                    pyobj_dom, state, reason, pyobj_cbData);
b79911
+
b79911
+    Py_DECREF(pyobj_cbData);
b79911
+    Py_DECREF(pyobj_dom);
b79911
+
b79911
+ cleanup:
b79911
+    if (!pyobj_ret) {
b79911
+        DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
b79911
+        PyErr_Print();
b79911
+    } else {
b79911
+        Py_DECREF(pyobj_ret);
b79911
+        ret = 0;
b79911
+    }
b79911
+
b79911
+    LIBVIRT_RELEASE_THREAD_STATE;
b79911
+    return ret;
b79911
+
b79911
+}
b79911
+#endif /* LIBVIR_CHECK_VERSION(1, 2, 11) */
b79911
+
b79911
+
b79911
 static PyObject *
b79911
 libvirt_virConnectDomainEventRegisterAny(ATTRIBUTE_UNUSED PyObject *self,
b79911
                                          PyObject *args)
b79911
@@ -6658,6 +6713,11 @@ libvirt_virConnectDomainEventRegisterAny(ATTRIBUTE_UNUSED PyObject *self,
b79911
         cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventTunableCallback);
b79911
         break;
b79911
 #endif /* LIBVIR_CHECK_VERSION(1, 2, 8) */
b79911
+#if LIBVIR_CHECK_VERSION(1, 2, 11)
b79911
+    case VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE:
b79911
+        cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventAgentLifecycleCallback);
b79911
+        break;
b79911
+#endif /* LIBVIR_CHECK_VERSION(1, 2, 11) */
b79911
     case VIR_DOMAIN_EVENT_ID_LAST:
b79911
         break;
b79911
     }
b79911
-- 
b79911
2.1.3
b79911