mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From 376064fc32f0ccfea5c32ff5cd2fd844fc399ca6 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Tue, 19 Jun 2018 18:02:21 +0100
Subject: [PATCH] v2v: -o rhv-upload: Always fetch server options when opening
 the connection.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously we lazily requested the server options in the can_*
callbacks.  The can_* callbacks are always called by nbdkit straight
after open, so this just adds complexity for no benefit.  This change
simply makes the code always fetch the server options during the open
callback.

This is — functionally at least — mostly just refactoring.  However I
also added a useful debug message so we can see what features the
imageio server is offering.

(cherry picked from commit a1e1f6ec887c2a7973612d2edf7066fd3194ba0b)
---
 v2v/rhv-upload-plugin.py | 63 +++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 33 deletions(-)

diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
index 5c036c46a..f215eaecf 100644
--- a/v2v/rhv-upload-plugin.py
+++ b/v2v/rhv-upload-plugin.py
@@ -165,34 +165,13 @@ def open(readonly):
         context = context
     )
 
-    # Save everything we need to make requests in the handle.
-    return {
-        'can_flush': False,
-        'can_trim': False,
-        'can_zero': False,
-        'connection': connection,
-        'disk': disk,
-        'disk_service': disk_service,
-        'failed': False,
-        'got_options': False,
-        'highestwrite': 0,
-        'http': http,
-        'needs_auth': not params['rhv_direct'],
-        'path': destination_url.path,
-        'transfer': transfer,
-        'transfer_service': transfer_service,
-    }
+    # The first request is to fetch the features of the server.
+    needs_auth = not params['rhv_direct']
+    can_flush = False
+    can_trim = False
+    can_zero = False
 
-# Can we issue zero, trim or flush requests?
-def get_options(h):
-    if h['got_options']:
-        return
-    h['got_options'] = True
-
-    http = h['http']
-    transfer = h['transfer']
-
-    http.putrequest("OPTIONS", h['path'])
+    http.putrequest("OPTIONS", destination_url.path)
     http.putheader("Authorization", transfer.signed_ticket)
     http.endheaders()
 
@@ -201,12 +180,12 @@ def get_options(h):
 
     if r.status == 200:
         # New imageio never needs authentication.
-        h['needs_auth'] = False
+        needs_auth = False
 
         j = json.loads(data)
-        h['can_zero'] = "zero" in j['features']
-        h['can_trim'] = "trim" in j['features']
-        h['can_flush'] = "flush" in j['features']
+        can_flush = "flush" in j['features']
+        can_trim = "trim" in j['features']
+        can_zero = "zero" in j['features']
 
     # Old imageio servers returned either 405 Method Not Allowed or
     # 204 No Content (with an empty body).  If we see that we leave
@@ -218,12 +197,30 @@ def get_options(h):
         raise RuntimeError("could not use OPTIONS request: %d: %s" %
                            (r.status, r.reason))
 
+    debug("imageio features: flush=%r trim=%r zero=%r" %
+          (can_flush, can_trim, can_zero))
+
+    # Save everything we need to make requests in the handle.
+    return {
+        'can_flush': can_flush,
+        'can_trim': can_trim,
+        'can_zero': can_zero,
+        'connection': connection,
+        'disk': disk,
+        'disk_service': disk_service,
+        'failed': False,
+        'highestwrite': 0,
+        'http': http,
+        'needs_auth': needs_auth,
+        'path': destination_url.path,
+        'transfer': transfer,
+        'transfer_service': transfer_service,
+    }
+
 def can_trim(h):
-    get_options(h)
     return h['can_trim']
 
 def can_flush(h):
-    get_options(h)
     return h['can_flush']
 
 def get_size(h):
-- 
2.20.1