U
    i©¹dˆ  ã                   @   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)ÚContainerRuntime)Úsos_get_command_output)Úquotec                   @   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 crioZcrioZ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   sv   g }d| j |rdndf }| jrrt|| jjd}|d dkrrt |d ¡}|d D ]}| |d	 |d
 d f¡ qR|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%s ps %s -o jsonz-aÚ ©ÚchrootÚstatusr   ÚoutputÚ
containersÚidZmetadataÚname)ÚbinaryÚactiver   ÚpolicyÚsysrootÚjsonÚloadsÚappend)r   Zget_allr   Z_cmdÚoutÚout_jsonÚ	containerr   r   r   Úget_containers   s    ÿz#CrioContainerRuntime.get_containersc                 C   sÀ   g }| j r¼td| j | jjd}|d dkr¼t |d ¡}|d D ]x}t|d ƒdkrx|d D ]}| ||d f¡ q^qBt|d	 ƒdkrŽd
}n|d	 d  	d¡d }| |d |d f¡ qB|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%s images -o jsonr   r   r   r   ÚimagesZrepoTagsr   ZrepoDigestsz<none>ú@z:<none>)
r   r   r   r   r   r   r   Úlenr   Úsplit)r   r   r   r   ZimageZrepo_tagZ
image_namer   r   r   Ú
get_images,   s     
ÿzCrioContainerRuntime.get_imagesc                 C   s8   |rt |ƒ}n|}|  |¡}|dk	r4d| j||f 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``
        Nz%s %s %sr
   )r   Zget_container_by_nameZrun_cmd)r   r   ÚcmdZquotecmdZ
quoted_cmdZcontainer_idr   r   r   Úfmt_container_cmdE   s    

ÿ
ÿÿz&CrioContainerRuntime.fmt_container_cmdN)F)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r	   r   r!   r#   r   r   r   r   r      s   
r   )r   Zsos.policies.runtimesr   Zsos.utilitiesr   Zpipesr   r   r   r   r   r   Ú<module>
   s   