a >hI@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|jjdrB|jjds8|jjdrBd|_dSdS)Nrrzsnap.lxd.daemonTF)rpolicysysrootZpackage_managerZ pkg_by_nameZ init_systemZ is_runningactive)selfr =/usr/lib/python3.9/site-packages/sos/policies/runtimes/lxd.pycheck_is_actives   z#LxdContainerRuntime.check_is_activeFcCsxg}|jd}|jrtt||jjd}|ddkrtt|d}|D].}|ddksX|rD||dd|d fqD|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 jsonchrootstatusroutputZRunningZexpanded_configz volatile.uuidname)binaryr rrr jsonloadsappend)r Zget_allZ containersZ_cmdoutout_json containerr r r get_containers"s   z"LxdContainerRuntime.get_containerscCslg}|jrht|jd|jjd}|ddkrht|d}|D]&}d|vr@||dd|dfq@|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 jsonrrrrZ update_sourcealiasZ fingerprintr rrrr rrr)r Zimagesrrentr r r get_images:s   zLxdContainerRuntime.get_imagescCsg}d}|jrt|jd|jjd}|ddkrvt|d}|D]0}|ddkrDd|d vrD|d dd }qvqDt|jd |d |jjd}|ddkrt|d}|D]}||dq|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 jsonrrrrrrootZdevicesZpoolz storage volume list z --format jsonr)r ZvolsZstg_poolrrZprofilerr r r get_volumesPs0     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