o
    O6ufM$  ã                   @   sL   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ G dd„ deƒZ	dS )é    N)ÚRemoteTransport)Úis_executableÚsos_get_command_outputÚSoSTimeoutErrorc                       sz   e Zd ZdZdZdZdd„ Zedd„ ƒZdd	„ Z	d
d„ Z
‡ fdd„Z		d‡ fdd„	Zdd„ Zedd„ ƒZdd„ Z‡  ZS )ÚOCTransportaq  
    This transport leverages the execution of commands via a locally
    available and configured ``oc`` binary for OCPv4 environments.

    The location of the oc binary MUST be in the $PATH used by the locally
    loaded SoS policy. Specifically this means that the binary cannot be in the
    running user's home directory, such as ~/.local/bin.

    OCPv4 clusters generally discourage the use of SSH, so this transport may
    be used to remove our use of SSH in favor of the environment provided
    method of connecting to nodes and executing commands via debug pods.

    The debug pod created will be a privileged pod that mounts the host's
    filesystem internally so that sos report collections reflect the host, and
    not the container in which it runs.

    This transport will execute within a temporary 'sos-collect-tmp' project
    created by the OCP cluster profile. The project will be removed at the end
    of execution.

    In the event of failures due to a misbehaving OCP API or oc binary, it is
    recommended to fallback to the control_persist transport by manually
    setting the --transport option.
    Úoczsos-collect-tmpc                 K   s   t d| j› d|› �fi |¤ŽS )z\Format and run a command with `oc` in the project defined for our
        execution
        úoc -n ú )r   Úproject)ÚselfÚcmdÚkwargs© r   ú=/usr/lib/python3/dist-packages/sos/collector/transports/oc.pyÚrun_oc1   s
   ÿþzOCTransport.run_occ                 C   s   |   d| j› �¡}|d dkS )Nz,wait --timeout=0s --for=condition=ready pod/Ústatusr   ©r   Úpod_name)r   Úupr   r   r   Ú	connected:   s   
ÿzOCTransport.connectedc                 C   sÚ   dd| j  d¡d › d�| jdœddd	d
dœdœddd
dœdœddd
dœdœddddœdœgd| jjs5dn| jjdgdddœgi dddœdddœdddœdddœgdddœddddœ
g| jjradndd | j dddd!œd"œS )#z©Based on our template for the debug container, add the node-specific
        items so that we can deploy one of these on each node we're collecting
        from
        ÚPodÚv1Ú.r   z-sos-collector)ÚnameÚ	namespacezsystem-cluster-criticalÚhostú/Ú	Directory)ÚpathÚtype)r   ÚhostPathÚrunz/runÚvarlogz/var/logz
machine-idz/etc/machine-idÚFilezsos-collector-tmpz&registry.redhat.io/rhel8/support-toolsz	/bin/bashÚHOSTz/host)r   Úvalue)r   Ú	mountPathT)Ú
privilegedÚ	runAsUser)
r   ÚimageÚcommandÚenvÚ	resourcesÚvolumeMountsÚsecurityContextÚstdinÚ	stdinOnceÚttyÚAlwaysÚIfNotPresentÚNever)ÚvolumesÚ
containersÚimagePullPolicyÚrestartPolicyÚnodeNameÚhostNetworkÚhostPIDÚhostIPC)ÚkindÚ
apiVersionÚmetadataÚpriorityClassNameÚspec)ÚaddressÚsplitr
   Úoptsr)   Úforce_pull_image©r   r   r   r   Úget_node_pod_configA   s†   þþþþþþþþþê ÿÿþÿþþþþóþÚÿ+±øzOCTransport.get_node_pod_configc              
   C   sv  t dƒsdS |  ¡ }|d d | _tj| jd�\}| _t|dƒ�}t 	||¡ W d   ƒ n1 s1w   Y  |  
d| j› d�¡ td	| j› �ƒ}|d
 dksYd| j› d�|d vrj|  d¡ |  
d|d › �¡ dS |  
d| j› d�¡ z| jd| j› d�dd�}|d
 dks�|  d¡ W dS W dS  ty    |  d¡ Y dS  tyº } z|  d|› �¡ W Y d }~dS d }~ww )Nr   Fr?   r   )ÚdirÚwz"Starting sos collector container 'ú'zoc create -f r   r   zpod/z createdÚoutputz Unable to deploy sos collect podzDebug pod deployment failed: zPod 'z=' successfully deployed, waiting for pod to enter ready statezwait --for=condition=Ready pod/z --timeout=30sé(   )Útimeoutz"Pod not available after 30 secondsz'Timeout while polling for pod readinessz)Error while waiting for pod to be ready: T)r   rG   r   ÚtempfileÚmkstempÚtmpdirÚpod_tmp_confÚopenÚjsonÚdumpÚ	log_debugr   Ú	log_errorr   r   Ú	Exception)r   ÚpasswordÚpodconfÚfdÚcfileÚoutr   Úerrr   r   r   Ú_connect¡   sH   ÿ
ÿÿ
ý
þ
ù
€þzOCTransport._connectc                    s0   |  d¡rd| j› d| j› d|› �S tƒ  |¡S )Nr   r   ú exec --request-timeout=0 z -- chroot /host )Ú
startswithr
   r   ÚsuperÚ_format_cmd_for_exec)r   r   ©Ú	__class__r   r   rb   Ë   s   

ÿÿz OCTransport._format_cmd_for_execé´   FNc                    s"   |r|d7 }t ƒ j||||dd�S )Né
   F)Ú	use_shell)ra   Úrun_command)r   r   rM   Ú	need_rootr+   rg   rc   r   r   rh   Ñ   s
   ÿzOCTransport.run_commandc                 C   sX   t j | j¡rt  | j¡ |  d| j› �¡}d|d vr*|  d| j› d|› �¡ dS dS )Nzdelete pod ÚdeletedrK   zCalling delete on pod 'z
' failed: FT)Úosr   ÚexistsrQ   Úunlinkr   r   rU   )r   Úremovedr   r   r   Ú_disconnectÜ   s   ÿzOCTransport._disconnectc                 C   s   d| j › d| j› d�S )Nr   r_   z -- /bin/bash -c)r
   r   rF   r   r   r   Úremote_execæ   s   
ÿzOCTransport.remote_execc              
   C   sR   | j ddd�}d|d v rdnd}|   d|› d	| j› d
|› d	|› �¡}|d dkS )Nzcp --retriesT)Ústderrzunknown flagrK   Ú z--retries=5zcp r	   ú:r   r   r   )r   ÚfnameÚdestÚresultÚflagsr   r   r   r   Ú_retrieve_fileë   s   $zOCTransport._retrieve_file)re   FNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r   Úpropertyr   rG   r^   rb   rh   ro   rp   rx   Ú__classcell__r   r   rc   r   r      s"    	
`*ÿ

r   )
rS   rN   rk   Úsos.collector.transportsr   Úsos.utilitiesr   r   r   r   r   r   r   r   Ú<module>   s   
