
    ([Qh>                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZ dZdZdZ G d de      Z G d de      Zy)    N)_sos)Policy)
InitSystem)SystemdInit)CrioContainerRuntime)PodmanContainerRuntime)DockerContainerRuntime)LxdContainerRuntime)	shell_outis_executableboldsos_get_command_outputz/etc/os-release	containerHOSTc                   &    e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZdZd	ZdZdZ	 	 d! fd
	Zed        Zed"d       Zd Zd Zd Zd Zd Zd Zed        Zed        Z ed        Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d#dZ*d Z+d  Z, xZ-S )$LinuxPolicyzfThis policy is meant to be an abc class that provides common
    implementations used in Linux distrosNonez/bin:/sbin:/usr/bin:/usr/sbinN dockerFz/usr/binzsos-collector-tmpc                    t         |   |||       |r|| _        n| j                         xs d| _        | j	                          ||| _        nJt        j                  j                  d      rt        | j                        | _        nt               | _        i | _        | j                  r$t        |       t        |       t        |       t!        |       g}|D ]  }|j#                         s|| j                  |j$                  <   |j$                  | j&                  k(  r&| j                  |j$                     | j                  d<   | j                  |j$                     j)                           | j                  rRd| j                  vrCt+        | j                  j-                               }| j                  |d      | j                  d<   y y y y )N)sysrootprobe_runtimeremote_exec/z/run/systemd/system/)chroot)policydefaultr   )super__init__r   _container_initinit_kernel_modulesinit_systemospathisdirr   r   runtimesr   r   r	   r   r
   check_is_activenamedefault_container_runtimeload_container_infolistkeys)	selfr   initr   r   _crunruntimeidx	__class__s	           ?/usr/lib/python3/dist-packages/sos/policies/distros/__init__.pyr   zLinuxPolicy.__init__;   s   '4%0 	 	2 "DL//18SDL  "#DWW]]12*$,,?D)|D&d3&d3$D1#40	E ! F**,29DMM',,/||t'E'EE37==3Ni0MM',,/CCEF }}$--!? 4==--/0+/==Q+@i(	 "@}     c                 
    ddgS )Nz/etc/passwdz/etc/shadow )clss    r3   set_forbidden_pathszLinuxPolicy.set_forbidden_pathsd   s     
 	
r4   c                      fd}|r ||      S t         j                  j                   j                        rqt         j                  j	                   j                        t         j                  j	                  t         j                  j                   j                              k(  ryt        t        dd      5 } ||j                               cddd       S # 1 sw Y   yxY w)|
        This function is responsible for determining if the underlying system
        is supported by this policy.
        c                    j                   g}j                  r|j                  j                         | j                         D ]@  }|j	                  d      s|j                  d      dd  d   j                  d      }||v s@ y y)N)zNAME=zID==   r   z"'TF)os_release_nameos_release_idappend
splitlines
startswithsplitstrip)content_matchesline_distror7   s       r3   _check_releasez)LinuxPolicy.check.<locals>._check_releaseq   s    ++,H   1 12**, $??#34"jjoab1!4::5AG(*#	$
 r4   Trutf-8encodingN)	r#   r$   isfileos_release_filebasenamerealpathopen
OS_RELEASEread)r7   remoterI   fs   `   r3   checkzLinuxPolicy.checkk   s    		 !&)) GGNN3../GGS00177##BGG$4$4S5H5H$IJK*cG4 	,!!&&(+	, 	, 	,s   =CC&c                     | j                   S N)releaser-   s    r3   kernel_versionzLinuxPolicy.kernel_version       ||r4   c                     | j                   S rY   )hostnamer[   s    r3   	host_namezLinuxPolicy.host_name   s    }}r4   c                     | j                   S rY   )smpr[   s    r3   is_kernel_smpzLinuxPolicy.is_kernel_smp   s    xxr4   c                     | j                   S rY   )machiner[   s    r3   get_archzLinuxPolicy.get_arch   r]   r4   c                 "    | j                         S )z)Returns the name usd in the pre_work step)r`   r[   s    r3   get_local_namezLinuxPolicy.get_local_name   s    ~~r4   c                 0    t        j                  dd|      S )Nz[^-a-z,A-Z.0-9]r   )resub)r-   r(   s     r3   sanitize_filenamezLinuxPolicy.sanitize_filename   s    vv("d33r4   c                     | t         k(  r| j                  |       y |j                  | j                   d       | j	                  |       y )Nz Distribution Policy)r   display_self_help	set_titler>   display_distro_helpr7   sections     r3   display_helpzLinuxPolicy.display_help   sC    +!!'*!4!4 55IJK##G,r4   c                 H    |j                  d       |j                  d       y )NzSoS Distribution PolicieszDistributions supported by SoS will each have a specific policy defined for them, to ensure proper operation of SoS on those systems.)ro   add_textrq   s     r3   rn   zLinuxPolicy.display_self_help   s$    56	
r4   c           	      `   | j                   r8| j                   t        j                   ur|j                  | j                          n|j                  d        | d d d      }|j                  d|j                          |j                  d|j                   d       |j                  d|j
                   d       |j                  d      }| j                  D ]$  }|j                  dd	|d
   d|d   dd       & |j                  d      }|j                  t        dd	dddddd      d       |j                  j                         D ]W  \  }}dj                  |j                  j                               }|j                  dd	|d|j                  d|dd       Y y )Nz;
Detailed help information for this policy is not availableFzDefault --upload location: zDefault container runtime: )newlinez $PATH used when running report: zReference URLs z>8r   z<30r=   z<40z#Presets Available With This Policy
zPreset Namez<20Descriptionz<45zEnabled Options)__doc__r   ru   _upload_urlr)   PATHadd_sectionvendor_urlsr   presetsitemsjoinoptsto_argsdesc)	r7   rr   _polrefsecurlpresecpresetvalue_optss	            r3   rp   zLinuxPolicy.display_distro_help   s   ;;3;;k.A.AAS[[)N 4u%)$*:*:);<	
 	)$*H*H)IJ 	 	
 	.tyyk: 	 	

 $$%56?? 	PCOOs2hs1vcl3q6#,?OO	P $$%KLr(=-mC-@$S)+  	 	
 "\\//1 	MFEHHUZZ//12EOOr(6#,uzz#&6uSkB  	r4   c                    t         t        j                  v rt        j                  t            dv rd| _        t        t        j                  v rut        j                  t           syt        j
                  j                  t        j                  t           | j                  z         | _        t        j                  t           S y)zCheck if sos is running in a container and perform container
        specific initialisation based on ENV_HOST_SYSROOT.
        )r   ocipodmanTN)ENV_CONTAINERr#   environ_in_containerENV_HOST_SYSROOTr$   abspath_tmp_dirr[   s    r3   r    zLinuxPolicy._container_init   s     BJJ&zz-(,GG%)"#rzz1::&67#$&GGOO

#34t}}D%DM ::&677r4   c                    g | _         t        j                         j                  }t	        dd| j
                        j                         }| j                   j                  |dd D cg c]#  }|j                         d   j                         % c}       | j                  d| d      }	 t        |dd	
      5 }|D ]C  }|j                  d      d   j                  d      d   }| j                   j                  |       E 	 ddd       ddd}d| d| df}	|	D ]6  }
| j                  |
      }
t        j                   j#                  |
      s4|
} n | j                  j                  d       yg }		 t        |dd	
      5 }|D ]*  }d|v s|	j                  |j                  d      d          , 	 ddd       |j%                         D ]%  \  }}||	v s| j                   j                  |       ' yc c}w # 1 sw Y   xY w# t        $ r)}| j                  j                  d|        Y d}~(d}~ww xY w# 1 sw Y   xY w# t        $ r(}| j                  j                  d|        Y d}~d}~ww xY w)zxObtain a list of loaded kernel modules to reference later for plugin
        enablement and SoSPredicate checks
        lsmodr   )timeoutr   r=   Nz/usr/lib/modules/z/modules.builtinrJ   rK   rL   r   z.koz Unable to read kernel builtins: CONFIG_NET_DEVLINKCONFIG_BLK_DEV_DM)devlinkdm_modz/boot/config-z/lib/modules/z/configz#Unable to find booted kernel configz=yz%Unable to read booted kernel config: )kernel_modsr#   unamerZ   r   r   rA   extendrC   rD   join_sysrootrR   r@   IOErrorsoslogwarningr$   existsr   )r-   rZ   linesrG   builtinsmfilekmoderrconfig_stringskconfigskconfigbooted_configkfilebuiltinr   s                  r3   r!   zLinuxPolicy.init_kernel_modules   s    ((*$$ '1T\\BMMO05ab	!
(,DJJLO!!#!
 	
 $$y(89
	Jhg6 2%! 2D::c?2.44U;A>D$$++D122 ,)
 G9%G9G,
   	G''0Gww~~g& '		 KK EF	OmS7; =u! =Dt| 

4(8(;<== -224 	1NGU   ''0	1]!
2 2  	JKK"B3% HII	J4= =  	OKK"Gu MNN	Osm   ((G7-H ;A	G<H 7I	 
H=%H=6I	 <HH 	H:H55H:=II	 		I:I55I:c                     | j                   rH| j                   dk7  r9t        j                  j                  | j                   |j	                  d            }|S )Nr   )r   r#   r$   r   lstrip)r-   r$   s     r3   r   zLinuxPolicy.join_sysroot#  s:    <<DLLC/77<<dkk#.>?Dr4   c                     | j                   d   }|j                  r| j                          | j                  |      | _        y )Ncmdlineopts)commonslow_priority_configure_low_priorityprompt_for_case_idcase_idr-   cmdline_optss     r3   pre_workzLinuxPolicy.pre_work(  s;     ||M2$$((* ..|<r4   c                     |j                   s1|j                  s%|j                  st        t	        d            |_        |j                  r|j                  nd| _        | j                  S )NzNOptionally, please enter the case id that you are generating this report for: r   )batchquietr   input_r   s     r3   r   zLinuxPolicy.prompt_for_case_id3  sc    !!""''', 5 6($
    $++&( 	 ||r4   c                     t        j                         }t        d      r_t        d| d      }|d   dk(  r| j                  j                  d       nFd|d	    d
|d    d}| j                  j                  |       n| j                  j                  d       	 t        j                  d       | j                  j                  d       y# t        $ r(}| j                  j                  d|        Y d}~yd}~ww xY w)ay  Used to constrain sos to a 'low priority' execution, potentially
        letting individual policies set their own definition of what that is.

        By default, this will attempt to assign sos to an idle io class via
        ionice if available. We will also renice our own pid to 19 in order to
        not cause competition with other host processes for CPU time.
        ionicezionice -c3 -p    )r   statusr   zSet IO class to idlez Error setting IO class to idle: outputz (exit code )zNWarning: unable to constrain report to idle IO class: ionice is not available.   zSet niceness of report to 19z%Error setting report niceness to 19: N)r#   getpidr   r   r   infoerrorui_logr   nice	Exception)r-   _pidretmsgr   s        r3   r   z#LinuxPolicy._configure_low_priority@  s     yy{"( 'C 8}!  !789#h- I%%(]O16!!#&KK+
	MGGBKKK;< 	MKK EcUKLL	Ms   0C 	C=C88C=c                      y)zIf sos report commands need to always be prefixed with something,
        for example running in a specific container image, then it should be
        defined here.

        If no prefix should be set, return an empty string instead of None.
        r   r6   r[   s    r3   set_sos_prefixzLinuxPolicy.set_sos_prefix_  s     r4   c                      y)zcIf a host requires additional cleanup, the command should be set and
        returned here
        r   r6   r[   s    r3   set_cleanup_cmdzLinuxPolicy.set_cleanup_cmdh  s     r4   c                      y)aI  Returns the command that will create the container that will be
        used for running commands inside a container on hosts that require it.

        This will use the container runtime defined for the host type to
        launch a container. From there, we use the defined runtime to exec into
        the container's namespace.

        :param image:   The name of the image if not using the policy default
        :type image:    ``str`` or ``None``

        :param auth:    The auth string required by the runtime to pull an
                        image from the registry
        :type auth:     ``str`` or ``None``

        :param force_pull:  Should the runtime forcibly pull the image
        :type force_pull:   ``bool``

        :returns:   The command to execute to launch the temp container
        :rtype:     ``str``
        r   r6   )r-   imageauth
force_pulls       r3   create_sos_containerz LinuxPolicy.create_sos_containero  s    * r4   c                 8    | j                    d| j                   S )a  Restarts the container created for sos collect if it has stopped.

        This is called immediately after create_sos_container() as the command
        to create the container will exit and the container will stop. For
        current container runtimes, subsequently starting the container will
        default to opening a bash shell in the container to keep it running,
        thus allowing us to exec into it again.
        z start container_runtimesos_container_namer[   s    r3   restart_sos_containerz!LinuxPolicy.restart_sos_container  s#     (()1H1H0IJJr4   c                 Z    | j                   r| j                    d| j                   d| S |S )a  Returns the command that allows us to exec into the created
        container for sos collect.

        :param cmd: The command to run in the sos container
        :type cmd: ``str``

        :returns: The command to execute to run `cmd` in the container
        :rtype: ``str``
        z exec rx   r   )r-   cmds     r3   format_container_commandz$LinuxPolicy.format_container_command  s<     !!--.fT5L5L4MQe 
r4   )NNTNr   )NNF).__name__
__module____qualname__rz   vendorr|   r.   rO   r>   r?   r{   r)   _preferred_hash_namecontainerizedcontainer_imagesos_path_stripsos_pkg_namesos_bin_pathr   container_version_commandcontainer_authfiler   classmethodr8   rW   r\   r`   rc   rf   rh   rl   rs   rn   rp   r    r!   r   r   r   r   r   r   r   r   r   __classcell__)r2   s   @r3   r   r   #   s.   -F*DD OOMK (MONLL, $>B!'AR 
 
 , ,8 4 - - 
 
 ( (T"91v
	=M>.
Kr4   r   c                   0    e Zd ZdZdgZdZdZedd       Zy)GenericLinuxPolicyzThis Policy will be returned if no other policy can be loaded. This
    should allow for IndependentPlugins to be executed on any system)zUpstream Projectz https://github.com/sosreport/sosSoSa  SoS was unable to determine that the distribution of this system is supported, and has loaded a generic configuration. This may not provide desired behavior, and users are encouraged to request a new distribution-specifc policy at the GitHub project above.
c                     t         )r:   )NotImplementedError)r7   rU   s     r3   rW   zGenericLinuxPolicy.check  s
     "!r4   Nr   )	r   r   r   rz   r~   r   vendor_textr   rW   r6   r4   r3   r   r     s3    H LLKF<K " "r4   r   )r#   rj   sosr   r   sos.policiesr   sos.policies.init_systemsr   !sos.policies.init_systems.systemdr   sos.policies.runtimes.crior   sos.policies.runtimes.podmanr   sos.policies.runtimes.dockerr	   sos.policies.runtimes.lxdr
   sos.utilitiesr   r   r   r   rS   r   r   r   r   r6   r4   r3   <module>r      s\    
 	   0 9 ; ? ? 93 3 
 }& }@" "r4   