3 ܓiI@s@ddlZddlmZddlmZddlmZGdddeZdS)N)ContainerRuntime)sos_get_command_output) is_executablec@sLeZdZdZdZdZddZdddZd d Zd d Z d dZ dddZ dS)LxdContainerRuntimez,Runtime class to use for systems running LXDlxdlxccCsFtd|jjrB|jjjdrB|jjjds8|jjjdrBd|_dSdS)Nrrzsnap.lxd.daemonTF)rpolicysysrootZpackage_managerZ pkg_by_nameZ init_systemZ is_runningactive)selfr /usr/lib/python3.6/lxd.pycheck_is_activesz#LxdContainerRuntime.check_is_activeFcCs|g}|jd}|jrxt||jjd}|ddkrxtj|d}x6|D].}|ddksZ|rF|j|dd|d fqFW|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)chrootstatusroutputZRunningZexpanded_configz volatile.uuidname)binaryr rrr jsonloadsappend)r Zget_allZ containersZ_cmdoutout_json containerr r r get_containers"s    z"LxdContainerRuntime.get_containerscCspg}|jrlt|jd|jjd}|ddkrltj|d}x.|D]&}d|krB|j|dd|dfqBW|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)rrrrZ update_sourcealiasZ fingerprint)r rrrr rrr)r Zimagesrrentr r r get_images:s     zLxdContainerRuntime.get_imagescCsg}d}|jrt|jd|jjd}|ddkrxtj|d}x6|D].}|ddkrFd|d krF|d dd }PqFWt|jd |d |jjd}|ddkrtj|d}x|D]}|j|dqW|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)rrrrrrootZdevicesZpoolz storage volume list z --format json)r rrrr rrr)r ZvolsZstg_poolrrZprofilerr r r get_volumesPs*         zLxdContainerRuntime.get_volumescCs|jd|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 rr r r get_logs_commandqs z$LxdContainerRuntime.get_logs_commandNcCs:|r |jd|d|d|S|jd||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 rpathdestZ sizelimitr r r get_copy_command}sz$LxdContainerRuntime.get_copy_command)F)N) __name__ __module__ __qualname____doc__rrrrrr r!r%r r r r rs ! r)rZsos.policies.runtimesrZ sos.utilitiesrrrr r r r  s