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

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