Fix controllerconfig subprocess py3 compatibility

Subprocess Popen.communicate and check_output return a string object
on python2 and a bytes object on python3.

Add universal_newlines=True parameter to keep backwards compatibility
and to return a string object on python3 also.

Story: 2008454
Task: 42675
Signed-off-by: Dan Voiculeasa <dan.voiculeasa@windriver.com>
Change-Id: I89c8ab3b83c6567cf9282027889f38237657c71c
(cherry picked from commit 1fda925b50ea798838980b4588abea69ea2a272f)
This commit is contained in:
Dan Voiculeasa 2021-06-22 20:25:05 +03:00 committed by Charles Short
parent a0645a4e09
commit 86710cc638
4 changed files with 34 additions and 16 deletions

View File

@ -44,7 +44,8 @@ def configure_management():
lldp_interface_list = list()
print("Enabling interfaces... ", end=' ')
ip_link_output = subprocess.check_output(['ip', '-o', 'link'])
ip_link_output = subprocess.check_output(['ip', '-o', 'link'],
universal_newlines=True)
for line in ip_link_output.splitlines():
interface = line.split()[1].rstrip(':')
@ -66,7 +67,8 @@ def configure_management():
print("Retrieving neighbor details... ", end=' ')
lldpcli_show_output = subprocess.check_output(
['sudo', 'lldpcli', 'show', 'neighbors', 'summary', '-f', 'json'])
['sudo', 'lldpcli', 'show', 'neighbors', 'summary', '-f', 'json'],
universal_newlines=True)
try:
lldp_interfaces = json.loads(
lldpcli_show_output)['lldp'][0]['interface']

View File

@ -205,7 +205,8 @@ def tidy_storage(result_file):
"ls",
"--pool",
"images"],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
except subprocess.CalledProcessError:
LOG.error("Failed to access rbd images pool")
raise TidyStorageFail("Failed to access rbd images pool")
@ -229,7 +230,8 @@ def tidy_storage(result_file):
["grep",
"fsid",
"/etc/ceph/ceph.conf"],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
ceph_cluster = [i.strip() for i in output.split('=')
if i.find('fsid') == -1]
@ -239,7 +241,9 @@ def tidy_storage(result_file):
try:
img_file = 'rbd:images/{}'.format(image)
output = subprocess.check_output(
["qemu-img", "info", img_file], stderr=subprocess.STDOUT)
["qemu-img", "info", img_file],
stderr=subprocess.STDOUT,
universal_newlines=True)
fields['disk_format'] = 'qcow2'
for line in output.split('\n'):
@ -281,7 +285,8 @@ def tidy_storage(result_file):
try:
output = subprocess.check_output(
["rbd", "ls", "--pool", "cinder-volumes"],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
except subprocess.CalledProcessError:
LOG.error("Failed to access rbd cinder-volumes pool")
raise TidyStorageFail(
@ -298,7 +303,8 @@ def tidy_storage(result_file):
try:
output = subprocess.check_output(
["rbd", "snap", "list", volume],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
keep_snap = False
for line in output.split('\n'):
@ -358,7 +364,8 @@ def tidy_storage(result_file):
try:
output = subprocess.check_output(
["rbd", "ls", "--pool", "cinder-volumes"],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
except subprocess.CalledProcessError:
LOG.error("Failed to access rbd cinder-volumes pool")
raise TidyStorageFail("Failed to access rbd cinder-volumes pool")
@ -383,7 +390,8 @@ def tidy_storage(result_file):
# Find out if the volume is a bootable one
output = subprocess.check_output(
["rbd", "info", volume],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
bootable = False
for line in output.split('\n'):
@ -394,7 +402,9 @@ def tidy_storage(result_file):
# Find out if the volume has any snapshots.
print("Checking if volume %s has snapshots...\n" % vol_id)
output = subprocess.check_output(
["rbd", "snap", "list", volume], stderr=subprocess.STDOUT)
["rbd", "snap", "list", volume],
stderr=subprocess.STDOUT,
universal_newlines=True)
snap_l = [item.strip() for item in output.split(' ')
if item.find('snapshot-') != -1]
@ -428,11 +438,13 @@ def tidy_storage(result_file):
del_snap = '{}@{}'.format(volume, snap)
output = subprocess.check_output(
["rbd", "snap", "unprotect", del_snap],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
output = subprocess.check_output(
["rbd", "snap", "rm", del_snap],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
except Exception as e:
LOG.exception(e)
@ -468,7 +480,8 @@ def tidy_storage(result_file):
print("Checking if volume %s has snapshots...\n" % vol_id)
output = subprocess.check_output(
["rbd", "snap", "list", volume],
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
universal_newlines=True)
snap_l = [item.strip() for item in output.split(' ')
if item.find('snapshot-') != -1]

View File

@ -242,7 +242,8 @@ def create_simplex_backup(software_upgrade):
'ansible-playbook',
'-e', ' '.join(backup_vars),
sysinv_constants.ANSIBLE_PLATFORM_BACKUP_PLAYBOOK]
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
universal_newlines=True)
out, _ = proc.communicate()
LOG.info(out)
if proc.returncode:

View File

@ -60,7 +60,8 @@ def restart_service(name):
def check_sm_service(service, state):
""" Check whether an SM service has the supplied state """
try:
output = subprocess.check_output(["sm-query", "service", service])
output = subprocess.check_output(["sm-query", "service", service],
universal_newlines=True)
return state in output
except subprocess.CalledProcessError:
return False
@ -321,7 +322,8 @@ def is_ssh_parent():
"""Determine if current process is started from a ssh session"""
command = ('pstree -s %d' % (os.getpid()))
try:
cmd_output = subprocess.check_output(command, shell=True)
cmd_output = subprocess.check_output(command, shell=True,
universal_newlines=True)
if "ssh" in cmd_output:
return True
else: