[PATCH 8/8] ch: Enable logging curl responses from ch

Praveen K Paladugu posted 8 patches 2 weeks, 1 day ago
There is a newer version of this series
[PATCH 8/8] ch: Enable logging curl responses from ch
Posted by Praveen K Paladugu 2 weeks, 1 day ago
Log curl responses from cloud-hypervisor process during Boot request, using
domain's logContext.

Signed-off-by: Praveen K Paladugu <praveenkpaladugu@gmail.com>
---
 src/ch/ch_monitor.c | 35 ++++++++++++++++++++++++++---------
 src/ch/ch_monitor.h |  3 ++-
 src/ch/ch_process.c |  2 +-
 3 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c
index 88df9d4a03..d3f969e01a 100644
--- a/src/ch/ch_monitor.c
+++ b/src/ch/ch_monitor.c
@@ -56,7 +56,8 @@ static int virCHMonitorOnceInit(void)
 VIR_ONCE_GLOBAL_INIT(virCHMonitor);
 
 int virCHMonitorShutdownVMM(virCHMonitor *mon);
-int virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint);
+int virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint,
+                             domainLogContext *logCtxt);
 
 static int
 virCHMonitorBuildCPUJson(virJSONValue *content, virDomainDef *vmdef)
@@ -694,12 +695,15 @@ curl_callback(void *contents, size_t size, size_t nmemb, void *userp)
 }
 
 int
-virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint)
+virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint,
+                         domainLogContext *logCtxt)
 {
     VIR_LOCK_GUARD lock = virObjectLockGuard(mon);
     g_autofree char *url = NULL;
     int responseCode = 0;
     int ret = -1;
+    struct curl_data data = {0};
+    struct curl_slist *headers = NULL;
 
     url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
 
@@ -712,8 +716,21 @@ virCHMonitorPutNoContent(virCHMonitor *mon, const char *endpoint)
     curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, NULL);
     curl_easy_setopt(mon->handle, CURLOPT_INFILESIZE, 0L);
 
+    headers = curl_slist_append(headers, "Accept: application/json");
+    curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
+    curl_easy_setopt(mon->handle, CURLOPT_WRITEFUNCTION, curl_callback);
+    curl_easy_setopt(mon->handle, CURLOPT_WRITEDATA, (void *)&data);
+
     responseCode = virCHMonitorCurlPerform(mon->handle);
 
+    if (logCtxt && data.size) {
+        /* Do this to append a NULL char at the end of data */
+        data.content = g_realloc(data.content, data.size + 1);
+        data.content[data.size] = 0;
+        domainLogContextWrite(logCtxt, "HTTP response code from CH: %d\n", responseCode);
+        domainLogContextWrite(logCtxt, "Response = %s\n", data.content);
+    }
+
     if (responseCode == 200 || responseCode == 204)
         ret = 0;
 
@@ -863,7 +880,7 @@ virCHMonitorGetThreadInfo(virCHMonitor *mon,
 int
 virCHMonitorShutdownVMM(virCHMonitor *mon)
 {
-    return virCHMonitorPutNoContent(mon, URL_VMM_SHUTDOWN);
+    return virCHMonitorPutNoContent(mon, URL_VMM_SHUTDOWN, NULL);
 }
 
 int
@@ -903,33 +920,33 @@ virCHMonitorCreateVM(virCHDriver *driver, virCHMonitor *mon)
 }
 
 int
-virCHMonitorBootVM(virCHMonitor *mon)
+virCHMonitorBootVM(virCHMonitor *mon, domainLogContext *logCtxt)
 {
-    return virCHMonitorPutNoContent(mon, URL_VM_BOOT);
+    return virCHMonitorPutNoContent(mon, URL_VM_BOOT, logCtxt);
 }
 
 int
 virCHMonitorShutdownVM(virCHMonitor *mon)
 {
-    return virCHMonitorPutNoContent(mon, URL_VM_SHUTDOWN);
+    return virCHMonitorPutNoContent(mon, URL_VM_SHUTDOWN, NULL);
 }
 
 int
 virCHMonitorRebootVM(virCHMonitor *mon)
 {
-    return virCHMonitorPutNoContent(mon, URL_VM_REBOOT);
+    return virCHMonitorPutNoContent(mon, URL_VM_REBOOT, NULL);
 }
 
 int
 virCHMonitorSuspendVM(virCHMonitor *mon)
 {
-    return virCHMonitorPutNoContent(mon, URL_VM_Suspend);
+    return virCHMonitorPutNoContent(mon, URL_VM_Suspend, NULL);
 }
 
 int
 virCHMonitorResumeVM(virCHMonitor *mon)
 {
-    return virCHMonitorPutNoContent(mon, URL_VM_RESUME);
+    return virCHMonitorPutNoContent(mon, URL_VM_RESUME, NULL);
 }
 
 int
diff --git a/src/ch/ch_monitor.h b/src/ch/ch_monitor.h
index 5c02447116..ca23301723 100644
--- a/src/ch/ch_monitor.h
+++ b/src/ch/ch_monitor.h
@@ -25,6 +25,7 @@
 #include "virobject.h"
 #include "virjson.h"
 #include "domain_conf.h"
+#include "domain_logcontext.h"
 #include "ch_conf.h"
 
 #define URL_ROOT "http://localhost/api/v1"
@@ -110,7 +111,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCHMonitor, virCHMonitorClose);
 
 
 int virCHMonitorCreateVM(virCHDriver *driver, virCHMonitor *mon);
-int virCHMonitorBootVM(virCHMonitor *mon);
+int virCHMonitorBootVM(virCHMonitor *mon, domainLogContext *logCtxt);
 int virCHMonitorShutdownVM(virCHMonitor *mon);
 int virCHMonitorRebootVM(virCHMonitor *mon);
 int virCHMonitorSuspendVM(virCHMonitor *mon);
diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c
index af44aa81a2..9a85f7869e 100644
--- a/src/ch/ch_process.c
+++ b/src/ch/ch_process.c
@@ -964,7 +964,7 @@ virCHProcessStart(virCHDriver *driver,
     if (virDomainInterfaceStartDevices(vm->def) < 0)
         return -1;
 
-    if (virCHMonitorBootVM(priv->monitor) < 0) {
+    if (virCHMonitorBootVM(priv->monitor, logCtxt) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("failed to boot guest VM"));
         goto cleanup;
-- 
2.47.0