From 69c8d37de30abe82c1013282f31ebf3620cc3501 Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Wed, 20 May 2020 17:25:26 +0000 Subject: [PATCH] Added compatibility for python 2.6 subprocess.check_output (#1112) * Added a definition for subprocess.check_outout for centos 6.9 (Python 2.6) * Also use /etc/system-release to detect distro * more fixes for centos 6.9 --- Utils/constants.py | 1 + Utils/distroutils.py | 16 +++++++++++----- Utils/extensionutils.py | 31 +++++++++++++++++++++++++++++++ VMAccess/manifest.xml | 2 +- VMAccess/vmaccess.py | 19 ++++++++++++------- 5 files changed, 56 insertions(+), 13 deletions(-) diff --git a/Utils/constants.py b/Utils/constants.py index 7ef4f15bf..8de073679 100644 --- a/Utils/constants.py +++ b/Utils/constants.py @@ -2,6 +2,7 @@ Openssl = "openssl" os_release = "/etc/os-release" +system_release = "/etc/system-release" class WALAEventOperation: diff --git a/Utils/distroutils.py b/Utils/distroutils.py index 6aa69a5d2..6e211e991 100644 --- a/Utils/distroutils.py +++ b/Utils/distroutils.py @@ -13,18 +13,24 @@ def get_my_distro(config): if 'FreeBSD' in platform.system(): return FreeBSDDistro(config) - os_name = ext_utils.get_line_starting_with("NAME", constants.os_release) + + if os.path.isfile(constants.os_release): + os_name = ext_utils.get_line_starting_with("NAME", constants.os_release) + elif os.path.isfile(constants.system_release): + os_name = ext_utils.get_file_contents(constants.system_release) + else: + return GenericDistro(config) if os_name is not None: - if re.match("fedora", os_name, re.IGNORECASE): + if re.search("fedora", os_name, re.IGNORECASE): # Fedora return FedoraDistro(config) - if re.match("red\s?hat", os_name, re.IGNORECASE): + if re.search("red\s?hat", os_name, re.IGNORECASE): # Red Hat return RedhatDistro(config) - if re.match("coreos", os_name, re.IGNORECASE): + if re.search("coreos", os_name, re.IGNORECASE): # CoreOs return CoreOSDistro(config) - if re.match("freebsd", os_name, re.IGNORECASE): + if re.search("freebsd", os_name, re.IGNORECASE): # FreeBSD return FreeBSDDistro(config) return GenericDistro(config) diff --git a/Utils/extensionutils.py b/Utils/extensionutils.py index 1aaefe915..c1ac9f4b1 100644 --- a/Utils/extensionutils.py +++ b/Utils/extensionutils.py @@ -10,6 +10,37 @@ import Utils.logger as logger +if not hasattr(subprocess, 'check_output'): + def check_output(*popenargs, **kwargs): + r"""Backport from subprocess module from python 2.7""" + if 'stdout' in kwargs: + raise ValueError('stdout argument not allowed, it will be overridden.') + process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) + output, unused_err = process.communicate() + retcode = process.poll() + if retcode: + cmd = kwargs.get("args") + if cmd is None: + cmd = popenargs[0] + raise subprocess.CalledProcessError(retcode, cmd, output=output) + return output + + + # Exception classes used by this module. + class CalledProcessError(Exception): + def __init__(self, returncode, cmd, output=None): + self.returncode = returncode + self.cmd = cmd + self.output = output + + def __str__(self): + return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) + + + subprocess.check_output = check_output + subprocess.CalledProcessError = CalledProcessError + + def change_owner(file_path, user): """ Lookup user. Attempt chown 'filepath' to 'user'. diff --git a/VMAccess/manifest.xml b/VMAccess/manifest.xml index 5228a500e..54eaa5270 100644 --- a/VMAccess/manifest.xml +++ b/VMAccess/manifest.xml @@ -2,7 +2,7 @@ Microsoft.OSTCExtensions VMAccessForLinux - 1.5.5 + 1.5.6 VmRole diff --git a/VMAccess/vmaccess.py b/VMAccess/vmaccess.py index 357f86ece..bae339e31 100644 --- a/VMAccess/vmaccess.py +++ b/VMAccess/vmaccess.py @@ -367,21 +367,26 @@ def _set_sshd_config(config, name, val): def _get_default_ssh_config_filename(): - os_name = ext_utils.get_line_starting_with("NAME", constants.os_release) + if os.path.isfile(constants.os_release): + os_name = ext_utils.get_line_starting_with("NAME", constants.os_release) + elif os.path.isfile(constants.system_release): + os_name = ext_utils.get_file_contents(constants.system_release) + else: + return "default" if os_name is not None: # the default ssh config files are present in # /var/lib/waagent/Microsoft.OSTCExtensions.VMAccessForLinux-/resources/ - if re.match("centos", os_name, re.IGNORECASE): + if re.search("centos", os_name, re.IGNORECASE): return "centos_default" - if re.match("debian", os_name, re.IGNORECASE): + if re.search("debian", os_name, re.IGNORECASE): return "debian_default" - if re.match("fedora", os_name, re.IGNORECASE): + if re.search("fedora", os_name, re.IGNORECASE): return "fedora_default" - if re.match("red\s?hat", os_name, re.IGNORECASE): + if re.search("red\s?hat", os_name, re.IGNORECASE): return "redhat_default" - if re.match("suse", os_name, re.IGNORECASE): + if re.search("suse", os_name, re.IGNORECASE): return "SuSE_default" - if re.match("ubuntu", os_name, re.IGNORECASE): + if re.search("ubuntu", os_name, re.IGNORECASE): return "ubuntu_default" return "default"