U
    i©¹ds™  ã                   @   sÎ   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mZmZ d dlmZ eeeeedœZG dd„ dƒZdS )é    N)Úquote)Úload)Ú
InitSystem)ÚJujuSSH)ÚSSHControlPersist)ÚLocalTransport)ÚOCTransport)ÚSaltStackMaster)ÚCommandTimeoutExceptionÚConnectionExceptionÚUnsupportedHostExceptionÚInvalidTransportException)Úparse_version)ÚlocalZcontrol_persistZocZ	saltstackZjujuc                   @   s¨  e Zd Zdgdd„Zedd„ ƒZdd„ Zd	d
„ Zdd„ Zedd„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zdhdd„Zedd„ ƒZdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zdid.d/„Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zdjd9d:„Zd;d<„ Zd=d>„ Z d?d@„ Z!dAdB„ Z"dCdD„ Z#dEdF„ Z$dGdH„ Z%dIdJ„ Z&dKdL„ Z'dMdN„ Z(dOdP„ Z)dQdR„ Z*dSdT„ Z+dUdV„ Z,dWdX„ Z-dYdZ„ Z.d[d\„ Z/d]d^„ Z0d_d`„ Z1dadb„ Z2dcdd„ Z3dedf„ Z4dS )kÚSosNodeNTc              
   C   sâ  |  ¡ | _|| _|d | _|  ¡  |d | _|d | _|d | _|d | _d| _	d | _
d | _d | _i | _i | _|px| jj| _| jjs–| jjs–| j| j_|r¢|| j_d | _d| _d| _g | _d g g g g |d dœ| _d	| _t d
¡| _t d¡| _|  |¡| _| jjr| jjdk| _z| j  | j¡ W n4 t!k
rZ } z|  "d| ¡ ‚ W 5 d }~X Y nX |  #¡ | _
| jj| _| j	r†| jj$r†d}| j%rÞ|rÞ| j
sª| j &¡  d S | j	rÄ|  '¡ rÄd| j
_(| j
j(rÖ|  )¡  |  *¡  d S )NZcmdlineoptsÚtmpdirÚhostlenÚ	need_sudoÚsos_optionsFÚsos_cmd)ÚversionÚenabledÚdisabledÚoptionsÚpresetsr   Ú	sosreportZsosZsos_uiÚrootz!Unable to open remote session: %s)+ÚstripÚaddressÚcommonsÚoptsÚ_assign_config_optsr   r   r   r   r   ÚhostÚclusterÚhostnameÚsos_env_varsÚ	_env_varsÚpasswordÚ	_passwordZnopasswd_sudoZsudo_pwÚsos_pathÚ	retrievedZhash_retrievedÚ	file_listÚsos_infoÚsos_binÚloggingZ	getLoggerÚsoslogÚui_logÚ_load_remote_transportÚ
_transportZdefault_userZconnectÚ	ExceptionÚ	log_errorÚdetermine_host_policyZno_localÚ	connectedÚ
disconnectÚcheck_in_containerÚcontainerizedÚcreate_sos_containerÚ_load_sos_info)Úselfr   r   r'   Z
local_sudoZ
load_factsÚerr© r>   ú7/usr/lib/python3/dist-packages/sos/collector/sosnode.pyÚ__init__*   sn    






ú





zSosNode.__init__c                 C   s   | j r| j jS dS )NT)r2   r6   ©r<   r>   r>   r?   r6   l   s    zSosNode.connectedc                 C   s   | j  ¡  dS )zDWrapper to close the remote session via our transport agent
        N)r2   r7   rA   r>   r>   r?   r7   s   s    zSosNode.disconnectc                 C   sv   | j dkrd| _t| j |ƒS | jjt ¡ kr@t| jj | j |ƒS | jjdkrj|  d| jj ¡ t| jjƒ‚t	| j |ƒS )z…Determine the type of remote transport to load for this node, then
        return an instantiated instance of that transport
        )Ú	localhostz	127.0.0.1TÚautoz6Connection failed: unknown or unsupported transport %s)
r   r   r   r    Ú	transportÚ
TRANSPORTSÚkeysr4   r   r   )r<   r   r>   r>   r?   r1   x   s    
ÿÿzSosNode._load_remote_transportc                 C   s   d  | j| jd |¡S )Nz{:<{}} : {}é   )ÚformatÚ	_hostnamer   ©r<   Úmsgr>   r>   r?   Ú_fmt_msg‰   s    zSosNode._fmt_msgc                 C   sl   | j sf| jrtj ¡ | _ nL|  d¡}|d dkrf|d  d¡D ]&}|sHq>| d¡}|d | j |d < q>| j S )Nz
env --nullÚstatusr   Úoutputú ú=rG   )r&   r   ÚosÚenvironÚcopyÚrun_commandÚsplit)r<   ÚretZlnÚ_valr>   r>   r?   Úenv_varsŒ   s    

zSosNode.env_varsc                 C   s`   || _ | j  d| j¡ | j  d| jj¡ | j  d| jd ¡ | j  dd¡ | j  d| jj¡ dS )	z>Set the manifest section that this node will write to
        r$   ÚpolicyZsos_versionr   Úfinal_sos_commandÚ rD   N)ÚmanifestÚ	add_fieldrI   r"   Údistror,   r2   Úname)r<   r\   r>   r>   r?   Úset_node_manifest›   s    zSosNode.set_node_manifestc                 C   s<   t j d¡r|  d¡ dS t j d¡dk	r8|  d¡ dS dS )zV
        Tries to identify if we are currently running in a container or not.
        z/run/.containerenvz/Found /run/.containerenv. Running in container.TZ	containerNz/Found env var 'container'. Running in containerF)rQ   ÚpathÚexistsÚ	log_debugrR   ÚgetrA   r>   r>   r?   r8   ¥   s    

zSosNode.check_in_containerc                 C   sê   | j jræ| j j| jj|  ¡ | jjd}| j|dd}|d dkrÐ|d dkr‚d|d krh|  d	¡ t	‚nd
|d kr‚|  d¡ t	‚| j| j  
¡ dd}|d dkr¸|  d| j j ¡ dS |  d|d  ¡ t	‚n|  d|d  ¡ t	‚dS )zJIf the host is containerized, create the container we'll be using
        )ÚimageZauthZ
force_pullT©Ú	need_rootrM   )r   é}   rh   zunable to retrieve auth tokenrN   zHCould not pull image. Provide either a username and password or authfilezunknown: Not foundz%Specified image not found on registryr   zTemporary container %s createdz*Could not start container after create: %sz&Could not create container on host: %sN)r"   r9   r:   r    re   Úget_container_authZforce_pull_imagerT   r4   r3   Zrestart_sos_containerÚlog_infoZsos_container_name)r<   ÚcmdÚresrV   r>   r>   r?   r:   ±   sB    ýÿ
ÿÿÿÿzSosNode.create_sos_containerc                 C   sF   | j jr$| jjd  | j j| j j¡S | jjd  | j jp>| jj¡S dS )zrDetermine what the auth string should be to pull the image used to
        deploy our temporary container
        ÚdefaultN)	r    Zregistry_userr"   ZruntimesZfmt_registry_credentialsZregistry_passwordZfmt_registry_authfileZregistry_authfileZcontainer_authfilerA   r>   r>   r?   ri   Ù   s    þÿzSosNode.get_container_authFc                 C   s<   z | j d| |d}|d dkW S  tk
r6   Y dS X dS )z3Checks for the presence of fname on the remote nodezstat %srf   rM   r   FN)rT   r3   )r<   Úfnamerg   rl   r>   r>   r?   Úfile_existsç   s
    zSosNode.file_existsc                 C   s   | j rd| j kr| j S | jS )NrB   )r$   r   rA   r>   r>   r?   rI   ï   s    zSosNode._hostnamec                 C   s   d}t  |d|¡S )zUAttempts to obfuscate sensitive information in log messages such as
        passwordsz>(?P<var>(pass|key|secret|PASS|KEY|SECRET).*?=)(?P<value>.*?\s)z\g<var>****** )ÚreÚsub)r<   rK   Zregr>   r>   r?   Ú_sanitize_log_msgõ   s    zSosNode._sanitize_log_msgc                 C   s   | j  |  |¡¡ dS )z:Format a ui message that includes host name and formattingN)r0   ÚinforL   rJ   r>   r>   r?   Úui_msgû   s    zSosNode.ui_msgc                 C   s0   t  ¡ d d }d| j||f }| j |¡ dS )z#Used to print and log info messagesrG   é   ú
[%s:%s] %sN)ÚinspectÚstackrI   r/   rs   ©r<   rK   ÚcallerZlmsgr>   r>   r?   rj   ÿ   s    zSosNode.log_infoc                 C   s0   t  ¡ d d }d| j||f }| j |¡ dS )z$Used to print and log error messagesrG   ru   rv   N)rw   rx   rI   r/   Úerrorry   r>   r>   r?   r4     s    zSosNode.log_errorc                 C   s:   |   |¡}t ¡ d d }d| j||f }| j |¡ dS )z$Used to print and log debug messagesrG   ru   rv   N)rr   rw   rx   rI   r/   Údebug)r<   rK   rz   r>   r>   r?   rc     s    
zSosNode.log_debugc                 C   s&   | j jrdt|ƒ S | jr"d| S |S )z‚If we need to provide a sudo or root password to a command, then
        here we prefix the command with the correct bits
        zsu -c %sz
sudo -S %s)r    Úbecome_rootr   r   )r<   rk   r>   r>   r?   Ú_format_cmd  s
    zSosNode._format_cmdc           	   
   C   s   d}d}| j jdkrN| j j | j j¡}|dk	r†d |d ¡}|d r†|d }n8| j| j jddd}|d dkr†|d	  ¡  d
¡\}}}|rðt	| d¡ƒdkr¤|d7 }z|d| d¡d  7 }W n0 t
k
rî } z|  d| ¡ W 5 d}~X Y nX || jd< | jd r|  d| jd  ¡ n$| j| jjks6|  d¡ d| _dS |  d¡rRd| _d| j }| j|ddd}|d dkrˆ|  |d	 ¡ |  d¡rœ|  ¡  dS )zLQueries the node for information about the installed version of sos
        NÚ.r   ÚreleaseT©Úuse_containerrg   rM   r   rN   ú-é   ú.0ú-%sz%Unable to fully parse sos release: %szsos version is %sz!sos is not installed on this nodeFz4.0z
sos reportz%s -lú3.6)r"   Zcontainer_version_commandÚpackage_managerZpkg_versionZsos_pkg_nameÚjoinrT   r   rU   Úlenr3   rc   r,   rj   r   r    Zprimaryr4   r6   Úcheck_sos_versionr-   Ú_load_sos_pluginsÚ_load_sos_presets)	r<   ÚverZrelÚpkgZpkgsÚ_r=   rk   Úsosinfor>   r>   r?   r;     sH    

 ÿ 


zSosNode._load_sos_infoc                 C   sl   d| j  }| j|ddd}|d dkrh|d  ¡ D ]4}| ¡  d¡r2| d¡d  ¡ }| jd	  |¡ q2d S )
Nz%s --list-presetsTr   rM   r   rN   zname:rG   r   )r-   rT   Ú
splitlinesr   Ú
startswithrU   r,   Úappend)r<   rk   rl   ÚlineZpnamer>   r>   r?   r   O  s    
zSosNode._load_sos_presetsc                 C   s’   d}d}d}d}d}|d | }|d | }|d | }	|d d }
|   ||¡| jd< |   ||¡| jd	< |   |	|¡| jd
< |   |
|d¡| jd< d S )Nz,The following plugins are currently enabled:z-The following plugins are currently disabled:z4The following options are available for ALL plugins:z+The following plugin options are available:z	Profiles:z(.*?)z

r   r   r   TZprofiles)Ú_regex_sos_helpr,   )r<   r‘   ZENABLEDZDISABLEDZALL_OPTIONSZOPTIONSZPROFILESZ	enableregZdisregZoptregZproregr>   r>   r?   rŒ   X  s    zSosNode._load_sos_pluginsc              
   C   s|   g }t  ||t j¡D ]b}| ¡ D ]T}|sVz| | ¡ d ¡ W qt tk
rR   Y qtX q | d¡}| dd„ |D ƒ¡ q q|S )Nr   ú,c                 s   s   | ]}|  ¡ r|  ¡ V  qd S ©N©r   )Ú.0Úpr>   r>   r?   Ú	<genexpr>t  s      z*SosNode._regex_sos_help.<locals>.<genexpr>)rp   ÚfindallÚSr’   r”   rU   r3   Úextend)r<   Zregexr‘   Zis_listrl   Úresultr•   Úrr>   r>   r?   r–   i  s    
zSosNode._regex_sos_helpc              
   C   s\   z|   d| ¡ | j |¡W S  tk
rV } z|  d||f ¡ W Y ¢dS d}~X Y nX dS )z1Reads the specified file and returns the contentszReading file %szException while reading %s: %sr[   N)rj   r2   Ú	read_filer3   r4   )r<   Zto_readr=   r>   r>   r?   r¢   w  s    zSosNode.read_filec              	   C   st   | j r&|  d| jd j ¡ | jd S ti | jjtƒ d| jj	|  
d¡d}|rb|  d|j ¡ |S |  d¡ t‚dS )	z[Attempts to identify the host installation against supported
        distributions
        zusing local policy %srY   Tz/etc/os-release)ÚcacheÚsysrootZinitZprobe_runtimeZremote_execZremote_checkzloaded policy %s for hostz4Unable to determine host installation. Ignoring nodeN)r   rj   r   r^   r   r    r¤   r   r2   rT   r¢   r4   r   )r<   r"   r>   r>   r?   r5   €  s    
ÿ
ý
zSosNode.determine_host_policyc              
      sp   ‡ fdd„}||ƒ}z t ˆ jd ƒ}t |ƒ}||kW S  tk
rj } zˆ  d| ¡ W Y ¢dS d}~X Y nX dS )a  Checks to see if the sos installation on the node is AT LEAST the
        given ver. This means that if the installed version is greater than
        ver, this will still return True

        :param ver: Version number we are trying to verify is installed
        :type ver:  ``str``

        :returns:   True if installed version is at least ``ver``, else False
        :rtype:     ``bool``
        c              
      sœ   zZ|   d¡d }d}d| kr8d|   d¡d   d¡d  }t|  d¡ƒdkrR|d7 }|| W S  tk
r– } zˆ  d| |f ¡ |  W Y ¢S d }~X Y nX d S )	Nrƒ   r   r[   éÿÿÿÿr   r„   r…   zUnable to format '%s': %s)rU   rŠ   r3   rc   )rŽ   Z_fverZ_relr=   rA   r>   r?   Ú_format_version  s    
z2SosNode.check_sos_version.<locals>._format_versionr   zError checking sos version: %sFN)r   r,   r3   r4   )r<   rŽ   r¦   Z_verZ	_node_verZ	_test_verr=   r>   rA   r?   r‹   ’  s    
zSosNode.check_sos_versionc                 C   s   | j s
dS | j j |¡dk	S )z2Checks if a given package is installed on the nodeFN)r"   rˆ   Zpkg_by_name)r<   r   r>   r>   r?   Úis_installed¶  s    zSosNode.is_installedé´   c           
   
   C   sÈ   | j sn| jsn|  d¡ z$| j | j¡}|s8|  d¡ t‚W n2 tk
rl } z|  d| ¡ ‚ W 5 d}~X Y nX |r†| jj	r†| j 
|¡}|r”|  |¡}d|kr d}|r´| j}	|	 |¡}| j |||||¡S )aš  Runs a given cmd, either via the SSH session or locally

        Arguments:
            cmd - the full command to be run
            timeout - time in seconds to wait for the command to complete
            get_pty - If a shell is absolutely needed to run a command, set
                      this to True
            need_root - if a command requires root privileges, setting this to
                        True tells sos-collector to format the command with
                        sudo or su - as appropriate and to input the password
            use_container - Run this command in a container *IF* the host is
                            containerized
        z-Node is disconnected, attempting to reconnectzFailed to reconnect to nodez#Error while trying to reconnect: %sNZatomicT)r6   r   rc   r2   Z	reconnectr(   r   r3   r"   r9   Zformat_container_commandr~   rX   ÚupdaterT   )
r<   rk   ÚtimeoutÚget_ptyrg   r‚   ÚenvZreconnectedr=   Z_cmd_envr>   r>   r?   rT   ¼  s,    



ÿzSosNode.run_commandc                 C   sX   z6|   ¡ }|r|  |¡ n
|  d¡ | jr4|  ¡ | _W n tk
rJ   Y nX |  ¡  dS )z.Run an sos report on the node, then collect itz'Unable to determine path of sos archiveN)Úexecute_sos_commandÚfinalize_sos_pathr4   r)   Úretrieve_sosreportr*   r3   Úcleanup)r<   ra   r>   r>   r?   r   ã  s    
zSosNode.sosreportc                 C   s   || j d kS )z/Verifies if the given preset exists on the noder   ©r,   )r<   Úpresetr>   r>   r?   Ú_preset_existsñ  s    zSosNode._preset_existsc                    s&   t ‡ fdd„| jd | jd fD ƒƒS )z/Verifies if the given plugin exists on the nodec                 3   s   | ]}ˆ |kV  qd S r˜   r>   )rš   Ús©Úpluginr>   r?   rœ   ÷  s     z)SosNode._plugin_exists.<locals>.<genexpr>r   r   )Úanyr,   ©r<   r¶   r>   rµ   r?   Ú_plugin_existsõ  s    ÿzSosNode._plugin_existsc                 C   s   || j d kS )z6Checks to see if the plugin is default enabled on noder   r±   r¸   r>   r>   r?   Ú_check_enabledú  s    zSosNode._check_enabledc                 C   s   || j d kS )z7Checks to see if the plugin is default disabled on noder   r±   r¸   r>   r>   r?   Ú_check_disabledþ  s    zSosNode._check_disabledc                 C   sR   |  d¡d }|  |¡sdS |  |¡r6|| jjkr6dS |  |¡rN|| jd kS dS )z÷Attempts to verify that the given option is available on the node.
        Note that we only get available options for enabled plugins, so if a
        plugin has been force-enabled we cannot validate if the plugin option
        is correct or notr   r   Fr   T)rU   r¹   r»   r    Úenable_pluginsrº   r,   )r<   ÚoptÚplugr>   r>   r?   Ú_plugin_option_exists  s    


ÿ
zSosNode._plugin_option_existsc                    s   d  ‡ fdd„|D ƒ¡S )z^Returns a comma delimited list for sos plugins that are confirmed
        to exist on the noder—   c                 3   s   | ]}ˆ   |¡r|V  qd S r˜   ©r¹   ©rš   ÚorA   r>   r?   rœ     s     
 z,SosNode._fmt_sos_opt_list.<locals>.<genexpr>)r‰   )r<   r    r>   rA   r?   Ú_fmt_sos_opt_list  s    zSosNode._fmt_sos_opt_listc                 C   s
   || _ dS )zVExpose the node to the cluster profile determined for the
        environment
        N)r#   )r<   r#   r>   r>   r?   Úset_cluster  s    zSosNode.set_clusterc              	      s,  | j jr,| js| j j| _n|  d| j j ¡ | j jrX| j jD ]}|| jkr<| j |¡ q<| j jrŠ| j jD ] ‰ ˆ | jkrh| j jˆ  | jˆ < qh| j jrØ| j jD ]<‰ t	‡ fdd„| j
D ƒƒsšdˆ | j jˆ  f }| j
 |¡ qš| j  | ¡r| j j | j  | ¡ W 5 Q R X n | j j | j  | ¡ W 5 Q R X dS )a+  This is used to modify the sos report command run on the nodes.
        By default, sos report is run without any options, using this will
        allow the profile to specify what plugins to run or not and what
        options to use.

        This will NOT override user supplied options.
        zYCluster specified preset %s but user has also defined a preset. Using user specification.c                 3   s   | ]}ˆ |kV  qd S r˜   r>   rÁ   ©r½   r>   r?   rœ   :  s     z2SosNode.update_cmd_from_cluster.<locals>.<genexpr>ú%s=%sN)r#   Z
sos_presetr²   rj   Zsos_pluginsr¼   r”   r   Zsos_plugin_optionsr·   ÚplugoptsZcheck_node_is_primaryÚlockZset_primary_optionsZset_node_options)r<   r¾   Zoptionr>   rÅ   r?   Úupdate_cmd_from_cluster  s4    þ


ÿ

zSosNode.update_cmd_from_clusterc                 C   sJ   t | jjƒ| _t | jjƒ| _t | jjƒ| _t | jjƒ| _t | jjƒ| _dS )zˆFrom the global opts configuration, assign those values locally
        to this node so that they may be acted on individually.
        N)Úlistr    Úonly_pluginsÚskip_pluginsr¼   rÇ   r²   rA   r>   r>   r?   r!   G  s
    zSosNode._assign_config_optsc                    sœ  ˆ j d }ˆ  ¡ }|r&d|t|ƒf }g }ˆ  d¡rZˆ jjdkrZ| dttˆ jjƒƒ ¡ ˆ  d¡r†ˆ jjr†| dttˆ jjƒƒ ¡ ˆ  d¡rÒˆ jj	r¢| d	¡ ˆ jj
r´| d
¡ ˆ jjrÒ| dtˆ jjƒ ¡ ˆ  d¡r*ˆ jjr| dtd ˆ jj¡ƒ ¡ ˆ jjr*| dtd ˆ jj¡ƒ ¡ ˆ  d¡rZˆ jjrZ| dttˆ jjƒƒ ¡ ˆ  d¡srˆ  d¡r®ˆ jjdkr’| dˆ jj ¡ ˆ jjr®| dˆ jj ¡ ˆ  d¡rìˆ jjrØ| dˆ jj› ¡ ˆ jjrì| d¡ ˆ  ¡  | dtj ˆ jjˆ j¡¡}ˆ jD ]0}ˆ j| }| d|› d|r:|nd› ¡ qˆ jrŒ‡ fdd„ˆ jD ƒ}|rŒ| d td d!d"„ |D ƒ¡ƒ ¡ ˆ jrÈˆ  ˆ j¡r¸| d#tˆ jƒ ¡ nˆ  d$ˆ j ¡ ˆ j rx‡ fd%d„ˆ j D ƒ}t!|ƒt!ˆ j ƒkrt"t#ˆ j ƒt#|ƒ ƒ}ˆ  d&| ¡ ˆ  $ˆ j ¡}	|	r@| d't|	ƒ ¡ d(|d |¡f ˆ _%ˆ  &d)ˆ j% ¡ ˆ j' (d*ˆ j%¡ d+S ˆ j)rî‡ fd,d„ˆ j)D ƒ}
t!|
ƒt!ˆ j)ƒkrÌt"t#ˆ j)ƒt#|
ƒ ƒ}ˆ  d-| ¡ ˆ  $|
¡}|rî| d.t|ƒ ¡ ˆ j*rd‡ fd/d„ˆ j*D ƒ}t!|ƒt!ˆ j*ƒkrBt"t#ˆ j*ƒt#|ƒ ƒ}ˆ  d0| ¡ ˆ  $|¡}|rd| d1t|ƒ ¡ d(|d |¡f ˆ _%ˆ  &d)ˆ j% ¡ ˆ j' (d*ˆ j%¡ d+S )2zZUse host facts and compare to the cluster type to modify the sos
        command if neededr   z%s %s r‡   é   z--threads=%sz3.7z--plugin-timeout=%sz3.8z--allow-system-changesz--no-env-varsz
--since=%sz4.1z--skip-commands=%sr—   z--skip-files=%sz4.2z--cmd-timeout=%sz4.3z4.2-13rC   z--container-runtime=%sz--namespaces=%sz4.5.2z--journal-size=z--low-priorityr   z--ú r[   c                    s8   g | ]0}ˆ   | d ¡d ¡rˆ  | d¡d ¡r|‘qS )r   r   rP   )r¹   rU   r¿   rÁ   rA   r>   r?   Ú
<listcomp>   s    þz,SosNode.finalize_sos_cmd.<locals>.<listcomp>z-k %sc                 s   s   | ]
}|V  qd S r˜   r>   rÁ   r>   r>   r?   rœ   ¤  s     z+SosNode.finalize_sos_cmd.<locals>.<genexpr>z--preset=%sz?Requested to enable preset %s but preset does not exist on nodec                    s   g | ]}ˆ   |¡r|‘qS r>   rÀ   rÁ   rA   r>   r?   rÏ   ®  s     
 zBRequested plugins %s were requested to be enabled but do not existz--only-plugins=%sz%s %szFinal sos command set to %srZ   Nc                    s   g | ]}ˆ   |¡r|‘qS r>   )rº   rÁ   rA   r>   r?   rÏ   ½  s     
 zARequested to skip plugins %s, but plugins are already not enabledz--skip-plugins=%sc                    s.   g | ]&}|ˆ j krˆ  |¡rˆ  |¡r|‘qS r>   )rÌ   r»   r¹   rÁ   rA   r>   r?   rÏ   È  s
    

 
þzORequested to enable plugins %s, but plugins are already enabled or do not existz--enable-plugins=%s)+r,   Údetermine_sos_labelr   r‹   r    Zthreadsr”   ÚstrZplugin_timeoutZallow_system_changesZno_env_varsZsinceZskip_commandsr‰   Z
skip_filesZcmd_timeoutZcontainer_runtimeZ
namespacesZjournal_sizeZlow_priorityrÉ   ÚreplacerQ   ra   r"   Zsos_bin_pathr-   r   rÇ   r²   r³   rc   rË   rŠ   rÊ   ÚsetrÃ   r   rj   r\   r]   rÌ   r¼   )r<   r   ÚlabelZsos_optsr½   rW   r    ZplugsZnot_onlyZonlyÚskipZnot_skipZskiplnZnot_onÚenabler>   rA   r?   Úfinalize_sos_cmdR  sÒ    


ÿ



ÿÿ
ÿ
ÿ
ÿ

ÿ


þ

$"ÿÿÿ
ÿ
zSosNode.finalize_sos_cmdc                 C   sŠ   d}|| j  | ¡7 }| jjr<||s.d| jj n
d| jj 7 }|sDdS |  d| ¡ |  d¡rbd}nd}d	| j d
¡d |f }d||f S )z?Determine what, if any, label should be added to the sos reportr[   z%sr†   NzLabel for sos report set to %sr‡   z--labelz--namez%s-%sr   r   rÆ   )r#   Zget_node_labelr    rÔ   rc   r‹   r   rU   )r<   rÔ   Zlcmdr>   r>   r?   rÐ   ×  s    
ÿ
zSosNode.determine_sos_labelc                 C   s\   | j j}|r| |d¡}| ¡ d }|  d| ¡ || _| d¡d | _| j d| j¡ dS )z\Use host facts to determine if we need to change the sos path
        we are retrieving fromr[   r   zFinal sos path: %sú/r¥   Zcollected_archiveN)	r"   Zsos_path_striprÒ   rU   rj   r)   Úarchiver\   r]   )r<   ra   Zpstripr>   r>   r?   r®   ë  s    zSosNode.finalize_sos_pathc                 C   sV   |dkrdS |dkr d|kr dS |dkr,dS t |ƒdkrJ| d	¡dd… S d
| S d S )Nr¥   z+sos report process received SIGKILL on noderG   Zsudozsudo attempt failedé   z4sos report terminated unexpectedly. Check disk spacer   Ú
zsos exited with code %s)rŠ   rU   )r<   ZrcÚstdoutr>   r>   r?   Údetermine_sos_error÷  s    zSosNode.determine_sos_errorc              
   C   s¨  |   d¡ z0d}d}| j| j| jjddd| jd}|d dkr|d  ¡ D ]H}t |d¡rf| ¡ }| 	d	¡r€| 
d
¡d }qN| 	d¡rN| 
¡ d }qN|rò| j d|¡ t|ƒdkrÆ| j dd¡ n*t|ƒdkrâ| j dd¡ n| j dd¡ n| j dd¡ n6|  |d |d ¡}|  d|d |d f ¡ t|ƒ‚|W S  tk
r^   |  d¡ ‚ Y nF tk
r¢ } z&|  d|› ¡ |   d|› ¡ ‚ W 5 d}~X Y nX dS )z2Run sos report and capture the resulting file pathzGenerating sos report...FT)rª   r«   rg   r‚   r¬   rM   r   rN   z*sosreport-*tar*)z sha256	z md5	ú	rG   zThe checksum is: ru   Úchecksumé    Zchecksum_typeZmd5é@   Zsha256Úunknownz*Error running sos report. rc = %s msg = %szTimeout exceededz'Exception during sos report execution: zError running sos report: N)rt   rT   r   r    rª   r%   r’   Úfnmatchr   r“   rU   r\   r]   rŠ   rÝ   rc   r3   r
   r4   rj   )r<   ra   rß   rl   r•   r=   r>   r>   r?   r­     sP    
 ü

ÿ
zSosNode.execute_sos_commandc              
   C   s¦   | j d }tj || d¡d ¡}zD|  |¡rP|  d||f ¡ | j ||¡W S |  	d| ¡ W dS W n: t
k
r  } z|  	d||f ¡ W Y ¢dS d}~X Y nX dS )z7Copies the specified file from the host to our temp dirrØ   r¥   zCopying remote %s to local %szFAttempting to copy remote file %s, but it does not exist on filesystemFzFailed to retrieve %s: %sN)r   rQ   ra   r‰   rU   ro   rj   r2   Úretrieve_filerc   r3   )r<   ra   ZdestdirÚdestr=   r>   r>   r?   rä   0  s    

ÿÿ
zSosNode.retrieve_filec              
   C   sÈ   d  | ¡ ¡}zzt| d¡ƒdkr6|  d| ¡ W dS |  |¡rr|  d| ¡ d| }| j|dd	}|d
 dkW S |  d| ¡ W dS W n: tk
rÂ } z|  d||f ¡ W Y ¢dS d}~X Y nX dS )z|Removes the spciefied file from the host. This should only be used
        after we have retrieved the file already
        r[   rØ   r„   zJRefusing to remove path %s: appears to be incorrect and possibly dangerousFzRemoving file %szrm -f %sTrf   rM   r   zHAttempting to remove remote file %s, but it does not exist on filesystemzFailed to remove %s: %sN)r‰   rU   rŠ   rc   ro   rj   rT   r3   )r<   ra   rk   rl   Úer>   r>   r?   Úremove_fileA  s&    ÿ
ÿ
zSosNode.remove_filec              
   C   s8  | j rÞ| js| jjrFz|  | j ¡ W n  tk
rD   |  d¡ Y dS X |  d| j ¡ |  	d¡ z|  
| j ¡}W n2 tk
r¢ } z|  |¡ W Y ¢dS d}~X Y nX |rÎ|  	d¡ | j | j  d¡d ¡ d	S |  	d
¡ dS nV| j ¡ rô| j ¡ }ndd„ | j ¡ D ƒd }| j d| j|f ¡ |  d| ¡ dS dS )z+Collect the sosreport archive from the nodezFailed to make archive readableFzRetrieving sos report from %szRetrieving sos report...Nz!Successfully collected sos reportrØ   r¥   TzFailed to retrieve sos reportc                 S   s   g | ]}|j r|  ¡ ‘qS r>   r™   )rš   Úxr>   r>   r?   rÏ   t  s      z.SosNode.retrieve_sosreport.<locals>.<listcomp>z"Failed to run sos report on %s: %szFailed to run sos report. %s)r)   r   r    r}   Úmake_archive_readabler3   r4   rj   r   rt   rä   r+   r”   rU   ÚstderrÚreadrÜ   Ú	readlinesr/   r{   )r<   rV   r=   ræ   r>   r>   r?   r¯   X  s8    





ÿzSosNode.retrieve_sosreportc                 C   sP   | j dks| jrdS d| j kr2|  d| j  ¡ dS |  | j ¡}|sL|  d¡ dS )zuRemove the sosreport archive from the node, since we have
        collected it and it would be wasted space otherwiseNr   zFNode sos report path %s looks incorrect. Not attempting to remove pathzFailed to remove sos report)r)   r   rc   rç   r4   )r<   Zremovedr>   r>   r?   Úremove_sos_archivez  s    
ÿzSosNode.remove_sos_archivec                 C   sL   |   ¡  | jr,dD ]}|  | j| ¡r q,q| j ¡ }|rH| j|dd dS )z<Remove the sos archive from the node once we have it locally)z.sha256z.md5Trf   N)rí   r)   rç   r"   Zset_cleanup_cmdrT   )r<   Zextr°   r>   r>   r?   r°   ‰  s    
zSosNode.cleanupc                 C   sð   |D ]æ}z¬| j s| jjrtz|  |¡ W nL tk
rr } z.|  d| ¡ |  d||f ¡ W Y ¢W qW 5 d}~X Y nX |  |¡}|r¤| j 	| 
d¡d ¡ |  |¡ n|  d| ¡ W q tk
rè } zd| }|  |¡ W 5 d}~X Y qX qdS )z4Collect the file created by a cluster outside of soszUnable to retrieve file %sz#Failed to make file %s readable: %sNrØ   r¥   z1Error collecting additional data from primary: %s)r   r    r}   ré   r3   r4   rc   rä   r+   r”   rU   rç   )r<   Ú	filenamesÚfilenamer=   rV   ræ   rK   r>   r>   r?   Úcollect_extra_cmd”  s&    ÿ
zSosNode.collect_extra_cmdc                 C   sJ   d| }| j |ddd}|d dkr(dS d}|  |||d f ¡ t‚dS )	z¼Used to make the given archive world-readable, which is slightly
        better than changing the ownership outright.

        This is only used when we're not connecting as root.
        zchmod o+r %sé
   T)rª   rg   rM   r   z6Exception while making %s readable. Return code was %sN)rT   r4   r3   )r<   Úfilepathrk   rl   rK   r>   r>   r?   ré   ª  s    zSosNode.make_archive_readable)NNT)F)F)r¨   FFFN)5Ú__name__Ú
__module__Ú__qualname__r@   Úpropertyr6   r7   r1   rL   rX   r`   r8   r:   ri   ro   rI   rr   rt   rj   r4   rc   r~   r;   r   rŒ   r–   r¢   r5   r‹   r§   rT   r   r³   r¹   rº   r»   r¿   rÃ   rÄ   rÉ   r!   r×   rÐ   r®   rÝ   r­   rä   rç   r¯   rí   r°   rð   ré   r>   r>   r>   r?   r   (   st     ÿ
B


(


3	
	$    ÿ
') ,"r   )rã   rw   r.   rQ   rp   Zpipesr   Zsos.policiesr   Zsos.policies.init_systemsr   Zsos.collector.transports.jujur   Z(sos.collector.transports.control_persistr   Zsos.collector.transports.localr   Zsos.collector.transports.ocr   Z"sos.collector.transports.saltstackr	   Zsos.collector.exceptionsr
   r   r   r   Zsos.utilitiesr   rE   r   r>   r>   r>   r?   Ú<module>   s*   û	