mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0062-v2v-o-rhv-upload-Always-fetch-server-options-when-op.patch

97ae69
From 376064fc32f0ccfea5c32ff5cd2fd844fc399ca6 Mon Sep 17 00:00:00 2001
97ae69
From: "Richard W.M. Jones" <rjones@redhat.com>
97ae69
Date: Tue, 19 Jun 2018 18:02:21 +0100
97ae69
Subject: [PATCH] v2v: -o rhv-upload: Always fetch server options when opening
97ae69
 the connection.
97ae69
MIME-Version: 1.0
97ae69
Content-Type: text/plain; charset=UTF-8
97ae69
Content-Transfer-Encoding: 8bit
97ae69
97ae69
Previously we lazily requested the server options in the can_*
97ae69
callbacks.  The can_* callbacks are always called by nbdkit straight
97ae69
after open, so this just adds complexity for no benefit.  This change
97ae69
simply makes the code always fetch the server options during the open
97ae69
callback.
97ae69
97ae69
This is — functionally at least — mostly just refactoring.  However I
97ae69
also added a useful debug message so we can see what features the
97ae69
imageio server is offering.
97ae69
97ae69
(cherry picked from commit a1e1f6ec887c2a7973612d2edf7066fd3194ba0b)
97ae69
---
97ae69
 v2v/rhv-upload-plugin.py | 63 +++++++++++++++++++---------------------
97ae69
 1 file changed, 30 insertions(+), 33 deletions(-)
97ae69
97ae69
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
97ae69
index 5c036c46a..f215eaecf 100644
97ae69
--- a/v2v/rhv-upload-plugin.py
97ae69
+++ b/v2v/rhv-upload-plugin.py
97ae69
@@ -165,34 +165,13 @@ def open(readonly):
97ae69
         context = context
97ae69
     )
97ae69
 
97ae69
-    # Save everything we need to make requests in the handle.
97ae69
-    return {
97ae69
-        'can_flush': False,
97ae69
-        'can_trim': False,
97ae69
-        'can_zero': False,
97ae69
-        'connection': connection,
97ae69
-        'disk': disk,
97ae69
-        'disk_service': disk_service,
97ae69
-        'failed': False,
97ae69
-        'got_options': False,
97ae69
-        'highestwrite': 0,
97ae69
-        'http': http,
97ae69
-        'needs_auth': not params['rhv_direct'],
97ae69
-        'path': destination_url.path,
97ae69
-        'transfer': transfer,
97ae69
-        'transfer_service': transfer_service,
97ae69
-    }
97ae69
+    # The first request is to fetch the features of the server.
97ae69
+    needs_auth = not params['rhv_direct']
97ae69
+    can_flush = False
97ae69
+    can_trim = False
97ae69
+    can_zero = False
97ae69
 
97ae69
-# Can we issue zero, trim or flush requests?
97ae69
-def get_options(h):
97ae69
-    if h['got_options']:
97ae69
-        return
97ae69
-    h['got_options'] = True
97ae69
-
97ae69
-    http = h['http']
97ae69
-    transfer = h['transfer']
97ae69
-
97ae69
-    http.putrequest("OPTIONS", h['path'])
97ae69
+    http.putrequest("OPTIONS", destination_url.path)
97ae69
     http.putheader("Authorization", transfer.signed_ticket)
97ae69
     http.endheaders()
97ae69
 
97ae69
@@ -201,12 +180,12 @@ def get_options(h):
97ae69
 
97ae69
     if r.status == 200:
97ae69
         # New imageio never needs authentication.
97ae69
-        h['needs_auth'] = False
97ae69
+        needs_auth = False
97ae69
 
97ae69
         j = json.loads(data)
97ae69
-        h['can_zero'] = "zero" in j['features']
97ae69
-        h['can_trim'] = "trim" in j['features']
97ae69
-        h['can_flush'] = "flush" in j['features']
97ae69
+        can_flush = "flush" in j['features']
97ae69
+        can_trim = "trim" in j['features']
97ae69
+        can_zero = "zero" in j['features']
97ae69
 
97ae69
     # Old imageio servers returned either 405 Method Not Allowed or
97ae69
     # 204 No Content (with an empty body).  If we see that we leave
97ae69
@@ -218,12 +197,30 @@ def get_options(h):
97ae69
         raise RuntimeError("could not use OPTIONS request: %d: %s" %
97ae69
                            (r.status, r.reason))
97ae69
 
97ae69
+    debug("imageio features: flush=%r trim=%r zero=%r" %
97ae69
+          (can_flush, can_trim, can_zero))
97ae69
+
97ae69
+    # Save everything we need to make requests in the handle.
97ae69
+    return {
97ae69
+        'can_flush': can_flush,
97ae69
+        'can_trim': can_trim,
97ae69
+        'can_zero': can_zero,
97ae69
+        'connection': connection,
97ae69
+        'disk': disk,
97ae69
+        'disk_service': disk_service,
97ae69
+        'failed': False,
97ae69
+        'highestwrite': 0,
97ae69
+        'http': http,
97ae69
+        'needs_auth': needs_auth,
97ae69
+        'path': destination_url.path,
97ae69
+        'transfer': transfer,
97ae69
+        'transfer_service': transfer_service,
97ae69
+    }
97ae69
+
97ae69
 def can_trim(h):
97ae69
-    get_options(h)
97ae69
     return h['can_trim']
97ae69
 
97ae69
 def can_flush(h):
97ae69
-    get_options(h)
97ae69
     return h['can_flush']
97ae69
 
97ae69
 def get_size(h):
97ae69
-- 
fd1da6
2.17.2
97ae69