Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[postgresql] Collect data for postgreSQL from RHSCL #1118

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 106 additions & 3 deletions sos/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,10 @@ def check_enabled(self):
corresponding paths, packages or commands and return True if any
are present.

For SCLPlugin subclasses, it will check whether the plugin can be run
for any of installed SCLs. If so, it will store names of these SCLs
on the plugin class in addition to returning True.

For plugins with more complex enablement checks this method may be
overridden.
"""
Expand All @@ -889,11 +893,34 @@ def check_enabled(self):
if isinstance(self.commands, six.string_types):
self.commands = [self.commands]

return (any(os.path.exists(fname) for fname in self.files) or
any(self.is_installed(pkg) for pkg in self.packages) or
any(is_executable(cmd) for cmd in self.commands))
if isinstance(self, SCLPlugin):
# save SCLs that match files or packages
type(self)._scls_matched = []
for scl in self._get_scls():
files = [f % {"scl_name": scl} for f in self.files]
packages = [p % {"scl_name": scl} for p in self.packages]
commands = [c % {"scl_name": scl} for c in self.commands]
if self._files_pkgs_or_cmds_present(files,
packages,
commands):
type(self)._scls_matched.append(scl)
return len(type(self)._scls_matched) > 0

return self._files_pkgs_or_cmds_present(self.files,
self.packages,
self.commands)

if isinstance(self, SCLPlugin):
# if files and packages weren't specified, we take all SCLs
type(self)._scls_matched = self._get_scls()

return True

def _files_pkgs_or_cmds_present(self, files, packages, commands):
return (any(os.path.exists(fname) for fname in files) or
any(self.is_installed(pkg) for pkg in packages) or
any(is_executable(cmd) for cmd in commands))

def default_enabled(self):
"""This decides whether a plugin should be automatically loaded or
only if manually specified in the command line."""
Expand Down Expand Up @@ -970,6 +997,82 @@ class RedHatPlugin(object):
pass


class SCLPlugin(RedHatPlugin):
"""Superclass for plugins operating on Software Collections (SCLs).

Subclasses of this plugin class can specify class.files and class.packages
using "%(scl_name)s" interpolation. The plugin invoking mechanism will try
to match these against all found SCLs on the system. SCLs that do match
class.files or class.packages are then accessible via self.scls_matched
when the plugin is invoked.

Additionally, this plugin class provides "add_cmd_output_scl" (run
a command in context of given SCL), and "add_copy_spec_scl" and
"add_copy_spec_limit_scl" (copy package from file system of given SCL).

For example, you can implement a plugin that will list all global npm
packages in every SCL that contains "npm" package:

class SCLNpmPlugin(Plugin, SCLPlugin):
packages = ("%(scl_name)s-npm",)

def setup(self):
for scl in self.scls_matched:
self.add_cmd_output_scl(scl, "npm ls -g --json")
"""

@property
def scls_matched(self):
if not hasattr(type(self), '_scls_matched'):
type(self)._scls_matched = []
return type(self)._scls_matched

def _get_scls(self):
output = sos_get_command_output("scl -l")["output"]
return [scl.strip() for scl in output.splitlines()]

def add_cmd_output_scl(self, scl, cmds, **kwargs):
"""Same as add_cmd_output, except that it wraps command in
"scl enable" call.
"""
if isinstance(cmds, six.string_types):
cmds = [cmds]
scl_cmds = []
scl_cmd_tpl = "scl enable %s \"%s\""
for cmd in cmds:
scl_cmds.append(scl_cmd_tpl % (scl, cmd))
self.add_cmd_output(scl_cmds, **kwargs)

# config files for Software Collections are under /etc/opt/rh/${scl} and
# var files are under /var/opt/rh/${scl}. So we need to insert the paths
# after the appropriate root dir.
def convert_copyspec_scl(self, scl, copyspec):
for rootdir in ['etc', 'var']:
p = re.compile('^/%s/' % rootdir)
copyspec = p.sub('/%s/opt/rh/%s/' % (rootdir, scl), copyspec)
return copyspec

def add_copy_spec_scl(self, scl, copyspecs):
"""Same as add_copy_spec, except that it prepends path to SCL root
to "copyspecs".
"""
if isinstance(copyspecs, six.string_types):
copyspecs = [copyspecs]
scl_copyspecs = []
for copyspec in copyspecs:
scl_copyspecs.append(self.convert_copyspec_scl(scl, copyspec))
self.add_copy_spec(scl_copyspecs)

def add_copy_spec_limit_scl(self, scl, copyspec, **kwargs):
"""Same as add_copy_spec_limit, except that it prepends path to SCL
root to "copyspec".
"""
self.add_copy_spec_limit(
self.convert_copyspec_scl(scl, copyspec),
**kwargs
)


class PowerKVMPlugin(RedHatPlugin):
"""Tagging class for IBM PowerKVM Linux"""
pass
Expand Down
117 changes: 65 additions & 52 deletions sos/plugins/postgresql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (C) 2017 Red Hat, Inc., Pavel Moravec <pmoravec@redhat.com>
# Copyright (C) 2014 Red Hat, Inc., Sandro Bonazzola <sbonazzo@redhat.com>
# Copyright (C) 2013 Chris J Arges <chris.j.arges@canonical.com>
# Copyright (C) 2012-2013 Red Hat, Inc., Bryn M. Reeves <bmr@redhat.com>
Expand All @@ -20,7 +21,8 @@
import os
import tempfile

from sos.plugins import Plugin, RedHatPlugin, UbuntuPlugin, DebianPlugin
from sos.plugins import (Plugin, RedHatPlugin, UbuntuPlugin, DebianPlugin,
SCLPlugin)
from sos.utilities import find


Expand All @@ -45,54 +47,53 @@ class PostgreSQL(Plugin):
('dbport', 'database server port number', '', '5432')
]

def pg_dump(self):
dest_file = os.path.join(self.tmp_dir, "sos_pgdump.tar")
# We're only modifying this for ourself and our children so there
# is no need to save and restore environment variables if the user
# decided to pass the password on the command line.
if self.get_option("password") is not False:
os.environ["PGPASSWORD"] = str(self.get_option("password"))

if self.get_option("dbhost"):
cmd = "pg_dump -U %s -h %s -p %s -w -f %s -F t %s" % (
self.get_option("username"),
self.get_option("dbhost"),
self.get_option("dbport"),
dest_file,
self.get_option("dbname")
)
else:
cmd = "pg_dump -C -U %s -w -f %s -F t %s " % (
self.get_option("username"),
dest_file,
self.get_option("dbname")
)

result = self.call_ext_prog(cmd)
if (result['status'] == 0):
self.add_copy_spec(dest_file)
else:
self._log_error(
"Unable to execute pg_dump. Error(%s)" % (result['output'])
)
self.add_alert(
"ERROR: Unable to execute pg_dump. Error(%s)" %
(result['output'])
)

def setup(self):
def pg_dump(self, pg_dump_command="pg_dump", filename="sos_pgdump.tar"):
if self.get_option("dbname"):
if self.get_option("password") or "PGPASSWORD" in os.environ:
self.tmp_dir = tempfile.mkdtemp()
self.pg_dump()
else:
dest_file = os.path.join(self.tmp_dir, filename)
# We're only modifying this for ourself and our children so
# there is no need to save and restore environment variables if
# the user decided to pass the password on the command line.
if self.get_option("password") is not False:
os.environ["PGPASSWORD"] = str(self.get_option("password"))

if self.get_option("dbhost"):
cmd = "%s -U %s -h %s -p %s -w -f %s -F t %s" % (
pg_dump_command,
self.get_option("username"),
self.get_option("dbhost"),
self.get_option("dbport"),
dest_file,
self.get_option("dbname")
)
else:
cmd = "%s -C -U %s -w -f %s -F t %s " % (
pg_dump_command,
self.get_option("username"),
dest_file,
self.get_option("dbname")
)

result = self.call_ext_prog(cmd)
if (result['status'] == 0):
self.add_copy_spec(dest_file)
else:
self._log_info(
"Unable to execute pg_dump. Error(%s)" %
(result['output'])
)
else: # no password in env or options
self.soslog.warning(
"password must be supplied to dump a database."
)
self.add_alert(
"WARN: password must be supplied to dump a database."
)

def setup(self):
self.pg_dump()

def postproc(self):
import shutil
if self.tmp_dir:
Expand All @@ -105,33 +106,45 @@ def postproc(self):
self.add_alert("ERROR: Unable to remove %s." % (self.tmp_dir))


class RedHatPostgreSQL(PostgreSQL, RedHatPlugin):
class RedHatPostgreSQL(PostgreSQL, SCLPlugin):

packages = ('postgresql', 'rh-postgresql95-postgresql-server', )

def setup(self):
super(RedHatPostgreSQL, self).setup()

scl = "rh-postgresql95"
pghome = self.get_option("pghome")

# Copy PostgreSQL log files.
for filename in find("*.log", self.get_option("pghome")):
for filename in find("*.log", pghome):
self.add_copy_spec(filename)
for filename in find("*.log", self.convert_copyspec_scl(scl, pghome)):
self.add_copy_spec(filename)

# Copy PostgreSQL config files.
for filename in find("*.conf", self.get_option("pghome")):
for filename in find("*.conf", pghome):
self.add_copy_spec(filename)
for filename in find("*.conf", self.convert_copyspec_scl(scl, pghome)):
self.add_copy_spec(filename)

self.add_copy_spec(
os.path.join(
self.get_option("pghome"),
"data",
"PG_VERSION"
)
)
self.add_copy_spec(
os.path.join(
self.get_option("pghome"),
self.add_copy_spec(os.path.join(pghome, "data", "PG_VERSION"))
self.add_copy_spec(os.path.join(pghome, "data", "postmaster.opts"))

self.add_copy_spec_scl(scl, os.path.join(pghome, "data", "PG_VERSION"))
self.add_copy_spec_scl(scl, os.path.join(
pghome,
"data",
"postmaster.opts"
)
)

if scl in self.scls_matched:
self.pg_dump(
pg_dump_command="scl enable rh-postgresql95 -- pg_dump",
filename="sos_scl_pgdump.tar"
)


class DebianPostgreSQL(PostgreSQL, DebianPlugin, UbuntuPlugin):

Expand Down