[Kimchi-devel] [PATCH 1/2] Modified code to parse the output of sosreport command using regular expression which gives the name and location of sosreport present in command output, instead of searching the sosreport file with regular pattern "sosreport-<name>*" in /tmp and /var/tmp

archus at linux.vnet.ibm.com archus at linux.vnet.ibm.com
Mon Nov 2 11:57:54 UTC 2015


From: Megha Smriti <mesmriti at linux.vnet.ibm.com>

---
 src/wok/plugins/gingerbase/i18n.py               |  7 ++-
 src/wok/plugins/gingerbase/model/debugreports.py | 67 +++++++++++++-----------
 2 files changed, 40 insertions(+), 34 deletions(-)

diff --git a/src/wok/plugins/gingerbase/i18n.py b/src/wok/plugins/gingerbase/i18n.py
index 8596f17..01573b0 100644
--- a/src/wok/plugins/gingerbase/i18n.py
+++ b/src/wok/plugins/gingerbase/i18n.py
@@ -29,14 +29,13 @@ messages = {
 
     "GGBDR0001E": _("Debug report %(name)s does not exist"),
     "GGBDR0002E": _("Debug report tool not found in system"),
-    "GGBDR0003E": _("Unable to create debug report %(name)s. Details: %(err)s."),
-    "GGBDR0004E": _("Can not find any debug report with the given name %(name)s"),
-    "GGBDR0005E": _("Unable to generate debug report %(name)s. Details: %(err)s"),
+    "GGBDR0003E": _("Unable to create sosreport report %(name)s. Details: %(err)s."),
+    "GGBDR0004E": _("Can not find any sosreport with the given name %(name)s"),
+    "GGBDR0005E": _("Unable to generate sosreport %(name)s. Details: %(err)s"),
     "GGBDR0006E": _("You should give a name for the debug report file."),
     "GGBDR0007E": _("Debug report name must be a string. Only letters, digits, underscore ('_') and "
                     "hyphen ('-') are allowed."),
     "GGBDR0008E": _("The debug report with specified name \"%(name)s\" already exists. Please use another one."),
-
     "GGBHOST0001E": _("Unable to shutdown host machine as there are running virtual machines"),
     "GGBHOST0002E": _("Unable to reboot host machine as there are running virtual machines"),
     "GGBHOST0005E": _("When specifying CPU topology, each element must be an integer greater than zero."),
diff --git a/src/wok/plugins/gingerbase/model/debugreports.py b/src/wok/plugins/gingerbase/model/debugreports.py
index 94ab7fe..0bb36fe 100644
--- a/src/wok/plugins/gingerbase/model/debugreports.py
+++ b/src/wok/plugins/gingerbase/model/debugreports.py
@@ -19,13 +19,13 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
-import fnmatch
 import glob
 import logging
 import os
 import shutil
 import subprocess
 import time
+import re
 
 from wok.exception import InvalidParameter, NotFoundError, OperationFailed
 from wok.exception import WokException
@@ -71,7 +71,7 @@ class DebugReportsModel(object):
         raise OperationFailed("GGBDR0002E")
 
     @staticmethod
-    def sosreport_generate(cb, name):
+    def debugreport_generate(cb, name):
         def log_error(e):
             log = logging.getLogger('Model')
             log.warning('Exception in generating debug file: %s', e)
@@ -87,38 +87,35 @@ class DebugReportsModel(object):
             # SOSREPORT might create file in /tmp or /var/tmp
             # FIXME: The right way should be passing the tar.xz file directory
             # though the parameter '--tmp-dir', but it is failing in Fedora 20
-            patterns = ['/tmp/sosreport-%s-*', '/var/tmp/sosreport-%s-*']
-            reports = []
-            reportFile = None
-            for p in patterns:
-                reports = reports + [f for f in glob.glob(p % name)]
-            for f in reports:
-                if not fnmatch.fnmatch(f, '*.md5'):
-                    reportFile = f
-                    break
+            sosreport_name = name.replace('_', '')
+            sosreport_name_regex = '(\s+)(\/\w+\/\w+\/|\/\w+\/)' \
+                                   '(sosreport-' +\
+                                   sosreport_name + '-\d+.tar.xz)'
+            sosreport_file = None
+            output = output.splitlines()
+            for line in output:
+                if line:
+                    matched_name = re.match(sosreport_name_regex, line)
+                    if matched_name:
+                        path = matched_name.groups()[1]
+                        fname = matched_name.groups()[2]
+                        sosreport_file = path + fname
+                        break
             # Some error in sosreport happened
-            if reportFile is None:
+            if sosreport_file is None:
                 wok_log.error('Debug report file not found. See sosreport '
                               'output for detail:\n%s', output)
-                fname = (patterns[0] % name).split('/')[-1]
-                raise OperationFailed('GGBDR0004E', {'name': fname})
-
-            md5_report_file = reportFile + '.md5'
-            report_file_extension = '.' + reportFile.split('.', 1)[1]
+                raise OperationFailed('GGBDR0004E', {'name': name})
+            md5_report_file = sosreport_file + '.md5'
+            report_file_extension = '.' + sosreport_file.split('.', 1)[1]
             path = config.get_debugreports_path()
-            target = os.path.join(path, name + report_file_extension)
-            # Moving report
-            msg = 'Moving debug report file "%s" to "%s"' % (reportFile,
-                                                             target)
-            wok_log.info(msg)
-            shutil.move(reportFile, target)
-            # Deleting md5
-            msg = 'Deleting report md5 file: "%s"' % (md5_report_file)
+            sosreport_target = os.path.join(path,
+                                            name + report_file_extension)
+            msg = 'Moving debug report file "%s" to "%s"' \
+                  % (sosreport_file, sosreport_target)
             wok_log.info(msg)
-            with open(md5_report_file) as f:
-                md5 = f.read().strip()
-                wok_log.info('Md5 file content: "%s"', md5)
-            os.remove(md5_report_file)
+            shutil.move(sosreport_file, sosreport_target)
+            delete_the_sosreport_md5_file(md5_report_file)
             cb('OK', True)
             return
 
@@ -143,7 +140,7 @@ class DebugReportsModel(object):
         # and implement the report generating function
         # based on the new report command
         report_tools = ({'cmd': 'sosreport --help',
-                         'fn': DebugReportsModel.sosreport_generate},)
+                         'fn': DebugReportsModel.debugreport_generate},)
 
         # check if the command can be found by shell one by one
         for helper_tool in report_tools:
@@ -213,3 +210,13 @@ class DebugReportContentModel(object):
 
     def lookup(self, name):
         return self._debugreport.lookup(name)
+
+
+def delete_the_sosreport_md5_file(md5_file):
+    # Deleting md5
+    msg = 'Deleting report md5 file: "%s"' % md5_file
+    wok_log.info(msg)
+    with open(md5_file) as f:
+        md5 = f.read().strip()
+        wok_log.info('Md5 file content: "%s"', md5)
+    os.remove(md5_file)
-- 
2.4.0




More information about the Kimchi-devel mailing list