Blame SOURCES/sos-bz1515113-postgresql-from-scl.patch

084cc6
From 138bc243aa592cd628f6e41a1b3c35f1f01f3c37 Mon Sep 17 00:00:00 2001
084cc6
From: "Bryn M. Reeves" <bmr@redhat.com>
084cc6
Date: Tue, 8 Aug 2017 16:48:40 +0100
084cc6
Subject: [PATCH] [Plugin] add executable command enablement checks
084cc6
084cc6
Add a new list/tuple member to the Plugin class that contains a
084cc6
list of executable commands that will enable the plugin if any
084cc6
are present.
084cc6
084cc6
For example, a plugin:
084cc6
084cc6
  class MyPlugin(Plugin, RedHatPlugin):
084cc6
084cc6
      commands = ('mycmd1', 'mycmd2')
084cc6
084cc6
Will be automatically enabled if either 'mycmd1' or 'mycmd2'
084cc6
is present and executable in the policy defined PATH for the
084cc6
run.
084cc6
084cc6
Related: #1051.
084cc6
084cc6
Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
084cc6
---
084cc6
 sos/plugins/__init__.py | 27 ++++++++++++++++++---------
084cc6
 1 file changed, 18 insertions(+), 9 deletions(-)
084cc6
084cc6
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
084cc6
index a31297f6c..2d6f6ff58 100644
084cc6
--- a/sos/plugins/__init__.py
084cc6
+++ b/sos/plugins/__init__.py
084cc6
@@ -19,7 +19,7 @@
084cc6
 from __future__ import with_statement
084cc6
 
084cc6
 from sos.utilities import (sos_get_command_output, import_module, grep,
084cc6
-                           fileobj, tail)
084cc6
+                           fileobj, tail, is_executable)
084cc6
 import os
084cc6
 import glob
084cc6
 import re
084cc6
@@ -111,6 +111,7 @@ class Plugin(object):
084cc6
     version = 'unversioned'
084cc6
     packages = ()
084cc6
     files = ()
084cc6
+    commands = ()
084cc6
     archive = None
084cc6
     profiles = ()
084cc6
     sysroot = '/'
084cc6
@@ -865,23 +866,31 @@ def get_description(self):
084cc6
 
084cc6
     def check_enabled(self):
084cc6
         """This method will be used to verify that a plugin should execute
084cc6
-        given the condition of the underlying environment. The default
084cc6
-        implementation will return True if neither class.files or
084cc6
-        class.packages is specified. If either are specified the plugin will
084cc6
-        check for the existence of any of the supplied files or packages and
084cc6
-        return True if any exist. It is encouraged to override this method if
084cc6
-        this behavior isn't applicable.
084cc6
+        given the condition of the underlying environment.
084cc6
+
084cc6
+        The default implementation will return True if none of class.files,
084cc6
+        class.packages, nor class.commands is specified. If any of these is
084cc6
+        specified the plugin will check for the existence of any of the
084cc6
+        corresponding paths, packages or commands and return True if any
084cc6
+        are present.
084cc6
+
084cc6
+        For plugins with more complex enablement checks this method may be
084cc6
+        overridden.
084cc6
         """
084cc6
         # some files or packages have been specified for this package
084cc6
-        if self.files or self.packages:
084cc6
+        if any([self.files, self.packages, self.commands]):
084cc6
             if isinstance(self.files, six.string_types):
084cc6
                 self.files = [self.files]
084cc6
 
084cc6
             if isinstance(self.packages, six.string_types):
084cc6
                 self.packages = [self.packages]
084cc6
 
084cc6
+            if isinstance(self.commands, six.string_types):
084cc6
+                self.commands = [self.commands]
084cc6
+
084cc6
             return (any(os.path.exists(fname) for fname in self.files) or
084cc6
-                    any(self.is_installed(pkg) for pkg in self.packages))
084cc6
+                    any(self.is_installed(pkg) for pkg in self.packages) or
084cc6
+                    any(is_executable(cmd) for cmd in self.commands))
084cc6
         return True
084cc6
 
084cc6
     def default_enabled(self):
084cc6
From 947e7089c58ac239bc2fd535ac0c77f93f11b895 Mon Sep 17 00:00:00 2001
084cc6
From: Pavel Moravec <pmoravec@redhat.com>
084cc6
Date: Tue, 3 Oct 2017 15:39:43 +0200
084cc6
Subject: [PATCH] [plugins] Add class SCLPlugin for Software Collections
084cc6
 support
084cc6
084cc6
Related to #900 and #1090
084cc6
084cc6
Original author: Bohuslav Kabrda <bkabrda@redhat.com>
084cc6
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
084cc6
---
084cc6
 sos/plugins/__init__.py | 109 ++++++++++++++++++++++++++++++++++++++++++++++--
084cc6
 1 file changed, 106 insertions(+), 3 deletions(-)
084cc6
084cc6
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
084cc6
index 61a3083e3..540d46596 100644
084cc6
--- a/sos/plugins/__init__.py
084cc6
+++ b/sos/plugins/__init__.py
084cc6
@@ -884,6 +884,10 @@ def check_enabled(self):
084cc6
         corresponding paths, packages or commands and return True if any
084cc6
         are present.
084cc6
 
084cc6
+        For SCLPlugin subclasses, it will check whether the plugin can be run
084cc6
+        for any of installed SCLs. If so, it will store names of these SCLs
084cc6
+        on the plugin class in addition to returning True.
084cc6
+
084cc6
         For plugins with more complex enablement checks this method may be
084cc6
         overridden.
084cc6
         """
084cc6
@@ -898,11 +902,34 @@ def check_enabled(self):
084cc6
             if isinstance(self.commands, six.string_types):
084cc6
                 self.commands = [self.commands]
084cc6
 
084cc6
-            return (any(os.path.exists(fname) for fname in self.files) or
084cc6
-                    any(self.is_installed(pkg) for pkg in self.packages) or
084cc6
-                    any(is_executable(cmd) for cmd in self.commands))
084cc6
+            if isinstance(self, SCLPlugin):
084cc6
+                # save SCLs that match files or packages
084cc6
+                type(self)._scls_matched = []
084cc6
+                for scl in self._get_scls():
084cc6
+                    files = [f % {"scl_name": scl} for f in self.files]
084cc6
+                    packages = [p % {"scl_name": scl} for p in self.packages]
084cc6
+                    commands = [c % {"scl_name": scl} for c in self.commands]
084cc6
+                    if self._files_pkgs_or_cmds_present(files,
084cc6
+                                                        packages,
084cc6
+                                                        commands):
084cc6
+                        type(self)._scls_matched.append(scl)
084cc6
+                return len(type(self)._scls_matched) > 0
084cc6
+
084cc6
+            return self._files_pkgs_or_cmds_present(self.files,
084cc6
+                                                    self.packages,
084cc6
+                                                    self.commands)
084cc6
+
084cc6
+        if isinstance(self, SCLPlugin):
084cc6
+            # if files and packages weren't specified, we take all SCLs
084cc6
+            type(self)._scls_matched = self._get_scls()
084cc6
+
084cc6
         return True
084cc6
 
084cc6
+    def _files_pkgs_or_cmds_present(self, files, packages, commands):
084cc6
+            return (any(os.path.exists(fname) for fname in files) or
084cc6
+                    any(self.is_installed(pkg) for pkg in packages) or
084cc6
+                    any(is_executable(cmd) for cmd in commands))
084cc6
+
084cc6
     def default_enabled(self):
084cc6
         """This decides whether a plugin should be automatically loaded or
084cc6
         only if manually specified in the command line."""
084cc6
@@ -979,6 +1006,82 @@ class RedHatPlugin(object):
084cc6
     pass
084cc6
 
084cc6
 
084cc6
+class SCLPlugin(RedHatPlugin):
084cc6
+    """Superclass for plugins operating on Software Collections (SCLs).
084cc6
+
084cc6
+    Subclasses of this plugin class can specify class.files and class.packages
084cc6
+    using "%(scl_name)s" interpolation. The plugin invoking mechanism will try
084cc6
+    to match these against all found SCLs on the system. SCLs that do match
084cc6
+    class.files or class.packages are then accessible via self.scls_matched
084cc6
+    when the plugin is invoked.
084cc6
+
084cc6
+    Additionally, this plugin class provides "add_cmd_output_scl" (run
084cc6
+    a command in context of given SCL), and "add_copy_spec_scl" and
084cc6
+    "add_copy_spec_limit_scl" (copy package from file system of given SCL).
084cc6
+
084cc6
+    For example, you can implement a plugin that will list all global npm
084cc6
+    packages in every SCL that contains "npm" package:
084cc6
+
084cc6
+    class SCLNpmPlugin(Plugin, SCLPlugin):
084cc6
+        packages = ("%(scl_name)s-npm",)
084cc6
+
084cc6
+        def setup(self):
084cc6
+            for scl in self.scls_matched:
084cc6
+                self.add_cmd_output_scl(scl, "npm ls -g --json")
084cc6
+    """
084cc6
+
084cc6
+    @property
084cc6
+    def scls_matched(self):
084cc6
+        if not hasattr(type(self), '_scls_matched'):
084cc6
+            type(self)._scls_matched = []
084cc6
+        return type(self)._scls_matched
084cc6
+
084cc6
+    def _get_scls(self):
084cc6
+        output = sos_get_command_output("scl -l")["output"]
084cc6
+        return [scl.strip() for scl in output.splitlines()]
084cc6
+
084cc6
+    def add_cmd_output_scl(self, scl, cmds, **kwargs):
084cc6
+        """Same as add_cmd_output, except that it wraps command in
084cc6
+        "scl enable" call.
084cc6
+        """
084cc6
+        if isinstance(cmds, six.string_types):
084cc6
+            cmds = [cmds]
084cc6
+        scl_cmds = []
084cc6
+        scl_cmd_tpl = "scl enable %s \"%s\""
084cc6
+        for cmd in cmds:
084cc6
+            scl_cmds.append(scl_cmd_tpl % (scl, cmd))
084cc6
+        self.add_cmd_output(scl_cmds, **kwargs)
084cc6
+
084cc6
+    # config files for Software Collections are under /etc/opt/rh/${scl} and
084cc6
+    # var files are under /var/opt/rh/${scl}. So we need to insert the paths
084cc6
+    # after the appropriate root dir.
084cc6
+    def convert_copyspec_scl(self, scl, copyspec):
084cc6
+        for rootdir in ['etc', 'var']:
084cc6
+            p = re.compile('^/%s/' % rootdir)
084cc6
+            copyspec = p.sub('/%s/opt/rh/%s/' % (rootdir, scl), copyspec)
084cc6
+        return copyspec
084cc6
+
084cc6
+    def add_copy_spec_scl(self, scl, copyspecs):
084cc6
+        """Same as add_copy_spec, except that it prepends path to SCL root
084cc6
+        to "copyspecs".
084cc6
+        """
084cc6
+        if isinstance(copyspecs, six.string_types):
084cc6
+            copyspecs = [copyspecs]
084cc6
+        scl_copyspecs = []
084cc6
+        for copyspec in copyspecs:
084cc6
+            scl_copyspecs.append(self.convert_copyspec_scl(scl, copyspec))
084cc6
+        self.add_copy_spec(scl_copyspecs)
084cc6
+
084cc6
+    def add_copy_spec_limit_scl(self, scl, copyspec, **kwargs):
084cc6
+        """Same as add_copy_spec_limit, except that it prepends path to SCL
084cc6
+        root to "copyspec".
084cc6
+        """
084cc6
+        self.add_copy_spec_limit(
084cc6
+            self.convert_copyspec_scl(scl, copyspec),
084cc6
+            **kwargs
084cc6
+        )
084cc6
+
084cc6
+
084cc6
 class PowerKVMPlugin(RedHatPlugin):
084cc6
     """Tagging class for IBM PowerKVM Linux"""
084cc6
     pass
084cc6
From 62d6435198403abb65b925e7bf63fc39f5394e6d Mon Sep 17 00:00:00 2001
084cc6
From: Pavel Moravec <pmoravec@redhat.com>
084cc6
Date: Mon, 16 Oct 2017 13:20:44 +0200
084cc6
Subject: [PATCH] [postgresql] Collect data for postgreSQL from RHSCL
084cc6
084cc6
Collect postgreSQL data also when postgreSQL is installed from
084cc6
Red Hat Software Collections.
084cc6
084cc6
Resolves: #1090
084cc6
084cc6
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
084cc6
---
084cc6
 sos/plugins/postgresql.py | 117 +++++++++++++++++++++++++---------------------
084cc6
 1 file changed, 65 insertions(+), 52 deletions(-)
084cc6
084cc6
diff --git a/sos/plugins/postgresql.py b/sos/plugins/postgresql.py
084cc6
index 345532223..45c87e890 100644
084cc6
--- a/sos/plugins/postgresql.py
084cc6
+++ b/sos/plugins/postgresql.py
084cc6
@@ -1,3 +1,4 @@
084cc6
+# Copyright (C) 2017 Red Hat, Inc., Pavel Moravec <pmoravec@redhat.com>
084cc6
 # Copyright (C) 2014 Red Hat, Inc., Sandro Bonazzola <sbonazzo@redhat.com>
084cc6
 # Copyright (C) 2013 Chris J Arges <chris.j.arges@canonical.com>
084cc6
 # Copyright (C) 2012-2013 Red Hat, Inc., Bryn M. Reeves <bmr@redhat.com>
084cc6
@@ -20,7 +21,8 @@
084cc6
 import os
084cc6
 import tempfile
084cc6
 
084cc6
-from sos.plugins import Plugin, RedHatPlugin, UbuntuPlugin, DebianPlugin
084cc6
+from sos.plugins import (Plugin, RedHatPlugin, UbuntuPlugin, DebianPlugin,
084cc6
+                         SCLPlugin)
084cc6
 from sos.utilities import find
084cc6
 
084cc6
 
084cc6
@@ -45,47 +47,43 @@ class PostgreSQL(Plugin):
084cc6
         ('dbport', 'database server port number', '', '5432')
084cc6
     ]
084cc6
 
084cc6
-    def pg_dump(self):
084cc6
-        dest_file = os.path.join(self.tmp_dir, "sos_pgdump.tar")
084cc6
-        # We're only modifying this for ourself and our children so there
084cc6
-        # is no need to save and restore environment variables if the user
084cc6
-        # decided to pass the password on the command line.
084cc6
-        if self.get_option("password") is not False:
084cc6
-            os.environ["PGPASSWORD"] = str(self.get_option("password"))
084cc6
-
084cc6
-        if self.get_option("dbhost"):
084cc6
-            cmd = "pg_dump -U %s -h %s -p %s -w -f %s -F t %s" % (
084cc6
-                self.get_option("username"),
084cc6
-                self.get_option("dbhost"),
084cc6
-                self.get_option("dbport"),
084cc6
-                dest_file,
084cc6
-                self.get_option("dbname")
084cc6
-            )
084cc6
-        else:
084cc6
-            cmd = "pg_dump -C -U %s -w -f %s -F t %s " % (
084cc6
-                self.get_option("username"),
084cc6
-                dest_file,
084cc6
-                self.get_option("dbname")
084cc6
-            )
084cc6
-
084cc6
-        result = self.call_ext_prog(cmd)
084cc6
-        if (result['status'] == 0):
084cc6
-            self.add_copy_spec(dest_file)
084cc6
-        else:
084cc6
-            self._log_error(
084cc6
-                "Unable to execute pg_dump. Error(%s)" % (result['output'])
084cc6
-            )
084cc6
-            self.add_alert(
084cc6
-                "ERROR: Unable to execute pg_dump. Error(%s)" %
084cc6
-                (result['output'])
084cc6
-            )
084cc6
-
084cc6
-    def setup(self):
084cc6
+    def pg_dump(self, pg_dump_command="pg_dump", filename="sos_pgdump.tar"):
084cc6
         if self.get_option("dbname"):
084cc6
             if self.get_option("password") or "PGPASSWORD" in os.environ:
084cc6
                 self.tmp_dir = tempfile.mkdtemp()
084cc6
-                self.pg_dump()
084cc6
-            else:
084cc6
+                dest_file = os.path.join(self.tmp_dir, filename)
084cc6
+                # We're only modifying this for ourself and our children so
084cc6
+                # there is no need to save and restore environment variables if
084cc6
+                # the user decided to pass the password on the command line.
084cc6
+                if self.get_option("password") is not False:
084cc6
+                    os.environ["PGPASSWORD"] = str(self.get_option("password"))
084cc6
+
084cc6
+                if self.get_option("dbhost"):
084cc6
+                    cmd = "%s -U %s -h %s -p %s -w -f %s -F t %s" % (
084cc6
+                        pg_dump_command,
084cc6
+                        self.get_option("username"),
084cc6
+                        self.get_option("dbhost"),
084cc6
+                        self.get_option("dbport"),
084cc6
+                        dest_file,
084cc6
+                        self.get_option("dbname")
084cc6
+                    )
084cc6
+                else:
084cc6
+                    cmd = "%s -C -U %s -w -f %s -F t %s " % (
084cc6
+                        pg_dump_command,
084cc6
+                        self.get_option("username"),
084cc6
+                        dest_file,
084cc6
+                        self.get_option("dbname")
084cc6
+                    )
084cc6
+
084cc6
+                result = self.call_ext_prog(cmd)
084cc6
+                if (result['status'] == 0):
084cc6
+                    self.add_copy_spec(dest_file)
084cc6
+                else:
084cc6
+                    self._log_info(
084cc6
+                        "Unable to execute pg_dump. Error(%s)" %
084cc6
+                        (result['output'])
084cc6
+                    )
084cc6
+            else:  # no password in env or options
084cc6
                 self.soslog.warning(
084cc6
                     "password must be supplied to dump a database."
084cc6
                 )
084cc6
@@ -93,6 +91,9 @@ def setup(self):
084cc6
                     "WARN: password must be supplied to dump a database."
084cc6
                 )
084cc6
 
084cc6
+    def setup(self):
084cc6
+        self.pg_dump()
084cc6
+
084cc6
     def postproc(self):
084cc6
         import shutil
084cc6
         if self.tmp_dir:
084cc6
@@ -105,33 +106,45 @@ def postproc(self):
084cc6
                 self.add_alert("ERROR: Unable to remove %s." % (self.tmp_dir))
084cc6
 
084cc6
 
084cc6
-class RedHatPostgreSQL(PostgreSQL, RedHatPlugin):
084cc6
+class RedHatPostgreSQL(PostgreSQL, SCLPlugin):
084cc6
+
084cc6
+    packages = ('postgresql', 'rh-postgresql95-postgresql-server', )
084cc6
 
084cc6
     def setup(self):
084cc6
         super(RedHatPostgreSQL, self).setup()
084cc6
 
084cc6
+        scl = "rh-postgresql95"
084cc6
+        pghome = self.get_option("pghome")
084cc6
+
084cc6
         # Copy PostgreSQL log files.
084cc6
-        for filename in find("*.log", self.get_option("pghome")):
084cc6
+        for filename in find("*.log", pghome):
084cc6
+            self.add_copy_spec(filename)
084cc6
+        for filename in find("*.log", self.convert_copyspec_scl(scl, pghome)):
084cc6
             self.add_copy_spec(filename)
084cc6
+
084cc6
         # Copy PostgreSQL config files.
084cc6
-        for filename in find("*.conf", self.get_option("pghome")):
084cc6
+        for filename in find("*.conf", pghome):
084cc6
+            self.add_copy_spec(filename)
084cc6
+        for filename in find("*.conf", self.convert_copyspec_scl(scl, pghome)):
084cc6
             self.add_copy_spec(filename)
084cc6
 
084cc6
-        self.add_copy_spec(
084cc6
-            os.path.join(
084cc6
-                self.get_option("pghome"),
084cc6
-                "data",
084cc6
-                "PG_VERSION"
084cc6
-            )
084cc6
-        )
084cc6
-        self.add_copy_spec(
084cc6
-            os.path.join(
084cc6
-                self.get_option("pghome"),
084cc6
+        self.add_copy_spec(os.path.join(pghome, "data", "PG_VERSION"))
084cc6
+        self.add_copy_spec(os.path.join(pghome, "data", "postmaster.opts"))
084cc6
+
084cc6
+        self.add_copy_spec_scl(scl, os.path.join(pghome, "data", "PG_VERSION"))
084cc6
+        self.add_copy_spec_scl(scl, os.path.join(
084cc6
+                pghome,
084cc6
                 "data",
084cc6
                 "postmaster.opts"
084cc6
             )
084cc6
         )
084cc6
 
084cc6
+        if scl in self.scls_matched:
084cc6
+            self.pg_dump(
084cc6
+                pg_dump_command="scl enable rh-postgresql95 -- pg_dump",
084cc6
+                filename="sos_scl_pgdump.tar"
084cc6
+            )
084cc6
+
084cc6
 
084cc6
 class DebianPostgreSQL(PostgreSQL, DebianPlugin, UbuntuPlugin):
084cc6
 
084cc6
From 0b93d1f69ccfcc76e1896ea0e5ff7854be69be13 Mon Sep 17 00:00:00 2001
084cc6
From: Pavel Moravec <pmoravec@redhat.com>
084cc6
Date: Sat, 25 Nov 2017 12:47:35 +0100
084cc6
Subject: [PATCH] [plugins] set proper PATH for SCL commands
084cc6
084cc6
As SCL packages are deployed under /opt/${provider}/${scl}/,
084cc6
calling a SCL command needs that prefix in any path in PATH.
084cc6
084cc6
Consequently, distro-specific SCL default path prefix of the provider must be
084cc6
defined in sos policies.
084cc6
084cc6
Relevant to: #1154
084cc6
084cc6
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
084cc6
---
084cc6
 sos/plugins/__init__.py  | 37 ++++++++++++++++++++++++++++++-------
084cc6
 sos/policies/__init__.py |  4 ++++
084cc6
 sos/policies/redhat.py   |  1 +
084cc6
 3 files changed, 35 insertions(+), 7 deletions(-)
084cc6
084cc6
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
084cc6
index aa69b19d..2a8bc516 100644
084cc6
--- a/sos/plugins/__init__.py
084cc6
+++ b/sos/plugins/__init__.py
084cc6
@@ -1066,25 +1066,48 @@ class SCLPlugin(RedHatPlugin):
084cc6
         output = sos_get_command_output("scl -l")["output"]
084cc6
         return [scl.strip() for scl in output.splitlines()]
084cc6
 
084cc6
+    def convert_cmd_scl(self, scl, cmd):
084cc6
+        """wrapping command in "scl enable" call and adds proper PATH
084cc6
+        """
084cc6
+        # load default SCL prefix to PATH
084cc6
+        prefix = self.policy().get_default_scl_prefix()
084cc6
+        # read prefix from /etc/scl/prefixes/${scl} and strip trailing '\n'
084cc6
+        try:
084cc6
+            prefix = open('/etc/scl/prefixes/%s' % scl, 'r').read()\
084cc6
+                     .rstrip('\n')
084cc6
+        except Exception as e:
084cc6
+            self._log_error("Failed to find prefix for SCL %s, using %s"
084cc6
+                            % (scl, prefix))
084cc6
+
084cc6
+        # expand PATH by equivalent prefixes under the SCL tree
084cc6
+        path = os.environ["PATH"]
084cc6
+        for p in path.split(':'):
084cc6
+            path = '%s/%s%s:%s' % (prefix, scl, p, path)
084cc6
+
084cc6
+        scl_cmd = "scl enable %s \"PATH=%s %s\"" % (scl, path, cmd)
084cc6
+        return scl_cmd
084cc6
+
084cc6
     def add_cmd_output_scl(self, scl, cmds, **kwargs):
084cc6
         """Same as add_cmd_output, except that it wraps command in
084cc6
-        "scl enable" call.
084cc6
+        "scl enable" call and sets proper PATH.
084cc6
         """
084cc6
         if isinstance(cmds, six.string_types):
084cc6
             cmds = [cmds]
084cc6
         scl_cmds = []
084cc6
-        scl_cmd_tpl = "scl enable %s \"%s\""
084cc6
         for cmd in cmds:
084cc6
-            scl_cmds.append(scl_cmd_tpl % (scl, cmd))
084cc6
+            scl_cmds.append(convert_cmd_scl(scl, cmd))
084cc6
         self.add_cmd_output(scl_cmds, **kwargs)
084cc6
 
084cc6
-    # config files for Software Collections are under /etc/opt/rh/${scl} and
084cc6
-    # var files are under /var/opt/rh/${scl}. So we need to insert the paths
084cc6
-    # after the appropriate root dir.
084cc6
+    # config files for Software Collections are under /etc/${prefix}/${scl} and
084cc6
+    # var files are under /var/${prefix}/${scl} where the ${prefix} is distro
084cc6
+    # specific path. So we need to insert the paths after the appropriate root
084cc6
+    # dir.
084cc6
     def convert_copyspec_scl(self, scl, copyspec):
084cc6
+        scl_prefix = self.policy().get_default_scl_prefix()
084cc6
         for rootdir in ['etc', 'var']:
084cc6
             p = re.compile('^/%s/' % rootdir)
084cc6
-            copyspec = p.sub('/%s/opt/rh/%s/' % (rootdir, scl), copyspec)
084cc6
+            copyspec = p.sub('/%s/%s/%s/' % (rootdir, scl_prefix, scl),
084cc6
+                             copyspec)
084cc6
         return copyspec
084cc6
 
084cc6
     def add_copy_spec_scl(self, scl, copyspecs):
084cc6
diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
084cc6
index dffd801c..dc043105 100644
084cc6
--- a/sos/policies/__init__.py
084cc6
+++ b/sos/policies/__init__.py
084cc6
@@ -194,6 +194,7 @@ No changes will be made to system configuration.
084cc6
     vendor_url = "http://www.example.com/"
084cc6
     vendor_text = ""
084cc6
     PATH = ""
084cc6
+    default_scl_prefix = ""
084cc6
 
084cc6
     _in_container = False
084cc6
     _host_sysroot = '/'
084cc6
@@ -271,6 +272,9 @@ No changes will be made to system configuration.
084cc6
             return tempfile.gettempdir()
084cc6
         return opt_tmp_dir
084cc6
 
084cc6
+    def get_default_scl_prefix(self):
084cc6
+        return self.default_scl_prefix
084cc6
+
084cc6
     def match_plugin(self, plugin_classes):
084cc6
         if len(plugin_classes) > 1:
084cc6
             for p in plugin_classes:
084cc6
diff --git a/sos/policies/redhat.py b/sos/policies/redhat.py
084cc6
index c7449439..2dfe0589 100644
084cc6
--- a/sos/policies/redhat.py
084cc6
+++ b/sos/policies/redhat.py
084cc6
@@ -44,6 +44,7 @@ class RedHatPolicy(LinuxPolicy):
084cc6
     _rpmq_cmd = 'rpm -qa --queryformat "%{NAME}|%{VERSION}\\n"'
084cc6
     _in_container = False
084cc6
     _host_sysroot = '/'
084cc6
+    default_scl_prefix = '/opt/rh'
084cc6
 
084cc6
     def __init__(self, sysroot=None):
084cc6
         super(RedHatPolicy, self).__init__(sysroot=sysroot)
084cc6
-- 
084cc6
2.13.6
084cc6
084cc6
From 419ebe48ea408b6596ff4d7d9837079dc3057fcf Mon Sep 17 00:00:00 2001
084cc6
From: Pavel Moravec <pmoravec@redhat.com>
084cc6
Date: Sat, 25 Nov 2017 12:58:16 +0100
084cc6
Subject: [PATCH] [postgresql] Call SCL pg_dump with proper path
084cc6
084cc6
Also stop storing pg_dump in an auxiliary tempdir but under regular
084cc6
sos_commands/postgresql directory.
084cc6
084cc6
Resolves: #1154
084cc6
084cc6
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
084cc6
---
084cc6
 sos/plugins/postgresql.py | 43 ++++++++-----------------------------------
084cc6
 1 file changed, 8 insertions(+), 35 deletions(-)
084cc6
084cc6
diff --git a/sos/plugins/postgresql.py b/sos/plugins/postgresql.py
084cc6
index 45c87e89..9ba696be 100644
084cc6
--- a/sos/plugins/postgresql.py
084cc6
+++ b/sos/plugins/postgresql.py
084cc6
@@ -34,8 +34,6 @@ class PostgreSQL(Plugin):
084cc6
 
084cc6
     packages = ('postgresql',)
084cc6
 
084cc6
-    tmp_dir = None
084cc6
-
084cc6
     password_warn_text = " (password visible in process listings)"
084cc6
 
084cc6
     option_list = [
084cc6
@@ -47,11 +45,9 @@ class PostgreSQL(Plugin):
084cc6
         ('dbport', 'database server port number', '', '5432')
084cc6
     ]
084cc6
 
084cc6
-    def pg_dump(self, pg_dump_command="pg_dump", filename="sos_pgdump.tar"):
084cc6
+    def do_pg_dump(self, scl=None, filename="pgdump.tar"):
084cc6
         if self.get_option("dbname"):
084cc6
             if self.get_option("password") or "PGPASSWORD" in os.environ:
084cc6
-                self.tmp_dir = tempfile.mkdtemp()
084cc6
-                dest_file = os.path.join(self.tmp_dir, filename)
084cc6
                 # We're only modifying this for ourself and our children so
084cc6
                 # there is no need to save and restore environment variables if
084cc6
                 # the user decided to pass the password on the command line.
084cc6
@@ -59,30 +55,21 @@ class PostgreSQL(Plugin):
084cc6
                     os.environ["PGPASSWORD"] = str(self.get_option("password"))
084cc6
 
084cc6
                 if self.get_option("dbhost"):
084cc6
-                    cmd = "%s -U %s -h %s -p %s -w -f %s -F t %s" % (
084cc6
-                        pg_dump_command,
084cc6
+                    cmd = "pg_dump -U %s -h %s -p %s -w -F t %s" % (
084cc6
                         self.get_option("username"),
084cc6
                         self.get_option("dbhost"),
084cc6
                         self.get_option("dbport"),
084cc6
-                        dest_file,
084cc6
                         self.get_option("dbname")
084cc6
                     )
084cc6
                 else:
084cc6
-                    cmd = "%s -C -U %s -w -f %s -F t %s " % (
084cc6
-                        pg_dump_command,
084cc6
+                    cmd = "pg_dump -C -U %s -w -F t %s " % (
084cc6
                         self.get_option("username"),
084cc6
-                        dest_file,
084cc6
                         self.get_option("dbname")
084cc6
                     )
084cc6
 
084cc6
-                result = self.call_ext_prog(cmd)
084cc6
-                if (result['status'] == 0):
084cc6
-                    self.add_copy_spec(dest_file)
084cc6
-                else:
084cc6
-                    self._log_info(
084cc6
-                        "Unable to execute pg_dump. Error(%s)" %
084cc6
-                        (result['output'])
084cc6
-                    )
084cc6
+                if scl is not None:
084cc6
+                    cmd = self.convert_cmd_scl(scl, cmd)
084cc6
+                self.add_cmd_output(cmd, suggest_filename=filename)
084cc6
             else:  # no password in env or options
084cc6
                 self.soslog.warning(
084cc6
                     "password must be supplied to dump a database."
084cc6
@@ -92,18 +79,7 @@ class PostgreSQL(Plugin):
084cc6
                 )
084cc6
 
084cc6
     def setup(self):
084cc6
-        self.pg_dump()
084cc6
-
084cc6
-    def postproc(self):
084cc6
-        import shutil
084cc6
-        if self.tmp_dir:
084cc6
-            try:
084cc6
-                shutil.rmtree(self.tmp_dir)
084cc6
-            except shutil.Error:
084cc6
-                self.soslog.exception(
084cc6
-                    "Unable to remove %s." % (self.tmp_dir)
084cc6
-                )
084cc6
-                self.add_alert("ERROR: Unable to remove %s." % (self.tmp_dir))
084cc6
+        self.do_pg_dump()
084cc6
 
084cc6
 
084cc6
 class RedHatPostgreSQL(PostgreSQL, SCLPlugin):
084cc6
@@ -140,10 +116,7 @@ class RedHatPostgreSQL(PostgreSQL, SCLPlugin):
084cc6
         )
084cc6
 
084cc6
         if scl in self.scls_matched:
084cc6
-            self.pg_dump(
084cc6
-                pg_dump_command="scl enable rh-postgresql95 -- pg_dump",
084cc6
-                filename="sos_scl_pgdump.tar"
084cc6
-            )
084cc6
+            self.do_pg_dump(scl=scl, filename="pgdump-scl-%s.tar" % scl)
084cc6
 
084cc6
 
084cc6
 class DebianPostgreSQL(PostgreSQL, DebianPlugin, UbuntuPlugin):
084cc6
-- 
084cc6
2.13.6
084cc6
084cc6
From ede50e9cb4a5f2755eaeaf608fb2b3708f911422 Mon Sep 17 00:00:00 2001
084cc6
From: Pavel Moravec <pmoravec@redhat.com>
084cc6
Date: Wed, 20 Dec 2017 11:47:33 +0100
084cc6
Subject: [PATCH] [plugins] allow add_cmd_output to collect binary output
084cc6
084cc6
If a command output is a true binary data, allow add_cmd_output to
084cc6
collect the raw content and dont try to decode it as UTF-8.
084cc6
084cc6
Resolves: #1169
084cc6
084cc6
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
084cc6
---
084cc6
 sos/archive.py            | 16 ++++++++++------
084cc6
 sos/plugins/__init__.py   | 33 ++++++++++++++++++---------------
084cc6
 sos/plugins/postgresql.py |  3 ++-
084cc6
 sos/utilities.py          |  5 +++--
084cc6
 4 files changed, 33 insertions(+), 24 deletions(-)
084cc6
084cc6
diff --git a/sos/archive.py b/sos/archive.py
084cc6
index 607312a71..4bc2bedea 100644
084cc6
--- a/sos/archive.py
084cc6
+++ b/sos/archive.py
084cc6
@@ -82,7 +82,7 @@ def log_debug(self, msg):
084cc6
     def add_file(self, src, dest=None):
084cc6
         raise NotImplementedError
084cc6
 
084cc6
-    def add_string(self, content, dest):
084cc6
+    def add_string(self, content, dest, treat_binary):
084cc6
         raise NotImplementedError
084cc6
 
084cc6
     def add_link(self, source, link_name):
084cc6
@@ -198,12 +198,14 @@ def add_file(self, src, dest=None):
084cc6
         self.log_debug("added %s to FileCacheArchive '%s'" %
084cc6
                        (file_name, self._archive_root))
084cc6
 
084cc6
-    def add_string(self, content, dest):
084cc6
+    def add_string(self, content, dest, treat_binary=False):
084cc6
         src = dest
084cc6
         dest = self.dest_path(dest)
084cc6
         self._check_path(dest)
084cc6
-        f = codecs.open(dest, 'w', encoding='utf-8')
084cc6
-        if isinstance(content, bytes):
084cc6
+        f = codecs.open(dest,
084cc6
+                        'wb' if treat_binary else 'w',
084cc6
+                        encoding=None if treat_binary else 'utf-8')
084cc6
+        if isinstance(content, bytes) and not treat_binary:
084cc6
             content = content.decode('utf8', 'ignore')
084cc6
         f.write(content)
084cc6
         if os.path.exists(src):
084cc6
@@ -212,8 +214,10 @@ def add_string(self, content, dest):
084cc6
             except OSError as e:
084cc6
                 self.log_error(
084cc6
                     "Unable to add '%s' to FileCacheArchive: %s" % (dest, e))
084cc6
-        self.log_debug("added string at '%s' to FileCacheArchive '%s'"
084cc6
-                       % (src, self._archive_root))
084cc6
+        self.log_debug("added %sstring at '%s' to FileCacheArchive '%s'"
084cc6
+                       % ('binary ' if treat_binary else '',
084cc6
+                           src,
084cc6
+                           self._archive_root))
084cc6
 
084cc6
     def add_link(self, source, link_name):
084cc6
         dest = self.dest_path(link_name)
084cc6
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
084cc6
index 2a8bc516e..156f5ed36 100644
084cc6
--- a/sos/plugins/__init__.py
084cc6
+++ b/sos/plugins/__init__.py
084cc6
@@ -587,7 +587,8 @@ def getmtime(path):
084cc6
                 self.archive.add_link(link_path, _file)
084cc6
 
084cc6
     def get_command_output(self, prog, timeout=300, stderr=True,
084cc6
-                           chroot=True, runat=None, env=None):
084cc6
+                           chroot=True, runat=None, env=None,
084cc6
+                           treat_binary=False):
084cc6
         if chroot or self.commons['cmdlineopts'].chroot == 'always':
084cc6
             root = self.sysroot
084cc6
         else:
084cc6
@@ -595,7 +596,7 @@ def get_command_output(self, prog, timeout=300, stderr=True,
084cc6
 
084cc6
         result = sos_get_command_output(prog, timeout=timeout, stderr=stderr,
084cc6
                                         chroot=root, chdir=runat,
084cc6
-                                        env=env)
084cc6
+                                        env=env, treat_binary=treat_binary)
084cc6
 
084cc6
         if result['status'] == 124:
084cc6
             self._log_warn("command '%s' timed out after %ds"
084cc6
@@ -586,7 +586,8 @@ class Plugin(object):
084cc6
                                % (prog.split()[0], root))
084cc6
                 return self.get_command_output(prog, timeout=timeout,
084cc6
                                                chroot=False, runat=runat,
084cc6
-                                               env=env)
084cc6
+                                               env=env,
084cc6
+                                               treat_binary=treat_binary)
084cc6
             self._log_debug("could not run '%s': command not found" % prog)
084cc6
         return result
084cc6
 
084cc6
@@ -632,14 +634,14 @@ def check_ext_prog(self, prog):
084cc6
 
084cc6
     def _add_cmd_output(self, cmd, suggest_filename=None,
084cc6
                         root_symlink=None, timeout=300, stderr=True,
084cc6
-                        chroot=True, runat=None, env=None):
084cc6
+                        chroot=True, runat=None, env=None, treat_binary=False):
084cc6
         """Internal helper to add a single command to the collection list."""
084cc6
         cmdt = (
084cc6
             cmd, suggest_filename,
084cc6
             root_symlink, timeout, stderr,
084cc6
-            chroot, runat, env
084cc6
+            chroot, runat, env, treat_binary
084cc6
         )
084cc6
-        _tuplefmt = "('%s', '%s', '%s', %s, '%s', '%s', '%s', '%s')"
084cc6
+        _tuplefmt = "('%s', '%s', '%s', %s, '%s', '%s', '%s', '%s', '%s')"
084cc6
         _logstr = "packed command tuple: " + _tuplefmt
084cc6
         self._log_debug(_logstr % cmdt)
084cc6
         self.collect_cmds.append(cmdt)
084cc6
@@ -647,7 +649,7 @@ def _add_cmd_output(self, cmd, suggest_filename=None,
084cc6
 
084cc6
     def add_cmd_output(self, cmds, suggest_filename=None,
084cc6
                        root_symlink=None, timeout=300, stderr=True,
084cc6
-                       chroot=True, runat=None, env=None):
084cc6
+                       chroot=True, runat=None, env=None, treat_binary=False):
084cc6
         """Run a program or a list of programs and collect the output"""
084cc6
         if isinstance(cmds, six.string_types):
084cc6
             cmds = [cmds]
084cc6
@@ -656,7 +658,7 @@ def add_cmd_output(self, cmds, suggest_filename=None,
084cc6
         for cmd in cmds:
084cc6
             self._add_cmd_output(cmd, suggest_filename,
084cc6
                                  root_symlink, timeout, stderr,
084cc6
-                                 chroot, runat, env)
084cc6
+                                 chroot, runat, env, treat_binary)
084cc6
 
084cc6
     def get_cmd_output_path(self, name=None, make=True):
084cc6
         """Return a path into which this module should store collected
084cc6
@@ -683,14 +684,15 @@ class Plugin(object):
084cc6
 
084cc6
     def get_cmd_output_now(self, exe, suggest_filename=None,
084cc6
                            root_symlink=False, timeout=300, stderr=True,
084cc6
-                           chroot=True, runat=None, env=None):
084cc6
+                           chroot=True, runat=None, env=None,
084cc6
+                           treat_binary=False):
084cc6
         """Execute a command and save the output to a file for inclusion in the
084cc6
         report.
084cc6
         """
084cc6
         start = time()
084cc6
         result = self.get_command_output(exe, timeout=timeout, stderr=stderr,
084cc6
                                          chroot=chroot, runat=runat,
084cc6
-                                         env=env)
084cc6
+                                         env=env, treat_binary=treat_binary)
084cc6
         # 126 means 'found but not executable'
084cc6
         if result['status'] == 126 or result['status'] == 127:
084cc6
             return None
084cc6
@@ -729,7 +732,7 @@ def get_cmd_output_now(self, exe, suggest_filename=None,
084cc6
             outfn = self._make_command_filename(exe)
084cc6
 
084cc6
         outfn_strip = outfn[len(self.commons['cmddir'])+1:]
084cc6
-        self.archive.add_string(result['output'], outfn)
084cc6
+        self.archive.add_string(result['output'], outfn, treat_binary)
084cc6
         if root_symlink:
084cc6
             self.archive.add_link(outfn, root_symlink)
084cc6
 
084cc6
@@ -839,16 +842,16 @@ def _collect_cmd_output(self):
084cc6
                 timeout,
084cc6
                 stderr,
084cc6
                 chroot, runat,
084cc6
-                env
084cc6
+                env, treat_binary
084cc6
             ) = progs[0]
084cc6
-            self._log_debug("unpacked command tuple: " +
084cc6
-                            "('%s', '%s', '%s', %s, '%s', '%s', '%s', '%s')" %
084cc6
-                            progs[0])
084cc6
+            self._log_debug(("unpacked command tuple: " +
084cc6
+                            "('%s', '%s', '%s', %s, '%s', '%s', '%s', '%s'," +
084cc6
+                            "'%s')") % progs[0])
084cc6
             self._log_info("collecting output of '%s'" % prog)
084cc6
             self.get_cmd_output_now(prog, suggest_filename=suggest_filename,
084cc6
                                     root_symlink=root_symlink, timeout=timeout,
084cc6
                                     stderr=stderr, chroot=chroot, runat=runat,
084cc6
-                                    env=env)
084cc6
+                                    env=env, treat_binary=treat_binary)
084cc6
 
084cc6
     def _collect_strings(self):
084cc6
         for string, file_name in self.copy_strings:
084cc6
diff --git a/sos/plugins/postgresql.py b/sos/plugins/postgresql.py
084cc6
index 9ba696be2..07db22fdd 100644
084cc6
--- a/sos/plugins/postgresql.py
084cc6
+++ b/sos/plugins/postgresql.py
084cc6
@@ -69,7 +69,8 @@ def do_pg_dump(self, scl=None, filename="pgdump.tar"):
084cc6
 
084cc6
                 if scl is not None:
084cc6
                     cmd = self.convert_cmd_scl(scl, cmd)
084cc6
-                self.add_cmd_output(cmd, suggest_filename=filename)
084cc6
+                self.add_cmd_output(cmd, suggest_filename=filename,
084cc6
+                                    treat_binary=True)
084cc6
             else:  # no password in env or options
084cc6
                 self.soslog.warning(
084cc6
                     "password must be supplied to dump a database."
084cc6
diff --git a/sos/utilities.py b/sos/utilities.py
084cc6
index 55bb1dc96..a9040ba28 100644
084cc6
--- a/sos/utilities.py
084cc6
+++ b/sos/utilities.py
084cc6
@@ -110,7 +110,8 @@ def is_executable(command):
084cc6
 
084cc6
 
084cc6
 def sos_get_command_output(command, timeout=300, stderr=False,
084cc6
-                           chroot=None, chdir=None, env=None):
084cc6
+                           chroot=None, chdir=None, env=None,
084cc6
+                           treat_binary=False):
084cc6
     """Execute a command and return a dictionary of status and output,
084cc6
     optionally changing root or current working directory before
084cc6
     executing command.
084cc6
@@ -164,7 +165,7 @@ def _child_prep_fn():
084cc6
 
084cc6
     return {
084cc6
         'status': p.returncode,
084cc6
-        'output': stdout.decode('utf-8', 'ignore')
084cc6
+        'output': stdout if treat_binary else stdout.decode('utf-8', 'ignore')
084cc6
     }
084cc6
 
084cc6