o
    O6ufI  ã                   @   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)Úis_executablec                   @   sL   e Zd ZdZdZdZdd„ Zddd„Zd	d
„ Zdd„ Z	dd„ Z
ddd„ZdS )ÚLxdContainerRuntimez,Runtime class to use for systems running LXDÚlxdÚlxcc                 C   sF   t d| jjƒr!| jj d¡r!| jj d¡s| jj d¡r!d| _dS dS )Nr   r   zsnap.lxd.daemonTF)r   ÚpolicyÚsysrootÚpackage_managerÚpkg_by_nameÚinit_systemÚ
is_runningÚactive)Úself© r   ú;/usr/lib/python3/dist-packages/sos/policies/runtimes/lxd.pyÚcheck_is_active   s   ÿþýz#LxdContainerRuntime.check_is_activeFc                 C   sx   g }| j › d�}| jr:t|| jjd�}|d dkr:t |d ¡}|D ]}|d dks,|r9| |d d |d	 f¡ q"|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 list --format json©ÚchrootÚstatusr   ÚoutputÚRunningÚexpanded_configzvolatile.uuidÚname)Úbinaryr   r   r   r	   ÚjsonÚloadsÚappend)r   Úget_allÚ
containersÚ_cmdÚoutÚout_jsonÚ	containerr   r   r   Úget_containers"   s   
ÿÿ€z"LxdContainerRuntime.get_containersc                 C   sl   g }| j r4t| j› d�| jjd�}|d dkr4t |d ¡}|D ]}d|v r3| |d d |d f¡ q |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 image list --format jsonr   r   r   r   Úupdate_sourceÚaliasÚfingerprint©r   r   r   r   r	   r   r   r   )r   Úimagesr!   r"   Úentr   r   r   Ú
get_images:   s    
þ
þ€zLxdContainerRuntime.get_imagesc                 C   sÊ   g }d}| j rct| j› d�| jjd�}|d dkr;t |d ¡}|D ]}|d dkr:d|d	 v r:|d	 d d
 } nq"t| j› d|› d�| jjd�}|d dkrct |d ¡}|D ]	}| |d ¡ qY|S )z�Get a list of container volumes present on the system

        :returns: A list of volume IDs on the system
        :rtype: ``list``
        Údefaultz profile list --format jsonr   r   r   r   r   ÚrootÚdevicesÚpoolz storage volume list z --format jsonr(   )r   ÚvolsÚstg_poolr!   r"   Úprofiler*   r   r   r   Úget_volumesP   s0   
þ€þzLxdContainerRuntime.get_volumesc                 C   s   | j › d|› 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 info z --show-log)r   )r   r#   r   r   r   Úget_logs_commandq   s   
z$LxdContainerRuntime.get_logs_commandNc                 C   s:   |r| j › d|› d|› d|› �S | j› 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``
        ú z	 tail -c z file pull )Úrun_cmdr   )r   r#   ÚpathÚdestÚ	sizelimitr   r   r   Úget_copy_command}   s   z$LxdContainerRuntime.get_copy_command)F)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r$   r+   r3   r4   r:   r   r   r   r   r      s    

!r   )r   Úsos.policies.runtimesr   Úsos.utilitiesr   r   r   r   r   r   r   Ú<module>   s
   
