mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0023-v2v-o-rhv-upload-split-vmcheck-out-of-precheck.patch

46b2f6
From ae560843517da393492418bb61c3dee43e64af2c Mon Sep 17 00:00:00 2001
46b2f6
From: Pino Toscano <ptoscano@redhat.com>
46b2f6
Date: Thu, 12 Sep 2019 13:19:48 +0200
46b2f6
Subject: [PATCH] v2v: -o rhv-upload: split vmcheck out of precheck
46b2f6
46b2f6
Split the VM existance check out of the precheck script to a new vmcheck
46b2f6
script, and invoke that in #prepare_targets.  Invoke the precheck script
46b2f6
in #precheck, as now it can be run with only values of command line
46b2f6
options.
46b2f6
46b2f6
This does not change which checks are performed; however, an invalid
46b2f6
cluster name will make virt-v2v fail way earlier (even before connecting
46b2f6
to the source).
46b2f6
46b2f6
(cherry picked from commit 6499fdc199790619745eee28fcae3421c32c4735)
46b2f6
---
46b2f6
 v2v/Makefile.am                           |  8 ++-
46b2f6
 v2v/output_rhv_upload.ml                  | 14 +++--
46b2f6
 v2v/output_rhv_upload_precheck_source.mli |  2 +-
46b2f6
 v2v/output_rhv_upload_vmcheck_source.mli  | 19 ++++++
46b2f6
 v2v/rhv-upload-precheck.py                | 12 +---
46b2f6
 v2v/rhv-upload-vmcheck.py                 | 73 +++++++++++++++++++++++
46b2f6
 6 files changed, 111 insertions(+), 17 deletions(-)
46b2f6
 create mode 100644 v2v/output_rhv_upload_vmcheck_source.mli
46b2f6
 create mode 100644 v2v/rhv-upload-vmcheck.py
46b2f6
46b2f6
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
46b2f6
index 53c137fc6..30f040d3e 100644
46b2f6
--- a/v2v/Makefile.am
46b2f6
+++ b/v2v/Makefile.am
46b2f6
@@ -26,7 +26,8 @@ BUILT_SOURCES = \
46b2f6
 	config.ml \
46b2f6
 	output_rhv_upload_createvm_source.ml \
46b2f6
 	output_rhv_upload_plugin_source.ml \
46b2f6
-	output_rhv_upload_precheck_source.ml
46b2f6
+	output_rhv_upload_precheck_source.ml \
46b2f6
+	output_rhv_upload_vmcheck_source.ml
46b2f6
 
46b2f6
 EXTRA_DIST = \
46b2f6
 	$(SOURCES_MLI) $(SOURCES_ML) $(SOURCES_C) \
46b2f6
@@ -36,6 +37,7 @@ EXTRA_DIST = \
46b2f6
 	rhv-upload-createvm.py \
46b2f6
 	rhv-upload-plugin.py \
46b2f6
 	rhv-upload-precheck.py \
46b2f6
+	rhv-upload-vmcheck.py \
46b2f6
 	v2v_unit_tests.ml \
46b2f6
 	virt-v2v.pod \
46b2f6
 	virt-v2v-copy-to-local.pod \
46b2f6
@@ -87,6 +89,7 @@ SOURCES_MLI = \
46b2f6
 	output_rhv_upload_createvm_source.mli \
46b2f6
 	output_rhv_upload_plugin_source.mli \
46b2f6
 	output_rhv_upload_precheck_source.mli \
46b2f6
+	output_rhv_upload_vmcheck_source.mli \
46b2f6
 	output_vdsm.mli \
46b2f6
 	parse_ova.mli \
46b2f6
 	parse_ovf_from_ova.mli \
46b2f6
@@ -152,6 +155,7 @@ SOURCES_ML = \
46b2f6
 	output_rhv_upload_createvm_source.ml \
46b2f6
 	output_rhv_upload_plugin_source.ml \
46b2f6
 	output_rhv_upload_precheck_source.ml \
46b2f6
+	output_rhv_upload_vmcheck_source.ml \
46b2f6
 	output_rhv_upload.ml \
46b2f6
 	output_vdsm.ml \
46b2f6
 	output_openstack.ml \
46b2f6
@@ -173,6 +177,8 @@ output_rhv_upload_plugin_source.ml: $(srcdir)/rhv-upload-plugin.py
46b2f6
 	$(srcdir)/embed.sh code $^ $@
46b2f6
 output_rhv_upload_precheck_source.ml: $(srcdir)/rhv-upload-precheck.py
46b2f6
 	$(srcdir)/embed.sh code $^ $@
46b2f6
+output_rhv_upload_vmcheck_source.ml: $(srcdir)/rhv-upload-vmcheck.py
46b2f6
+	$(srcdir)/embed.sh code $^ $@
46b2f6
 
46b2f6
 if HAVE_OCAML
46b2f6
 
46b2f6
diff --git a/v2v/output_rhv_upload.ml b/v2v/output_rhv_upload.ml
46b2f6
index c2a5c72c7..adcbdf25f 100644
46b2f6
--- a/v2v/output_rhv_upload.ml
46b2f6
+++ b/v2v/output_rhv_upload.ml
46b2f6
@@ -94,10 +94,13 @@ class output_rhv_upload output_alloc output_conn
46b2f6
 
46b2f6
   let diskid_file_of_id id = tmpdir // sprintf "diskid.%d" id in
46b2f6
 
46b2f6
-  (* Create Python scripts for precheck, plugin and create VM. *)
46b2f6
+  (* Create Python scripts for precheck, vmcheck, plugin and create VM. *)
46b2f6
   let precheck_script =
46b2f6
     Python_script.create ~name:"rhv-upload-precheck.py"
46b2f6
                          Output_rhv_upload_precheck_source.code in
46b2f6
+  let vmcheck_script =
46b2f6
+    Python_script.create ~name:"rhv-upload-vmcheck.py"
46b2f6
+                         Output_rhv_upload_vmcheck_source.code in
46b2f6
   let plugin_script =
46b2f6
     Python_script.create ~name:"rhv-upload-plugin.py"
46b2f6
                          Output_rhv_upload_plugin_source.code in
46b2f6
@@ -230,6 +233,9 @@ object
46b2f6
     error_unless_nbdkit_working ();
46b2f6
     error_unless_nbdkit_python_plugin_working ();
46b2f6
     error_unless_output_alloc_sparse ();
46b2f6
+    (* Python code prechecks. *)
46b2f6
+    if Python_script.run_command precheck_script json_params [] <> 0 then
46b2f6
+      error (f_"failed server prechecks, see earlier errors");
46b2f6
     if have_selinux then
46b2f6
       error_unless_nbdkit_compiled_with_selinux ()
46b2f6
 
46b2f6
@@ -251,11 +257,11 @@ object
46b2f6
     let json_params =
46b2f6
       ("output_name", JSON.String output_name) :: json_params in
46b2f6
 
46b2f6
-    (* Python code prechecks.  These can't run in #precheck because
46b2f6
+    (* Check that the VM does not exist.  This can't run in #precheck because
46b2f6
      * we need to know the name of the virtual machine.
46b2f6
      *)
46b2f6
-    if Python_script.run_command precheck_script json_params [] <> 0 then
46b2f6
-      error (f_"failed server prechecks, see earlier errors");
46b2f6
+    if Python_script.run_command vmcheck_script json_params [] <> 0 then
46b2f6
+      error (f_"failed vmchecks, see earlier errors");
46b2f6
 
46b2f6
     (* Create an nbdkit instance for each disk and set the
46b2f6
      * target URI to point to the NBD socket.
46b2f6
diff --git a/v2v/output_rhv_upload_precheck_source.mli b/v2v/output_rhv_upload_precheck_source.mli
46b2f6
index c1bafa15b..aa33bc548 100644
46b2f6
--- a/v2v/output_rhv_upload_precheck_source.mli
46b2f6
+++ b/v2v/output_rhv_upload_precheck_source.mli
46b2f6
@@ -1,5 +1,5 @@
46b2f6
 (* virt-v2v
46b2f6
- * Copyright (C) 2018 Red Hat Inc.
46b2f6
+ * Copyright (C) 2019 Red Hat Inc.
46b2f6
  *
46b2f6
  * This program is free software; you can redistribute it and/or modify
46b2f6
  * it under the terms of the GNU General Public License as published by
46b2f6
diff --git a/v2v/output_rhv_upload_vmcheck_source.mli b/v2v/output_rhv_upload_vmcheck_source.mli
46b2f6
new file mode 100644
46b2f6
index 000000000..c1bafa15b
46b2f6
--- /dev/null
46b2f6
+++ b/v2v/output_rhv_upload_vmcheck_source.mli
46b2f6
@@ -0,0 +1,19 @@
46b2f6
+(* virt-v2v
46b2f6
+ * Copyright (C) 2018 Red Hat Inc.
46b2f6
+ *
46b2f6
+ * This program is free software; you can redistribute it and/or modify
46b2f6
+ * it under the terms of the GNU General Public License as published by
46b2f6
+ * the Free Software Foundation; either version 2 of the License, or
46b2f6
+ * (at your option) any later version.
46b2f6
+ *
46b2f6
+ * This program is distributed in the hope that it will be useful,
46b2f6
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
46b2f6
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46b2f6
+ * GNU General Public License for more details.
46b2f6
+ *
46b2f6
+ * You should have received a copy of the GNU General Public License along
46b2f6
+ * with this program; if not, write to the Free Software Foundation, Inc.,
46b2f6
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46b2f6
+ *)
46b2f6
+
46b2f6
+val code : string
46b2f6
diff --git a/v2v/rhv-upload-precheck.py b/v2v/rhv-upload-precheck.py
46b2f6
index b79f91b4a..d6a58f0fc 100644
46b2f6
--- a/v2v/rhv-upload-precheck.py
46b2f6
+++ b/v2v/rhv-upload-precheck.py
46b2f6
@@ -1,6 +1,6 @@
46b2f6
 # -*- python -*-
46b2f6
 # oVirt or RHV pre-upload checks used by ‘virt-v2v -o rhv-upload’
46b2f6
-# Copyright (C) 2018 Red Hat Inc.
46b2f6
+# Copyright (C) 2018-2019 Red Hat Inc.
46b2f6
 #
46b2f6
 # This program is free software; you can redistribute it and/or modify
46b2f6
 # it under the terms of the GNU General Public License as published by
46b2f6
@@ -60,16 +60,6 @@ connection = sdk.Connection(
46b2f6
 
46b2f6
 system_service = connection.system_service()
46b2f6
 
46b2f6
-# Find if a virtual machine already exists with that name.
46b2f6
-vms_service = system_service.vms_service()
46b2f6
-vms = vms_service.list(
46b2f6
-    search = ("name=%s" % params['output_name']),
46b2f6
-)
46b2f6
-if len(vms) > 0:
46b2f6
-    vm = vms[0]
46b2f6
-    raise RuntimeError("VM already exists with name ‘%s’, id ‘%s’" %
46b2f6
-                       (params['output_name'], vm.id))
46b2f6
-
46b2f6
 # Check whether the specified cluster exists.
46b2f6
 clusters_service = system_service.clusters_service()
46b2f6
 clusters = clusters_service.list(
46b2f6
diff --git a/v2v/rhv-upload-vmcheck.py b/v2v/rhv-upload-vmcheck.py
46b2f6
new file mode 100644
46b2f6
index 000000000..fbb884b94
46b2f6
--- /dev/null
46b2f6
+++ b/v2v/rhv-upload-vmcheck.py
46b2f6
@@ -0,0 +1,73 @@
46b2f6
+# -*- python -*-
46b2f6
+# oVirt or RHV VM existance check used by ‘virt-v2v -o rhv-upload’
46b2f6
+# Copyright (C) 2018-2019 Red Hat Inc.
46b2f6
+#
46b2f6
+# This program is free software; you can redistribute it and/or modify
46b2f6
+# it under the terms of the GNU General Public License as published by
46b2f6
+# the Free Software Foundation; either version 2 of the License, or
46b2f6
+# (at your option) any later version.
46b2f6
+#
46b2f6
+# This program is distributed in the hope that it will be useful,
46b2f6
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
46b2f6
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46b2f6
+# GNU General Public License for more details.
46b2f6
+#
46b2f6
+# You should have received a copy of the GNU General Public License along
46b2f6
+# with this program; if not, write to the Free Software Foundation, Inc.,
46b2f6
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46b2f6
+
46b2f6
+import json
46b2f6
+import logging
46b2f6
+import sys
46b2f6
+import time
46b2f6
+
46b2f6
+from http.client import HTTPSConnection
46b2f6
+from urllib.parse import urlparse
46b2f6
+
46b2f6
+import ovirtsdk4 as sdk
46b2f6
+import ovirtsdk4.types as types
46b2f6
+
46b2f6
+# Parameters are passed in via a JSON doc from the OCaml code.
46b2f6
+# Because this Python code ships embedded inside virt-v2v there
46b2f6
+# is no formal API here.
46b2f6
+params = None
46b2f6
+
46b2f6
+if len(sys.argv) != 2:
46b2f6
+    raise RuntimeError("incorrect number of parameters")
46b2f6
+
46b2f6
+# Parameters are passed in via a JSON document.
46b2f6
+with open(sys.argv[1], 'r') as fp:
46b2f6
+    params = json.load(fp)
46b2f6
+
46b2f6
+# What is passed in is a password file, read the actual password.
46b2f6
+with open(params['output_password'], 'r') as fp:
46b2f6
+    output_password = fp.read()
46b2f6
+output_password = output_password.rstrip()
46b2f6
+
46b2f6
+# Parse out the username from the output_conn URL.
46b2f6
+parsed = urlparse(params['output_conn'])
46b2f6
+username = parsed.username or "admin@internal"
46b2f6
+
46b2f6
+# Connect to the server.
46b2f6
+connection = sdk.Connection(
46b2f6
+    url = params['output_conn'],
46b2f6
+    username = username,
46b2f6
+    password = output_password,
46b2f6
+    ca_file = params['rhv_cafile'],
46b2f6
+    log = logging.getLogger(),
46b2f6
+    insecure = params['insecure'],
46b2f6
+)
46b2f6
+
46b2f6
+system_service = connection.system_service()
46b2f6
+
46b2f6
+# Find if a virtual machine already exists with that name.
46b2f6
+vms_service = system_service.vms_service()
46b2f6
+vms = vms_service.list(
46b2f6
+    search = ("name=%s" % params['output_name']),
46b2f6
+)
46b2f6
+if len(vms) > 0:
46b2f6
+    vm = vms[0]
46b2f6
+    raise RuntimeError("VM already exists with name ‘%s’, id ‘%s’" %
46b2f6
+                       (params['output_name'], vm.id))
46b2f6
+
46b2f6
+# Otherwise everything is OK, exit with no error.
46b2f6
-- 
b155d0
2.26.2
46b2f6