aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPavel Moravec <pmoravec@redhat.com>2019-04-15 22:32:52 +0200
committerBryn M. Reeves <bmr@redhat.com>2019-05-14 11:27:12 +0100
commit5847189f85d1b63afa8c1d8803779348eaa7dc68 (patch)
treeda10837140466b26e8d87efb07388c216e65493b
parentea334bd14055f48ebd152833c748f58f5ab22fc4 (diff)
downloadsos-5847189f85d1b63afa8c1d8803779348eaa7dc68.tar.gz
[foreman] support external postgres DB
When external postgres DB is configured, we should query mongo commands against that DB instead of localhost. Resolves: #1649 Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
-rw-r--r--sos/plugins/foreman.py44
1 files changed, 37 insertions, 7 deletions
diff --git a/sos/plugins/foreman.py b/sos/plugins/foreman.py
index 8bcd26bd..ceee9619 100644
--- a/sos/plugins/foreman.py
+++ b/sos/plugins/foreman.py
@@ -12,6 +12,8 @@
from sos.plugins import Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin,\
SCLPlugin
from pipes import quote
+from re import match
+import os
class Foreman(Plugin):
@@ -27,6 +29,38 @@ class Foreman(Plugin):
]
def setup(self):
+ # for external DB, search in /etc/foreman/database.yml for:
+ # production:
+ # ..
+ # host: some.hostname
+ production_scope = False
+ self.dbhost = "localhost"
+ self.dbpasswd = ""
+ try:
+ for line in open("/etc/foreman/database.yml").read().splitlines():
+ # skip empty lines and lines with comments
+ if not line or line[0] == '#':
+ continue
+ if line.startswith("production:"):
+ production_scope = True
+ continue
+ if production_scope and match(r"\s+host:\s+\S+", line):
+ self.dbhost = line.split()[1]
+ if production_scope and match(r"\s+password:\s+\S+", line):
+ self.dbpasswd = line.split()[1]
+ # if line starts with a text, it is a different scope
+ if not line.startswith(" "):
+ production_scope = False
+ except IOError:
+ # fallback when the cfg file is not accessible
+ pass
+ # strip wrapping ".." or '..' around password
+ if (self.dbpasswd.startswith('"') and self.dbpasswd.endswith('"')) or \
+ (self.dbpasswd.startswith('\'') and self.dbpasswd.endswith('\'')):
+ self.dbpasswd = self.dbpasswd[1:-1]
+ # set the password to os.environ to prevent printing it in sos logs
+ os.environ["PGPASSWORD"] = self.dbpasswd
+
self.add_forbidden_path([
"/etc/foreman*/*key.pem",
"/etc/foreman*/encryption_key.rb"
@@ -169,13 +203,9 @@ class Foreman(Plugin):
shell and postgres parsing requirements. Note that this will generate
a large amount of quoting in sos logs referencing the command being run
"""
- _cmd = "su postgres -c %s"
- if not csv:
- _dbcmd = "psql foreman -c %s"
- else:
- _dbcmd = "psql foreman -A -F , -X -c %s"
- dbq = _dbcmd % quote(query)
- return _cmd % quote(dbq)
+ csvformat = "-A -F , -X" if csv else ""
+ _dbcmd = "psql -h %s -p 5432 -U foreman -d foreman %s -c %s"
+ return _dbcmd % (self.dbhost, csvformat, quote(query))
def postproc(self):
satreg = r"((foreman.*)?(\"::(foreman(.*?)|katello).*)?(::(.*)::.*" \