U
    -_g%                     @   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                       s~   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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|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sjd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rdndd | j dddd!d"S )#zBased 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
        ZPodZv1.r   z-sos-collector)name	namespacezsystem-cluster-criticalZhost/Z	Directory)pathtype)r   ZhostPathrunz/runZvarlogz/var/logz
machine-idz/etc/machine-idZFilezsos-collector-tmpz&registry.redhat.io/rhel8/support-toolsz	/bin/bashZHOSTz/host)r   value)r   Z	mountPathT)Z
privilegedZ	runAsUser)
r   imageZcommandenvZ	resourcesZvolumeMountsZsecurityContextstdinZ	stdinOnceZttyZAlwaysZIfNotPresentZNever)ZvolumesZ
containersZimagePullPolicyZrestartPolicyZnodeNameZhostNetworkZhostPIDZhostIPC)ZkindZ
apiVersionmetadataZpriorityClassNamespec)Zaddresssplitr
   Zoptsr   Zforce_pull_imager   r   r   r   get_node_pod_configA   s     +zOCTransport.get_node_pod_configc              
   C   sr  t dsdS |  }|d d | _tj| jd\}| _t|ddd}t	|| W 5 Q R X | 
d	| j d
 td| j }|d dksd| j d|d kr| d | 
d|d   dS | 
d| j d z:| jd| j ddd}|d dks| d W dS W nZ tk
r4   | d Y dS  tk
rl } z| d|  W Y dS d }~X Y nX dS )Nr   Fr!   r   )dirwzutf-8)encodingz"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   r%   r   tempfileZmkstempZtmpdirpod_tmp_confopenjsondump	log_debugr   Z	log_errorr   r   	Exception)r   ZpasswordZpodconffdcfileoutr   errr   r   r   _connect   s@    




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   r=      s    
z OCTransport._format_cmd_for_exec   FNc                    s"   |r|d7 }t  j||||ddS )N
   F)	use_shell)r<   run_command)r   r   r-   Z	need_rootr   rB   r>   r   r   rC      s     zOCTransport.run_commandc                 C   sX   t j| jrt | j | d| j }d|d krT| d| j d|  dS dS )Nzdelete pod Zdeletedr*   zCalling delete on pod 'z
' failed: FT)osr   existsr/   unlinkr   r   r3   )r   Zremovedr   r   r   _disconnect   s    zOCTransport._disconnectc                 C   s   d| j  d| j dS )Nr   r:   z -- /bin/bash -c)r
   r   r$   r   r   r   remote_exec   s    zOCTransport.remote_execc              	   C   sV   | j ddd}d|d krdnd}| j d| d	| d	| j d
| dd}|d dkS )Ncp --retriesTstderrunknown flagr*    --retries=5cp r	   :   r,   r   r   r   r   fnamedestresultflagsr   r   r   r   _copy_file_to_remote   s     z OCTransport._copy_file_to_remotec              
   C   sR   | j ddd}d|d krdnd}|  d| d	| j d
| d	| }|d dkS )NrI   TrJ   rL   r*   rM   rN   rO   r	   rP   r   r   r   rR   r   r   r   _retrieve_file   s    $zOCTransport._retrieve_file)r@   FNF)__name__
__module____qualname____doc__r   r
   r   propertyr   r%   r9   r=   rC   rG   rH   rW   rX   __classcell__r   r   r>   r   r      s"   	
`*  

r   )
r1   r.   rD   Zsos.collector.transportsr   Zsos.utilitiesr   r   r   r   r   r   r   r   <module>   s
   