]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
librbd/api: re-use migration open source image state machine
authorJason Dillaman <dillaman@redhat.com>
Wed, 2 Dec 2020 01:01:23 +0000 (20:01 -0500)
committerJason Dillaman <dillaman@redhat.com>
Wed, 2 Dec 2020 01:01:23 +0000 (20:01 -0500)
Let the open source image state machine parse the provided
source-spec JSON and instantiate the source image ctx for the
prepare migration step.

Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/librbd/api/Migration.cc

index 9f7d4d79149012f8cc8ea5db4673fdab2efa97d2..d9d2518992d184f2b81953c327fc1daac0ed17d8 100644 (file)
@@ -35,8 +35,8 @@
 #include "librbd/image/Types.h"
 #include "librbd/internal.h"
 #include "librbd/migration/FormatInterface.h"
+#include "librbd/migration/OpenSourceImageRequest.h"
 #include "librbd/migration/NativeFormat.h"
-#include "librbd/migration/SourceSpecBuilder.h"
 #include "librbd/mirror/DisableRequest.h"
 #include "librbd/mirror/EnableRequest.h"
 
@@ -532,38 +532,20 @@ int Migration<I>::prepare_import(
                  << dest_io_ctx.get_pool_name() << "/"
                  << dest_image_name << ", opts=" << opts << dendl;
 
-  auto src_image_ctx = I::create("", "", nullptr, dest_io_ctx, true);
-  auto asio_engine = src_image_ctx->asio_engine;
-
-  migration::SourceSpecBuilder<I> source_spec_builder(src_image_ctx);
-  json_spirit::mObject source_spec_object;
-  int r = source_spec_builder.parse_source_spec(source_spec,
-                                                &source_spec_object);
-  if (r < 0) {
-    lderr(cct) << "failed to parse source spec: " << cpp_strerror(r)
-               << dendl;
-    src_image_ctx->state->close();
-    return r;
-  }
-
-  std::unique_ptr<migration::FormatInterface> format;
-  r = source_spec_builder.build_format(source_spec_object, true, &format);
-  if (r < 0) {
-    lderr(cct) << "failed to build migration format handler: "
-               << cpp_strerror(r) << dendl;
-    src_image_ctx->state->close();
-    return r;
-  }
-
+  I* src_image_ctx = nullptr;
   C_SaferCond open_ctx;
-  format->open(&open_ctx);
-  r = open_ctx.wait();
+  auto req = migration::OpenSourceImageRequest<I>::create(
+    dest_io_ctx, nullptr, CEPH_NOSNAP,
+    {-1, "", "", "", source_spec, {}, 0, false}, &src_image_ctx, &open_ctx);
+  req->send();
+
+  int r = open_ctx.wait();
   if (r < 0) {
-    lderr(cct) << "failed to open migration source: " << cpp_strerror(r)
-               << dendl;
+    lderr(cct) << "failed to open source image: " << cpp_strerror(r) << dendl;
     return r;
   }
 
+  auto asio_engine = src_image_ctx->asio_engine;
   BOOST_SCOPE_EXIT_TPL(src_image_ctx) {
     src_image_ctx->state->close();
   } BOOST_SCOPE_EXIT_END;
@@ -584,6 +566,18 @@ int Migration<I>::prepare_import(
 
   ldout(cct, 20) << "updated opts=" << opts << dendl;
 
+  // use json-spirit to clean-up json formatting
+  json_spirit::mObject source_spec_object;
+  json_spirit::mValue json_root;
+  if(json_spirit::read(source_spec, json_root)) {
+    try {
+      source_spec_object = json_root.get_obj();
+    } catch (std::runtime_error&) {
+      lderr(cct) << "failed to clean source spec" << dendl;
+      return -EINVAL;
+    }
+  }
+
   auto dst_image_ctx = I::create(
     dest_image_name, util::generate_image_id(dest_io_ctx), nullptr,
     dest_io_ctx, false);