Blame SOURCES/sos-bz1956672-add-plugin-for-pulp-3.patch

5866eb
From 36c678fe3dd8f94e116600ece082bcbdf0404e55 Mon Sep 17 00:00:00 2001
5866eb
From: Pavel Moravec <pmoravec@redhat.com>
5866eb
Date: Tue, 4 May 2021 10:43:07 +0200
5866eb
Subject: [PATCH] [pulpcore] add plugin for pulp-3
5866eb
5866eb
Backport #2512 to add pulpcore plugin also to legacy-3.9 branch.
5866eb
5866eb
Related: #2512
5866eb
Resolves: #2520
5866eb
5866eb
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
5866eb
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
5866eb
---
5866eb
 sos/plugins/pulpcore.py | 120 ++++++++++++++++++++++++++++++++++++++++++++++++
5866eb
 1 file changed, 120 insertions(+)
5866eb
 create mode 100644 sos/plugins/pulpcore.py
5866eb
5866eb
diff --git a/sos/plugins/pulpcore.py b/sos/plugins/pulpcore.py
5866eb
new file mode 100644
5866eb
index 0000000..46fa0d5
5866eb
--- /dev/null
5866eb
+++ b/sos/plugins/pulpcore.py
5866eb
@@ -0,0 +1,120 @@
5866eb
+# Copyright (C) 2021 Red Hat, Inc., Pavel Moravec <pmoravec@redhat.com>
5866eb
+
5866eb
+# This file is part of the sos project: https://github.com/sosreport/sos
5866eb
+#
5866eb
+# This copyrighted material is made available to anyone wishing to use,
5866eb
+# modify, copy, or redistribute it subject to the terms and conditions of
5866eb
+# version 2 of the GNU General Public License.
5866eb
+#
5866eb
+# See the LICENSE file in the source distribution for further information.
5866eb
+
5866eb
+from sos.plugins import Plugin, IndependentPlugin
5866eb
+from pipes import quote
5866eb
+from re import match
5866eb
+
5866eb
+
5866eb
+class PulpCore(Plugin, IndependentPlugin):
5866eb
+
5866eb
+    short_desc = 'Pulp-3 aka pulpcore'
5866eb
+
5866eb
+    plugin_name = "pulpcore"
5866eb
+    commands = ("pulpcore-manager",)
5866eb
+    files = ("/etc/pulp/settings.py",)
5866eb
+    option_list = [
5866eb
+        ('task-days', 'days of tasks history', 'fast', 7)
5866eb
+    ]
5866eb
+
5866eb
+    def parse_settings_config(self):
5866eb
+        databases_scope = False
5866eb
+        self.dbhost = "localhost"
5866eb
+        self.dbport = 5432
5866eb
+        self.dbpasswd = ""
5866eb
+        # TODO: read also redis config (we dont expect much customisations)
5866eb
+        # TODO: read also db user (pulp) and database name (pulpcore)
5866eb
+        self.staticroot = "/var/lib/pulp/assets"
5866eb
+        self.uploaddir = "/var/lib/pulp/media/upload"
5866eb
+
5866eb
+        def separate_value(line, sep=':'):
5866eb
+            # an auxiliary method to parse values from lines like:
5866eb
+            #       'HOST': 'localhost',
5866eb
+            val = line.split(sep)[1].lstrip().rstrip(',')
5866eb
+            if (val.startswith('"') and val.endswith('"')) or \
5866eb
+               (val.startswith('\'') and val.endswith('\'')):
5866eb
+                val = val[1:-1]
5866eb
+            return val
5866eb
+
5866eb
+        try:
5866eb
+            for line in open("/etc/pulp/settings.py").read().splitlines():
5866eb
+                # skip empty lines and lines with comments
5866eb
+                if not line or line[0] == '#':
5866eb
+                    continue
5866eb
+                if line.startswith("DATABASES"):
5866eb
+                    databases_scope = True
5866eb
+                    continue
5866eb
+                # example HOST line to parse:
5866eb
+                #         'HOST': 'localhost',
5866eb
+                if databases_scope and match(r"\s+'HOST'\s*:\s+\S+", line):
5866eb
+                    self.dbhost = separate_value(line)
5866eb
+                if databases_scope and match(r"\s+'PORT'\s*:\s+\S+", line):
5866eb
+                    self.dbport = separate_value(line)
5866eb
+                if databases_scope and match(r"\s+'PASSWORD'\s*:\s+\S+", line):
5866eb
+                    self.dbpasswd = separate_value(line)
5866eb
+                # if line contains closing '}' database_scope end
5866eb
+                if databases_scope and '}' in line:
5866eb
+                    databases_scope = False
5866eb
+                if line.startswith("STATIC_ROOT = "):
5866eb
+                    self.staticroot = separate_value(line, sep='=')
5866eb
+                if line.startswith("CHUNKED_UPLOAD_DIR = "):
5866eb
+                    self.uploaddir = separate_value(line, sep='=')
5866eb
+        except IOError:
5866eb
+            # fallback when the cfg file is not accessible
5866eb
+            pass
5866eb
+        # set the password to os.environ when calling psql commands to prevent
5866eb
+        # printing it in sos logs
5866eb
+        # we can't set os.environ directly now: other plugins can overwrite it
5866eb
+        self.env = {"PGPASSWORD": self.dbpasswd}
5866eb
+
5866eb
+    def setup(self):
5866eb
+        self.parse_settings_config()
5866eb
+
5866eb
+        self.add_copy_spec("/etc/pulp/settings.py")
5866eb
+
5866eb
+        self.add_cmd_output("rq info -u redis://localhost:6379/8",
5866eb
+                            env={"LC_ALL": "en_US.UTF-8"},
5866eb
+                            suggest_filename="rq_info")
5866eb
+        self.add_cmd_output("curl -ks https://localhost/pulp/api/v3/status/",
5866eb
+                            suggest_filename="pulp_status")
5866eb
+        dynaconf_env = {"LC_ALL": "en_US.UTF-8",
5866eb
+                        "PULP_SETTINGS": "/etc/pulp/settings.py",
5866eb
+                        "DJANGO_SETTINGS_MODULE": "pulpcore.app.settings"}
5866eb
+        self.add_cmd_output("dynaconf list", env=dynaconf_env)
5866eb
+        for _dir in [self.staticroot, self.uploaddir]:
5866eb
+            self.add_cmd_output("ls -l %s" % _dir)
5866eb
+
5866eb
+        task_days = self.get_option('task-days')
5866eb
+        for table in ['core_task', 'core_taskgroup',
5866eb
+                      'core_reservedresourcerecord',
5866eb
+                      'core_taskreservedresourcerecord',
5866eb
+                      'core_groupprogressreport', 'core_progressreport']:
5866eb
+            _query = "select * from %s where pulp_last_updated > NOW() - " \
5866eb
+                     "interval '%s days' order by pulp_last_updated" % \
5866eb
+                     (table, task_days)
5866eb
+            _cmd = "psql -h %s -p %s -U pulp -d pulpcore -c %s" % \
5866eb
+                   (self.dbhost, self.dbport, quote(_query))
5866eb
+            self.add_cmd_output(_cmd, env=self.env, suggest_filename=table)
5866eb
+
5866eb
+    def postproc(self):
5866eb
+        # TODO obfuscate from /etc/pulp/settings.py :
5866eb
+        # SECRET_KEY = "eKfeDkTnvss7p5WFqYdGPWxXfHnsbDBx"
5866eb
+        # 'PASSWORD': 'tGrag2DmtLqKLTWTQ6U68f6MAhbqZVQj',
5866eb
+        self.do_path_regex_sub(
5866eb
+            "/etc/pulp/settings.py",
5866eb
+            r"(SECRET_KEY\s*=\s*)(.*)",
5866eb
+            r"\1********")
5866eb
+        self.do_path_regex_sub(
5866eb
+            "/etc/pulp/settings.py",
5866eb
+            r"(PASSWORD\S*\s*:\s*)(.*)",
5866eb
+            r"\1********")
5866eb
+
5866eb
+
5866eb
+# vim: set et ts=4 sw=4 :
5866eb
-- 
5866eb
1.8.3.1
5866eb