3
ܓiI @ s@ d dl Z d dlmZ d dlmZ d dlmZ G dd deZdS ) N)ContainerRuntime)sos_get_command_output)
is_executablec @ sL e Zd ZdZdZdZdd ZdddZd d
Zdd Z d
d Z
dddZdS )LxdContainerRuntimez,Runtime class to use for systems running LXDlxdlxcc C sF t d| jjrB| jjjdrB| jjjds8| jjjdrBd| _dS dS )Nr r zsnap.lxd.daemonTF)r policysysrootZpackage_managerZpkg_by_nameZinit_systemZ
is_runningactive)self r /usr/lib/python3.6/lxd.pycheck_is_active s z#LxdContainerRuntime.check_is_activeFc C s| g }| j d}| jrxt|| jjd}|d dkrxtj|d }x6|D ].}|d dksZ|rF|j|d d |d f qFW |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)chrootstatusr outputZRunningZexpanded_configz
volatile.uuidname)binaryr
r r r jsonloadsappend)r Zget_allZ
containersZ_cmdoutout_json containerr r r
get_containers" s
z"LxdContainerRuntime.get_containersc C sp g }| j rlt| j d| jjd}|d dkrltj|d }x.|D ]&}d|krB|j|d d |d f qBW |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 json)r r r r Z
update_sourcealiasZfingerprint)r
r r r r r r r )r Zimagesr r entr r r
get_images: s
zLxdContainerRuntime.get_imagesc C s g }d}| j rt| j d| jjd}|d dkrxtj|d }x6|D ].}|d dkrFd|d krF|d d d
}P qFW t| j d| d| jjd}|d dkrtj|d }x|D ]}|j|d qW |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 json)r r r r r rootZdevicesZpoolz storage volume list z --format json)r
r r r r r r r )r ZvolsZstg_poolr r Zprofiler r r r
get_volumesP s*
zLxdContainerRuntime.get_volumesc C s | j d| dS )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 )Zrun_cmdr )r r pathdestZ 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 Zsos.policies.runtimesr Z
sos.utilitiesr r r r r r r