[PATCH v2 5/6] ch: events: facilitate lifecycle events handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Implement `virCHProcessEvent` that maps event string to corresponding
event type and take appropriate actions. As part of this, handle the
shutdown event by correctly updating the domain state. this change also
facilitates the handling of other VM lifecycle events, such as booting,
rebooting, pause, resume, etc.

Signed-off-by: Purna Pavan Chandra Aekkaladevi <paekkaladevi@xxxxxxxxxxxxxxxxxxx>
---
 src/ch/ch_events.c | 103 ++++++++++++++++++++++++++++++++++++++++++++-
 src/ch/ch_events.h |  26 ++++++++++++
 2 files changed, 128 insertions(+), 1 deletion(-)

diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c
index ba82581463..25d0de97cd 100644
--- a/src/ch/ch_events.c
+++ b/src/ch/ch_events.c
@@ -22,12 +22,110 @@
 
 #include <fcntl.h>
 
+#include "ch_domain.h"
 #include "ch_events.h"
+#include "ch_process.h"
 #include "virfile.h"
 #include "virlog.h"
 
 VIR_LOG_INIT("ch.ch_events");
 
+VIR_ENUM_IMPL(virCHEvent,
+              VIR_CH_EVENT_LAST,
+              "vmm:starting",
+              "vmm:shutdown",
+              "vm:booting",
+              "vm:booted",
+              "vm:rebooting",
+              "vm:rebooted",
+              "vm:shutdown",
+              "vm:deleted",
+              "vm:pausing",
+              "vm:paused",
+              "vm:resuming",
+              "vm:resumed",
+              "vm:snapshotting",
+              "vm:snapshotted",
+              "vm:restoring",
+              "vm:restored",
+);
+
+static int virCHEventStopProcess(virDomainObj *vm,
+                                 virDomainShutoffReason reason)
+{
+    virCHDriver *driver =  ((virCHDomainObjPrivate *)vm->privateData)->driver;
+
+    if (virDomainObjBeginJob(vm, VIR_JOB_MODIFY))
+        return -1;
+    virCHProcessStop(driver, vm, reason);
+    virDomainObjEndJob(vm);
+
+    return 0;
+}
+
+static int virCHProcessEvent(virCHMonitor *mon,
+                             virJSONValue *eventJSON)
+{
+    const char *event;
+    const char *source;
+    virCHEvent ev;
+    g_autofree char *timestamp = NULL;
+    g_autofree char *full_event = NULL;
+    virDomainObj *vm = mon->vm;
+    int ret = 0;
+
+    if (virJSONValueObjectHasKey(eventJSON, "source") == 0) {
+        VIR_WARN("Invalid JSON from monitor, no source key");
+        return -1;
+    }
+    if (virJSONValueObjectHasKey(eventJSON, "event") == 0) {
+        VIR_WARN("Invalid JSON from monitor, no event key");
+        return -1;
+    }
+    source = virJSONValueObjectGetString(eventJSON, "source");
+    event = virJSONValueObjectGetString(eventJSON, "event");
+    full_event = g_strdup_printf("%s:%s", source, event);
+    ev = virCHEventTypeFromString(full_event);
+    VIR_DEBUG("Source: %s Event: %s, ev: %d", source, event, ev);
+
+    switch (ev) {
+        case VIR_CH_EVENT_VMM_STARTING:
+        case VIR_CH_EVENT_VM_BOOTING:
+        case VIR_CH_EVENT_VM_BOOTED:
+        case VIR_CH_EVENT_VM_REBOOTING:
+        case VIR_CH_EVENT_VM_REBOOTED:
+        case VIR_CH_EVENT_VM_PAUSING:
+        case VIR_CH_EVENT_VM_PAUSED:
+        case VIR_CH_EVENT_VM_RESUMING:
+        case VIR_CH_EVENT_VM_RESUMED:
+        case VIR_CH_EVENT_VM_SNAPSHOTTING:
+        case VIR_CH_EVENT_VM_SNAPSHOTTED:
+        case VIR_CH_EVENT_VM_RESTORING:
+        case VIR_CH_EVENT_VM_RESTORED:
+        case VIR_CH_EVENT_VM_DELETED:
+            break;
+        case VIR_CH_EVENT_VMM_SHUTDOWN:
+            virObjectLock(vm);
+            if (virCHEventStopProcess(vm, VIR_DOMAIN_SHUTOFF_SHUTDOWN)) {
+                VIR_WARN("Failed to mark the VM(%s) as SHUTDOWN!",
+                          vm->def->name);
+                ret = -1;
+            }
+            virObjectUnlock(vm);
+            break;
+        case VIR_CH_EVENT_VM_SHUTDOWN:
+            virObjectLock(vm);
+            virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
+            virObjectUnlock(vm);
+            break;
+        case VIR_CH_EVENT_LAST:
+        default:
+            VIR_WARN("Unknown event: %s", full_event);
+    }
+
+    return ret;
+}
+
 /**
  * virCHProcessEvents:
  * @mon: the CH monitor object
@@ -103,7 +201,10 @@ static int virCHProcessEvents(virCHMonitor *mon)
                 json_start = buf + start_index;
 
                 if ((obj = virJSONValueFromString(json_start))) {
-                    /* Process the event string (obj) here */
+                    if (virCHProcessEvent(mon, obj) < 0) {
+                        VIR_WARN("Failed to process JSON event doc: %s", json_start);
+                        ret = -1;
+                    }
                     virJSONValueFree(obj);
                 } else {
                     VIR_WARN("Invalid JSON event doc: %s", json_start);
diff --git a/src/ch/ch_events.h b/src/ch/ch_events.h
index 2e9cdf03bb..3b360628f7 100644
--- a/src/ch/ch_events.h
+++ b/src/ch/ch_events.h
@@ -24,5 +24,31 @@
 
 #define CH_EVENT_BUFFER_SZ  PIPE_BUF
 
+typedef enum {
+    /* source: vmm */
+    VIR_CH_EVENT_VMM_STARTING = 0,
+    VIR_CH_EVENT_VMM_SHUTDOWN,
+
+    /* source: vm */
+    VIR_CH_EVENT_VM_BOOTING,
+    VIR_CH_EVENT_VM_BOOTED,
+    VIR_CH_EVENT_VM_REBOOTING,
+    VIR_CH_EVENT_VM_REBOOTED,
+    VIR_CH_EVENT_VM_SHUTDOWN,
+    VIR_CH_EVENT_VM_DELETED,
+    VIR_CH_EVENT_VM_PAUSING,
+    VIR_CH_EVENT_VM_PAUSED,
+    VIR_CH_EVENT_VM_RESUMING,
+    VIR_CH_EVENT_VM_RESUMED,
+    VIR_CH_EVENT_VM_SNAPSHOTTING,
+    VIR_CH_EVENT_VM_SNAPSHOTTED,
+    VIR_CH_EVENT_VM_RESTORING,
+    VIR_CH_EVENT_VM_RESTORED,
+
+    VIR_CH_EVENT_LAST
+} virCHEvent;
+
+VIR_ENUM_DECL(virCHEvent);
+
 int virCHStartEventHandler(virCHMonitor *mon);
 void virCHStopEventHandler(virCHMonitor *mon);
-- 
2.34.1



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux