Skip to content

Commit

Permalink
RHEL 7: -o rhv-upload: Use Python 2 instead of Python 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwmjones committed Jun 22, 2018
1 parent d04460a commit e5826e6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 31 deletions.
21 changes: 10 additions & 11 deletions v2v/output_rhv_upload.ml
Expand Up @@ -77,7 +77,6 @@ let parse_output_options options =

{ rhv_cafile; rhv_cluster; rhv_direct; rhv_verifypeer }

let python3 = "python3" (* Defined by PEP 394 *)
let pidfile_timeout = 30
let finalization_timeout = 5*60

Expand Down Expand Up @@ -119,10 +118,10 @@ class output_rhv_upload output_alloc output_conn

(* Check that the Python binary is available. *)
let error_unless_python_binary_on_path () =
try ignore (which python3)
try ignore (which "python")
with Executable_not_found _ ->
error (f_"no python binary called ‘%s’ can be found on the $PATH")
python3
"python"
in

(* Check that nbdkit is available and new enough. *)
Expand All @@ -142,14 +141,14 @@ class output_rhv_upload output_alloc output_conn
error (f_"nbdkit is not new enough, you need to upgrade to nbdkit ≥ 1.1.16")
in

(* Check that the python3 plugin is installed and working
(* Check that the python plugin is installed and working
* and can load the plugin script.
*)
let error_unless_nbdkit_python3_working () =
let error_unless_nbdkit_python_working () =
let cmd = sprintf "nbdkit %s %s --dump-plugin >/dev/null"
python3 (quote plugin) in
"python" (quote plugin) in
if Sys.command cmd <> 0 then
error (f_"nbdkit Python 3 plugin is not installed or not working. It is required if you want to use ‘-o rhv-upload’.
error (f_"nbdkit Python plugin is not installed or not working. It is required if you want to use ‘-o rhv-upload’.
See also \"OUTPUT TO RHV\" in the virt-v2v(1) manual.")
in
Expand Down Expand Up @@ -208,7 +207,7 @@ See also \"OUTPUT TO RHV\" in the virt-v2v(1) manual.")
"--newstyle"; (* use newstyle NBD protocol *)
"--exportname"; "/";

"python3"; (* use the nbdkit Python 3 plugin *)
"python"; (* use the nbdkit Python plugin *)
plugin; (* Python plugin script *)
] in
let args = if verbose () then args @ ["--verbose"] else args in
Expand All @@ -225,7 +224,7 @@ object
method precheck () =
error_unless_python_binary_on_path ();
error_unless_nbdkit_working ();
error_unless_nbdkit_python3_working ();
error_unless_nbdkit_python_working ();
if have_selinux then
error_unless_nbdkit_compiled_with_selinux ()

Expand Down Expand Up @@ -254,7 +253,7 @@ object
with_open_out
json_param_file
(fun chan -> output_string chan (JSON.string_of_doc json_params));
if run_command [ python3; precheck; json_param_file ] <> 0 then
if run_command [ "python"; precheck; json_param_file ] <> 0 then
error (f_"failed server prechecks, see earlier errors");

(* Create an nbdkit instance for each disk and set the
Expand Down Expand Up @@ -403,7 +402,7 @@ If the messages above are not sufficient to diagnose the problem then add the

let ovf_file = tmpdir // "vm.ovf" in
with_open_out ovf_file (fun chan -> output_string chan ovf);
if run_command [ python3; createvm; json_param_file; ovf_file ] <> 0 then
if run_command [ "python"; createvm; json_param_file; ovf_file ] <> 0 then
error (f_"failed to create virtual machine, see earlier errors")

end
Expand Down
20 changes: 16 additions & 4 deletions v2v/rhv-upload-createvm.py
@@ -1,5 +1,6 @@
# -*- python -*-
# oVirt or RHV upload create VM used by ‘virt-v2v -o rhv-upload’
# coding: utf-8
# oVirt or RHV upload create VM used by 'virt-v2v -o rhv-upload'
# Copyright (C) 2018 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -21,8 +22,8 @@
import sys
import time

from http.client import HTTPSConnection
from urllib.parse import urlparse
from httplib import HTTPSConnection
from urlparse import urlparse

import ovirtsdk4 as sdk
import ovirtsdk4.types as types
Expand All @@ -37,8 +38,19 @@
raise RuntimeError("incorrect number of parameters")

# Parameters are passed in via a JSON document.
# https://stackoverflow.com/a/13105359
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
with open(sys.argv[1], 'r') as fp:
params = json.load(fp)
params = byteify(json.load(fp))

# What is passed in is a password file, read the actual password.
with open(params['output_password'], 'r') as fp:
Expand Down
33 changes: 23 additions & 10 deletions v2v/rhv-upload-plugin.py
@@ -1,5 +1,6 @@
# -*- python -*-
# oVirt or RHV upload nbdkit plugin used by ‘virt-v2v -o rhv-upload’
# coding: utf-8
# oVirt or RHV upload nbdkit plugin used by 'virt-v2v -o rhv-upload'
# Copyright (C) 2018 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -16,16 +17,16 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import builtins
from __builtin__ import open as builtin_open
import json
import logging
import socket
import ssl
import sys
import time

from http.client import HTTPSConnection, HTTPConnection
from urllib.parse import urlparse
from httplib import HTTPSConnection, HTTPConnection
from urlparse import urlparse

import ovirtsdk4 as sdk
import ovirtsdk4.types as types
Expand All @@ -37,14 +38,25 @@
# 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.
# https://stackoverflow.com/a/13105359
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
params = None

def config(key, value):
global params

if key == "params":
with builtins.open(value, 'r') as fp:
params = json.load(fp)
with builtin_open(value, 'r') as fp:
params = byteify(json.load(fp))
else:
raise RuntimeError("unknown configuration key '%s'" % key)

Expand All @@ -54,7 +66,8 @@ def config_complete():

def debug(s):
if params['verbose']:
print(s, file=sys.stderr)
sys.stderr.write(s)
sys.stderr.write("\n")
sys.stderr.flush()

def open(readonly):
Expand All @@ -63,7 +76,7 @@ def open(readonly):
username = parsed.username or "admin@internal"

# Read the password from file.
with builtins.open(params['output_password'], 'r') as fp:
with builtin_open(params['output_password'], 'r') as fp:
password = fp.read()
password = password.rstrip()

Expand Down Expand Up @@ -200,7 +213,7 @@ def open(readonly):
# New imageio never needs authentication.
needs_auth = False

j = json.loads(r.read())
j = byteify(json.loads(r.read()))
can_flush = "flush" in j['features']
can_trim = "trim" in j['features']
can_zero = "zero" in j['features']
Expand Down Expand Up @@ -472,7 +485,7 @@ def close(h):
pass

# Write the disk ID file. Only do this on successful completion.
with builtins.open(params['diskid_file'], 'w') as fp:
with builtin_open(params['diskid_file'], 'w') as fp:
fp.write(disk.id)

except:
Expand Down
22 changes: 17 additions & 5 deletions v2v/rhv-upload-precheck.py
@@ -1,5 +1,6 @@
# -*- python -*-
# oVirt or RHV pre-upload checks used by ‘virt-v2v -o rhv-upload’
# coding: utf-8
# oVirt or RHV pre-upload checks used by 'virt-v2v -o rhv-upload'
# Copyright (C) 2018 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -21,8 +22,8 @@
import sys
import time

from http.client import HTTPSConnection
from urllib.parse import urlparse
from httplib import HTTPSConnection
from urlparse import urlparse

import ovirtsdk4 as sdk
import ovirtsdk4.types as types
Expand All @@ -36,8 +37,19 @@
raise RuntimeError("incorrect number of parameters")

# Parameters are passed in via a JSON document.
# https://stackoverflow.com/a/13105359
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
with open(sys.argv[1], 'r') as fp:
params = json.load(fp)
params = byteify(json.load(fp))

# What is passed in is a password file, read the actual password.
with open(params['output_password'], 'r') as fp:
Expand Down Expand Up @@ -67,7 +79,7 @@
)
if len(vms) > 0:
vm = vms[0]
raise RuntimeError("VM already exists with name ‘%s’, id ‘%s’" %
raise RuntimeError("VM already exists with name '%s', id '%s'" %
(params['output_name'], vm.id))

# Otherwise everything is OK, exit with no error.
2 changes: 1 addition & 1 deletion v2v/test-v2v-python-syntax.sh
Expand Up @@ -25,7 +25,7 @@ skip_if_skipped
files="rhv-upload-createvm.py rhv-upload-plugin.py rhv-upload-precheck.py"

# Base version of Python.
python=python3
python=python

# Checks the files are syntactically correct, but not very much else.
for f in $files; do
Expand Down

0 comments on commit e5826e6

Please sign in to comment.