U
    -_g$O                     @   s   d dl Z d dlZd dlZd dlZ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 G dd dZG dd dZdS )    N)SUPPRESS)datetime)getpass)rmtree)Path)__version__)TarFileArchive)
SoSOptions)TempFileUtil	shell_outc                   @   s   e Zd ZdZdZi ZdZdZdZdZ	dddddddddd	dd
dZ
dddddgZdd Zdd Zdd Zdd Zd7ddZdd Zdd Zedd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd8d+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 ZdS )9SoSComponenta  Any sub-command that sos supports needs to subclass SoSComponent in
    order to be properly supported by the sos binary.

    This class contains the standardized entrypoint for subcommands, as well as
    building out supported options from both globally shared option lists, and
    options supported by that specific subcommand.

    When sos initializes, it will load an unintialized instance of each class
    found within one recursion of the module root directory that subclasses
    SoSComponent.

    If sos is able to match the user-specified subcommand to one that exists
    locally, then that SoSComponent is initialized, logging is setup, and a
    policy is loaded. From there, the component's execute() method takes over.

    Added in 4.0
    ZunsetTFautoz/etc/sos/sos.confN    r   )batchcompression_typeconfig_filedebugencryptencrypt_keyencrypt_passquietthreadstmp_dirsysroot	verbosityzsos_logs/sos.logzsos_reports/manifest.jsonzsos_commands/process/ps_*zsos_commands/selinux/ps_*z+sos_commands/systemd/systemctl_status_--allc              
   C   s  || _ || _|| _d| _d | _d | _d | _d | _zdd l}||j	| 
  W n6 tk
r } ztjd| d W 5 d }~X Y nX t| jd| _| jr|   | j| j |  | _| jrD|  }t | _tj|rt|tjsd| d}|d7 }tj| |  d	 || _!t"j#d
| j!d| _t$| j| _| %  | jd k	r| j&dt' | j&dd(| j | j&dt)*  | j&dd | j&dd | j&dd | j&d| j | j&d| j+ | j&d| j,j- | j.d d S )NFr   z'Notice: Could not set SIGTERM handler: 
arg_defaultsztemporary directory  z"does not exist or is not writable
   zsos.)prefixdirversioncmdlineZ
start_timeZend_timer   Zrun_timeZcompressiontmpdirZtmpdir_fs_typepolicyZ
components)/parserargsr$   exit_processarchiver%   tempfile_utilmanifestsignalSIGTERMget_exit_handler	Exceptionsysstdoutwriter	   _arg_defaultsoptsload_policyload_local_policyupdater   load_optionsconfigure_loggingget_tmpdir_defaultSoSMetadataospathisdiraccessW_OKstderr_exitZsys_tmptempfileZmkdtempr
   _setup_logging	add_fieldr   joinr   Znow	tmpfstyper&   Zos_release_nameadd_section)selfr'   Zparsed_argsZcmdline_argsr-   errr%   msg rM   //usr/lib/python3/dist-packages/sos/component.py__init__Q   sV    &

zSoSComponent.__init__c                 C   s^   z.dd l }|jj| jj| jd| _| jj| _W n tk
rL   | d Y nX | j	 | _
d S )Nr   )r   Zprobe_runtime)Zsos.policiesZpoliciesloadr5   r   
load_prober&   KeyboardInterruptrC   Zis_rootZ_is_root)rJ   sosrM   rM   rN   r7      s    zSoSComponent.load_local_policyc                 C   s   t d S NNotImplementedErrorrJ   rM   rM   rN   execute   s    zSoSComponent.executec                    s    fdd}|S )Nc                    s   d _    d S )NT)r)   rC   )ZsignumframerW   rM   rN   exit_handler   s    z3SoSComponent.get_exit_handler.<locals>.exit_handlerrM   )rJ   rZ   rM   rW   rN   r/      s    zSoSComponent.get_exit_handlerc                 C   s(   |r| j d | j | t|d S )Nr   )ui_logerror
SystemExit)rJ   r\   rL   rM   rM   rN   rC      s    zSoSComponent._exitc                 C   s   | j jrtj| j j}ntddp(d}tddr\tddr\tjtd|d}td| 	 | _
| j
dkrtd	 td
 |S )zIf --tmp-dir is not specified, provide a default location.
        Normally this is /var/tmp, but if we detect we are in a container, then
        use a standardized env var to redirect to the host's filesystem instead
        ZTMPDIRNz/var/tmpZHOSTZ	container/zstat --file-system --format=%T ZtmpfszWARNING: tmp-dir is set to a tmpfs filesystem. This may increase memory pressure and cause instability on low memory systems, or when using --all-logs.   )r5   r   r=   r>   abspathgetenvrG   lstripr   striprH   printtimesleep)rJ   r%   rM   rM   rN   r;      s    


zSoSComponent.get_tmpdir_defaultc                    s6   dd  j   D }|r2t fdd|D S dS )Nc                 S   s   g | ]}| d r|qS )list)
startswith).0orM   rM   rN   
<listcomp>   s     
 z6SoSComponent.check_listing_options.<locals>.<listcomp>c                 3   s   | ]}t  j|V  qd S rT   )getattrr5   )ri   optrW   rM   rN   	<genexpr>   s     z5SoSComponent.check_listing_options.<locals>.<genexpr>F)r5   dictkeysany)rJ   r5   rM   rW   rN   check_listing_options   s    z"SoSComponent.check_listing_optionsc                 C   s   t dS )ziThis should be overridden by each subcommand to add its own unique
        options to the parser
        NrU   )clsr'   rM   rM   rN   add_parser_options   s    zSoSComponent.add_parser_optionsc              	      s   t  j| j| j|jddd}|jdd}| D ]\}}||j ks6|dg dfkr\q6|dkrdD ]>}t	|t	t
||@   rht|| fddt
||D  qh||j| kr6t||| q6|S )	a.  (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

        F)Zpreset_filterr   Nr   )Zenable_pluginsZskip_pluginsZonly_pluginsc                    s   g | ]}| kr|qS rM   rM   )ri   xcommonrM   rN   rk      s    z;SoSComponent.apply_options_from_cmdline.<locals>.<listcomp>)r	   Z	from_argsr'   
parse_argsr$   ro   itemsr   rp   setrl   setattr)rJ   r5   ZcmdoptsZcodictrm   valZooptrM   rv   rN   apply_options_from_cmdline   s     
"z'SoSComponent.apply_options_from_cmdlinec                 C   sj  t | jd}| jjD ]}|jtkrd|_q|| jj| jj	 d| j
ksRd| j
krb|| jjd t dkrtjt d}tj|r||| jj	 | |}d| _t|drf|j| jd kr
| j|j| _| js
tjd	|j d
 | j | _d|_| js| j | _|| jj | |}t| jjdrf| jjjdkrf| | jjj |S )zxCompile arguments loaded from defaults, config files, and the
        command line into a usable set of options
        r   Nz--cleanz--maskcleanr   z.config/sos/sos.confpresetzUnknown preset: 'z'
Tr   ) r	   r4   r'   Z_actionsdefaultr   Zupdate_from_confr(   r   Z	componentr$   r=   getuidr>   rG   r   homeexistsr}   r   hasattrr&   Zfind_presetr1   rB   r3   Zprobe_presetZlist_presetsmerger5   r   set_loggers_verbosity)rJ   r5   ZoptionZuserconfrM   rM   rN   r9      s:    


zSoSComponent.load_optionsc              
   C   sr   z4| j r| j   | jr"| j  | jr2t| j W n8 tk
rl } ztd| d| j  W 5 d }~X Y nX d S )NzFailed to finish cleanup: z
Contents may remain in )r*   cleanupr+   r~   r%   r   r0   rd   )rJ   rK   rM   rM   rN   r   .  s    

zSoSComponent.cleanupc                 C   sb   d}t jdr&t jd| j_d}n t jdrFt jd| j_d}| j| | j| d S )NzFNo encryption environment variables set, archive will not be encryptedZSOSENCRYPTKEYz+Encryption key set via environment variableZSOSENCRYPTPASSz2Encryption passphrase set via environment variable)	r=   environgetr5   r   r   sosloginfor[   )rJ   rL   rM   rM   rN   _set_encrypt_from_env_vars<  s    z'SoSComponent._set_encrypt_from_env_varsc                 C   s   | j jsd }|dkr"td }q|dkr8td| j _q|dkrNtd| j _q|dkr`|   qd | j _d | j _| j	d | j
d	 n|   d S )
N)PKENzLSpecify encryption method [P]assphrase, [K]ey, [E]nv vars, [N]o encryption: r   zSpecify encryption passphrase: r   zSpecify encryption key: r   z@User specified --encrypt, but chose no encryption when prompted.zArchive will not be encrypted)r5   r   inputupperr   r   r   r   r   r   r[   Zwarning)rJ   Z_encrM   rM   rN   _get_encryption_methodH  s"    

z#SoSComponent._get_encryption_methodc                 C   s   | j jr|   | j jp| j j| j j| j jd}|s>| j }tj	| j
|}| j jdkr| j }||| j
| j| j j|| j| j| _n"t|| j
| j| j j|| j| j| _| j| j jdk d S )N)r   keyZpasswordr   r_   )r5   r   r   r   r   r&   Zget_archive_namer=   r>   rG   r%   r   Zget_preferred_archiver   r   r,   r*   r   Z	set_debugr   )rJ   nameZenc_optsZarchive_nameZauto_archiverM   rM   rN   setup_archive`  s4    

    zSoSComponent.setup_archivec                 C   sV   | j  }| jD ]@}t||D ],}tj||}dD ]}| j ||d q8q"qd S )N)z(--upload-pass[\s=]+)\S+z$(--upload-url[\s=]+\S+://.*:)([^@]*)z!(--upload-s3-secret-key[\s=]+)\S+z
\1********)	r*   Zget_archive_pathfiles_with_upload_passwdr   globr=   r>   relpathZdo_file_sub)rJ   Z	_arc_pathr>   frerM   rM   rN   _obfuscate_upload_passwordsz  s    

z(SoSComponent._obfuscate_upload_passwordsc                 C   sJ   t tj}|t d || jjdkr2t j	nt j
 | j| d S )N%(message)sr    )loggingStreamHandlerr1   r2   setFormatter	FormattersetLevelr5   r   DEBUGINFOr[   
addHandler)rJ   Z
ui_consolerM   rM   rN   add_ui_log_to_stdout  s    z!SoSComponent.add_ui_log_to_stdoutc                 C   s\   t | dd r|r| jtj t | dd rX|rJ| jjdkrJ| jtj n| jtj d S )Nflogconsoler    )	rl   r   r   r   r   r5   r   r   ZWARNING)rJ   r   rM   rM   rN   r     s    z"SoSComponent.set_loggers_verbosityc                 C   sh  t d| _| jt j d| _|  sn|  | _t 	| j| _| j
t d | jt j | j| j | jjst 	tj| _| j
t d | | jj | j| j n4t 	tj}|
t d |t j | j| t d| _| j| jjdkrt jnt j |  sR|  | _t 	| j}|
t d | j| | jjsd|   dS )zCreates the log handler that shall be used by all components and any
        and all related bits to those components that need to log either to the
        console or to the log file for that run of sos.
        rS   Nz&%(asctime)s %(levelname)s: %(message)sr   Zsos_uir    )r   Z	getLoggerr   r   r   r   rr   get_temp_fileZsos_log_filer   r   r   r   r   r5   r   r1   r2   r   r   r   rB   ZERRORr[   Zsos_ui_log_filer   )rJ   Zconsole_errZui_fhandlerrM   rM   rN   rE     sB    




zSoSComponent._setup_loggingc                 C   s
   | j  S rT   )r+   newrW   rM   rM   rN   r     s    zSoSComponent.get_temp_file)r   N)r   ) __name__
__module____qualname____doc__Zdescr   r:   r6   rQ   Zroot_requiredr4   r   rO   r7   rX   r/   rC   r;   rr   classmethodrt   r}   r9   r   r   r   r   r   r   r   rE   r   rM   rM   rM   rN   r      s\   8


/3
	.r   c                   @   sV   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	g fddZ
dddZdS )r<   a8  This class is used to record metadata from a sos execution that will
    then be stored as a JSON-formatted manifest within the final tarball.

    It can be extended by adding further instances of SoSMetadata to represent
    dict-like structures throughout the various sos bits that record to
    metadata
    c                 C   s
   i | _ d S rT   _valuesrW   rM   rM   rN   rO     s    zSoSMetadata.__init__c                 c   s   | j  D ]}|d V  q
d S )Nr    )r   ry   rJ   itemrM   rM   rN   __iter__  s    zSoSMetadata.__iter__c                 C   s
   | j | S rT   r   r   rM   rM   rN   __getitem__  s    zSoSMetadata.__getitem__c                 C   s
   | j | S rT   r   )rJ   attrrM   rM   rN   __getattr__  s    zSoSMetadata.__getattr__c                 C   s   || j |< dS )z@Add a key, value entry to the current metadata instance
        Nr   )rJ   Z
field_namecontentrM   rM   rN   rF     s    zSoSMetadata.add_fieldc                 C   s   t  | j|< | j| S )zCAdds a new instance of SoSMetadata to the current instance
        )r<   r   )rJ   Zsection_namerM   rM   rN   rI     s    zSoSMetadata.add_sectionc                 C   s    t |tstd|| j|< dS )zAdd a named list element to the current instance. If content is not
        supplied, then add an empty list that can alter be appended to
        zcontent added must be listN)
isinstancerg   	TypeErrorr   )rJ   Z	list_namer   rM   rM   rN   add_list  s    
zSoSMetadata.add_listNc                 C   s   t j| dd |dS )zConvert contents of this SoSMetdata instance, and all other nested
        instances (sections), into a json-formatted output.

        Used to write manifest.json to the final archives.
        c                 S   s   t | dt| S )Nr   )rl   str)rj   rM   rM   rN   <lambda>      z&SoSMetadata.get_json.<locals>.<lambda>)r   indent)jsondumps)rJ   r   rM   rM   rN   get_json  s    zSoSMetadata.get_json)N)r   r   r   r   rO   r   r   r   rF   rI   r   r   rM   rM   rM   rN   r<     s   r<   )r   r   r=   rD   r1   re   argparser   r   r   Zshutilr   pathlibr   rS   r   Zsos.archiver   Zsos.optionsr	   Zsos.utilitiesr
   r   r   r<   rM   rM   rM   rN   <module>   s&      3