U
    -_gm                     @   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 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   sz   g }| j  d|rdnd d}| jrvt|| jjd}|d dkrvt|d }|d	 D ]}||d
 |d d f qV|S )zGet 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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 rt| j d| jjd}|d dkrt|d }|d D ]x}t|d dkrz|d D ]}|||d f q`qDt|d	 dkrd
}n|d	 d 	dd }||d |d f qD|S )zGet 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   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   s>   |rt |}n|}| |}|dk	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   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shlexr   Zsos.policies.runtimesr   Zsos.utilitiesr   r   r   r   r   r   <module>
   s   