|
|
1b1872 |
From 7097f737339f0cde6da923a4ce16a008d229cda7 Mon Sep 17 00:00:00 2001
|
|
|
1b1872 |
From: Pavel Moravec <pmoravec@redhat.com>
|
|
|
1b1872 |
Date: Mon, 16 Sep 2019 17:13:27 +0200
|
|
|
1b1872 |
Subject: [PATCH 1/2] [plugins] extend SoSPredicate by command output inclusion
|
|
|
1b1872 |
test
|
|
|
1b1872 |
|
|
|
1b1872 |
Add a predicate type in form
|
|
|
1b1872 |
|
|
|
1b1872 |
cmd_outputs={'cmd': 'foo --help', 'output': 'bar'}
|
|
|
1b1872 |
|
|
|
1b1872 |
that checks whether output of given command contains given string.
|
|
|
1b1872 |
|
|
|
1b1872 |
Multiple commands/outputs can be provided in a list.
|
|
|
1b1872 |
|
|
|
1b1872 |
Related to: #1682
|
|
|
1b1872 |
|
|
|
1b1872 |
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
|
|
|
1b1872 |
---
|
|
|
1b1872 |
sos/plugins/__init__.py | 57 +++++++++++++++++++++++++++++++++++------
|
|
|
1b1872 |
1 file changed, 49 insertions(+), 8 deletions(-)
|
|
|
1b1872 |
|
|
|
1b1872 |
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
|
|
|
1b1872 |
index a0b291bea..516a61109 100644
|
|
|
1b1872 |
--- a/sos/plugins/__init__.py
|
|
|
1b1872 |
+++ b/sos/plugins/__init__.py
|
|
|
1b1872 |
@@ -115,6 +115,9 @@ class SoSPredicate(object):
|
|
|
1b1872 |
#: Services enablement list
|
|
|
1b1872 |
services = []
|
|
|
1b1872 |
|
|
|
1b1872 |
+ # Command output inclusion pairs {'cmd': 'foo --help', 'output': 'bar'}
|
|
|
1b1872 |
+ cmd_outputs = []
|
|
|
1b1872 |
+
|
|
|
1b1872 |
def __str(self, quote=False, prefix="", suffix=""):
|
|
|
1b1872 |
"""Return a string representation of this SoSPredicate with
|
|
|
1b1872 |
optional prefix, suffix and value quoting.
|
|
|
1b1872 |
@@ -128,14 +131,23 @@ class SoSPredicate(object):
|
|
|
1b1872 |
|
|
|
1b1872 |
services = self.services
|
|
|
1b1872 |
services = [quotes % s for s in services] if quote else services
|
|
|
1b1872 |
- pstr += "services=[%s]" % (",".join(services))
|
|
|
1b1872 |
+ pstr += "services=[%s], " % (",".join(services))
|
|
|
1b1872 |
+
|
|
|
1b1872 |
+ cmdoutputs = [
|
|
|
1b1872 |
+ "{ %s: %s, %s: %s }" % (quotes % "cmd",
|
|
|
1b1872 |
+ quotes % cmdoutput['cmd'],
|
|
|
1b1872 |
+ quotes % "output",
|
|
|
1b1872 |
+ quotes % cmdoutput['output'])
|
|
|
1b1872 |
+ for cmdoutput in self.cmd_outputs
|
|
|
1b1872 |
+ ]
|
|
|
1b1872 |
+ pstr += "cmdoutputs=[%s]" % (",".join(cmdoutputs))
|
|
|
1b1872 |
|
|
|
1b1872 |
return prefix + pstr + suffix
|
|
|
1b1872 |
|
|
|
1b1872 |
def __str__(self):
|
|
|
1b1872 |
"""Return a string representation of this SoSPredicate.
|
|
|
1b1872 |
|
|
|
1b1872 |
- "dry_run=False, kmods=[], services=[]"
|
|
|
1b1872 |
+ "dry_run=False, kmods=[], services=[], cmdoutputs=[]"
|
|
|
1b1872 |
"""
|
|
|
1b1872 |
return self.__str()
|
|
|
1b1872 |
|
|
|
1b1872 |
@@ -143,7 +155,7 @@ class SoSPredicate(object):
|
|
|
1b1872 |
"""Return a machine readable string representation of this
|
|
|
1b1872 |
SoSPredicate.
|
|
|
1b1872 |
|
|
|
1b1872 |
- "SoSPredicate(dry_run=False, kmods=[], services=[])"
|
|
|
1b1872 |
+ "SoSPredicate(dry_run=False, kmods=[], services=[], cmdoutputs=[])"
|
|
|
1b1872 |
"""
|
|
|
1b1872 |
return self.__str(quote=True, prefix="SoSPredicate(", suffix=")")
|
|
|
1b1872 |
|
|
|
1b1872 |
@@ -170,15 +182,39 @@ class SoSPredicate(object):
|
|
|
1b1872 |
else:
|
|
|
1b1872 |
return all(_svcs)
|
|
|
1b1872 |
|
|
|
1b1872 |
+ def _eval_cmd_output(self, cmd_output):
|
|
|
1b1872 |
+ '''Does 'cmd' output contain string 'output'?'''
|
|
|
1b1872 |
+ if 'cmd' not in cmd_output or 'output' not in cmd_output:
|
|
|
1b1872 |
+ return False
|
|
|
1b1872 |
+ result = sos_get_command_output(cmd_output['cmd'])
|
|
|
1b1872 |
+ if result['status'] != 0:
|
|
|
1b1872 |
+ return False
|
|
|
1b1872 |
+ for line in result['output'].splitlines():
|
|
|
1b1872 |
+ if cmd_output['output'] in line:
|
|
|
1b1872 |
+ return True
|
|
|
1b1872 |
+ return False
|
|
|
1b1872 |
+
|
|
|
1b1872 |
+ def _eval_cmd_outputs(self):
|
|
|
1b1872 |
+ if not self.cmd_outputs:
|
|
|
1b1872 |
+ return True
|
|
|
1b1872 |
+
|
|
|
1b1872 |
+ _cmds = [self._eval_cmd_output(c) for c in self.cmd_outputs]
|
|
|
1b1872 |
+
|
|
|
1b1872 |
+ if self.required['commands'] == 'any':
|
|
|
1b1872 |
+ return any(_cmds)
|
|
|
1b1872 |
+ else:
|
|
|
1b1872 |
+ return all(_cmds)
|
|
|
1b1872 |
+
|
|
|
1b1872 |
def __nonzero__(self):
|
|
|
1b1872 |
"""Predicate evaluation hook.
|
|
|
1b1872 |
"""
|
|
|
1b1872 |
|
|
|
1b1872 |
# Null predicate?
|
|
|
1b1872 |
- if not any([self.kmods, self.services, self.dry_run]):
|
|
|
1b1872 |
+ if not any([self.kmods, self.services, self.cmd_outputs, self.dry_run]):
|
|
|
1b1872 |
return True
|
|
|
1b1872 |
|
|
|
1b1872 |
- return ((self._eval_kmods() and self._eval_services()) and not
|
|
|
1b1872 |
+ return ((self._eval_kmods() and self._eval_services() and
|
|
|
1b1872 |
+ self._eval_cmd_outputs()) and not
|
|
|
1b1872 |
self.dry_run)
|
|
|
1b1872 |
|
|
|
1b1872 |
def __bool__(self):
|
|
|
1b1872 |
@@ -187,14 +223,17 @@ class SoSPredicate(object):
|
|
|
1b1872 |
return self.__nonzero__()
|
|
|
1b1872 |
|
|
|
1b1872 |
def __init__(self, owner, dry_run=False, kmods=[], services=[],
|
|
|
1b1872 |
- required={}):
|
|
|
1b1872 |
+ cmd_outputs=[], required={}):
|
|
|
1b1872 |
"""Initialise a new SoSPredicate object.
|
|
|
1b1872 |
"""
|
|
|
1b1872 |
self._owner = owner
|
|
|
1b1872 |
self.kmods = list(kmods)
|
|
|
1b1872 |
self.services = list(services)
|
|
|
1b1872 |
+ if not isinstance(cmd_outputs, list):
|
|
|
1b1872 |
+ cmd_outputs = [cmd_outputs]
|
|
|
1b1872 |
+ self.cmd_outputs = cmd_outputs
|
|
|
1b1872 |
self.dry_run = dry_run | self._owner.commons['cmdlineopts'].dry_run
|
|
|
1b1872 |
- self.required = {'kmods': 'any', 'services': 'any'}
|
|
|
1b1872 |
+ self.required = {'kmods': 'any', 'services': 'any', 'commands': 'any'}
|
|
|
1b1872 |
self.required.update({
|
|
|
1b1872 |
k: v for k, v in required.items() if
|
|
|
1b1872 |
required[k] != self.required[k]
|
|
|
1b1872 |
|
|
|
1b1872 |
From 47e434c50e63f80e4b620e74d81c636c8c8a8d97 Mon Sep 17 00:00:00 2001
|
|
|
1b1872 |
From: Pavel Moravec <pmoravec@redhat.com>
|
|
|
1b1872 |
Date: Mon, 16 Sep 2019 17:15:40 +0200
|
|
|
1b1872 |
Subject: [PATCH 2/2] [grub2] call grub2-config with --no-grubenv-update when
|
|
|
1b1872 |
appropriate
|
|
|
1b1872 |
|
|
|
1b1872 |
On some newer grub2 versions, grub2-config removes extra args in
|
|
|
1b1872 |
$kernel_opts until --no-grubenv-update option is used.
|
|
|
1b1872 |
|
|
|
1b1872 |
Test if the option is present in "grub2-config --help" and if so, use it.
|
|
|
1b1872 |
|
|
|
1b1872 |
Resolves: #1682
|
|
|
1b1872 |
|
|
|
1b1872 |
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
|
|
|
1b1872 |
---
|
|
|
1b1872 |
diff --git a/sos/plugins/grub2.py b/sos/plugins/grub2.py
|
|
|
1b1872 |
index 9786de44d..0ca6fe096 100644
|
|
|
1b1872 |
--- a/sos/plugins/grub2.py
|
|
|
1b1872 |
+++ b/sos/plugins/grub2.py
|
|
|
1b1872 |
@@ -6,7 +6,8 @@
|
|
|
1b1872 |
#
|
|
|
1b1872 |
# See the LICENSE file in the source distribution for further information.
|
|
|
1b1872 |
|
|
|
1b1872 |
-from sos.plugins import Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin
|
|
|
1b1872 |
+from sos.plugins import (Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin,
|
|
|
1b1872 |
+ SoSPredicate)
|
|
|
1b1872 |
|
|
|
1b1872 |
|
|
|
1b1872 |
class Grub2(Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin):
|
|
|
1b1872 |
@@ -32,9 +33,16 @@ def setup(self):
|
|
|
1b1872 |
self.add_cmd_output("ls -lanR /boot")
|
|
|
1b1872 |
# call grub2-mkconfig with GRUB_DISABLE_OS_PROBER=true to prevent
|
|
|
1b1872 |
# possible unwanted loading of some kernel modules
|
|
|
1b1872 |
+ # further, check if the command supports --no-grubenv-update option
|
|
|
1b1872 |
+ # to prevent removing of extra args in $kernel_opts, and (only) if so,
|
|
|
1b1872 |
+ # call the command with this argument
|
|
|
1b1872 |
env = {}
|
|
|
1b1872 |
env['GRUB_DISABLE_OS_PROBER'] = 'true'
|
|
|
1b1872 |
- self.add_cmd_output("grub2-mkconfig", env=env)
|
|
|
1b1872 |
+ grub_cmd = 'grub2-mkconfig'
|
|
|
1b1872 |
+ co = {'cmd': 'grub2-mkconfig --help', 'output': '--no-grubenv-update'}
|
|
|
1b1872 |
+ if self.test_predicate(self, pred=SoSPredicate(self, cmd_outputs=co)):
|
|
|
1b1872 |
+ grub_cmd += ' --no-grubenv-update'
|
|
|
1b1872 |
+ self.add_cmd_output(grub_cmd, env=env)
|
|
|
1b1872 |
|
|
|
1b1872 |
def postproc(self):
|
|
|
1b1872 |
# the trailing space is required; python treats '_' as whitespace
|