a >h"@s6ddlZddlmZddlmZmZGdddZdS)N)quote)sos_get_command_output is_executablec@seZdZdZdZgZgZgZdZdZ d"ddZ dd Z d d Z d d Z d#ddZddZddZddZddZddZddZddZddZd$d d!ZdS)%ContainerRuntimeaEncapsulates a container runtime that provides the ability to plugins to check runtime status, check for the presence of specific containers, and to format commands to run in those containers :param policy: The loaded policy for the system :type policy: ``Policy()`` :cvar name: The name of the container runtime, e.g. 'podman' :vartype name: ``str`` :cvar containers: A list of containers known to the runtime :vartype containers: ``list`` :cvar images: A list of images known to the runtime :vartype images: ``list`` :cvar binary: The binary command to run for the runtime, must exit within $PATH :vartype binary: ``str`` Z UndefinedFNcCs||_|jd|_dS)Nz exec )policybinaryrun_cmd)selfrr B/usr/lib/python3.9/site-packages/sos/policies/runtimes/__init__.py__init__.szContainerRuntime.__init__cCs"||_||_||_dS)z{If this runtime is found to be active, attempt to load information on the objects existing in the runtime. N)get_containers containers get_imagesimages get_volumesvolumesr r r r load_container_info2s  z$ContainerRuntime.load_container_infocCst|j|jjrd|_dSdS)a@Check to see if the container runtime is both present AND active. Active in this sense means that the runtime can be used to glean information about the runtime itself and containers that are running. :returns: ``True`` if the runtime is active, else ``False`` :rtype: ``bool`` TF)rrrsysrootactiverr r r check_is_active:s z ContainerRuntime.check_is_activecCsdS)zkCheck if the runtime supports copying files out of containers and onto the host filesystem Tr rr r r check_can_copyHszContainerRuntime.check_can_copycCszg}|jd|rdnd}|jrvt||jjd}|ddkrv|ddd D]"}|}||d|d fqR|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 ps z-archrootstatusroutputN)rrrrr splitlinessplitappend)r Zget_allrZ_cmdoutentr r r rNs zContainerRuntime.get_containerscCs>|jr|durdS|jD] }t||dr|dSqdS)a Get the container ID for the container matching the provided name :param name: The name of the container, note this can be a regex :type name: ``str`` :returns: The id of the first container to match `name`, else ``None`` :rtype: ``str`` Nrr)rrrematch)r namecr r r get_container_by_name_s  z&ContainerRuntime.get_container_by_namecCslg}d}|jrht|jd|d|jjd}|ddkrh|dD]"}|}||d|dfqD|S) zGet a list of images present on the system :returns: A list of 2-tuples containing (image_name, image_id) :rtype: ``list`` z2{{lower .Repository}}:{{lower .Tag}} {{lower .ID}}z images --format ''rrrrrrrrrrr r!r")r rfmtr#r$r r r rps zContainerRuntime.get_imagescCsbg}|jr^t|jd|jjd}|ddkr^|dddD]}|}||dqB|S) zGet a list of container volumes present on the system :returns: A list of volume IDs on the system :rtype: ``list`` z volume lsrrrrrNrr+)r Zvolsr#r$r r r rs  zContainerRuntime.get_volumescCs|jD]}||vrdSqdS)aCheck if a given container ID or name exists on the system from the perspective of the container runtime. Note that this will only check _running_ containers :param container: The name or ID of the container :type container: ``str`` :returns: True if the container exists, else False :rtype: ``bool`` TF)r)r containerZ_contupr r r container_existss z!ContainerRuntime.container_existscCs(|rt|}n|}|jd|d|S)aFormat a command to run inside a container using the runtime :param container: The name or ID of the container in which to run :type container: ``str`` :param cmd: The command to run inside `container` :type cmd: ``str`` :param quotecmd: Whether the cmd should be quoted. :type quotecmd: ``bool`` :returns: Formatted string to run `cmd` inside `container` :rtype: ``str``  )rr )r r-cmdZquotecmdZ quoted_cmdr r r fmt_container_cmds z"ContainerRuntime.fmt_container_cmdcCsd||rd|ndS)aFormat a string to pass to the 'run' command of the runtime to enable authorization for pulling the image during `sos collect`, if needed using username and optional password creds :param username: The name of the registry user :type username: ``str`` :param password: The password of the registry user :type password: ``str`` or ``None`` :returns: The string to use to enable a run command to pull the image :rtype: ``str`` z--creds=:rr )r usernamepasswordr r r fmt_registry_credentialssz)ContainerRuntime.fmt_registry_credentialscCs|rd|SdS)zFormat a string to pass to the 'run' command of the runtime to enable authorization for pulling the image during `sos collect`, if needed using an authfile. z --authfile rr )r Zauthfiler r r fmt_registry_authfiles z&ContainerRuntime.fmt_registry_authfilecCs|jd|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 logs -t )r)r r-r r r get_logs_commands z!ContainerRuntime.get_logs_commandcCs<|r |jd|d|d|S|jd|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`` r/z tail -c z cp r2)r r)r r-pathdestZ sizelimitr r r get_copy_commandsz!ContainerRuntime.get_copy_command)N)F)N)__name__ __module__ __qualname____doc__r'rrrrrr rrrrr)rrr.r1r5r6r7r:r r r r rs*    r)r%ZshlexrZ sos.utilitiesrrrr r r r  s