U
    -_gI                     @   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rB| jjdrB| jjds8| jjdrBd| _dS dS )Nr   r   zsnap.lxd.daemonTF)r   policysysrootZpackage_managerZpkg_by_nameZinit_systemZ
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rtt|| jjd}|d dkrtt|d }|D ].}|d dksX|rD||d d |d	 f qD|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 list --format jsonchrootstatusr   outputZRunningZexpanded_configzvolatile.uuidname)binaryr
   r   r   r	   jsonloadsappend)r   Zget_allZ
containersZ_cmdoutout_json	containerr   r   r   get_containers"   s    
z"LxdContainerRuntime.get_containersc                 C   sl   g }| j rht| j d| jjd}|d dkrht|d }|D ]&}d|kr@||d d |d f q@|S )	zGet 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   Zupdate_sourcealiasZfingerprintr
   r   r   r   r	   r   r   r   )r   Zimagesr   r   entr   r   r   
get_images:   s    

zLxdContainerRuntime.get_imagesc                 C   s   g }d}| j rt| j d| jjd}|d dkrvt|d }|D ]0}|d dkrDd|d	 krD|d	 d d
 } qvqDt| j d| d| jjd}|d dkrt|d }|D ]}||d  q|S )zGet 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   rootZdevicesZpoolz storage volume list z --format jsonr   )r   ZvolsZstg_poolr   r   Z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 )Zrun_cmdr   )r   r   pathdestZ	sizelimitr   r   r   get_copy_command}   s    z$LxdContainerRuntime.get_copy_command)F)N)__name__
__module____qualname____doc__r   r   r   r   r   r"   r#   r'   r   r   r   r   r      s   

!r   )r   Zsos.policies.runtimesr   Zsos.utilitiesr   r   r   r   r   r   r   <module>   s   