def apply_options_from_cmdline(self, opts): """(Re-)apply options specified via the cmdline to an options instance There are several cases where we may need to re-apply the options from the cmdline over previously loaded options - for instance when an option is specified in both a config file and cmdline, or a preset and the cmdline, or all three. Use this to re-apply cmdline option overrides to anything that may change the default values of options Positional arguments: :param opts: SoSOptions object to update """ # override the values from config files with the values from the # cmdline iff that value was explicitly specified, and compare it to # the _current_ set of opts from the config files as a default cmdopts = SoSOptions().from_args( self.parser.parse_args(self.cmdline), arg_defaults=opts.dict(preset_filter=False)) # we can't use merge() here, as argparse will pass default values for # unset options which would effectively negate config file settings and # set all values back to their normal default codict = cmdopts.dict(preset_filter=False) for opt, val in codict.items(): if opt not in cmdopts.arg_defaults.keys(): continue if val not in [None, [], ''] and val != opts.arg_defaults[opt]: setattr(opts, opt, val) return opts
def load_options(self): """Compile arguments loaded from defaults, config files, and the command line into a usable set of options """ # load the defaults defined by the component and the shared options opts = SoSOptions(arg_defaults=self._arg_defaults) for option in self.parser._actions: if option.default != SUPPRESS: option.default = None opts.update_from_conf(self.args.config_file, self.args.component) # directly check the cmdline options here as they have yet to be loaded # as SoSOptions, and if we do this check after they are loaded we would # need to do a second update from cmdline options for overriding config # file values if '--clean' in self.cmdline or '--mask' in self.cmdline: opts.update_from_conf(self.args.config_file, 'clean') if os.getuid() != 0: userconf = os.path.join(Path.home(), '.config/sos/sos.conf') if os.path.exists(userconf): opts.update_from_conf(userconf, self.args.component) opts = self.apply_options_from_cmdline(opts) return opts
def load_presets(self, presets_path=None): """Load presets from disk. Read JSON formatted preset data from the specified path, or the default location at ``/var/lib/sos/presets``. :param presets_path: a directory containing JSON presets. """ presets_path = presets_path or self.presets_path if not os.path.exists(presets_path): return for preset_path in os.listdir(presets_path): preset_path = os.path.join(presets_path, preset_path) with open(preset_path) as pf: try: preset_data = json.load(pf) except ValueError: continue for preset in preset_data.keys(): pd = PresetDefaults(preset, opts=SoSOptions()) data = preset_data[preset] pd.desc = data[DESC] if DESC in data else "" pd.note = data[NOTE] if NOTE in data else "" if OPTS in data: for arg in data[OPTS]: setattr(pd.opts, arg, data[OPTS][arg]) pd.builtin = False self.presets[preset] = pd
def __init__(self, name="", desc="", note=None, opts=SoSOptions()): """Initialise a new ``PresetDefaults`` object with the specified arguments. :returns: The newly initialised ``PresetDefaults`` """ self.name = name self.desc = desc self.note = note self.opts = opts
def __init__(self, name="", desc="", note=None, opts=SoSOptions()): """Initialise a new ``PresetDefaults`` object with the specified arguments. :param name: The name of the new preset :param desc: A description for the new preset :param note: Note for the new preset :param opts: Options set for the new preset :returns: The newly initialised ``PresetDefaults`` """ self.name = name self.desc = desc self.note = note self.opts = opts
def add_preset(self, name=None, desc=None, note=None, opts=SoSOptions()): """Add a new on-disk preset and write it to the configured presets path. :param preset: the new PresetDefaults to add """ presets_path = self.presets_path if not name: raise ValueError("Preset name cannot be empty") if name in self.presets.keys(): raise ValueError("A preset with name '%s' already exists" % name) preset = PresetDefaults(name=name, desc=desc, note=note, opts=opts) preset.builtin = False self.presets[preset.name] = preset preset.write(presets_path)
def load_options(self): """Compile arguments loaded from defaults, config files, and the command line into a usable set of options """ # load the defaults defined by the component and the shared options opts = SoSOptions(arg_defaults=self._arg_defaults) for option in self.parser._actions: if option.default != SUPPRESS: option.default = None # load values from cmdline cmdopts = SoSOptions().from_args(self.parser.parse_args(self.cmdline)) opts.merge(cmdopts) # load values from config file opts.update_from_conf(opts.config_file) return opts
def load_options(self): """Compile arguments loaded from defaults, config files, and the command line into a usable set of options """ # load the defaults defined by the component and the shared options opts = SoSOptions(arg_defaults=self._arg_defaults) for option in self.parser._actions: if option.default != SUPPRESS: option.default = None opts.update_from_conf(self.args.config_file, self.args.component) if os.getuid() != 0: userconf = os.path.join(Path.home(), '.config/sos/sos.conf') if os.path.exists(userconf): opts.update_from_conf(userconf, self.args.component) opts = self.apply_options_from_cmdline(opts) return opts
else: if onoff == "on": ret.append(int(runlevel)) return ret def get_tmp_dir(self, opt_tmp_dir): if not opt_tmp_dir: return self._tmp_dir return opt_tmp_dir # Container environment variables on Red Hat systems. ENV_CONTAINER = 'container' ENV_HOST_SYSROOT = 'HOST' _opts_verify = SoSOptions(verify=True) _opts_all_logs = SoSOptions(all_logs=True) _opts_all_logs_verify = SoSOptions(all_logs=True, verify=True) _cb_profiles = ['boot', 'storage', 'system'] _cb_plugopts = ['boot.all-images=on', 'rpm.rpmva=on', 'rpm.rpmdb=on'] RHEL_RELEASE_STR = "Red Hat Enterprise Linux" RHV = "rhv" RHV_DESC = "Red Hat Virtualization" RHEL = "rhel" RHEL_DESC = RHEL_RELEASE_STR RHOSP = "rhosp" RHOSP_DESC = "Red Hat OpenStack Platform"
class PresetDefaults(object): """Preset command line defaults. """ #: Preset name, used for selection name = None #: Human readable preset description desc = None #: Notes on preset behaviour note = None #: Options set for this preset opts = SoSOptions() #: ``True`` if this preset if built-in or ``False`` otherwise. builtin = True def __str__(self): """Return a human readable string representation of this ``PresetDefaults`` object. """ return ("name=%s desc=%s note=%s opts=(%s)" % (self.name, self.desc, self.note, str(self.opts))) def __repr__(self): """Return a machine readable string representation of this ``PresetDefaults`` object. """ return ("PresetDefaults(name='%s' desc='%s' note='%s' opts=(%s)" % (self.name, self.desc, self.note, repr(self.opts))) def __init__(self, name="", desc="", note=None, opts=SoSOptions()): """Initialise a new ``PresetDefaults`` object with the specified arguments. :param name: The name of the new preset :param desc: A description for the new preset :param note: Note for the new preset :param opts: Options set for the new preset :returns: The newly initialised ``PresetDefaults`` """ self.name = name self.desc = desc self.note = note self.opts = opts def write(self, presets_path): """Write this preset to disk in JSON notation. :param presets_path: the directory where the preset will be written. """ if self.builtin: raise TypeError("Cannot write built-in preset") # Make dictionaries of PresetDefaults values odict = self.opts.dict() pdict = {self.name: {DESC: self.desc, NOTE: self.note, OPTS: odict}} if not os.path.exists(presets_path): os.makedirs(presets_path, mode=0o755) with open(os.path.join(presets_path, self.name), "w") as pfile: json.dump(pdict, pfile) def delete(self, presets_path): os.unlink(os.path.join(presets_path, self.name))
json.dump(pdict, pfile) def delete(self, presets_path): os.unlink(os.path.join(presets_path, self.name)) NO_PRESET = 'none' NO_PRESET_DESC = 'Do not load a preset' NO_PRESET_NOTE = 'Use to disable automatically loaded presets' GENERIC_PRESETS = { NO_PRESET: PresetDefaults(name=NO_PRESET, desc=NO_PRESET_DESC, note=NO_PRESET_NOTE, opts=SoSOptions()) } class Policy(object): msg = _("""\ This command will collect system configuration and diagnostic information \ from this %(distro)s 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 \
os.makedirs(presets_path, mode=0o755) with open(os.path.join(presets_path, self.name), "w") as pfile: json.dump(pdict, pfile) def delete(self, presets_path): os.unlink(os.path.join(presets_path, self.name)) NO_PRESET = 'none' NO_PRESET_DESC = 'Do not load a preset' NO_PRESET_NOTE = 'Use to disable automatically loaded presets' GENERIC_PRESETS = { NO_PRESET: PresetDefaults(name=NO_PRESET, desc=NO_PRESET_DESC, note=NO_PRESET_NOTE, opts=SoSOptions()) } class Policy(object): msg = _("""\ This command will collect system configuration and diagnostic information \ from this %(distro)s 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 \
# Copyright (C) 2020 Red Hat, Inc., Jake Hunsaker <*****@*****.**> # This file is part of the sos project: https://github.com/sosreport/sos # # This copyrighted material is made available to anyone wishing to use, # modify, copy, or redistribute it subject to the terms and conditions of # version 2 of the GNU General Public License. # # See the LICENSE file in the source distribution for further information. from sos.options import SoSOptions from sos.presets import PresetDefaults RHEL_RELEASE_STR = "Red Hat Enterprise Linux" _opts_verify = SoSOptions(verify=True) _opts_all_logs = SoSOptions(all_logs=True) _opts_all_logs_verify = SoSOptions(all_logs=True, verify=True) _cb_profiles = ['boot', 'storage', 'system'] _cb_plugopts = ['boot.all-images=on', 'rpm.rpmva=on', 'rpm.rpmdb=on'] RHV = "rhv" RHV_DESC = "Red Hat Virtualization" RHEL = "rhel" RHEL_DESC = RHEL_RELEASE_STR RHOSP = "rhosp" RHOSP_DESC = "Red Hat OpenStack Platform" RHOSP_OPTS = SoSOptions(plugopts=[ 'process.lsof=off', 'networking.ethtool_namespaces=False',