mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0024-v2v-o-rhv-upload-Log-full-imageio-response-on-failur.patch

df3bb2
From c2ed35e2bc17bcacf333626b21cd6796d4d8d6cf Mon Sep 17 00:00:00 2001
df3bb2
From: "Richard W.M. Jones" <rjones@redhat.com>
df3bb2
Date: Tue, 5 Jun 2018 13:27:43 +0100
df3bb2
Subject: [PATCH] v2v: -o rhv-upload: Log full imageio response on failure.
df3bb2
df3bb2
Thanks: Nir Soffer
df3bb2
(cherry picked from commit 831a75cd11c5a87e40fccdadcb62353f6a4d5a72)
df3bb2
---
df3bb2
 v2v/rhv-upload-plugin.py | 69 ++++++++++++++++++++++++----------------
df3bb2
 1 file changed, 42 insertions(+), 27 deletions(-)
df3bb2
df3bb2
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
df3bb2
index 9ad354b84..7c5084efd 100644
df3bb2
--- a/v2v/rhv-upload-plugin.py
df3bb2
+++ b/v2v/rhv-upload-plugin.py
df3bb2
@@ -227,6 +227,32 @@ def can_flush(h):
df3bb2
 def get_size(h):
df3bb2
     return params['disk_size']
df3bb2
 
df3bb2
+# Any unexpected HTTP response status from the server will end up
df3bb2
+# calling this function which logs the full error, pauses the
df3bb2
+# transfer, sets the failed state, and raises a RuntimeError
df3bb2
+# exception.
df3bb2
+def request_failed(h, r, msg):
df3bb2
+    # Setting the failed flag in the handle causes the disk to be
df3bb2
+    # cleaned up on close.
df3bb2
+    h['failed'] = True
df3bb2
+    h['transfer_service'].pause()
df3bb2
+
df3bb2
+    status = r.status
df3bb2
+    reason = r.reason
df3bb2
+    try:
df3bb2
+        body = r.read()
df3bb2
+    except EnvironmentError as e:
df3bb2
+        body = "(Unable to read response body: %s)" % e
df3bb2
+
df3bb2
+    # Log the full error if we're verbose.
df3bb2
+    debug("unexpected response from imageio server:")
df3bb2
+    debug(msg)
df3bb2
+    debug("%d: %s" % (status, reason))
df3bb2
+    debug(body)
df3bb2
+
df3bb2
+    # Only a short error is included in the exception.
df3bb2
+    raise RuntimeError("%s: %d %s: %r", msg, status, reason, body[:200])
df3bb2
+
df3bb2
 # For documentation see:
df3bb2
 # https://github.com/oVirt/ovirt-imageio/blob/master/docs/random-io.md
df3bb2
 # For examples of working code to read/write from the server, see:
df3bb2
@@ -247,16 +273,14 @@ def pread(h, count, offset):
df3bb2
     r = http.getresponse()
df3bb2
     # 206 = HTTP Partial Content.
df3bb2
     if r.status != 206:
df3bb2
-        h['transfer_service'].pause()
df3bb2
-        h['failed'] = True
df3bb2
-        raise RuntimeError("could not read sector (%d, %d): %d: %s" %
df3bb2
-                           (offset, count, r.status, r.reason))
df3bb2
+        request_failed(h, r,
df3bb2
+                       "could not read sector offset %d size %d" %
df3bb2
+                       (offset, count))
df3bb2
     return r.read()
df3bb2
 
df3bb2
 def pwrite(h, buf, offset):
df3bb2
     http = h['http']
df3bb2
     transfer = h['transfer']
df3bb2
-    transfer_service = h['transfer_service']
df3bb2
 
df3bb2
     count = len(buf)
df3bb2
     h['highestwrite'] = max(h['highestwrite'], offset+count)
df3bb2
@@ -274,15 +298,13 @@ def pwrite(h, buf, offset):
df3bb2
 
df3bb2
     r = http.getresponse()
df3bb2
     if r.status != 200:
df3bb2
-        transfer_service.pause()
df3bb2
-        h['failed'] = True
df3bb2
-        raise RuntimeError("could not write sector (%d, %d): %d: %s" %
df3bb2
-                           (offset, count, r.status, r.reason))
df3bb2
+        request_failed(h, r,
df3bb2
+                       "could not write sector offset %d size %d" %
df3bb2
+                       (offset, count))
df3bb2
 
df3bb2
 def zero(h, count, offset, may_trim):
df3bb2
     http = h['http']
df3bb2
     transfer = h['transfer']
df3bb2
-    transfer_service = h['transfer_service']
df3bb2
 
df3bb2
     # Unlike the trim and flush calls, there is no 'can_zero' method
df3bb2
     # so nbdkit could call this even if the server doesn't support
df3bb2
@@ -304,10 +326,9 @@ def zero(h, count, offset, may_trim):
df3bb2
 
df3bb2
     r = http.getresponse()
df3bb2
     if r.status != 200:
df3bb2
-        transfer_service.pause()
df3bb2
-        h['failed'] = True
df3bb2
-        raise RuntimeError("could not zero sector (%d, %d): %d: %s" %
df3bb2
-                           (offset, count, r.status, r.reason))
df3bb2
+        request_failed(h, r,
df3bb2
+                       "could not zero sector offset %d size %d" %
df3bb2
+                       (offset, count))
df3bb2
 
df3bb2
 def emulate_zero(h, count, offset):
df3bb2
     # qemu-img convert starts by trying to zero/trim the whole device.
df3bb2
@@ -332,15 +353,13 @@ def emulate_zero(h, count, offset):
df3bb2
 
df3bb2
         r = http.getresponse()
df3bb2
         if r.status != 200:
df3bb2
-            transfer_service.pause()
df3bb2
-            h['failed'] = True
df3bb2
-            raise RuntimeError("could not write zeroes (%d, %d): %d: %s" %
df3bb2
-                               (offset, count, r.status, r.reason))
df3bb2
+            request_failed(h, r,
df3bb2
+                           "could not write zeroes offset %d size %d" %
df3bb2
+                           (offset, count))
df3bb2
 
df3bb2
 def trim(h, count, offset):
df3bb2
     http = h['http']
df3bb2
     transfer = h['transfer']
df3bb2
-    transfer_service = h['transfer_service']
df3bb2
 
df3bb2
     # Construct the JSON request for trimming.
df3bb2
     buf = json.dumps({'op': "trim",
df3bb2
@@ -355,15 +374,13 @@ def trim(h, count, offset):
df3bb2
 
df3bb2
     r = http.getresponse()
df3bb2
     if r.status != 200:
df3bb2
-        transfer_service.pause()
df3bb2
-        h['failed'] = True
df3bb2
-        raise RuntimeError("could not trim sector (%d, %d): %d: %s" %
df3bb2
-                           (offset, count, r.status, r.reason))
df3bb2
+        request_failed(h, r,
df3bb2
+                       "could not trim sector offset %d size %d" %
df3bb2
+                       (offset, count))
df3bb2
 
df3bb2
 def flush(h):
df3bb2
     http = h['http']
df3bb2
     transfer = h['transfer']
df3bb2
-    transfer_service = h['transfer_service']
df3bb2
 
df3bb2
     # Construct the JSON request for flushing.
df3bb2
     buf = json.dumps({'op': "flush"}).encode()
df3bb2
@@ -375,9 +392,7 @@ def flush(h):
df3bb2
 
df3bb2
     r = http.getresponse()
df3bb2
     if r.status != 200:
df3bb2
-        transfer_service.pause()
df3bb2
-        h['failed'] = True
df3bb2
-        raise RuntimeError("could not flush: %d: %s" % (r.status, r.reason))
df3bb2
+        request_failed(h, r, "could not flush")
df3bb2
 
df3bb2
 def delete_disk_on_failure(h):
df3bb2
     disk_service = h['disk_service']
df3bb2
-- 
df3bb2
2.21.0
df3bb2