From 7ccd5f679d8f154132e094673d0d4d6a3b39c1a7 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Fri, 22 Mar 2013 18:53:48 +0000 Subject: Improve policy template handling The current boilerplate text in the policy class has inconsistent formatting and does not make sense with some distribution names (e.g. Red Hat Enterprise Linux). Fix this by storing the string in paragraphs and formatting it with textwrap and adding new tags to make the construction of the message more flexible: vendor vendor_url vendor_text tmpdir Signed-off-by: Bryn M. Reeves --- sos/policies/__init__.py | 40 ++++++++++++++++++------- sos/policies/debian.py | 13 ++++---- sos/policies/redhat.py | 78 ++++++++++++++++++++++++++++++++++++++++-------- sos/policies/ubuntu.py | 7 +++-- sos/sosreport.py | 1 + 5 files changed, 110 insertions(+), 29 deletions(-) diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py index 4a4623aa..1cea2970 100644 --- a/sos/policies/__init__.py +++ b/sos/policies/__init__.py @@ -13,6 +13,7 @@ from sos.utilities import ImporterHelper, \ from sos.plugins import IndependentPlugin from sos import _sos as _ import hashlib +from textwrap import fill def import_policy(name): policy_fqname = "sos.policies.%s" % name @@ -111,19 +112,27 @@ class PackageManager(object): class Policy(object): - msg = _("""This utility will collect some detailed information about the -hardware and setup of your %(distro)s system. -The information is collected and an archive is packaged under -/tmp, which you can send to a support representative. -%(distro)s will use this information for diagnostic purposes ONLY -and it will be considered confidential information. + msg = _("""\ +This command will collect system configuration and diagnostic information \ +from this %(distro)s system. An archive containing the collected information \ +will be generated in %(tmpdir)s. -This process may take a while to complete. -No changes will be made to your system. +For more information on %(vendor)s visit: + %(vendor_url)s + +The generated archive may contain data considered sensitive and its content \ +should be reviewed by the originating organization before being passed to \ +any third party. + +No changes will be made to system configuration. +%(vendor_text)s """) - distro = "" + distro = "Unknown" + vendor = "Unknown" + vendor_url = "http://www.example.com/" + vendor_text = "" def __init__(self): """Subclasses that choose to override this initializer should call @@ -343,7 +352,15 @@ No changes will be made to your system. the user in non-batch mode. If your policy sets self.distro that text will be substituted accordingly. You can also override this method to do something more complicated.""" - return self.msg % {'distro': self.distro} + width = 60 + _msg = self.msg % {'distro': self.distro, 'vendor': self.vendor, + 'vendor_url': self.vendor_url, + 'vendor_text': self.vendor_text, + 'tmpdir': self.commons['tmpdir']} + _fmt = "" + for line in _msg.splitlines(): + _fmt = _fmt + fill(line, width, replace_whitespace = False) + '\n' + return _fmt class GenericPolicy(Policy): @@ -358,6 +375,9 @@ class LinuxPolicy(Policy): """This policy is meant to be an abc class that provides common implementations used in Linux distros""" + distro = "Linux" + vendor = "None" + def __init__(self): super(LinuxPolicy, self).__init__() diff --git a/sos/policies/debian.py b/sos/policies/debian.py index 5b7f2af2..98526e3a 100644 --- a/sos/policies/debian.py +++ b/sos/policies/debian.py @@ -4,13 +4,16 @@ from sos.policies import PackageManager, LinuxPolicy import os class DebianPolicy(LinuxPolicy): + distro = "Debian" + vendor = "the Debian project" + vendor_url = "http://www.debian.org/" + reportName = "" + ticketNumber = "" + package_manager = PackageManager("dpkg-query -W -f='${Package}|${Version}\\n' \*") + valid_subclasses = [DebianPlugin] + def __init__(self): super(DebianPolicy, self).__init__() - self.reportName = "" - self.ticketNumber = "" - self.package_manager = PackageManager("dpkg-query -W -f='${Package}|${Version}\\n' \*") - self.valid_subclasses = [DebianPlugin] - self.distro = "Debian" @classmethod def check(self): diff --git a/sos/policies/redhat.py b/sos/policies/redhat.py index c17e4420..89c7fd26 100644 --- a/sos/policies/redhat.py +++ b/sos/policies/redhat.py @@ -20,6 +20,7 @@ import sys from sos.plugins import RedHatPlugin from sos.policies import LinuxPolicy, PackageManager +from sos import _sos as _ sys.path.insert(0, "/usr/share/rhn/") try: @@ -30,11 +31,13 @@ except: # might fail if non-RHEL pass - -class RHELPolicy(LinuxPolicy): +class RedHatPolicy(LinuxPolicy): + distro = "Red Hat" + vendor = "Red Hat" + vendor_url = "http://www.redhat.com/" def __init__(self): - super(RHELPolicy, self).__init__() + super(RedHatPolicy, self).__init__() self.reportName = "" self.ticketNumber = "" self.package_manager = PackageManager('rpm -qa --queryformat "%{NAME}|%{VERSION}\\n"') @@ -42,10 +45,10 @@ class RHELPolicy(LinuxPolicy): @classmethod def check(self): - """This method checks to see if we are running on RHEL. It returns True - or False.""" - return (os.path.isfile('/etc/redhat-release') - or os.path.isfile('/etc/fedora-release')) + """This method checks to see if we are running on Red Hat. It must be overriden + by concrete subclasses to return True when running on a Fedora, RHEL or other + Red Hat distribution or False otherwise.""" + return False def runlevelByService(self, name): from subprocess import Popen, PIPE @@ -68,6 +71,43 @@ class RHELPolicy(LinuxPolicy): ret.append(int(runlevel)) return ret + def getLocalName(self): + return self.hostName() + +class RHELPolicy(RedHatPolicy): + + msg = _("""\ +This command will collect system configuration and diagnostic information \ +from this %(distro)s system. An archive containing the collected information \ +will be generated in %(tmpdir)s and may be provided to a %(vendor)s support \ +representative or used for local diagnostic or recording purposes. + +Any information provided to %(vendor)s will be treated in strict confidence \ +in accordance with the published support policies at: + + %(vendor_url)s + +The generated archive may contain data considered sensitive and its content \ +should be reviewed by the originating organization before being passed to \ +any third party. + +No changes will be made to system configuration. +%(vendor_text)s +""") + + distro = "Red Hat Enterprise Linux" + vendor = "Red Hat" + vendor_url = "https://access.redhat.com/support/" + + def __init__(self): + super(RHELPolicy, self).__init__() + + @classmethod + def check(self): + """This method checks to see if we are running on RHEL. It returns True + or False.""" + return (os.path.isfile('/etc/redhat-release')) + def rhelVersion(self): try: pkg = self.pkgByName("redhat-release") or \ @@ -95,10 +135,24 @@ class RHELPolicy(LinuxPolicy): def getLocalName(self): return self.rhnUsername() or self.hostName() - def get_msg(self): - msg_dict = {"distro": "Red Hat Enterprise Linux"} - if os.path.isfile('/etc/fedora-release'): - msg_dict['distro'] = 'Fedora' - return self.msg % msg_dict +class FedoraPolicy(LinuxPolicy): + + distro = "Fedora" + vendor = "the Fedora Project" + vendor_url = "https://fedoraproject.org/" + + def __init__(self): + super(FedoraPolicy, self).__init__() + + @classmethod + def check(self): + """This method checks to see if we are running on Fedora. It returns True + or False.""" + return os.path.isfile('/etc/fedora-release') + + def fedoraVersion(self): + pkg = self.pkgByName("fedora-release") or \ + self.allPkgsByNameRegex("fedora-release-.*")[-1] + return int(pkg["version"]) # vim: ts=4 sw=4 et diff --git a/sos/policies/ubuntu.py b/sos/policies/ubuntu.py index a5eea4da..97f2d14a 100644 --- a/sos/policies/ubuntu.py +++ b/sos/policies/ubuntu.py @@ -6,10 +6,13 @@ from sos.plugins import UbuntuPlugin, IndependentPlugin from sos.policies.debian import DebianPolicy class UbuntuPolicy(DebianPolicy): + distro = "Ubuntu" + vendor = "Ubuntu" + vendor_url = "http://www.ubuntu.com/" + valid_subclasses = [UbuntuPlugin] + def __init__(self): super(UbuntuPolicy, self).__init__() - self.distro = "Ubuntu" - self.valid_subclasses = [UbuntuPlugin] @classmethod def check(self): diff --git a/sos/sosreport.py b/sos/sosreport.py index ec943988..ef49a09c 100644 --- a/sos/sosreport.py +++ b/sos/sosreport.py @@ -228,6 +228,7 @@ class SoSReport(object): 'cmddir': self.cmddir, 'logdir': self.logdir, 'rptdir': self.rptdir, + 'tmpdir': self.opts.tmp_dir, 'soslog': self.soslog, 'proflog' : self.proflog, 'policy': self.policy, -- cgit