o
    O6ufm  ã                   @   s@   d dl Z d dlmZ d dlmZ d dlmZ G dd„ deƒZdS )é    N)Úquote)ÚContainerRuntime)Úsos_get_command_outputc                   @   s:   e Zd ZdZdZdZdd„ Zddd„Zd	d
„ Zdd„ Z	dS )ÚCrioContainerRuntimez-Runtime class to use for systems running crioÚcrioÚcrictlc                 C   s   dS )NF© )Úselfr   r   ú</usr/lib/python3/dist-packages/sos/policies/runtimes/crio.pyÚcheck_can_copy   s   z#CrioContainerRuntime.check_can_copyFc                 C   sz   g }| j › d|r
dnd› d�}| jr;t|| jjd�}|d dkr;t |d ¡}|d	 D ]}| |d
 |d d f¡ q+|S )zœGet a list of containers present on the system.

        :param get_all: If set, include stopped containers as well
        :type get_all: ``bool``
        z ps z-aÚ z -o json©ÚchrootÚstatusr   ÚoutputÚ
containersÚidÚmetadataÚname)ÚbinaryÚactiver   ÚpolicyÚsysrootÚjsonÚloadsÚappend)r	   Úget_allr   Ú_cmdÚoutÚout_jsonÚ	containerr   r   r
   Úget_containers   s   ÿz#CrioContainerRuntime.get_containersc                 C   sÂ   g }| j r_t| j› d�| jjd�}|d dkr_t |d ¡}|d D ]<}t|d ƒdkr=|d D ]}| ||d f¡ q0q"t|d	 ƒdkrHd
}n|d	 d  	d¡d }| |d |d f¡ q"|S )z”Get a list of images present on the system

        :returns: A list of 2-tuples containing (image_name, image_id)
        :rtype: ``list``
        z images -o jsonr   r   r   r   ÚimagesÚrepoTagsr   ÚrepoDigestsz<none>ú@z:<none>)
r   r   r   r   r   r   r   Úlenr   Úsplit)r	   r"   r   r   ÚimageÚrepo_tagÚ
image_namer   r   r
   Ú
get_images,   s"   ÿÿzCrioContainerRuntime.get_imagesc                 C   s>   |rt |ƒ}n|}|  |¡}|dur| j› d|› d|› �S dS )aÀ  Format a command to run inside a container using the runtime

        :param container: The name or ID of the container in which to run
        :type container: ``str``

        :param cmd: The command to run inside `container`
        :type cmd: ``str``

        :param quotecmd: Whether the cmd should be quoted.
        :type quotecmd: ``bool``

        :returns: Formatted string to run `cmd` inside `container`
        :rtype: ``str``
        Nú r   )r   Úget_container_by_nameÚrun_cmd)r	   r    ÚcmdÚquotecmdÚ
quoted_cmdÚcontainer_idr   r   r
   Úfmt_container_cmdE   s   

ÿÿz&CrioContainerRuntime.fmt_container_cmdN)F)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r+   r3   r   r   r   r
   r      s    
r   )r   Úshlexr   Úsos.policies.runtimesr   Úsos.utilitiesr   r   r   r   r   r
   Ú<module>   s
   	