Skip to content

Commit

Permalink
Added compatibility for python 2.6 subprocess.check_output (#1112)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
D1v38om83r authored May 20, 2020
1 parent 77f0f66 commit 69c8d37
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions Utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Openssl = "openssl"
os_release = "/etc/os-release"
system_release = "/etc/system-release"


class WALAEventOperation:
Expand Down
16 changes: 11 additions & 5 deletions Utils/distroutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions Utils/extensionutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down
2 changes: 1 addition & 1 deletion VMAccess/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ExtensionImage xmlns="http://schemas.microsoft.com/windowsazure">
<ProviderNameSpace>Microsoft.OSTCExtensions</ProviderNameSpace>
<Type>VMAccessForLinux</Type>
<Version>1.5.5</Version>
<Version>1.5.6</Version>
<Label>Microsoft Azure VM Access Extension for Linux Virtual Machines</Label>
<HostingResources>VmRole</HostingResources>
<MediaLink></MediaLink>
Expand Down
19 changes: 12 additions & 7 deletions VMAccess/vmaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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-<version>/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"

Expand Down

0 comments on commit 69c8d37

Please sign in to comment.