o id_@sDddlZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z m Z mZmZmZddlmZmZddlmZddlmZddlmZzddlZdZWn ey[d ZYnwd Zd Zd Z Gd ddeZ!dZ"dZ#dZ$Gddde!Z%Gddde%Z&Gddde%Z'Gddde%Z(Gddde'Z)Gddde!Z*dS)N) RedHatPlugin) RHEL_PRESETSATOMIC_PRESETSRHVRHELCBRHOSPRHOCPRH_CFME RH_SATELLITEATOMIC) LinuxPolicyENV_HOST_SYSROOT)RpmPackageManager)bold)_sosTFz/etc/os-releasezRed Hat Enterprise LinuxAtomiccseZdZdZdZddgZdZdZdZdZ dZ dZ d Z d Z d Z dfd d ZedddZefddZddZddZddZZS) RedHatPolicyRed Hat)zDistribution Websitezhttps://www.redhat.com/)zCommercial Supportzhttps://access.redhat.com/z/var/tmpFz/opt/rhfriendlyNpodmansosz /usr/sbinTcstt|j||||dd|_t|j|d|_|jtg7_|jj |_ |j s2t j dt d||j |_|jr@d|_nd|_|jtjd7_|jtjd 7_|js^||dS) Nsysrootinit probe_runtime remote_execF)chrootrz'Could not obtain installed package listz/usr/sbin:/usr/bin:/root/binz'/sbin:/bin:/usr/sbin:/usr/bin:/root/binz/usr/local/binz/usr/local/sbin)superr__init__usrmoverrpackage_managervalid_subclassesrpackagespkgssysstderrwriteexit check_usrmovePATHospathsepr set_exec_path load_presetsselfrrrr __class__=/usr/lib/python3/dist-packages/sos/policies/distros/redhat.pyr 5s,    zRedHatPolicy.__init__cCdS)aThis 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. If `remote` is provided, it should be the contents of a remote host's os-release, or comparable, file to be used in place of the locally available one. Fr4clsremoter4r4r5checkTs zRedHatPolicy.checkcsl|turtt||dS|dttttd}|D]}td|}|jd d|||j ddqdS)NzThis policy is a building block for all other Red Hat family distributions. You are likely looking for one of the distributions listed below. )centosrhel redhatcoreosfedoraz policies.%sz{:>8}{:<35}{:<30} F)newline) rrdisplay_distro_helpadd_text CentOsPolicy RHELPolicyRedHatCoreOSPolicy FedoraPolicyrformatdistro)r9sectionsubssubcsublnr2r4r5rB`s$ z RedHatPolicy.display_distro_helpcCs@d|vrtjdotjdS|dd}|ddkrdSdS) aqTest whether the running system implements UsrMove. If the 'filesystem' package is present, it will check that the version is greater than 3. If the package is not present the '/bin' and '/sbin' paths are checked and UsrMove is assumed if both are symbolic links. :param pkgs: a packages dictionary filesystemz/binz/sbinversionr3TF)r,pathislink)r1r%filesys_versionr4r4r5r*ys  zRedHatPolicy.check_usrmovecCs2g}dd}|jr|D] }|||q |S|S)a2Mangle paths for post-UsrMove systems. If the system implements UsrMove, all files will be in '/usr/[s]bin'. This method substitutes all the /[s]bin references in the 'files' list with '/usr/[s]bin'. :param files: the list of package managed files cSs:ddg}||vr|tjd|ddfStdd|fS)Nz/bin/rpmz /bin/mailxz/usrrz (^)(/s?bin)z\1/usr\2)r,rQjoinresub)rQ skip_pathsr4r4r5transform_pathsz8RedHatPolicy.mangle_package_path..transform_path)r!extend)r1filespathsrXfr4r4r5mangle_package_paths z RedHatPolicy.mangle_package_pathcCs|s|jS|SN)_tmp_dir)r1 opt_tmp_dirr4r4r5 get_tmp_dirszRedHatPolicy.get_tmp_dirNNTNr6)__name__ __module__ __qualname__rIvendor vendor_urlsr_ _in_containerdefault_scl_prefix name_pattern upload_url upload_userdefault_container_runtime sos_pkg_name sos_bin_pathr classmethodr;rBr*r]ra __classcell__r4r4r2r5r$s2  ra[ Any information provided to %(vendor)s will be treated in accordance with the published support policies at: %(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. No changes will be made to system configuration. zhttps://api.access.redhat.comzsftp://sftp.access.redhat.comcseZdZdZeZdZededZ e Z dZ  dfdd Z ed d d Zfd dZddZddZddZddZfddZfddZddZddZZS)!rEat The RHEL policy is used specifically for Red Hat Enterprise Linux, of any release, and not forks or derivative distributions. For example, this policy will be loaded for any RHEL 8 installation, but will not be loaded for CentOS Stream 8 or Red Hat CoreOS, for which there are separate policies. Plugins activated by installed packages will only be activated if those packages are installed via RPM (dnf/yum inclusive). Packages installed by other means are not considered by this policy. By default, --upload will be directed to using the SFTP location provided by Red Hat for technical support cases. Users who provide login credentials for their Red Hat Customer Portal account will have their archives uploaded to a user-specific directory. If users provide those credentials as well as a case number, --upload will instead attempt to directly upload archives to the referenced case, thus streamlining the process of providing data to technical support engineers. If either or both of the credentials or case number are omitted or are incorrect, then a temporary anonymous user will be used for upload to the SFTP server, and users will need to provide that information to their technical support engineer. This information will be printed at the end of the upload process for any sos report execution. raThis command will collect diagnostic and configuration information from this %(distro)s system and installed applications. An archive containing the collected information will be generated in %(tmpdir)s and may be provided to a %(vendor)s support representative. %(vendor_text)s postNTc&tt|j||||d|tdSNr)rrEr register_presetsrr0r2r4r5r zRHELPolicy.__init__r6cCs|r|j|vStjtsdSttd.}|D]"}|dr9|d\}}|d}||jr9WddSqWddS1sEwYdS)aTest to see if the running host is a RHEL installation. Checks for the presence of the "Red Hat Enterprise Linux" release string at the beginning of the NAME field in the `/etc/os-release` file and returns ``True`` if it is found, and ``False`` otherwise. :returns: ``True`` if the host is running RHEL or ``False`` otherwise. FrNAME=z"'NT) rIr,rQexists OS_RELEASEopen startswithsplitstrip)r9r:r\linenamevaluer4r4r5r;s$       zRHELPolicy.checkcsv|jdjrdS|jdjrtt|dS|s9|jr'tt d|_dSt |_|j dtt d|_dSdS)N cmdlineoptszVEnter your Red Hat Customer Portal username for uploading [empty for anonymous SFTP]: z&No case id provided, uploading to SFTPzYEnter your Red Hat Customer Portal username for uploading to SFTP [empty for anonymous]: ) commonsrmrlrrEprompt_for_upload_userget_upload_usercase_idinput_ RH_SFTP_HOSTui_loginfor1r2r4r5rs     z!RHELPolicy.prompt_for_upload_usercCsJ|jr|jS|jdjr|jdjS|jdjdkrtSd}t||jS)Nrsftpz /support/v1/cases/%s/attachments)rlrupload_protocolr RH_API_HOSTr)r1 rh_case_apir4r4r5get_upload_urls  zRHELPolicy.get_upload_urlcCs|tr dddSiS)Nfalsezno-cache) isPrivatez cache-control)rrrrr4r4r5_get_upload_headers&s zRHELPolicy._get_upload_headerscCs*|tr dS|trdS|jS)NzRed Hat Customer PortalzRed Hat Secure FTP)rrrrrlrr4r4r5get_upload_url_string+s z RHELPolicy.get_upload_url_stringcCs>|jdd}|jrd|j|f}|jrtj|j|}|S)zThe RH SFTP server will only automatically connect file uploads to cases if the filename _starts_ with the case number /z%s_%s)upload_archive_namerrupload_directoryr,rQrT)r1fnamer4r4r5_get_sftp_upload_name2s z RHELPolicy._get_sftp_upload_namec sZtdd|vrtt|Ststdd}d}td}| r[| r[| }t j ||dd}|jdkrF| }t|jd }n|jd |jd ||jd |sd di}t j |t|dd}|jdkrt|j}|d}|d }|jtd|dn|jd|jd ||r|rtt|j||dStd)zOverride the base upload_sftp to allow for setting an on-demand generated anonymous login for the RH SFTP server if a username and password are not given z//rzPpython3-requests is not installed and is required for obtaining SFTP auth token.Nz/support/v2/sftp/token )authtimeouttokenz$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous. isAnonymousT)datarusernamezUser z^ used for anonymous upload. Please inform your support engineer so they may retrieve the data.z)DEBUG: anonymous request failed (status: )userpasswordz1Could not retrieve valid or anonymous credentials)rrrrrE upload_sftpREQUESTS_LOADED Exceptionrrget_upload_passwordget_upload_https_authrequestsrt status_codejsonloadstextrdebugerrordumpsrr) r1_token_userurlrretadataanonrespr2r4r5r=sV     zRHELPolicy.upload_sftpcsz|jr|jtr|r|st|_tt||}W|St yHd}|jts/|j t dtt|_tt||}Y|Sw)zOverride the base upload_archive to provide for automatic failover from RHCP failures to the public RH dropbox Fz1Upload to Red Hat Customer Portal failed. Trying ) rlrrrrrrrEupload_archiverrrr)r1archiveuploadedr2r4r5rts,  zRHELPolicy.upload_archivecCsBz|jd}|j|dd}t|dWSty YdSw)Nzredhat-release*rrOF)r"all_pkgs_by_name_regexr%intr)r1rrpkgnamer4r4r5 dist_versions  zRHELPolicy.dist_versioncCsdD]}|jd|dr|tSq|ddur!|tS|ddur-|tS|ddur9|tS|ddusG|ddurL|tS|t S) N)rescue emergencyz %s.targetFzsatellite-commonz rhosp-releasecfmez ovirt-enginevdsm) init_system is_running find_presetr pkg_by_namer rr rr)r1targetr4r4r5 probe_presets     zRHELPolicy.probe_presetrbrc)rdrerf__doc__RHEL_RELEASE_STRrIrgrdisclaimer_textmsgr _upload_url_upload_methodr rqr;rrrrrrrrrrrr4r4r2r5rEs2     7 rEc@seZdZdZdZdgZdS)rDCentOSCommunity Websitezhttps://www.centos.org/NrdrerfrIrgrhr4r4r4r5rD rDcsreZdZdZededZdZdZdZ dZ dZ  dfd d Z e dd dZddZdddZddZZS)RedHatAtomicPolicyzRed Hat Atomic HostThis command will collect diagnostic and configuration 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. rsTdockerz.registry.access.redhat.com/rhel7/support-tools/host rpm -q sosNcrurv)rrr rwrr0r2r4r5r rxzRedHatAtomicPolicy.__init__r6cCs~|r|j|vSd}ttjvr|Stjtt}tj|sdSzt|d D]}|t |vO}q)W|St y>Y|Sw)NFry) rIrr,environr}rQr|r~read splitlinesATOMIC_RELEASE_STRIOError)r9r:atomic host_releaserr4r4r5r;s"    zRedHatAtomicPolicy.checkcCs|dr |tS|tS)Nzatomic-openshift)rrr r rr4r4r5rs   zRedHatAtomicPolicy.probe_presetFcC6d}|p|j}|r dnd}|j|j|j|||pddSNa{runtime} run -di --name {name} --privileged --ipc=host --net=host --pid=host -e HOST=/host -e NAME={name} -e IMAGE={image} {pull} -v /run:/run -v /var/log:/var/log -v /etc/machine-id:/etc/machine-id -v /etc/localtime:/etc/localtime -v /:/host {auth} {image}z --pull=alwaysr6)runtimerimagepullrcontainer_imagerHcontainer_runtimesos_container_namer1rr force_pull_cmd_image_pullr4r4r5create_sos_container  z'RedHatAtomicPolicy.create_sos_containercCr7)Nz#docker rm --force sos-collector-tmpr4rr4r4r5set_cleanup_cmdsz"RedHatAtomicPolicy.set_cleanup_cmdrbrcNNF)rdrerfrIrrr containerzedrrsos_path_stripcontainer_version_commandr rqr;rrrrrr4r4r2r5rs(   rcszeZdZdZdZededZdZdZ dZ dZ d Z d Z  dfd d ZedddZddZdddZddZZS)rFaB Red Hat CoreOS is a containerized host built upon Red Hat Enterprise Linux and as such this policy is built on top of the RHEL policy. For users, this should be entirely transparent as any behavior exhibited or influenced on RHEL systems by that policy will be seen on RHCOS systems as well. The one change is that this policy ensures that sos collect will deploy a container on RHCOS systems in order to facilitate sos report collection, as RHCOS discourages non-default package installation via rpm-ostree which is used to maintain atomicity for RHCOS nodes. The default container image used by this policy is the support-tools image maintained by Red Hat on registry.redhat.io. Note that this policy is only loaded when sos is directly run on an RHCOS node - if sos collect uses the `oc` transport (the default transport that will be attempted by the ocp cluster profile), then the policy loaded inside the launched pod will be RHEL. Again, this is expected and will not impact how sos report collections are performed. zRed Hat CoreOSrrsTrz®istry.redhat.io/rhel8/support-toolsrrz/var/lib/kubelet/config.jsonNctt|j||||ddSrv)rrFr r0r2r4r5r  zRedHatCoreOSPolicy.__init__r6cCsl|rd|vSd}ttjvr|Stjtt}zt|dD]}|d|vO}q W|Sty5Y|Sw)NCoreOSFryzRed Hat Enterprise Linux CoreOS)rr,rr}r~rrr)r9r:coreosrrr4r4r5r;s  zRedHatCoreOSPolicy.checkcCs |tSr^)rr rr4r4r5r0s zRedHatCoreOSPolicy.probe_presetFcCrrrrr4r4r5r5rz'RedHatCoreOSPolicy.create_sos_containercCs d|jS)Nzpodman rm --force %s)rrr4r4r5rCs z"RedHatCoreOSPolicy.set_cleanup_cmdrbrcr)rdrerfrrIrrr containerizedrrrrcontainer_authfiler rqr;rrrrrr4r4r2r5rFs,   rFc@seZdZdZdZdgZdS)CentOsAtomicPolicyzCentOS Atomic HostrrNrr4r4r4r5rGrrcsLeZdZdZdZdZddgZ  dfdd Zedd d Z d dZ Z S)rGaj The policy for Fedora based systems, regardless of spin/edition. This policy is based on the parent Red Hat policy, and thus will only check for RPM packages when considering packaged-based plugin enablement. Packages installed by other sources are not considered. There is no default --upload location for this policy. If users need to upload an sos report archive from a Fedora system, they will need to provide the location via --upload-url, and optionally login credentials for that location via --upload-user and --upload-pass (or the appropriate environment variables). Fedorazthe Fedora Project)rzhttps://fedoraproject.org/)zCommunity Forumsz%https://discussion.fedoraproject.org/NTcrrv)rrGr r0r2r4r5r brzFedoraPolicy.__init__r6cCs|r|j|vStjdS)zXThis method checks to see if we are running on Fedora. It returns True or False.z/etc/fedora-release)rIr,rQisfiler8r4r4r5r;hs  zFedoraPolicy.checkcCs$|dp |dd}t|dS)Nzfedora-releasezfedora-release-.*rrO)rrr)r1pkgr4r4r5fedora_versionrs   zFedoraPolicy.fedora_versionrbrc) rdrerfrrIrgrhr rqr;rrrr4r4r2r5rGMs   rG)+rr,r&rUsos.report.pluginsrsos.presets.redhatrrrrrrr r r r sos.policies.distrosr r!sos.policies.package_managers.rpmr sos.utilitiesrrrrrr ImportErrorr}rrrrrrrErDrrFrrGr4r4r4r5s>  0     qCT