mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From fb5e3592a94b535abe686b65e51577ad0e36fcd0 Mon Sep 17 00:00:00 2001
From: Pino Toscano <ptoscano@redhat.com>
Date: Thu, 12 Sep 2019 13:19:48 +0200
Subject: [PATCH] v2v: -o rhv-upload: split vmcheck out of precheck

Split the VM existance check out of the precheck script to a new vmcheck
script, and invoke that in #prepare_targets.  Invoke the precheck script
in #precheck, as now it can be run with only values of command line
options.

This does not change which checks are performed; however, an invalid
cluster name will make virt-v2v fail way earlier (even before connecting
to the source).

(cherry picked from commit 6499fdc199790619745eee28fcae3421c32c4735)
---
 v2v/Makefile.am                           |  8 ++-
 v2v/output_rhv_upload.ml                  | 14 +++--
 v2v/output_rhv_upload_precheck_source.mli |  2 +-
 v2v/output_rhv_upload_vmcheck_source.mli  | 19 ++++++
 v2v/rhv-upload-precheck.py                | 12 +---
 v2v/rhv-upload-vmcheck.py                 | 73 +++++++++++++++++++++++
 6 files changed, 111 insertions(+), 17 deletions(-)
 create mode 100644 v2v/output_rhv_upload_vmcheck_source.mli
 create mode 100644 v2v/rhv-upload-vmcheck.py

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