
    -_g͗                         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      Zy)    N)quote)load)
InitSystem)JujuSSH)SSHControlPersist)LocalTransport)OCTransport)SaltStackMaster)CommandTimeoutExceptionConnectionExceptionUnsupportedHostExceptionInvalidTransportException)sos_parse_version)localcontrol_persistoc	saltstackjujuc                   `   e Zd Z	 	 d3dZed        Zd Zd Zd Zed        Z	d Z
d	 Zd
 Zd Zd4dZed        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd4dZd Zd Zd Zd Z	 	 d5d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. Z0d/ Z1d0 Z2d1 Z3d2 Z4y)6SosNodeNc                 F   |j                         | _        || _        |d   | _        | j	                          |d   | _        |d   | _        |d   | _        |d   | _        | j                  j                  | _	        | j                  j                  | _
        d| _        d | _        d | _        d | _        i | _        i | _        |xs | j                  j"                  | _        | j                  j&                  s1| j                  j(                  s| j$                  | j                  _        |r|| j                  _        d | _        d| _        d| _        g | _        d g g g g |d   d| _        d	| _        t7        j8                  d
      | _        t7        j8                  d      | _        | j?                  |      | _         | j@                  jB                  r| j@                  jB                  dk7  | _        	 | j@                  jE                  | j$                         | jK                         | _        | j@                  j                  | _        | j                  r| j                  jL                  rd}| jN                  r|r| j                  s| j@                  jQ                          y | j                  r!| jS                         rd| j                  _*        | j                  jT                  r| jW                          | jY                          y # tF        $ r}| jI                  d|         d }~ww xY w)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: )-stripaddresscommonsopts_assign_config_optsr   r   r   r   node_config_fileinherit_config_filer   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)   r3   
local_sudo
load_factserrs          7/usr/lib/python3/dist-packages/sos/collector/sosnode.py__init__zSosNode.__init__,   s   }}M*	  "h'y) -"=1 $		 : :#'99#@#@ 
	!7TYY%7%7yy&&tyy/@/@ $DII *DII#y)
 #''.''155g> ??''!__99VCDN	OO##DNN3 ..0	00::$)),,J>>j99**,zz**,.3DII+yy&&))+!+  	NN<SEBC	s   +%K= =	L LL c                 H    | j                   r| j                   j                  S y)NT)rB   rI   rO   s    rS   rI   zSosNode.connectedq   s    ????,,,    c                 8    | j                   j                          y)zDWrapper to close the remote session via our transport agent
        N)rB   rJ   rV   s    rS   rJ   zSosNode.disconnectx   s     	""$rW   c                    | j                   dv rd| _        t        | j                   |      S | j                  j                  t
        v r-t        | j                  j                     | j                   |      S | j                  j                  dk7  rG| j                  d| j                  j                          t        | j                  j                        t        | j                   |      S )zDetermine the type of remote transport to load for this node, then
        return an instantiated instance of that transport
        )	localhostz	127.0.0.1Tautoz4Connection failed: unknown or unsupported transport )	r(   r   r   r*   	transport
TRANSPORTSrF   r   r   )rO   r)   s     rS   rA   zSosNode._load_remote_transport}   s     <<55DJ!$,,8899*,dii1124<<II99&(NNF99&&') ,DII,?,?@@ w77rW   c                 H    | j                   d| j                  dz    dd| S )N<    z : )	_hostnamer   rO   msgs     rS   _fmt_msgzSosNode._fmt_msg   s-    ..4<<!#3"4@ 45S>>rW   c                 ^   | j                   s| j                  r/t        j                  j	                         | _         | j                   S | j                  d      }|d   dk(  rB|d   j                  d      D ]+  }|s|j                  d      }|d   | j                   |d   <   - | j                   S )Nz
env --nullstatusr   output =r`   )r2   r   osenvironcopyrun_commandsplit)rO   retln_vals       rS   env_varszSosNode.env_vars   s    ~~zz!#!2 ~~ &&|4x=A%!(m11&9 :!$!xx}26q'tAw/	:
 ~~rW   c                    || _         | j                   j                  d| j                         | j                   j                  d| j                  j                         | j                   j                  d| j
                  d          | j                   j                  dd       | j                   j                  d| j                  j                         y)	z>Set the manifest section that this node will write to
        r0   policysos_versionr   final_sos_commandra   r\   N)manifest	add_fieldrb   r.   os_release_namer;   rB   name)rO   rx   s     rS   set_node_manifestzSosNode.set_node_manifest   s     !
DNN;$))*C*CDt}}Y/GH 3R8T__-A-ABrW   c                     t         j                  j                  d      r| j                  d       yt         j                  j                  d      | j                  d       yy)zV
        Tries to identify if we are currently running in a container or not.
        z/run/.containerenvz/Found /run/.containerenv. Running in container.T	containerz/Found env var 'container'. Running in containerF)rk   pathexists	log_debugrl   getrV   s    rS   rK   zSosNode.check_in_container   sK     77>>./NNLM::>>+&2NNLMrW   c                    | j                   j                  rI| j                   j                  | j                  j                  | j                         | j                  j                        }| j                  |d      }|d   dv r|d   dk(  r<d|d   v r| j                  d	       t        d
|d   v r| j                  d       t        | j                  | j                   j                         d      }|d   dk(  r*| j                  d| j                   j                   d       y| j                  d|d           t        | j                  d|d           t        y)zJIf the host is containerized, create the container we'll be using
        )imageauth
force_pullT	need_rootrg   )r   }   r   zunable to retrieve auth tokenrh   zHCould not pull image. Provide either a username and password or authfilezunknown: Not foundz%Specified image not found on registryr   zTemporary container z createdz(Could not start container after create: z$Could not create container on host: F)r.   rL   rM   r*   r   get_container_authforce_pull_imagern   rF   rE   restart_sos_containerlog_infosos_container_name)rO   cmdresrp   s       rS   rM   zSosNode.create_sos_container   sf    99""))00iioo,,.9955 1 C
 ""3$"7C8}(x=C'6#h-G7 (+s8}<'NO'
 &&tyy'F'F'H15 ' 7x=A%MM"8%)YY%A%A$B(#L MI"%h- 2 3NNA!(m_. /OrW   c                 x   | j                   j                  rQ| j                  j                  d   j	                  | j                   j                  | j                   j
                        S | j                  j                  d   j                  | j                   j                  xs | 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_authfilerV   s    rS   r   zSosNode.get_container_auth   s     99""99%%i0II		''		++  yy!!),BBII''G499+G+G
 	
rW   c                 ^    	 | j                  d| |      }|d   dk(  S # t        $ r Y yw xY w)z3Checks for the presence of fname on the remote nodezstat r   rg   r   F)rn   rE   )rO   fnamer   r   s       rS   file_existszSosNode.file_exists   sC    	""U5'?i"HCx=A%% 		s     	,,c                 f    | j                   rd| j                   vr| j                   S | j                  S )NrZ   )r0   r(   rV   s    rS   rb   zSosNode._hostname   s(    ==[=== ||rW   c                 4    d}t        j                  |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)rO   rd   regs      rS   _sanitize_log_msgzSosNode._sanitize_log_msg   s     Pvvc,c22rW   c                 X    | j                   j                  | j                  |             y)z:Format a ui message that includes host name and formattingN)r@   infore   rc   s     rS   ui_msgzSosNode.ui_msg   s    s+,rW   c                     t        j                         d   d   }d| j                   d| d| }| j                  j	                  |       y)z#Used to print and log info messagesr`      [:] N)inspectstackrb   r?   r   rO   rd   callerlmsgs       rS   r   zSosNode.log_info  sF    #A&4>>"!F82cU3rW   c                     t        j                         d   d   }d| j                   d| d| }| j                  j	                  |       y)z$Used to print and log error messagesr`   r   r   r   r   N)r   r   rb   r?   errorr   s       rS   rF   zSosNode.log_error	  sF    #A&4>>"!F82cU3$rW   c                     | j                  |      }t        j                         d   d   }d| j                   d| d| }| j                  j                  |       y)z$Used to print and log debug messagesr`   r   r   r   r   N)r   r   r   rb   r?   debug)rO   rd   r   s      rS   r   zSosNode.log_debug  sV    $$S)#A&$..!6("SE2#rW   c                    | j                   j                  dt        j                  j                  d            dk(  }| j                  r|r|S | j
                  j                  rdt        |       S | j                  rd| S |S )zIf we need to provide a sudo or root password to a command, then
        here we prefix the command with the correct bits
        USERr&   zsu -c zsudo -S )	r2   r   rk   rl   r   r*   become_rootr   r   )rO   r   is_roots      rS   _format_cmdzSosNode._format_cmd  sq     ..$$VRZZ^^F-CDN::'J99  E#J<((>>cU##
rW   c                 T   d}d}| j                   j                  Z| j                   j                  j                  | j                   j                        }|udj                  |d         }|d   r\|d   }nV| j                  | j                   j                  dd      }|d   dk(  r&|d	   j                         j                  d
      \  }}}|r=t        |j                  d            dk(  r|dz  }	 |d
|j                  d      d    z  }|| j                  d<   | j                  d   r"| j                  d| j                  d           n<| j                  | j                  j                   k(  s| j#                  d       d| _        y| j'                  d      rd| _        | j(                   d}| j                  |dd      }|d   dk(  r| j+                  |d	          | j'                  d      r| j-                          y# t        $ r}| j                  d|        Y d}~d}~ww xY w)zLQueries the node for information about the installed version of sos
        N.r   releaseTuse_containerr   rg   r   rh   -   z.0z#Unable to fully parse sos release: zsos version is z!sos is not installed on this nodeFz4.0z
sos reportz -l3.6)r.   container_version_commandpackage_managerpkg_versionsos_pkg_namejoinrn   r'   ro   lenrE   r   r;   r   r(   r*   primaryrF   rI   check_sos_versionr<   _load_sos_plugins_load_sos_presets)	rO   verrelpkgpkgs_rR   r   sosinfos	            rS   rN   zSosNode._load_sos_info#  s    99..6))++77		8N8NOChhs9~.y>i.C ##DII$G$G26$ $ HDH~""8n224::3?3399S>"a' tL399S>!,-.. $'i ==#MMODMM),D+EFG<<499#4#44 BC"DN!!%('DLc"""3dd"K8!""78#45!!%(""$1  L!DSEJKKLs   0G? ?	H'H""H'c                 L   | j                    d}| j                  |dd      }|d   dk(  ry|d   j                         D ]b  }|j                         j	                  d      s#|j                  d      d   j                         }| j                  d	   j                  |       d y y )
Nz --list-presetsTr   rg   r   rh   zname:r`   r"   )r<   rn   
splitlinesr'   
startswithro   r;   append)rO   r   r   linepnames        rS   r   zSosNode._load_sos_presetsW  s    o.s$$Gx=AH002 ;::<**73 JJw/288:EMM),33E:; rW   c                 R   d}d}d}d}d}|dz   |z   }|dz   |z   }|dz   |z   }	|dz   dz   }
| j                  ||      | j                  d<   | j                  ||      | j                  d	<   | j                  |	|      | j                  d
<   | j                  |
|d      | j                  d<   y )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_helpr;   )rO   r   ENABLEDDISABLEDALL_OPTIONSOPTIONSPROFILES	enableregdisregoptregproregs              rS   r   zSosNode._load_sos_plugins`  s    @BL?g%0	G#k17"X-G#f,#'#7#7	7#Ki $($8$8$Ij!#'#7#7#Hi $($8$8$$Oj!rW   c                    g }t        j                  ||t         j                        D ]j  }|j                         D ]U  }|s(	 |j	                         x}r|j                  |d          -|j	                  d      }	|j                  d |	D               W l |S # t        $ r}| j                  d|        Y d }~~d }~ww xY w)Nr   zError parsing sos help: ,c              3   ^   K   | ]%  }|j                         s|j                          ' y wN)r'   ).0ps     rS   	<genexpr>z*SosNode._regex_sos_help.<locals>.<genexpr>}  s     AQqwwyqwwyAs   --)	r   findallSr   ro   r   rE   r   extend)
rO   regexr   is_listr   resultr   lsrR   rs
             rS   r   zSosNode._regex_sos_helpq  s    jj6 
	BF))+ 	BI!%-2-JJr!u- 

3AJJA!AA	B
	B 
 % I)A#'GHHIs   &B	B> B99B>c                     	 | j                  d|        | j                  j                  |      S # t        $ r!}| j	                  d| d|        Y d}~yd}~ww xY w)z1Reads the specified file and returns the contentszReading file zException while reading : Nra   )r   rB   	read_filerE   rF   )rO   to_readrR   s      rS   r   zSosNode.read_file  s\    	MMM'34??,,W55 	NN5gYbFG	s   .1 	AAAc           
         | j                   r:| j                  d| j                  d   j                          | j                  d   S t	        i | j
                  j                  t               d| j                  j                  | j                  d            }|r!| j                  d|j                   d       |S | j                  d       t        )	z[Attempts to identify the host installation against supported
        distributions
        zusing local policy ru   Tz/etc/os-release)cachesysrootinitprobe_runtimeremote_execremote_checkzloaded policy z	 for hostz4Unable to determine host installation. Ignoring node)r   r   r)   rz   r   r*   r   r   rB   rn   r   rF   r   )rO   r.   s     rS   rG   zSosNode.determine_host_policy  s     ::MM%dll8&<&L&L%MNP<<))"dii&7&7jl"& $ ; ;!%0A!BD MMN4+?+?*@	JKKMN&&rW   c                     	 | j                   d   }t        |      t        |      k\  S # t        $ r}| j                  d|        Y d}~yd}~ww xY 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``
        r   zError checking sos version: NF)r;   r   rE   rF   )rO   r   	_node_verrR   s       rS   r   zSosNode.check_sos_version  sR    	i0I$Y/3DS3III 	NN9#?@	s   %( 	AA

Ac                 j    | j                   sy| j                   j                  j                  |      duS )z2Checks if a given package is installed on the nodeFN)r.   r   pkg_by_name)rO   r   s     rS   is_installedzSosNode.is_installed  s,    yyyy((44S9EErW   c                 (   | j                   s]| j                  sQ| j                  d       	 | j                  j	                  | j
                        }|s| j                  d       t        	 |r1| j                  j                  r| j                  j                  |      }|r| j                  |      }|r| j                  }	|	j                  |      }| j                  j                  |||||      S # t        $ r}| j                  d|         d}~ww xY w)a  Runs a given cmd, either via the SSH session or locally

        :param cmd:     The full command to be run
        :type cmd:      ``str``

        :param timeout: Time in seconds to wait for `cmd` to complete
        :type timeout:  ``int``

        :param use_shell: If a shell is needed to run `cmd`, set to True
        :type use_shell:  ``bool`` or ``auto`` for transport-determined

        :param use_container: Run this command in a container *IF* the host
                              is a containerized host
        :type use_container: ``bool``

        :param env: Pass environment variables to set for this `cmd`
        :type env:  ``dict``
        z-Node is disconnected, attempting to reconnectzFailed to reconnect to nodez!Error while trying to reconnect: N)rI   r   r   rB   	reconnectr4   r   rE   r.   rL   format_container_commandr   rs   updatern   )
rO   r   timeout	use_shellr   r   envreconnectedrR   _cmd_envs
             rS   rn   zSosNode.run_command  s    ( ~~djjNNJK"oo77G"NN#@A-- # TYY44))44S9C""3'C}}H//#&C**3C+46 	6  !B3%HIs   >C. .	D7DDc                 &   	 | j                         }|r| j                  |       n| j                  d       | j                  r| j	                         | _        | j                          y# t        $ r}| j                  d|        Y d}~3d}~ww xY w)z.Run an sos report on the node, then collect itz'Unable to determine path of sos archivezError during sos execution: N)execute_sos_commandfinalize_sos_pathrF   r7   retrieve_sosreportr8   rE   cleanup)rO   r   rR   s      rS   r#   zSosNode.sosreport  s    		A++-D&&t,HI}}!%!8!8!: 	  	ANN9#?@@	As   AA) )	B2BBc                 $    || j                   d   v S )z/Verifies if the given preset exists on the noder"   r;   )rO   presets     rS   _preset_existszSosNode._preset_exists      y111rW   c                 d    t        fd| j                  d   | j                  d   fD              S )z/Verifies if the given plugin exists on the nodec              3   &   K   | ]  }|v  
 y wr    )r   splugins     rS   r   z)SosNode._plugin_exists.<locals>.<genexpr>  s      E16Q; E   r   r    )anyr;   rO   r  s    `rS   _plugin_existszSosNode._plugin_exists  s;     Ey)A)-z)B)D E E 	ErW   c                 $    || j                   d   v S )z6Checks to see if the plugin is default enabled on noder   r  r  s     rS   _check_enabledzSosNode._check_enabled  r  rW   c                 $    || j                   d   v S )z7Checks to see if the plugin is default disabled on noder    r  r  s     rS   _check_disabledzSosNode._check_disabled  s    z222rW   c                     |j                  d      d   }| j                  |      sy| j                  |      r|| j                  j                  vry| j                  |      r|| j                  d   v S y)zAttempts 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)ro   r  r!  r*   enable_pluginsr  r;   )rO   optplugs      rS   _plugin_option_existszSosNode._plugin_option_exists  sq    
 yy~a ""4(  &DII444t$$--	222 rW   c                 8     dj                   fd|D              S )z^Returns a comma delimited list for sos plugins that are confirmed
        to exist on the noder   c              3   F   K   | ]  }j                  |      s|  y wr   )r  )r   orO   s     rS   r   z,SosNode._fmt_sos_opt_list.<locals>.<genexpr>  s     Ba4+>+>q+ABs   !!)r   )rO   r*   s   ` rS   _fmt_sos_opt_listzSosNode._fmt_sos_opt_list  s     xxB4BBBrW   c                     || _         y)zVExpose the node to the cluster profile determined for the
        environment
        N)r/   )rO   r/   s     rS   set_clusterzSosNode.set_cluster  s     rW   c                    | j                   j                  rQ| j                  s| j                   j                  | _        n)| j                  d| j                   j                   d       | j                   j                  rE| j                   j                  D ],  }|| j
                  vs| j
                  j                  |       . | j                   j                  rP| j                   j                  D ]7  | j                  vs| j                   j                     | j                  <   9 | j                   j                  rs| j                   j                  D ]Z  t        fd| j                  D              r" d| j                   j                      }| j                  j                  |       \ | j                   j                  |       r;| j                   j                  5  | j                   j                  |        ddd       y| j                   j                  5  | j                   j                  |        ddd       y# 1 sw Y   yxY w# 1 sw Y   yxY w)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.
        zCluster specified preset z> but user has also defined a preset. Using user specification.c              3   &   K   | ]  }|v  
 y wr   r  )r   r)  r$  s     rS   r   z2SosNode.update_cmd_from_cluster.<locals>.<genexpr>2  s     ;3!8;r  rj   N)r/   
sos_presetr  r   sos_pluginsr#  r   r   sos_plugin_optionsr  plugoptscheck_node_is_primarylockset_primary_optionsset_node_options)rO   r%  optionr$  s      @rS   update_cmd_from_clusterzSosNode.update_cmd_from_cluster  s    <<"";;"ll559!%!8!8 9 :LL M <<##00 5t222''..t45 <<##||// Jd...,0LL,D,DS,ID$$S)J
 <<**||66 1;T]];; #uAdll&E&Ec&J%KLFMM((01 <<--d3"" 70067 7 "" 4--d34 47 74 4s   $IIIIc                 l   t        | j                  j                        | _        t        | j                  j                        | _        t        | j                  j                        | _        t        | j                  j
                        | _        t        | j                  j                        | _        y)zFrom 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#  r2  r  rV   s    rS   r+   zSosNode._assign_config_opts>  su    
 !!7!78 !7!78"499#;#;<TYY//0499++,rW   c           	         | j                   d   }| j                         }|r| dt        |       d}g }| j                  d      rS| j                  j
                  dk7  r:|j                  dt        t        | j                  j
                                      | j                  d      rP| j                  j                  r:|j                  dt        t        | j                  j                                      | j                  d      r| j                  j                  r|j                  d	       | j                  j                  r|j                  d
       | j                  j                  r1|j                  dt        | j                  j                                | j                  d      r| j                  j                  r@|j                  dt        dj                  | j                  j                                      | j                  j                  r@|j                  dt        dj                  | j                  j                                      | j                  d      rP| j                  j                  r:|j                  dt        t        | j                  j                                      | j                  d      s| j                  d      r| j                  j                   dk7  r(|j                  d| j                  j                           | j                  j"                  r(|j                  d| j                  j"                          | j                  d      re| j                  j$                  r(|j                  d| j                  j$                          | j                  j&                  r|j                  d       | j)                          |j+                  dt,        j.                  j                  | j0                  j2                  | j4                              }| j6                  D ],  }| j6                  |   }|j                  d| d|r|nd        . | j8                  r| j8                  D cg c]J  }| j;                  |j=                  d      d         r%| j?                  |j=                  d      d         r|L }}|r3|j                  d t        dj                  d! |D                            | j@                  rb| jC                  | j@                        r(|j                  d"t        | j@                                n| jE                  d#| j@                   d$       | jF                  r%| jF                  D cg c]  }| j;                  |      s| }}tI        |      tI        | jF                        k7  r?tK        tM        | jF                        tM        |      z
        }	| jE                  d%|	 d&       | jO                  | jF                        }
|
r|j                  d't        |
              | ddj                  |       | _(        | jS                  d(| jP                          | jT                  jW                  d)| jP                         y*| jX                  r| jX                  D cg c]  }| j[                  |      s| }}tI        |      tI        | jX                        k7  r?tK        tM        | jX                        tM        |      z
        }| jE                  d+| d,       | jO                  |      }|r|j                  d-t        |              | j\                  r| j\                  D cg c]4  }|| jX                  vr$| j_                  |      r| j;                  |      r|6 }}tI        |      tI        | j\                        k7  r?tK        tM        | j\                        tM        |      z
        }| jE                  d.| d/       | jO                  |      }|r|j                  d0t        |              | ddj                  |       | _(        | jS                  d(| jP                          | jT                  jW                  d)| jP                         y*c c}w c c}w c c}w c c}w )1zZUse host facts and compare to the cluster type to modify the sos
        command if neededr    r      z
--threads=z3.7z--plugin-timeout=z3.8z--allow-system-changesz--no-env-varsz--since=z4.1z--skip-commands=r   z--skip-files=z4.2z--cmd-timeout=z4.3z4.2-13r[   z--container-runtime=z--namespaces=z4.5.2z--journal-size=z--low-priorityr#   z--ra   r   r   rj   z-k c              3       K   | ]  }|  y wr   r  )r   r)  s     rS   r   z+SosNode.finalize_sos_cmd.<locals>.<genexpr>  s     4E1Q4Es   z	--preset=zRequested to enable preset z" but preset does not exist on nodezRequested plugins z. were requested to be enabled but do not existz--only-plugins=zFinal sos command set to rw   NzRequested to skip plugins z%, but plugins are already not enabledz--skip-plugins=zRequested to enable plugins z1, but plugins are already enabled or do not existz--enable-plugins=)0r;   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_priorityr8  replacerk   r   r.   sos_bin_pathr<   r   r2  r  ro   r&  r  r  r   r;  r   r:  setr*  r   r   rx   ry   r<  r  r#  r!  )rO   r   labelsos_optsr$  rr   r)  r*   plugsnot_onlyonlyskipnot_skipskiplnnot_onenables                   rS   finalize_sos_cmdzSosNode.finalize_sos_cmdI  sE    --	*((* 	5<.2G !!%(yy  A%*U3tyy7H7H3I-J,K LM !!%(yy'''c$))2J2J.K(L'MNP !!%(yy-- 89yy$$0yy(5+A*B CD!!%(yy&& 2#($))2I2I)J#K"L!N Oyy###E#((4993G3G*H$I#JK !!%(yy$$$U3tyy/D/D+E%F$GH
 !!%(D,B,B8,Lyy**f4*499+F+F*GH yy###DII$8$8#9: !!'*yy%%/$))2H2H1I JKyy%% 01$$&//GGLL//>

 ## 	?C##C(DOObQtt&<=>	? ==#}} E!**1773<?;221773<?C  ED E #eCHH4E4E,E&F%G HI;;""4;;/)E$++,>+? @A!<T[[M J?  ? @  $ 1 1L1T5H5H5KQLEL5zS!2!233D$5$5 6U CD!3H: >@  @ A))$*;*;<D/%+ ?@%Ya(:';<DLMM5dll^DEMM##$7F#00K!D4G4G4JAKDK4yC 1 122D$5$5 6T BC!;H: FA  A B++D1F/%- AB#22 L! 1 11,,Q/D4G4G4J  LD L 4yC 3 344c$"5"56TBC!=fX FM  M N++D1F"3E&M? CD!!CHHX$6#781$,,@A 3T\\BkE M LLs%   Ab:b?.b?cc9c	c                    d}|| j                   j                  |       z  }| j                  j                  r5||s| j                  j                   nd| j                  j                   z  }|sy| j	                  d|        | j                  d      rd}n%d}| j                  j                  d      d	    d| }| d
| S )z?Determine what, if any, label should be added to the sos reportra   r   NzLabel for sos report set to r   z--labelz--namer   r   rj   )r/   get_node_labelr*   rR  r   r   r(   ro   )rO   rR  lcmds      rS   rA  zSosNode.determine_sos_label  s    ,,T2299??%)tyy/02E 5eW=>!!%(DD||))#.q12!E7;Eq  rW   c                 2   | j                   j                  }|r|j                  |d      }|j                         d   }| j	                  d|        || _        |j                  d      d   | _        | j                  j                  d| j                         y)z\Use host facts to determine if we need to change the sos path
        we are retrieving fromra   r   zFinal sos path: /collected_archiveN)	r.   sos_path_striprO  ro   r   r7   archiverx   ry   )rO   r   pstrips      rS   r  zSosNode.finalize_sos_path  s}     ))<<+Dzz|A(/0zz#r* 3T\\BrW   c                 |    |dk(  ry|dk(  rd|v ry|dk(  ryt        |      dkD  r|j                  d	      dd S d
| S )Nrb  z+sos report process received SIGKILL on noder`   sudozsudo attempt failed   z4sos report terminated unexpectedly. Check disk spacer   
zsos exited with code )r   ro   )rO   rcstdouts      rS   determine_sos_errorzSosNode.determine_sos_error  sV    8@7,9Iv;?<<%a**&rd++rW   c                    | j                  d       	 d}d}d}| j                  j                  rd| j                  j                   }n| j                  j                  r}| j                  sXd| j
                  j                  d      d    d}| j                  j                  | j                  j                  |       d| }nd| j                  j                   }|r| j                   d	| | _
        | j                  | j                  | j                  j                  d
d
d
| j                        }|d   dk(  r;|d   j                         D ]s  }t        j                  |d      r|j!                         }|j#                  d      r|j                  d      d   }O|j#                  d      sa|j                         d   }u |r| j$                  j'                  d|       t)        |      dk(  r| j$                  j'                  dd       |S t)        |      dk(  r| j$                  j'                  dd       |S | j$                  j'                  dd       |S | j$                  j'                  dd       |S | j+                  |d   |d         }| j-                  d|d    d|d           t/        |      # t0        $ r | j3                  d        t.        $ r.}| j5                  d|        | j                  d |         d!}~ww xY w)"z2Run sos report and capture the resulting file pathzGenerating sos report...Fra   z--config-file=z/tmp/ra  rb  z.confr>  T)r  r  r   r   r  rg   r   rh   z*sosreport-*tar*)z sha256	z md5		r`   zThe checksum is: r   checksum    checksum_typemd5@   sha256unknownzError running sos report. rc = z msg = zTimeout exceededz'Exception during sos report execution: zError running sos report: N)r   r*   r,   r-   r   r   ro   rB   copy_file_to_remoteconfig_filer   rn   r  r1   r   fnmatchr'   r   rx   ry   r   rm  r   rE   r   rF   r   )rO   r   rp  config_file_argremote_configr   r   rR   s           rS   r  zSosNode.execute_sos_command  s   ./7	DH Oyy))$24993M3M2N"O..zz&+DKK,=,=c,B2,F+Gu$MMOO77		--%' )7}o&FO ))>)>(?@ $"&,,q0AB""4<<+/99+<+<-1-115'+'8'8 # :C 8}!M446 3Dt-?@#zz|'>?#'::d#3A#6)<=#'::<?3 MM++JA8}*//G K X",//J K //K K MM++OYG K	 ..s8}c(mL@"%h-H H In$& 	NN-. 	MMCC5IJKK4SE:;	s8   FJ7 0AJ7 +J7 ;J7 J7 7A J7 7#L)LLc                    | j                   dz   }t        j                  j                  ||j	                  d      d         }	 | j                  |      r3| j                  d| d|        | j                  j                  ||      S | j                  d| d       y# t        $ r!}| j                  d| d	|        Y d
}~yd
}~ww xY w)z7Copies the specified file from the host to our temp dirra  rb  zCopying remote z
 to local zAttempting to copy remote file %, but it does not exist on filesystemFzFailed to retrieve r   N)r   rk   r   r   ro   r   r   rB   retrieve_filer   rE   )rO   r   destdirdestrR   s        rS   r~  zSosNode.retrieve_file5  s    ++#ww||GTZZ_R%89		%vZyIJ44T4@@NN<TF C; ; < 	NN0b>?	s   AB B 	C&CCc                    dj                  |j                               }	 t        |j                  d            dk  r| j                  d| d       y| j	                  |      r4| j                  d|        d| }| j                  |d	
      }|d   dk(  S | j                  d| d       y# t        $ r!}| j                  d| d|        Y d}~yd}~ww xY w)z|Removes the spciefied file from the host. This should only be used
        after we have retrieved the file already
        ra   ra  r   zRefusing to remove path z0: appears to be incorrect and possibly dangerousFzRemoving file zrm -f Tr   rg   r   z!Attempting to remove remote file r}  zFailed to remove r   N)r   ro   r   r   r   r   rn   rE   )rO   r   r   r   es        rS   remove_filezSosNode.remove_fileD  s     wwtzz|$	4::c?#q(!9$ @E  E F%tf56tfo&&sd&;8}))NN>tf E= = > 	NN.tfBqc:;	s$   2B/ AB/ B/ /	C8CCc                 \   | j                   s| j                  j                  r	 | j                  | j                         | j                  d| j                          | j                  d       	 | j                  | j                        }|rI| j                  d       | j                  j                  | j                  j                  d      d          y	| j                  d
       y# t
        $ r | j                  d       Y yw xY w# t
        $ r}| j                  |       Y d}~yd}~ww xY w)z,Collect the sos report archive from the nodezFailed to make archive readableFzRetrieving sos report from zRetrieving sos report...Nz!Successfully collected sos reportra  rb  TzFailed to retrieve sos report)r   r*   r   make_archive_readabler7   rE   rF   r   r(   r   r~  r:   r   ro   )rO   rp   rR   s      rS   r  zSosNode.retrieve_sosreportZ  s    >>TYY22**4==9 	3DLL>BC./	$$T]]3C KK;<NN!!$--"5"5c":2">?34  @A  	NN3	s)   C' /D 'DD	D+D&&D+c                     | j                   | j                  ryd| j                   vr | j                  d| j                    d       y| j                  | j                         }|s| j	                  d       yy)zvRemove the sos report archive from the node, since we have
        collected it and it would be wasted space otherwiseNr#   zNode sos report path z/ looks incorrect. Not attempting to remove pathzFailed to remove sos report)r7   r   r   r  rF   )rO   removeds     rS   remove_sos_archivezSosNode.remove_sos_archivep  sv     == DJJ dmm+NN24==/ BF F G""4==1NN89 rW   c                     | j                          | j                  r'dD ]"  }| j                  | j                  |z         s" n | j                  j	                         }|r| j                  |d       yy)z<Remove the sos archive from the node once we have it locally)z.sha256z.md5Tr   N)r  r7   r  r.   set_cleanup_cmdrn   )rO   extr  s      rS   r  zSosNode.cleanup  sm    !==* ##DMMC$78 ))++-W5 rW   c                 $   |D ]  }	 | j                   s| j                  j                  r	 | j                  |       | j                  |      }|r?| j                  j                  |j                  d      d          | j                  |       n| j                  d|         y# t        $ r5}| j                  d|        | j                  d| d|        Y d}~d}~ww xY w# t        $ r!}d| }| j                  |       Y d}~d}~ww xY w)z4Collect the file created by a cluster outside of soszUnable to retrieve file zFailed to make file z readable: Nra  rb  z/Error collecting additional data from primary: )r   r*   r   r  rE   rF   r   r~  r:   r   ro   r  )rO   	filenamesfilenamerR   rp   r  rd   s          rS   collect_extra_cmdzSosNode.collect_extra_cmd  s   ! 	$H$>>TYY%:%:!228< ((2NN))(..*=b*AB$$X.NN%=hZ#HI	$
 % !)A('LM)=hZ H4475(: ; 	!  $GsKs##$s@   "C%B$A&C%$	C"-+CC%C""C%%	D.D

Dc                     d| }| j                  |dd      }|d   dk(  ryd}| j                  |||d   fz         t        )zUsed 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 
   T)r  r   rg   r   z6Exception while making %s readable. Return code was %s)rn   rF   rE   )rO   filepathr   r   rd   s        rS   r  zSosNode.make_archive_readable  sZ     8*%sB$?x=AFshH667rW   )NNT)F)   r[   FFN)5__name__
__module____qualname__rT   propertyrI   rJ   rA   re   rs   r|   rK   rM   r   r   rb   r   r   r   rF   r   r   rN   r   r   r   r   rG   r   r  rn   r#   r  r  r  r!  r&  r*  r,  r8  r+   r\  rA  r  rm  r  r~  r  r  r  r  r  r  r  rW   rS   r   r   *   sD   CG CJ  %
8"?  C
&P
  
3- 2h;P"'$$F IN-1&6P2E
23"C
&4P	-BCH!(
C
,:x,,:	6$,rW   r   )ry  r   r=   rk   r   shlexr   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   r]   r   r  rW   rS   <module>r     sc       	 	   0 1 F 9 3 >A A , (
 
B BrW   