o
    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)ÚlocalÚcontrol_persistÚocÚ	saltstackÚ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dhd.d/„Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Z		did9d:„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 )jÚSosNodeNTc              
   C   sÌ  |  ¡ | _|| _|d | _|  ¡  |d | _|d | _|d | _|d | _d| _	d | _
d | _d | _i | _i | _|p<| jj| _| jjsK| jjsK| j| j_|rQ|| 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 n t!y¦ } z|  "d| ¡ ‚ d }~ww |  #¡ | _
| 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 d S d S )NÚcmdlineoptsÚtmpdirÚhostlenÚ	need_sudoÚsos_optionsFÚsos_cmd)ÚversionÚenabledÚdisabledÚoptionsÚpresetsr   Ú	sosreportÚsosÚ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Ú	_passwordÚnopasswd_sudoÚsudo_pwÚsos_pathÚ	retrievedÚhash_retrievedÚ	file_listÚsos_infoÚsos_binÚloggingÚ	getLoggerÚsoslogÚui_logÚ_load_remote_transportÚ
_transportÚdefault_userÚconnectÚ	ExceptionÚ	log_errorÚdetermine_host_policyÚno_localÚ	connectedÚ
disconnectÚcheck_in_containerÚcontainerizedÚcreate_sos_containerÚ_load_sos_info)Úselfr%   r&   r.   Ú
local_sudoÚ
load_factsÚerr© rN   ú7/usr/lib/python3/dist-packages/sos/collector/sosnode.pyÚ__init__*   st   






ú€þ



÷zSosNode.__init__c                 C   s   | j r| j jS dS )NT)r=   rD   ©rJ   rN   rN   rO   rD   l   s   zSosNode.connectedc                 C   s   | j  ¡  dS )zDWrapper to close the remote session via our transport agent
        N)r=   rE   rQ   rN   rN   rO   rE   s   s   zSosNode.disconnectc                 C   sv   | j dv rd| _t| j |ƒS | jjt ¡ v r t| jj | j |ƒS | jjdkr5|  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ÚkeysrA   r   r   )rJ   r&   rN   rN   rO   r<   x   s   
ÿÿzSosNode._load_remote_transportc                 C   s   d  | j| jd |¡S )Nz{:<{}} : {}é   )ÚformatÚ	_hostnamer   ©rJ   ÚmsgrN   rN   rO   Ú_fmt_msg‰   s   zSosNode._fmt_msgc                 C   sp   | j s5| jrtj ¡ | _ | j S |  d¡}|d dkr5|d  d¡D ]}|s&q!| d¡}|d | j |d < q!| j S )Nz
env --nullÚstatusr   Úoutputú ú=rW   )r-   r   ÚosÚenvironÚcopyÚrun_commandÚsplit)rJ   ÚretÚlnÚ_valrN   rN   rO   Ú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+   ÚpolicyÚsos_versionr   Úfinal_sos_commandÚ rT   N)ÚmanifestÚ	add_fieldrY   r)   Údistror6   r=   Úname)rJ   rn   rN   rN   rO   Úset_node_manifest›   s   zSosNode.set_node_manifestc                 C   s<   t j d¡r|  d¡ dS t j d¡dur|  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.TÚ	containerNz/Found env var 'container'. Running in containerF)ra   ÚpathÚexistsÚ	log_debugrb   ÚgetrQ   rN   rN   rO   rF   ¥   s   

zSosNode.check_in_containerc                 C   sæ   | j jrq| j j| jj|  ¡ | jjd}| j|dd}|d dv rf|d dkr@d|d v r3|  d	¡ t	‚d
|d v r@|  d¡ t	‚| j| j  
¡ dd}|d dkr[|  d| j j ¡ dS |  d|d  ¡ t	‚|  d|d  ¡ t	‚dS )zJIf the host is containerized, create the container we'll be using
        )ÚimageÚauthÚ
force_pullT©Ú	need_rootr]   )r   é}   r}   zunable to retrieve auth tokenr^   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)   rG   rH   r'   rx   Úget_container_authÚforce_pull_imagerd   rA   r@   Úrestart_sos_containerÚlog_infoÚsos_container_name)rJ   ÚcmdÚresrf   rN   rN   rO   rH   ±   sD   ýÿ
ÿÿÿÿÝzSosNode.create_sos_containerc                 C   sB   | j jr| jjd  | j j| j j¡S | jjd  | j jp| jj¡S )zrDetermine what the auth string should be to pull the image used to
        deploy our temporary container
        Údefault)	r'   Úregistry_userr)   ÚruntimesÚfmt_registry_credentialsÚregistry_passwordÚfmt_registry_authfileÚregistry_authfileÚcontainer_authfilerQ   rN   rN   rO   r~   Ù   s   þÿzSosNode.get_container_authFc                 C   s6   z| j d| |d}|d dkW S  ty   Y dS w )z3Checks for the presence of fname on the remote nodezstat %sr{   r]   r   F)rd   r@   )rJ   Úfnamer|   r„   rN   rN   rO   Úfile_existsç   s   ÿzSosNode.file_existsc                 C   s   | j rd| j vr| j S | jS )NrR   )r+   r%   rQ   rN   rN   rO   rY   ï   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)rJ   r[   ÚregrN   rN   rO   Ú_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)r;   Úinfor\   rZ   rN   rN   rO   Úui_msgû   s   zSosNode.ui_msgc                 C   ó0   t  ¡ d d }d| j||f }| j |¡ dS )z#Used to print and log info messagesrW   é   ú
[%s:%s] %sN)ÚinspectÚstackrY   r:   r“   ©rJ   r[   ÚcallerÚlmsgrN   rN   rO   r   ÿ   ó   zSosNode.log_infoc                 C   r•   )z$Used to print and log error messagesrW   r–   r—   N)r˜   r™   rY   r:   Úerrorrš   rN   rN   rO   rA     r   zSosNode.log_errorc                 C   s:   |   |¡}t ¡ d d }d| j||f }| j |¡ dS )z$Used to print and log debug messagesrW   r–   r—   N)r’   r˜   r™   rY   r:   Údebug)rJ   r[   r›   rN   rN   rO   rv     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   )rJ   rƒ   rN   rN   rO   Ú_format_cmd  s
   zSosNode._format_cmdc           	   
   C   sœ  d}d}| j jdu r'| j j | j j¡}|dur&d |d ¡}|d r&|d }n| j| j jddd}|d dkrC|d	  ¡  d
¡\}}}|ryt	| d¡ƒdkrR|d7 }z|d| d¡d  7 }W n t
yx } z|  d| ¡ W Y d}~nd}~ww || jd< | jd rŽ|  d| jd  ¡ n| j| jjksš|  d¡ d| _dS |  d¡r§d| _d| j }| j|ddd}|d dkrÁ|  |d	 ¡ |  d¡rÌ|  ¡  dS dS )zLQueries the node for information about the installed version of sos
        NÚ.r   ÚreleaseT©Úuse_containerr|   r]   r   r^   ú-é   ú.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)   Úcontainer_version_commandÚpackage_managerÚpkg_versionÚsos_pkg_nameÚjoinrd   r$   re   Úlenr@   rv   r6   r   r%   r'   ÚprimaryrA   rD   Úcheck_sos_versionr7   Ú_load_sos_pluginsÚ_load_sos_presets)	rJ   ÚverÚrelÚpkgÚpkgsÚ_rM   rƒ   ÚsosinforN   rN   rO   rI     sN   €
ÿ€ÿ





ÿzSosNode._load_sos_infoc                 C   sp   d| j  }| j|ddd}|d dkr4|d  ¡ D ]}| ¡  d¡r3| d¡d  ¡ }| jd	  |¡ qd S d S )
Nz%s --list-presetsTr¤   r]   r   r^   zname:rW   r   )r7   rd   Ú
splitlinesr$   Ú
startswithre   r6   Úappend)rJ   rƒ   r„   ÚlineÚpnamerN   rN   rO   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   TÚprofiles)Ú_regex_sos_helpr6   )rJ   rº   ÚENABLEDÚDISABLEDÚALL_OPTIONSÚOPTIONSÚPROFILESÚ	enableregÚdisregÚoptregÚproregrN   rN   rO   r³   X  s   zSosNode._load_sos_pluginsc              
   C   sx   g }t  ||t j¡D ]/}| ¡ D ](}|s)z| | ¡ d ¡ W q ty(   Y qw | d¡}| dd„ |D ƒ¡ qq
|S )Nr   ú,c                 s   s     | ]}|  ¡ r|  ¡ V  qd S ©N©r$   )Ú.0ÚprN   rN   rO   Ú	<genexpr>t  s   € z*SosNode._regex_sos_help.<locals>.<genexpr>)r   ÚfindallÚSr»   r½   re   r@   Úextend)rJ   Úregexrº   Úis_listr„   Úresultr¾   ÚrrN   rN   rO   rÁ   i  s   ÿ
ø	zSosNode._regex_sos_helpc              
   C   sV   z|   d| ¡ | j |¡W S  ty* } z|  d||f ¡ W Y d}~dS d}~ww )z1Reads the specified file and returns the contentszReading file %szException while reading %s: %sNrm   )r   r=   Ú	read_filer@   rA   )rJ   Úto_readrM   rN   rN   rO   rØ   w  s   €þzSosNode.read_filec              	   C   sp   | j r|  d| jd j ¡ | jd S ti | jjtƒ d| jj	|  
d¡d}|r1|  d|j ¡ |S |  d¡ t‚)z[Attempts to identify the host installation against supported
        distributions
        zusing local policy %srj   Tz/etc/os-release)ÚcacheÚsysrootÚinitÚprobe_runtimeÚremote_execÚremote_checkzloaded policy %s for hostz4Unable to determine host installation. Ignoring node)r   r   r&   rp   r   r'   rÛ   r   r=   rd   rØ   rA   r   )rJ   r)   rN   rN   rO   rB   €  s   
ÿ
ý
zSosNode.determine_host_policyc              
      sj   ‡ fdd„}||ƒ}zt ˆ jd ƒ}t |ƒ}||kW S  ty4 } zˆ  d| ¡ W Y d}~dS d}~ww )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–   z-|   d¡d }d}d| v rd|   d¡d   d¡d  }t|  d¡ƒdkr)|d7 }|| W S  tyJ } zˆ  d| |f ¡ | W  Y d }~S d }~ww )	Nr¦   r   rm   éÿÿÿÿr¢   r§   r¨   zUnable to format '%s': %s)re   r°   r@   rv   )rµ   Ú_fverÚ_relrM   rQ   rN   rO   Ú_format_version  s   
€þz2SosNode.check_sos_version.<locals>._format_versionr   zError checking sos version: %sNF)r   r6   r@   rA   )rJ   rµ   rã   Ú_verÚ	_node_verÚ	_test_verrM   rN   rQ   rO   r²   ’  s   
€þzSosNode.check_sos_versionc                 C   s   | j sdS | j j |¡duS )z2Checks if a given package is installed on the nodeFN)r)   r¬   Úpkg_by_name)rJ   r·   rN   rN   rO   Úis_installed¶  s   zSosNode.is_installedé´   c           
   
   C   s¾   | j s2| js2|  d¡ z| j | j¡}|s|  d¡ t‚W n ty1 } z|  d| ¡ ‚ d}~ww |r>| jj	r>| j 
|¡}|rE|  |¡}d|v rKd}|rU| 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: %sNÚatomicT)rD   r   rv   r=   Ú	reconnectr/   r   r@   r)   rG   Úformat_container_commandr¡   ri   Úupdaterd   )
rJ   rƒ   ÚtimeoutÚget_ptyr|   r¥   ÚenvÚreconnectedrM   Ú_cmd_envrN   rN   rO   rd   ¼  s2   

þ€þ

ÿzSosNode.run_commandc                 C   sV   z|   ¡ }|r|  |¡ n|  d¡ | jr|  ¡ | _W n	 ty$   Y nw |  ¡  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_pathrA   r2   Úretrieve_sosreportr3   r@   Úcleanup)rJ   rt   rN   rN   rO   r    ã  s   

€ÿzSosNode.sosreportc                 C   ó   || j d v S )z/Verifies if the given preset exists on the noder   ©r6   )rJ   ÚpresetrN   rN   rO   Ú_preset_existsñ  ó   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   ó    | ]}ˆ |v V  qd S rÌ   rN   )rÎ   Ús©ÚpluginrN   rO   rÐ   ÷  ó   € z)SosNode._plugin_exists.<locals>.<genexpr>r   r   )Úanyr6   ©rJ   rÿ   rN   rþ   rO   Ú_plugin_existsõ  s   
ÿzSosNode._plugin_existsc                 C   r÷   )z6Checks to see if the plugin is default enabled on noder   rø   r  rN   rN   rO   Ú_check_enabledú  rû   zSosNode._check_enabledc                 C   r÷   )z7Checks to see if the plugin is default disabled on noder   rø   r  rN   rN   rO   Ú_check_disabledþ  rû   zSosNode._check_disabledc                 C   sR   |  d¡d }|  |¡sdS |  |¡r|| jjvrdS |  |¡r'|| jd v 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)re   r  r  r'   Úenable_pluginsr  r6   )rJ   ÚoptÚplugrN   rN   rO   Ú_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Î   ÚorQ   rN   rO   rÐ     s   € z,SosNode._fmt_sos_opt_list.<locals>.<genexpr>)r¯   )rJ   r'   rN   rQ   rO   Ú_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*   )rJ   r*   rN   rN   rO   Úset_cluster  s   
zSosNode.set_clusterc                    sX  | j jr| js| j j| _n	|  d| j j ¡ | j jr,| j jD ]}|| jvr+| j |¡ q| j jrE| j jD ]‰ ˆ | jvrD| j jˆ  | jˆ < q4| j jrl| j jD ]‰ t	‡ fdd„| j
D ƒƒskdˆ | j jˆ  f }| j
 |¡ qM| j  | ¡r| j j | j  | ¡ W d  ƒ dS 1 sˆw   Y  dS | j j | j  | ¡ W d  ƒ dS 1 s¥w   Y  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   rü   rÌ   rN   r  ©r  rN   rO   rÐ   :  r   z2SosNode.update_cmd_from_cluster.<locals>.<genexpr>ú%s=%sN)r*   Ú
sos_presetrù   r   Úsos_pluginsr  r½   r   Úsos_plugin_optionsr  ÚplugoptsÚcheck_node_is_primaryÚlockÚset_primary_optionsÚset_node_options)rJ   r  ÚoptionrN   r  rO   Úupdate_cmd_from_cluster  s>   þ
€
€
ÿ€
"ÿ
"ÿ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ù   rQ   rN   rN   rO   r(   G  s
   zSosNode._assign_config_optsc                    s‚  ˆ j d }ˆ  ¡ }|rd|t|ƒf }g }ˆ  d¡r-ˆ jjdkr-| dttˆ jjƒƒ ¡ ˆ  d¡rCˆ jjrC| dttˆ jjƒƒ ¡ ˆ  d¡riˆ jj	rQ| d	¡ ˆ jj
rZ| d
¡ ˆ jjri| dtˆ jjƒ ¡ ˆ  d¡r’ˆ jjr€| dtd ˆ jj¡ƒ ¡ ˆ jjr’| dtd ˆ jj¡ƒ ¡ ˆ  d¡r¨ˆ jjr¨| dttˆ jjƒƒ ¡ ˆ  d¡s²ˆ  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 ]}ˆ j| }| d|› d|r|nd› ¡ qÿˆ jr9‡ fdd„ˆ jD ƒ}|r9| d td d!d"„ |D ƒ¡ƒ ¡ ˆ jrWˆ  ˆ j¡rO| d#tˆ jƒ ¡ nˆ  d$ˆ j ¡ ˆ j r¯‡ fd%d„ˆ j D ƒ}t!|ƒt!ˆ j ƒkrt"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*r%‡ fd/d„ˆ j*D ƒ}t!|ƒt!ˆ j*ƒkrt"t#ˆ j*ƒt#|ƒ ƒ}ˆ  d0| ¡ ˆ  $|¡}|r%| 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-13rS   z--container-runtime=%sz--namespaces=%sz4.5.2z--journal-size=z--low-priorityr    z--ú rm   c                    s8   g | ]}ˆ   | d ¡d ¡rˆ  | d¡d ¡r|‘qS )r¢   r   r`   )r  re   r	  r  rQ   rN   rO   Ú
<listcomp>   s
    ÿ
þz,SosNode.finalize_sos_cmd.<locals>.<listcomp>z-k %sc                 s   s    | ]}|V  qd S rÌ   rN   r  rN   rN   rO   rÐ   ¤  s   € z+SosNode.finalize_sos_cmd.<locals>.<genexpr>z--preset=%sz?Requested to enable preset %s but preset does not exist on nodec                    ó   g | ]	}ˆ   |¡r|‘qS rN   r
  r  rQ   rN   rO   r   ®  ó    zBRequested plugins %s were requested to be enabled but do not existz--only-plugins=%sz%s %szFinal sos command set to %srl   Nc                    r!  rN   )r  r  rQ   rN   rO   r   ½  r"  zARequested to skip plugins %s, but plugins are already not enabledz--skip-plugins=%sc                    s.   g | ]}|ˆ j vrˆ  |¡rˆ  |¡r|‘qS rN   )r  r  r  r  rQ   rN   rO   r   È  s    
ÿÿÿzORequested to enable plugins %s, but plugins are already enabled or do not existz--enable-plugins=%s)+r6   Údetermine_sos_labelr   r²   r'   Úthreadsr½   ÚstrÚplugin_timeoutÚallow_system_changesÚno_env_varsÚsinceÚskip_commandsr¯   Ú
skip_filesÚcmd_timeoutÚcontainer_runtimeÚ
namespacesÚjournal_sizeÚlow_priorityr  Úreplacera   rt   r)   Úsos_bin_pathr7   r   r  rù   rú   rv   r  r°   r  Úsetr  r   r   rn   ro   r  r  )rJ   r   ÚlabelÚsos_optsr  rh   r'   ÚplugsÚnot_onlyÚonlyÚskipÚnot_skipÚskiplnÚnot_onÚenablerN   rQ   rO   Ú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 }|s"dS |  d| ¡ |  d¡r1d}nd}d	| j d
¡d |f }d||f S )z?Determine what, if any, label should be added to the sos reportrm   z%sr©   NzLabel for sos report set to %srª   z--labelz--namez%s-%sr¢   r   r  )r*   Úget_node_labelr'   r4  rv   r²   r%   re   )rJ   r4  ÚlcmdrN   rN   rO   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 fromrm   r   zFinal sos path: %sú/rà   Úcollected_archiveN)	r)   Úsos_path_stripr1  re   r   r2   Úarchivern   ro   )rJ   rt   ÚpstriprN   rN   rO   rô   ë  s   zSosNode.finalize_sos_pathc                 C   sR   |dkrdS |dkrd|v rdS |dkrdS t |ƒdkr%| d	¡dd… S d
| S )Nrà   z+sos report process received SIGKILL on noderW   Úsudozsudo attempt failedé   z4sos report terminated unexpectedly. Check disk spacer   Ú
zsos exited with code %s)r°   re   )rJ   ÚrcÚstdoutrN   rN   rO   Údetermine_sos_error÷  s   zSosNode.determine_sos_errorc              
   C   s–  |   d¡ zœd}d}| j| j| jjddd| jd}|d dkr‡|d  ¡ D ]$}t |d¡r1| ¡ }| 	d	¡r>| 
d
¡d }q%| 	d¡rI| 
¡ d }q%|r}| j d|¡ t|ƒdkrc| j dd¡ |W S t|ƒdkrs| j dd¡ |W S | j dd¡ |W S | j dd¡ |W S |  |d |d ¡}|  d|d |d f ¡ t|ƒ‚ ty®   |  d¡ ‚  tyÊ } z|  d|› ¡ |   d|› ¡ ‚ d}~ww )z2Run sos report and capture the resulting file pathzGenerating sos report...FT)rî   rï   r|   r¥   rð   r]   r   r^   z*sosreport-*tar*)z sha256	z md5	ú	rW   zThe checksum is: r–   Úchecksumé    Úchecksum_typeÚmd5é@   Úsha256Úunknownz*Error running sos report. rc = %s msg = %szTimeout exceededz'Exception during sos report execution: zError running sos report: N)r”   rd   r   r'   rî   r,   r»   Úfnmatchr$   r¼   re   rn   ro   r°   rK  rv   r@   r
   rA   r   )rJ   rt   rM  r„   r¾   rM   rN   rN   rO   ró     sZ   
ü

€õ
øúüÿ
€ýzSosNode.execute_sos_commandc              
   C   sœ   | j d }tj || d¡d ¡}z |  |¡r(|  d||f ¡ | j ||¡W S |  	d| ¡ W dS  t
yM } z|  	d||f ¡ W Y d}~dS d}~ww )z7Copies the specified file from the host to our temp dirrA  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   ra   rt   r¯   re   rŽ   r   r=   Úretrieve_filerv   r@   )rJ   rt   ÚdestdirÚdestrM   rN   rN   rO   rU  0  s"   

ÿÿ€þzSosNode.retrieve_filec              
   C   s¾   d  | ¡ ¡}z;t| d¡ƒdkr|  d| ¡ W dS |  |¡r9|  d| ¡ d| }| j|dd	}|d
 dkW S |  d| ¡ W dS  ty^ } z|  d||f ¡ W Y d}~dS d}~ww )z|Removes the spciefied file from the host. This should only be used
        after we have retrieved the file already
        rm   rA  r§   zJRefusing to remove path %s: appears to be incorrect and possibly dangerousFzRemoving file %szrm -f %sTr{   r]   r   zHAttempting to remove remote file %s, but it does not exist on filesystemzFailed to remove %s: %sN)r¯   re   r°   rv   rŽ   r   rd   r@   )rJ   rt   rƒ   r„   ÚerN   rN   rO   Úremove_fileA  s*   ÿ
ÿ€þzSosNode.remove_filec              
   C   s.  | j rl| js
| jjr"z|  | j ¡ W n ty!   |  d¡ Y dS w |  d| j ¡ |  	d¡ z|  
| j ¡}W n tyO } z|  |¡ W Y d}~dS d}~ww |re|  	d¡ | j | j  d¡d ¡ d	S |  	d
¡ dS | j ¡ rw| j ¡ }ndd„ | j ¡ D ƒd }| j d| j|f ¡ |  d| ¡ 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 reportrA  rà   TzFailed to retrieve sos reportc                 S   s   g | ]	}|j r|  ¡ ‘qS rN   rÍ   )rÎ   ÚxrN   rN   rO   r   t  r"  z.SosNode.retrieve_sosreport.<locals>.<listcomp>z"Failed to run sos report on %s: %szFailed to run sos report. %s)r2   r   r'   r    Úmake_archive_readabler@   rA   r   r%   r”   rU  r5   r½   re   ÚstderrÚreadrJ  Ú	readlinesr:   rž   )rJ   rf   rM   rX  rN   rN   rO   rõ   X  s>   
þ

€þ


ÿzSosNode.retrieve_sosreportc                 C   sT   | j du s| jr
dS d| j vr|  d| j  ¡ dS |  | j ¡}|s(|  d¡ dS 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)r2   r   rv   rY  rA   )rJ   ÚremovedrN   rN   rO   Úremove_sos_archivez  s   
ÿÿzSosNode.remove_sos_archivec                 C   sP   |   ¡  | jrdD ]}|  | j| ¡r nq	| j ¡ }|r&| j|dd dS dS )z<Remove the sos archive from the node once we have it locally)z.sha256z.md5Tr{   N)r`  r2   rY  r)   Úset_cleanup_cmdrd   )rJ   Úextrö   rN   rN   rO   rö   ‰  s   ÿ
ÿzSosNode.cleanupc                 C   sê   |D ]p}zS| j s| jjr7z|  |¡ W n# ty6 } z|  d| ¡ |  d||f ¡ W Y d}~W qd}~ww |  |¡}|rO| j 	| 
d¡d ¡ |  |¡ n|  d| ¡ W q tyr } zd| }|  |¡ W Y d}~qd}~ww dS )z4Collect the file created by a cluster outside of soszUnable to retrieve file %sz#Failed to make file %s readable: %sNrA  rà   z1Error collecting additional data from primary: %s)r   r'   r    r[  r@   rA   rv   rU  r5   r½   re   rY  )rJ   Ú	filenamesÚfilenamerM   rf   rX  r[   rN   rN   rO   Úcollect_extra_cmd”  s2   ÿ€ü
€€þðzSosNode.collect_extra_cmdc                 C   sF   d| }| j |ddd}|d dkrdS d}|  |||d f ¡ t‚)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î   r|   r]   r   z6Exception while making %s readable. Return code was %s)rd   rA   r@   )rJ   Úfilepathrƒ   r„   r[   rN   rN   rO   r[  ª  s   zSosNode.make_archive_readable)NNT)F)ré   FFFN)5Ú__name__Ú
__module__Ú__qualname__rP   ÚpropertyrD   rE   r<   r\   ri   rr   rF   rH   r~   rŽ   rY   r’   r”   r   rA   rv   r¡   rI   r´   r³   rÁ   rØ   rB   r²   rè   rd   r    rú   r  r  r  r	  r  r  r  r(   r>  r#  rô   rK  ró   rU  rY  rõ   r`  rö   re  r[  rN   rN   rN   rO   r   (   st    
ÿB


(


3	
	$
ÿ') ,"r   )rT  r˜   r8   ra   r   Úpipesr   Úsos.policiesr   Úsos.policies.init_systemsr   Úsos.collector.transports.jujur   Ú(sos.collector.transports.control_persistr   Úsos.collector.transports.localr   Úsos.collector.transports.ocr   Ú"sos.collector.transports.saltstackr	   Úsos.collector.exceptionsr
   r   r   r   Úsos.utilitiesr   rU   r   rN   rN   rN   rO   Ú<module>   s,   
û	