Blob Blame History Raw
From 5f95c24ccd3224d441eb1dcab3e63c33847b18bb Mon Sep 17 00:00:00 2001
From: Barbora Vassova <bvassova@redhat.com>
Date: Tue, 5 Apr 2022 13:01:33 +0200
Subject: [PATCH] In dynflow >=1.6.3, dynflow* tables in postgres are encoded
 by msgpack which makes plain CSV dumps unreadable. In such a case,
 psql-msgpack-decode wrapper tool from dynflow-utils (of any version) must be
 used instead of the plain psql command.

Resolves: #2900

Signed-off-by: Barbora Vassova <bvassova@redhat.com>
---
 sos/plugins/foreman.py | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/sos/plugins/foreman.py b/sos/plugins/foreman.py
index c742ebcab..e0b6eeb15 100644
--- a/sos/plugins/foreman.py
+++ b/sos/plugins/foreman.py
@@ -244,15 +244,23 @@ def setup(self):
             self.add_cmd_output(_cmd, suggest_filename=table, timeout=600,
                                 sizelimit=100, env=self.env)
 
+        # dynflow* tables on dynflow >=1.6.3 are encoded and hence in that
+        # case, psql-msgpack-decode wrapper tool from dynflow-utils (any
+        # version) must be used instead of plain psql command
+        dynutils = self.is_installed('dynflow-utils')
         for dyn in foremancsv:
-            _cmd = self.build_query_cmd(foremancsv[dyn], csv=True)
+            binary = "psql"
+            if dyn != 'foreman_tasks_tasks' and dynutils:
+                binary = "/usr/libexec/psql-msgpack-decode"
+            _cmd = self.build_query_cmd(foremancsv[dyn], csv=True,
+                                        binary=binary)
             self.add_cmd_output(_cmd, suggest_filename=dyn, timeout=600,
                                 sizelimit=100, env=self.env)
 
         # collect http[|s]_proxy env.variables
         self.add_env_var(["http_proxy", "https_proxy"])
 
-    def build_query_cmd(self, query, csv=False):
+    def build_query_cmd(self, query, csv=False, binary="psql"):
         """
         Builds the command needed to invoke the pgsql query as the postgres
         user.
@@ -260,10 +268,11 @@ def build_query_cmd(self, query, csv=False):
         shell and postgres parsing requirements. Note that this will generate
         a large amount of quoting in sos logs referencing the command being run
         """
-        csvformat = "-A -F , -X" if csv else ""
-        _dbcmd = "psql --no-password -h %s -p 5432 -U foreman -d foreman %s \
-                  -c %s"
-        return _dbcmd % (self.dbhost, csvformat, quote(query))
+        if csv:
+            query = "COPY (%s) TO STDOUT " \
+                    "WITH (FORMAT 'csv', DELIMITER ',', HEADER)" % query
+        _dbcmd = "%s --no-password -h %s -p 5432 -U foreman -d foreman -c %s"
+        return _dbcmd % (binary, self.dbhost, quote(query))
 
     def postproc(self):
         satreg = r"((foreman.*)?(\"::(foreman(.*?)|katello).*)?((::(.*)::.*" \