
    -_gm                     F    d dl Z d dlmZ d dlmZ d dlmZ  G d de      Zy)    N)quote)ContainerRuntime)sos_get_command_outputc                   2    e Zd ZdZdZdZd Zd	dZd Zd 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|rdnd d}| j                  rgt        || j                  j                        }|d   dk(  r>t        j                  |d         }|d	   D ]  }|j                  |d
   |d   d   f         |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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     
++d74";8D;;(dkk6I6IJC8}!::c(m4!),!7 JI%%"4)J*?*GHJJ r   c                    g }| j                   rt        | j                   d| j                  j                        }|d   dk(  rt        j                  |d         }|d   D ]{  }t        |d         dkD  r!|d   D ]  }|j                  ||d   f        5t        |d	         dk(  rd
}n|d	   d   j                  d      d   }|j                  |dz   |d   f       } |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   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_imageszCrioContainerRuntime.get_images,   s     ;;(DKK=)H040C0CEC8}!::c(m4%h/ 
ME5,-1(-j(9 CH"MM8U4[*ABC u]349)1J).})=a)@)F)Fs)KA)NJzI'=uT{&KL
M r   c                 t    |rt        |      }n|}| j                  |      }|| 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``
         r   )r   get_container_by_namerun_cmd)r   r%   cmdquotecmd
quoted_cmdcontainer_ids         r   fmt_container_cmdz&CrioContainerRuntime.fmt_container_cmdE   sR     sJJ11)<+ <<.,q= 	513	5r   N)F)
__name__
__module____qualname____doc__r   r   r   r&   r1   r:   r   r   r   r   r      s#    7DF$25r   r   )r   shlexr   sos.policies.runtimesr   sos.utilitiesr   r   r   r   r   <module>rB      s"      2 0I5+ I5r   