
    -_gI                     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)ContainerRuntime)sos_get_command_output)is_executablec                   @    e Zd ZdZdZdZd ZddZd Zd Z	d Z
dd
Zy	)LxdContainerRuntimez,Runtime class to use for systems running LXDlxdlxcc                 2   t        d| j                  j                        rw| j                  j                  j	                  d      rR| j                  j
                  j                  d      s%| j                  j
                  j                  d      rd| _        yy)Nr	   r   zsnap.lxd.daemonTF)r   policysysrootpackage_managerpkg_by_nameinit_system
is_runningactive)selfs    ;/usr/lib/python3/dist-packages/sos/policies/runtimes/lxd.pycheck_is_activez#LxdContainerRuntime.check_is_active   sl    %!4!45++77>((33E:((334EFDK    c                    g }| j                    d}| j                  rot        || j                  j                        }|d   dk(  rFt        j                  |d         }|D ])  }|d   dk(  s|s|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 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	containers          r   get_containersz"LxdContainerRuntime.get_containers"   s     
++12;;(dkk6I6IJC8}!::c(m4!) 1I *i77"))&'89/J&v.011 r   c                    g }| j                   rvt        | j                   d| j                  j                        }|d   dk(  r@t        j                  |d         }|D ]#  }d|v s|j                  |d   d   |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 image list --format jsonr   r   r   r   update_sourcealiasfingerprintr   r   r   r   r   r   r    r!   )r   imagesr%   r&   ents        r   
get_imageszLxdContainerRuntime.get_images:   s     ;;(;;-89{{**C 8}!::c(m4# 1C&#-09.'0 11 r   c                    g }d}| j                   rt        | j                   d| j                  j                        }|d   dk(  r<t        j                  |d         }|D ]  }|d   dk(  sd|d	   v s|d	   d   d
   } n t        | j                   d| d| j                  j                        }|d   dk(  r3t        j                  |d         }|D ]  }|j                  |d           |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   rootdevicespoolz storage volume list z --format jsonr-   )r   volsstg_poolr%   r&   profiler/   s          r   get_volumeszLxdContainerRuntime.get_volumesP   s    ;; );;-:;{{**C 8}!::c(m4' G94"gi&88#*9#5f#=f#E	 );;-4XJnM{{**C 8}!::c(m4# -CKKF,-r   c                 &    | 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'   s     r   get_logs_commandz$LxdContainerRuntime.get_logs_commandq   s     ++fYK{;;r   Nc                 `    |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	sizelimits        r   get_copy_commandz$LxdContainerRuntime.get_copy_command}   sG    , ll^1YKy1TFKK++k)TF!D6BBr   )F)N)__name__
__module____qualname____doc__r   r   r   r(   r0   r9   r;   rB    r   r   r   r      s/    6DF0,B
<Cr   r   )r   sos.policies.runtimesr   sos.utilitiesr   r   r   rG   r   r   <module>rJ      s$     2 0 'CC* CCr   