a >hzV@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZmZmZmZmZmZmZddlmZddlmZmZmZmZddlmZmZddlm Z dd l!m"Z#d d Z$iddd dd fddZ%GdddZ&dS)N)getpwuid)fill) NO_PRESETGENERIC_PRESETS PRESETS_PATHPresetDefaultsDESCNOTEOPTS)PackageManager)ImporterHelper import_moduleget_human_readablebold)IndependentPluginExperimentalPlugin) SoSOptions)_soscCs0d|}z t|tWSty*YdS0dS)Nzsos.policies.distros.)r Policy ImportError)nameZ policy_fqnamer9/usr/lib/python3.9/site-packages/sos/policies/__init__.py import_policys    rTc Csd|vr|dSddl}t|jj}|D]6}t|D](} | j|dr:| ||||d|d<q.q:q.tj dkrxt dd|vr|jj |d<|dS)Npolicyr)remote)sysrootinit probe_runtime remote_execZlinuxz%SoS is not supported on this platform) getZsos.policies.distrosr ZpoliciesZdistrosZ get_modulesrchecksysplatform ExceptionZGenericLinuxPolicy) cacherrrr Z remote_checksoshelpermodulerrrrload s"       r*c@sneZdZdZedZdZdZdZdZ dgZ dZ dZ dZ deiZeZdZdId d ZedJd dZeddZeddZddZddZddZddZddZddZdd ZdKd!d"Z d#d$Z!d%d&Z"d'd(Z#d)d*Z$d+d,Z%d-d.Z&d/d0Z'd1d2Z(d3d4Z)ed5d6Z*dLd7d8Z+d9d:Z,d;d<Z-dMd=d>Z.d?d@Z/dAdBZ0dNdCdDZ1d d d e2fdEdFZ3dOdGdHZ4d S)PraPolicies represent distributions that sos supports, and define the way in which sos behaves on those distributions. A policy should define at minimum a way to identify the distribution, and a package manager to allow for package based plugin enablement. Policies also control preferred ContainerRuntime()'s, upload support to default locations for distribution vendors, disclaimer text, and default presets supported by that distribution or vendor's products. Every Policy will also need at least one "tagging class" for plugins. :param sysroot: Set the sysroot for the system, if not / :type sysroot: ``str`` or ``None`` :param probe_runtime: Should the Policy try to load a ContainerRuntime :type probe_runtime: ``bool`` :param remote_exec: If this policy is loaded for a remote node, use this to facilitate executing commands via the SoSTransport in use :type remote_exec: ``SoSTranport.run_command()`` :cvar os_release_name: The name of the distribution as it appears in the os-release (-esque) file for the NAME variable. :vartype os_release_name: ``str`` :cvar os_release_id: The ID variable to match in a distribution's release file. :vartype os_release_id: ``str`` :cvar os_release_file: The filepath of the distribution's os-release file :vartype os_release_file: ``str`` :cvar vendor: The name of the vendor producing the distribution :vartype vendor: ``str`` :cvar vendor_urls: List of URLs for the vendor's website, or support portal :vartype vendor_urls: ``list`` of ``tuples`` formatted ``(``description``, ``url``)`` :cvar vendor_text: Additional text to add to the banner message :vartype vendor_text: ``str`` :cvar name_pattern: The naming pattern to be used for naming archives generated by sos. Values of `legacy`, and `friendly` are preset patterns. May also be set to an explicit custom pattern, see `get_archive_name()` :vartype name_pattern: ``str`` ayThis command will collect system configuration and diagnostic information from this %(os_release_name)s system. For more information on %(vendor)s visit: %(vendor_urls)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. %(changes_text)s %(vendor_text)s ZUnknownr)z Example URLzhttp://www.example.com/zz*Policy.get_archive_name..r+z -%Y%m%d%H%M%Sz sosreport-Zfriendly-z -%Y-%m-%d) Zget_local_namesplitr/commonslabeljoinrange name_patternZsanitize_filenametimestrftime)r4rZcaserUdateZrandZnstrrrrget_archive_names    zPolicy.get_archive_namecCs|SrIr)r4Zbinaryrrr_get_pkg_name_for_binaryszPolicy._get_pkg_name_for_binarycCs|s tS|SrI)tempfileZ gettempdir)r4Z opt_tmp_dirrrr get_tmp_dirszPolicy.get_tmp_dircCs6t|dkr.|D]}t||jdr|Sq|dS)azDetermine what subclass of a Plugin should be used based on the tagging classes assigned to the Plugin :param plugin_classes: The classes that the Plugin subclasses :type plugin_classes: ``list`` :returns: The first tagging class that matches one of the Policy's `valid_subclasses` :rtype: ``PluginDistroTag`` r)len issubclassr1)r4Zplugin_classesprrr match_plugins  zPolicy.match_plugincs0tg|j}|r|tg7}tfdd|DS)a# Verifies that the plugin_class should execute under this policy :param plugin_class: The tagging class being checked :type plugin_class: ``PluginDistroTag`` :returns: ``True`` if the `plugin_class` is allowed by the policy :rtype: ``bool`` c3s|]}t|VqdSrI)rb)rMclass_ plugin_classrrrO9sz)Policy.validate_plugin..)rr1rany)r4rgZ experimentalr1rrfrvalidate_plugin,s   zPolicy.validate_plugincCsdS)z> This function is called prior to collection. NrrBrrrpre_work<szPolicy.pre_workcCsdS)zR This function is called after the sos report has been generated. NrrBrrr post_workAszPolicy.post_workcCs |j|S)zWrapper to retrieve a package from the Policy's package manager :param pkg: The name of the package :type pkg: ``str`` :returns: The first package that matches `pkg` :rtype: ``str`` )r0 pkg_by_name)r4pkgrrrrlFs zPolicy.pkg_by_namecCsBt\}}}}}}||_||_||_|ddk|_||_dS)Nr`ZSMP)r$unamesystemhostnamereleaserSZsmpmachine)r4roZnoderqversionrr_rrrr.QszPolicy._parse_unamecCs ||_dS)z9Set common host data for the Policy to reference N)rT)r4rTrrr set_commonsZszPolicy.set_commonscCs|tjd<dS)NPATH)osenviron)r4pathrrr _set_PATH_szPolicy._set_PATHcCs||jdSrI)rzrvrBrrrr2bszPolicy.set_exec_pathcCs tdkS)zThis method should return true if the user calling the script is considered to be a superuser :returns: ``True`` if user is superuser, else ``False`` :rtype: ``bool`` r)rwgetuidrBrrris_rooteszPolicy.is_rootcCsdS)zRReturns the string name of the hashlib-supported checksum algorithm to useZsha256rrBrrrget_preferred_hash_namenszPolicy.get_preferred_hash_namecCs|d|d|dtddtdd|dd d d d d }|d}|d|D](\}}|jdd|d|dddqhdS)Nz SoS PolicieszPolicies help govern how SoS operates on across different distributions of Linux. They control aspects such as plugin enablement, $PATH determination, how/which package managers are queried, default upload specifications, and more.z0When SoS intializes most functions, for example z sos reportz and z sos collecta, one of the first operations is to determine the correct policy to load for the local system. Policies will determine the proper package manager to use, any applicable container runtime(s), and init systems so that SoS and report plugins can properly function for collections. Generally speaking a single policy will map to a single distribution; for example there are separate policies for Debian, Ubuntu, RHEL, and Fedora.zRIt is currently not possible for users to directly control which policy is loaded.z*The Google Cloud-Optimized OS distributionzThe Debian distributionz=Red Hat family distributions, not necessarily including forkszUbuntu/Canonical distributions)z policies.coszpolicies.debianzpolicies.redhatzpolicies.ubuntuzSee Alsoz9For more information on distribution policies, see below  z>8z<20z<30F)newline)Z set_titleadd_textrZ add_sectionitems)r7sectionZpolsZseealsoZpolvaluerrr display_helpss2   zPolicy.display_helpcCs|s |s dS|r&|jtd||r~|jtd|d|jtdt|j|jtdt|jjn|jtd|d|r|jd|d ||jtd d S) ahDisplay final information about a generated archive :param archive: The name of the archive that was generated :type archive: ``str`` :param directory: The build directory for sos if --build was used :type directory: ``str`` :param checksum: The checksum of the archive :type checksum: ``str`` :param archivestat: stat() information for the archive :type archivestat: `os.stat_result` :param map_file: If sos clean was invoked, the location of the mapping file for this run :type map_file: ``str`` Fz3 A mapping of obfuscated elements is available at z3 Your sos report has been generated and saved in:  z Size z Owner z3Your sos report build tree has been generated in: r~ z7 Please send this file to your support representative. N) r-infortrst_sizerst_uidpw_namer})r4archiveZ directoryZchecksumZ archivestatZmap_filerrrdisplay_resultss>zPolicy.display_resultscCsn|jdjrd}nd}d}|j|j|j||j|jd|d}d}|D]}|t||dd d }qN|S) a_This method is used to prepare the preamble text to display to the user in non-batch mode. If your policy sets self.os_release_name, that text will be substituted accordingly. You can also override this method to do something more complicated. :returns: Formatted banner message string :rtype: ``str`` rHz,Changes CAN be made to system configuration.z0No changes will be made to system configuration.Htmpdir)os_release_namevendor vendor_urls vendor_textr changes_textrF)Zreplace_whitespacer) rTZallow_system_changesmsgrr_fmt_vendor_urlsr splitlinesr)r4rwidthZ_msgZ_fmtlinerrrget_msgs  zPolicy.get_msgcs.tdd|jDdfdd|jDS)zFormats all items in the ``vendor_urls`` class attr into a usable string for the banner message. :returns: Formatted string of URLS :rtype: ``str`` css|]}t|dVqdS)rN)ra)rMvrrrrOrPz*Policy._fmt_vendor_urls..rc3s.|]&}d|ddd|dVqdS)rr`` objects specifying the command line defaults. :param presets: dictionary of presets to add or replace :param replace: replace presets rather than merge new presets. N)presetsupdate)r4rreplacerrrr3szPolicy.register_presetscCs(|jD]\}}||kr |Sq dS)zFind a preset profile matching the specified preset string. :param preset: a string containing a preset profile name. :returns: a matching PresetProfile. N)rr)r4presetmatchrrrr find_preset s zPolicy.find_presetcCs |jtS)zReturn a ``PresetDefaults`` object matching the runing host. Stub method to be implemented by derived policy classes. :returns: a ``PresetDefaults`` object. )rrrBrrr probe_presetszPolicy.probe_presetc Cs(|p|j}tj|sdSt|D]}tj||}t|ddB}zt|}Wn"t ytYWdq$Yn0Wdn1s0Y| D]}t |t d}||}t |vr|t nd|_t|vr|tnd|_t|vr|tD]}t|j||t|qd|_||j|<qq$dS)zLoad presets from disk. Read JSON formatted preset data from the specified path, or the default location at ``/etc/sos/presets.d``. :param presets_path: a directory containing JSON presets. Nzutf-8)encoding)optsrF) presets_pathrwryexistslistdirrVopenjsonr* ValueErrorkeysrrrdescr noter setattrrbuiltinr) r4rZ preset_pathZpfZ preset_datarpddataargrrr load_presets#s(   4   zPolicy.load_presetscCs\|j}|std||jvr,td|dt||||d}d|_||j|j<||dS)zAdd a new on-disk preset and write it to the configured presets path. :param preset: the new PresetDefaults to add zPreset name cannot be emptyzA preset with name 'z' already exists)rrrrFN)rrrrrrwrite)r4rrrrrrrrr add_presetCs  zPolicy.add_presetcCs\|r||jvrtd|d|j|}|jr@td|jd||j|j|dS)NzUnknown profile: ''zCannot delete built-in preset ')rrrrdeleterpop)r4rrrrr del_presetVs  zPolicy.del_preset)NTN)r)F)NN)F)N)r)5__name__ __module__ __qualname____doc__rtrrZos_release_fileZ os_release_idrrrrvrXrrrrrAr5 classmethodr"propertyr@r9rCrDrFr\r]r_rdrirjrkrlr.rurzr2r|r}rrrrr3rrrrrrrrrrr8sd2     5     ) 9   r)'r,rwr$rYrr^rJrLr#pwdrtextwraprZ sos.presetsrrrrrr r Zsos.policies.package_managersr Z sos.utilitiesr r rrZsos.report.pluginsrrZ sos.optionsrr'rrtrr*rrrrrs*  $