�
i��d�
� �F � d dl Z d dlmZ d dlmZ d dlmZ G d� de� Zy)� N)�ContainerRuntime)�sos_get_command_output)�quotec �2 � e Zd ZdZdZdZd� Zd d�Zd� Zd� Z y)
�CrioContainerRuntimez-Runtime class to use for systems running crio�crio�crictlc � � y)NF� )�selfs �</usr/lib/python3/dist-packages/sos/policies/runtimes/crio.py�check_can_copyz#CrioContainerRuntime.check_can_copy s � �� c � � g }| j �d|rdnd�d�}| j rgt || j j �� }|d dk( r>t j |d � }|d D ] }|j |d
|d d f� � |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� containers r
�get_containersz#CrioContainerRuntime.get_containers s� � � �
�%)�[�[�'�$�r�2I�J���;�;�(��d�k�k�6I�6I�J�C��8�}��!��:�:�c�(�m�4��!)�,�!7� J�I��%�%�"�4��)�J�*?��*G�H�J�J� �r c �� � g }| j r�t d| j z | j j �� }|d dk( r�t j |d � }|d D ]{ }t |d � dkD r!|d D ] }|j ||d f� � �5t |d � dk( rd
}n|d d j d� d }|j |dz |d f� �} |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 �images�repoTagsr �repoDigestsz<none>�@z:<none>)
r r r r r r r �lenr �split)r r( r# r$ �image�repo_tag�
image_names r
�
get_imageszCrioContainerRuntime.get_images, s� � � ���;�;�(�)<�t�{�{�)J�04���0C�0C�E�C��8�}��!��:�:�c�(�m�4��%�h�/�
M�E��5��,�-��1�(-�j�(9� C�H�"�M�M�8�U�4�[�*A�B�C� �u�]�3�4��9�)1�J�).�}�)=�a�)@�)F�)F�s�)K�A�)N�J��
�
�z�I�'=�u�T�{�&K�L�
M� �
r c �t � |rt |� }n|}| j |� }|�| j �d|�d|��S dS )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``
� r )r �get_container_by_name�run_cmd)r r% �cmd�quotecmd�
quoted_cmd�container_ids r
�fmt_container_cmdz&CrioContainerRuntime.fmt_container_cmdE sM � � ��s��J��J��1�1�)�<��,8�,D� "�\�\�<�'�)� M�JL� Mr N)F)
�__name__�
__module__�__qualname__�__doc__r r r r& |