Blame SOURCES/sos-bz1956673-pulpcore-plugin.patch

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