%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/sos/policies/runtimes/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/sos/policies/runtimes/__pycache__/__init__.cpython-312.pyc

�

i��dk"��<�ddlZddlmZddlmZmZGd�d�Zy)�N)�quote)�sos_get_command_output�
is_executablec��eZdZdZdZgZgZgZdZdZ	dd�Z
d�Zd�Zd	�Z
dd
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zdd�Zy)�ContainerRuntimea�Encapsulates a container runtime that provides the ability to plugins to
    check runtime status, check for the presence of specific containers, and
    to format commands to run in those containers

    :param policy: The loaded policy for the system
    :type policy: ``Policy()``

    :cvar name: The name of the container runtime, e.g. 'podman'
    :vartype name: ``str``

    :cvar containers: A list of containers known to the runtime
    :vartype containers: ``list``

    :cvar images: A list of images known to the runtime
    :vartype images: ``list``

    :cvar binary: The binary command to run for the runtime, must exit within
                  $PATH
    :vartype binary: ``str``
    �	Undefined�FNc�:�||_d|jz|_y)Nz%s exec )�policy�binary�run_cmd)�selfrs  �@/usr/lib/python3/dist-packages/sos/policies/runtimes/__init__.py�__init__zContainerRuntime.__init__.s�����!�D�K�K�/���c��|j�|_|j�|_|j	�|_y)z{If this runtime is found to be active, attempt to load information
        on the objects existing in the runtime.
        N)�get_containers�
containers�
get_images�images�get_volumes�volumes�rs r�load_container_infoz$ContainerRuntime.load_container_info2s2���-�-�/����o�o�'����'�'�)��rc�h�t|j|jj�rd|_yy)a@Check to see if the container runtime is both present AND active.

        Active in this sense means that the runtime can be used to glean
        information about the runtime itself and containers that are running.

        :returns: ``True`` if the runtime is active, else ``False``
        :rtype: ``bool``
        TF)rrr�sysroot�activers r�check_is_activez ContainerRuntime.check_is_active:s)������d�k�k�&9�&9�:��D�K��rc��y)zkCheck if the runtime supports copying files out of containers and
        onto the host filesystem
        T�rs r�check_can_copyzContainerRuntime.check_can_copyHs��rc�&�g}|j�d|rdnd��}|jrmt||jj��}|ddk(rD|dj�dd	D]+}|j
�}|j|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-ar	��chroot�statusr�output�N���)rrrrr�
splitlines�split�append)r�get_allr�_cmd�out�ents      rrzContainerRuntime.get_containersNs����
�!�[�[�'�$�r�*A�B���;�;�(��d�k�k�6I�6I�J�C��8�}��!��x�=�3�3�5�a�b�9�9�C��)�)�+�C��%�%�s�1�v�s�2�w�&7�8�9��rc��|jr|�y|jD]"}tj||d�s�|dcSy)a Get the container ID for the container matching the provided
        name

        :param name: The name of the container, note this can be a regex
        :type name: ``str``

        :returns: The id of the first container to match `name`, else ``None``
        :rtype: ``str``
        Nr'r)rr�re�match)r�name�cs   r�get_container_by_namez&ContainerRuntime.get_container_by_name_sE���{�{�d�l�����	�A��x�x��a��d�#���t��	�rc��g}d}|jrzt|j�d|�d�|jj��}|ddk(rA|dj�D]+}|j
�}|j|d|df��-|S)	z�Get a list of images present on the system

        :returns: A list of 2-tuples containing (image_name, image_id)
        :rtype: ``list``
        z2{{lower .Repository}}:{{lower .Tag}} {{lower .ID}}z images --format '�'r#r%rr&r'�rrrrrr)r*r+)rr�fmtr.r/s     rrzContainerRuntime.get_imagesps�����B���;�;�(�-1�[�[�#�>��{�{�*�*��C��8�}��!��x�=�3�3�5�4�C��)�)�+�C��M�M�3�q�6�3�q�6�"2�3�4��
rc��g}|jrutd|jz|jj��}|ddk(r?|dj�ddD]&}|j
�}|j|d��(|S)	z�Get a list of container volumes present on the system

        :returns: A list of volume IDs on the system
        :rtype: ``list``
        z%s volume lsr#r%rr&r'Nr(r8)r�volsr.r/s    rrzContainerRuntime.get_volumes�s������;�;�(�����,��{�{�*�*��C��8�}��!��x�=�3�3�5�a�b�9�)�C��)�)�+�C��K�K��B��(�)��rc�2�|jD]}||vs�yy)a�Check if a given container ID or name exists on the system from the
        perspective of the container runtime.

        Note that this will only check _running_ containers

        :param container:       The name or ID of the container
        :type container:        ``str``

        :returns:               True if the container exists, else False
        :rtype:                 ``bool``
        TF)r)r�	container�_contups   r�container_existsz!ContainerRuntime.container_exists�s'�����	�G��G�#��	�rc�J�|rt|�}n|}|j�d|�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``
        � )rr
)rr=�cmd�quotecmd�
quoted_cmds     r�fmt_container_cmdz"ContainerRuntime.fmt_container_cmd�s&����s��J��J�!�\�\�9�j�A�Arc�"�d|�|rd|z��Sd��S)aFormat a string to pass to the 'run' command of the runtime to
        enable authorization for pulling the image during `sos collect`, if
        needed using username and optional password creds

        :param username:    The name of the registry user
        :type username:     ``str``

        :param password:    The password of the registry user
        :type password:     ``str`` or ``None``

        :returns:  The string to use to enable a run command to pull the image
        :rtype:    ``str``
        z--creds=�:r	r )r�username�passwords   r�fmt_registry_credentialsz)ContainerRuntime.fmt_registry_credentials�s#��"*�X�3��>�+M�N�N�2�+M�N�Nrc��|rd|zSy)z�Format a string to pass to the 'run' command of the runtime to
        enable authorization for pulling the image during `sos collect`, if
        needed using an authfile.
        z
--authfile %sr	r )r�authfiles  r�fmt_registry_authfilez&ContainerRuntime.fmt_registry_authfile�s��
�"�X�-�-�rc�$�|j�d|��S)a!Get the command string used to dump container logs from the
        runtime

        :param container: The name or ID of the container to get logs for
        :type container: ``str``

        :returns: Formatted runtime command to get logs from `container`
        :type: ``str``
        z	 logs -t )r)rr=s  r�get_logs_commandz!ContainerRuntime.get_logs_command�s��#'�+�+�y�9�9rc�b�|r|j�d|�d|�d|��S|j�d|�d|�d|��S)a,Generate the command string used to copy a file out of a container
        by way of the runtime.

        :param container:   The name or ID of the container
        :type container:    ``str``

        :param path:        The path to copy from the container. Note that at
                            this time, no supported runtime supports globbing
        :type path:         ``str``

        :param dest:        The destination on the *host* filesystem to write
                            the file to
        :type dest:         ``str``

        :param sizelimit:   Limit the collection to the last X bytes of the
                            file at PATH
        :type sizelimit:    ``int``

        :returns:   Formatted runtime command to copy a file from a container
        :rtype:     ``str``
        rAz	 tail -c z cp rG)r
r)rr=�path�dest�	sizelimits     r�get_copy_commandz!ContainerRuntime.get_copy_command�s2��,�,0�L�L�)�Y�,0�2�
2�#'�;�;�	�4��F�Fr)N)F)�__name__�
__module__�__qualname__�__doc__r3rrrrrrrrr!rr5rrr?rErJrMrOrTr rrrrsp���*�D��J�
�F��G�
�F�
�F�0�*����"�"�(�$�"B�*O� �
:�Grr)r1�pipesr�
sos.utilitiesrrrr rr�<module>r[s��
��?�iG�iGr

Zerion Mini Shell 1.0