
    Vh                       d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZm Z  dd	l!m"Z"m#Z#m$Z$ d
dgZ%dZ&dZ'dZ( G d d      Z)ed@d       Z* G d dejV                        Z, ejZ                  d       G d d             Z.edAd       Z/dBdZ0	 dC	 	 	 	 	 	 	 	 	 	 	 dDdZ1 G d d      Z2dEdZ3edFd       Z4dGdZ5edHd        Z6edHd!       Z7edHd"       Z8edId#       Z9edId$       Z:edHd%       Z;edId&       Z<dJd'Z=dJd(Z>dKd)Z?	 dC	 	 	 	 	 	 	 	 	 dLd*Z@	 	 dM	 	 	 	 	 	 	 	 	 	 	 dNd+ZA	 	 	 	 	 	 	 	 dOd,ZBdPd-ZC G d. d/eD      ZE G d0 d1eE      ZF G d2 d3      ZGdQdRd4ZHdSd5ZI G d6 d7      ZJedQdTd8       ZK G d9 d:      ZLdQdUd;ZMdPd<ZN	 	 	 	 	 	 dV	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dWd=ZO	 	 	 	 	 	 dX	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dYd>ZPdZd?ZQy)[z2Functions for accessing docker via the docker cli.    )annotationsN   )	ApplicationErrorcommon_environmentdisplayfind_executableSubprocessErrorcacheOutputStreamInternalErrorformat_command_output)run_commandraw_command)CommonConfig)mutex
named_lock)CGroupEntry
MountEntry	MountTypedockerpodmanz4quay.io/ansible/ansible-test-utility-container:3.1.0i (  l    c                      e Zd ZdZedd       ZddZedd       Zedd       Z	edd       Z
edd       Zedd       Zedd	       Zedd
       Zedd       Zedd       Zedd       Zy)
DockerInfozLThe results of `docker info` and `docker version` for the container runtime.c                N   t               j                  }t        |g ddd      d   }t        j                  |      }|j                  d      x}rt        ddj                  |      z         t        |g ddd      d   }t        j                  |      }t        ||||      }|S )	z,Initialize and return a DockerInfo instance.)info--format{{ json . }}Tcapturealwaysr   ServerErrorsz*Unable to get container host information: 
)versionr   r   )	require_dockercommanddocker_commandjsonloadsgetr   joinr   )clsargsr%   info_stdoutr   server_errorsversion_stdoutr#   s           R/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/docker_util.pyinitzDockerInfo.initC   s     !"**$T+OY]fjklmnzz+& HH^44=4 ##ORVR[R[\iRj#jkk'.U_clpqrst**^,$w7    c                <    || _         || _        || _        || _        y N)r,   enginer   r#   )selfr,   r5   r   r#   s        r0   __init__zDockerInfo.__init__X   s    		r2   c                V    | j                   j                  d      }|st        d      |S )zThe client version details.Clientz0Unable to get container host client information.)r#   r)   r   )r6   clients     r0   r:   zDockerInfo.client^   s,     !!(+"#UVVr2   c                    | j                   j                  d      }|s&| j                  dk(  r| j                  S t	        d      |S )zThe server version details.Serverr   z0Unable to get container host server information.)r#   r)   r5   r:   r   )r6   servers     r0   r=   zDockerInfo.serverh   sD     !!(+{{h&
 {{""#UVVr2   c                     | j                   d   S )zThe client version.Version)r:   r6   s    r0   client_versionzDockerInfo.client_versiony        {{9%%r2   c                     | j                   d   S )zThe server version.r?   )r=   r@   s    r0   server_versionzDockerInfo.server_version~   rB   r2   c                p    | j                   j                  d      dd \  }}t        |      t        |      fS )z#The client major and minor version..N   )rA   splitintr6   majorminors      r0   client_major_minor_versionz%DockerInfo.client_major_minor_version   8     **005bq9u5z3u:%%r2   c                p    | j                   j                  d      dd \  }}t        |      t        |      fS )z#The server major and minor version.rF   NrG   )rD   rH   rI   rJ   s      r0   server_major_minor_versionz%DockerInfo.server_major_minor_version   rN   r2   c                    | j                   dk(  r | j                  dk\  xr | j                  dk\  S t        | j                         )zLReturn True if the `--cgroupns` option is supported, otherwise return False.r      
   )r5   rM   rP   NotImplementedErrorr@   s    r0   cgroupns_option_supportedz$DockerInfo.cgroupns_option_supported   sC     ;;(" 22h>n4CbCbfnCnn!$++..r2   c                   | j                   }|j                  d      }|rt        |d   j                  d            S 	 t        |d         S # t        $ r Y nw xY w| j
                  dk  ryd| j                   d| j                   d	}t        | j                        j                  rt        d
|       t        j                  d| d       y)z)The cgroup version of the container host.hostcgroupVersionvCgroupVersionrR   r   zThe Docker client version is z. The Docker server version is z7. Upgrade your Docker client to version 20.10 or later.zBUnsupported Docker client and server combination using cgroup v2. z0Detected Docker server cgroup v1 using probing. Tunique)r   r)   rI   lstripKeyErrorrP   rA   rD   detect_host_propertiesr,   	cgroup_v2r   r   warning)r6   r   rX   messages       r0   cgroup_versionzDockerInfo.cgroup_version   s     yyxx tO,33C899	tO,-- 		 **X5
 ,D,?,?+@ A,,0,?,?+@ ADD 	 "$)),66 #%ghogp#qrrJ7)T]abs   A 	AAc                    | j                   }|j                  d      }|j                  d      }|xr |j                  d      xr |dk(  }|S )zPReturn True if Docker Desktop integrated with WSL2 is detected, otherwise False.KernelVersionOperatingSystemz-WSL2zDocker Desktop)r   r)   endswith)r6   r   kernel_versionoperating_systemdd_wsl2s        r0   docker_desktop_wsl2zDockerInfo.docker_desktop_wsl2   sQ     yy/288$56 n^%<%<W%EnJZ^nJnr2   c                :   t        | j                  | j                  d| j                         }| j                  g|j                         D cg c]  \  }}| d|  c}}z   }| j                  r|j                  d       ddj                  |       S c c}}w )zDescribe the container runtime.rZ   )r:   r=   cgroup=zDD+WSL2zContainer runtime:  )	dictrA   rD   rd   r5   itemsrl   appendr*   )r6   tagskeyvaluelabelss        r0   descriptionzDockerInfo.description   s     &&&&t**+,
 ++TZZ\!RzsESE5'"2!RR##MM)$$SXXf%5$677 "Ss   BNr,   r   returnr   )
r,   r   r5   strr   dict[str, t.Any]r#   r|   rz   Nonerz   r|   rz   r{   )rz   ztuple[int, int]rz   boolrz   rI   )__name__
__module____qualname____doc__classmethodr1   r7   propertyr:   r=   rA   rD   rM   rP   rV   rd   rl   rx    r2   r0   r   r   @   s    V (      & & & & & &
 & &
 / / + +Z 	 	 8 8r2   r   c                    	 t         j                  S # t        $ r Y nw xY wt        j	                  |       }t        j                  |j                  d       |t         _        |S )zFReturn info for the current container runtime. The results are cached.r   	verbosity)get_docker_infor   AttributeErrorr   r1   r   rx   )r,   r   s     r0   r   r      sV    ###  ??4 DLL!!Q/OKs    	c                       e Zd ZdZdZdZdZdZy)SystemdControlGroupV1StatuszCThe state of the cgroup v1 systemd hierarchy on the container host.z+The systemd cgroup subsystem was not found.z7The "/sys/fs/cgroup/systemd" filesystem is not mounted.z7The "/sys/fs/cgroup/systemd" mount type is not correct.z,The "/sys/fs/cgroup/systemd" mount is valid.N)r   r   r   r   SUBSYSTEM_MISSINGFILESYSTEM_NOT_MOUNTEDMOUNT_TYPE_NOT_CORRECTVALIDr   r2   r0   r   r      s    MEVV:Er2   r   T)frozenc                  D    e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   y)ContainerHostPropertiesz/Container host properties detected at run time.r{   
audit_coderI   max_open_fileszt.Optional[int]loginuidr   	cgroup_v1r   ra   N)r   r   r   r   __annotations__r   r2   r0   r   r      s!    9O**Or2   r   c                   	 t         j                  S # t        $ r Y nw xY wd}dj                  |      ddf}ddg}ddd	j                  |      g}t	        | d
||      \  }}| j
                  r&t        dt        t        t        j                  d      S |j                  d      }t        |      t        |      k7  rCdt        |       dt        |       d}|t        ||      z  }t        |j                               |d   j                  d      }	|	d   j                  dd      }
t!        |
d         }|
d   }t!        |	d         }t!        |	d         }|	d   rt!        |	d         nd}t#        j$                  |d         }t'        j$                  |d         }|t        k  r||k  rt)               j*                  dk(  rkddt-        |t               g}g d}	 t	        | d||      d   }t!        |      }n4# t.        $ r(}t1        j2                  t5        |             Y d}~nd}~ww xY wt7        d |D              }|D ci c]  }|j8                  |j:                   nc c}w }}d|vrt        j<                  }nk|j?                  tA        jB                  d             x}st        jD                  }n4|tF        jH                  k7  rt        jJ                  }nt        j                  }|j?                  tA        jB                  d!            tF        jL                  k(  }t1        jN                  d"| d#| d$d%       t1        jN                  d&| d%       t1        jN                  d'||nd( |t        k(  rd)nd* d%       |t        k  r!t1        j2                  d+t         d,| d-       nt        }t        |||||      }|t         _        |S ).a+  
    Detect and return properties of the container host.

    The information collected is:

      - The errno result from attempting to query the container host's audit status.
      - The max number of open files supported by the container host to run containers.
        This value may be capped to the maximum value used by ansible-test.
        If the value is below the desired limit, a warning is displayed.
      - The loginuid used by the container host to run containers, or None if the audit subsystem is unavailable.
      - The cgroup subsystems registered with the Linux kernel.
      - The mounts visible within a container.
      - The status of the systemd cgroup v1 hierarchy.

    This information is collected together to reduce the number of container runs to probe the container host.
    )zaudit-statuszcat /proc/sys/fs/nr_open
ulimit -Hnz(cat /proc/1/loginuid; echo)z && zcat /proc/1/cgroupzcat /proc/1/mountinfoz--volumez/sys/fs/cgroup:/probe:rosh-cz && echo "-" && zansible-test-probez???F)r   r   r   r   ra   z
-
z"Unexpected probe output. Expected z blocks but found z.
r   r"   rp   r   rG      Nr   z--ulimitznofile=)r   r   r   zansible-test-ulimitc              3  4   K   | ]  }|j                     y wr4   )	subsystem).0rn   s     r0   	<genexpr>z)detect_host_properties.<locals>.<genexpr>u  s     <&V%%<s   systemdz/probe/systemdz/probezContainer host audit status: z ()r   zContainer host max open files: zContainer loginuid: unavailablez
 (not set) z*Unable to set container max open files to z . Using container host limit of z	 instead.)(r`   
propertiesr   r*   run_utility_containerexplainr   MAX_NUM_OPEN_FILESLOGINUID_NOT_SETr   r   rH   lenr   r   striprI   r   r(   r   r$   r%   minr	   r   rb   r{   setpathtyper   r)   pathlibPurePosixPathr   r   	CGROUP_V1r   	CGROUP_V2r   )r,   single_line_commandsmulti_line_commandsoptionscmdstdoutstderrblocksrc   valuesaudit_partsaudit_statusr   system_limit
hard_limitr   cgroupsmountsex
subsystemsmountmount_typesr   
mount_typera   r   s                             r0   r`   r`   
  s   $%000  	() 56G)../BC
DC*41EsGTNFF||&-%177
 	
 \\'"F
6{c-..6s;N7O6PPbcfgmcnboors(88GMMO,,AY__T"F)//#q)K{1~&LQJvay>LVAYJ!'s6!9~Hq	*GfQi(F&&:+DIYIaIaemIm \;M)N(OPQ(	%*41FWUVWXF VJ  	%OOCG$$	%2 <G<<J7=>e5::uzz)>>K>
"/AA	'OOG,A,ABR,STTjT/FF		y**	*/FF	/55	 5 5h ?@IDWDWWILL0B|nANZ[\LL2:,?1MLL'H4Hm'\$,0@$@LbIKVWY &&DEWDXXx  zD  yE  EN  O  	P'
( !	J )3%s*    	G6 6	H'?H""H' Ic                $    | d| j                    S )zQReturn the given container name with the current test session name applied to it.-)session_name)r,   names     r0   get_session_container_namer     s    V1T&&'((r2   c                    t        | |      }|d|dgz   }|r|j                  d       t        | t               t	        | t        |||      S )z`Run the specified command using the ansible-test utility container, returning stdout and stderr.z--namez--rm-i)r   rs   docker_pullUTILITY_IMAGE
docker_run)r,   r   r   r   datas        r0   r   r     sU     &dD1D$ G
 tm$dM7C>>r2   c                  *    e Zd ZdZddZedd       Zy)DockerCommandz+Details about the available docker command.c                .    || _         || _        || _        y r4   )r%   
executabler#   )r6   r%   r   r#   s       r0   r7   zDockerCommand.__init__  s    $r2   c                 j   t         j                  j                  d      rt        t	        t
                    } nt
        } | D ]p  }t        |d      }|st        |dgt               d      d   j                         }|dk(  rd	|v rFt        j                  d
|d|d       t        |||      c S  y)z8Detect and return the available docker command, or None.ANSIBLE_TEST_PREFER_PODMANF)requiredz-vTenvr   r   r   r   z
Detected "z" container runtime version: r   r   N)osenvironr)   listreversedDOCKER_COMMANDSr   r   docker_environmentr   r   r   r   )commandsr%   r   r#   s       r0   detectzDockerCommand.detect  s     ::>>67H_56H&H 	CG(5AJ%wo;M;OY]^_`aggih&8w+>gW^_klm$Wj'BB	C r2   N)r%   r{   r   r{   r#   r{   rz   r}   rz   zt.Optional[DockerCommand])r   r   r   r   r7   staticmethodr   r   r2   r0   r   r     s    5
  r2   r   c                 `    t               x} r| S t        ddj                  t                     )zTReturn the docker command to invoke. Raises an exception if docker is not available.z3No container runtime detected. Supported commands: , )get_docker_commandr   r*   r   )r%   s    r0   r$   r$     s6    $&&w&
PQUQZQZ[jQkPlm
nnr2   c                 *    t         j                         S )zHReturn the docker command to invoke, or None if docker is not available.)r   r   r   r2   r0   r   r     s     !!r2   c                 (    t        t                     S )z;Return True if docker is available, otherwise return False.)r   r   r   r2   r0   docker_availabler     s    "$%%r2   c                 t    t        j                  t                     } t        j                  d| z  d       | S )z!Return the IP of the Docker host.zDetected docker host IP: %sr   r   )socketgethostbynameget_docker_hostnamer   r   )docker_host_ips    r0   get_docker_host_ipr     2     ))*=*?@NLL.?1Mr2   c                    t         j                  j                  d      } | rb| j                  d      rQ	 t        j
                  j                  |       d   j                  d      d   }t        j                  d|z  d       |S d}t        j                  d
d       |S # t        $ r d}t        j                  d	| z         Y |S w xY w)z*Return the hostname of the Docker service.DOCKER_HOST)ztcp://ssh://r   :r   zDetected Docker host: %sr   	localhostzQCould not parse DOCKER_HOST environment variable "%s", falling back to localhost.z*Assuming Docker is available on localhost.)r   r   r)   
startswithurllibparseurlparserH   r   r   
ValueErrorrb   )docker_hosthostnames     r0   r   r     s     **../K{--.BC	||,,[9!<BB3GJHLL3h>!L O AQOO  	"HOOor}}~
 O	s   AB #CCc                 t    t        j                  t                     } t        j                  d| z  d       | S )z!Return the IP of the Podman host.zDetected Podman host IP: %sr   r   )r   r   get_podman_hostnamer   r   )podman_host_ips    r0   get_podman_host_ipr  	  r   r2   c                    d} 	 t        g dt               d      d   }	 t        j                  |      }|D ]  }|d   d   d	k(  s|d
   }  | S  | S # t        $ r d}Y ?w xY w# t        j
                  j                  $ r | cY S w xY w)z
    Return the default hostname of the Podman service.

    --format was added in podman 3.3.0, this functionality depends on its availability
    N)r   system
connectionr   z--format=jsonTr   r   []Name*URI)r   r   r	   r'   r(   decoderJSONDecodeError)r  r   connectionsr	  s       r0   get_podman_default_hostnamer    s     !%HX^p^r  }A  B  CD  Ejj( " 
fb!S(!%(HO O  
 <<'' s"   A A" AA"BBc                 b   d} t         j                  j                  d      }|s
t               }|rW|j	                  d      rF	 t
        j                  j                  |      j                  } t        j                  d| z  d       | S | S # t        $ r t        j                  d|z         Y | S w xY w)zAReturn the remote podman hostname, if any, otherwise return None.NCONTAINER_HOSTr   zDetected Podman remote: %sr   r   zCould not parse podman URI "%s")r   r   r)   r  r   r   r   r   r  r   r   r   rb   )r  podman_hosts     r0   get_podman_remoter  .  s     H**..!12K13{--h7	O||,,[9BBH LL5@ANO8O	  	MOO=KL O		Ms   )B	 	!B.-B.c                 P    t               } | sd} t        j                  dd       | S )z*Return the hostname of the Podman service.r   z*Assuming Podman is available on localhost.r   r   )r  r   r   )r  s    r0   r  r  F  s(     !"HAQOOr2   c                    t        j                  d      } d}d}| j                         rt        j                  | j                               }|D ]  }t        |j                        dk(  st        j                  dt        |j                              x}r|j                  d      }d} nBt        j                  dt        |j                              x}s|j                  d      }d} n |rt        j                  d	| d
| d       |S )zQReturn the current container ID if running in a container, otherwise return None.z/proc/self/mountinfoNz/etc/hostnamez(/(?P<id>[0-9a-f]{64})/userdata/hostname$idPodmanz/(?P<id>[0-9a-f]{64})/hostname$DockerzDetected execution in z container ID: r   r   )r   Pathis_filer   r(   	read_textr{   r   researchrootgroupr   r   )mountinfo_pathcontainer_idr5   r   r   matchs         r0   get_docker_container_idr&  R  s     \\"89NLF !!.":":"<= 	E5::/1 II&PRUV[V`V`Rabb5b#(;;t#4L%F II&GUZZYY5Y#(;;t#4L%F3	6 -fX_\NS_`ar2   c                f    t        d|       5 }|rt        | |       ddd       y# 1 sw Y   yxY w)aR  
    Pull the specified image if it is not available.
    Images without a tag or digest will not be pulled.
    Retries up to 10 times if the pull fails.
    A warning will be shown for any image with volumes defined.
    Images will be pulled only once.
    Concurrent pulls for the same image will block until the first completes.
    zdocker_pull:N)r   __docker_pull)r,   imagefirsts      r0   r   r     s7     
l5'*	+ 'u$&' ' 's   '0c                   d|vr+d|vr't        j                  d|z  d       t        | |      }nt        | |d      x}rt        j                  d|z  d       nft        d	d
      D ]H  }	 t	        | d|gd       t        | |      x}s| j
                  r n*t        j                  d| d       J t        d| d      |rd|j                  rWt        j                  d| dt        |j                         ddj                  t        |j                               dd       yyy# t        $ r1 t        j                  d| d       t        j                  d       Y w xY w)z>Internal implementation for docker_pull. Do not call directly.@r   z0Skipping pull of image without tag or digest: %srG   r   T)r    z#Skipping pull of existing image: %sr   rT   pullFr   zImage "zL" not found after pull completed. Waiting a few seconds before trying again.z Failed to pull container image "z-". Waiting a few seconds before trying again.r   z".z" contains z volume(s): r   z
This may result in leaking anonymous volumes. It may also prevent the image from working on some hosts or container engines.
The image should be rebuilt without the use of the VOLUME instruction.r\   N)r   r   docker_image_inspectranger&   r   rb   r	   timesleepr   volumesr   r*   sorted)r,   r)  inspect
_iterations       r0   r(  r(    se   
%Cu,G%O[\]&tU3(uTB	B	B:UBaP2, 	QJ	tfe_eD3D%@@G@T\\'%0| }~	Q #%EeWB#OPP7??'%C4H3IVZV_V_`fgngvgv`wVxUy za a  $	% #w # "B5'Iv wx

1s   +*D(D((7E"!E"c                0    t        | d||d|gd       y)z'Copy a file to the specified container.cpr   Tr.  Nr&   )r,   r$  srcdsts       r0   docker_cp_tor<    s    4$c%BCTRr2   c                4    t        | dg|z   |gz   |z   d      S )z0Create a container using the given docker image.createTr.  r9  )r,   r)  r   r   s       r0   docker_creater?    s&     $
W 4w > DdSSr2   c                6    t        | dg|z   |gz   |z   |d      S )z-Run a container using the given docker image.runT)r   r   r9  )r,   r)  r   r   r   s        r0   r   r     s)     $' 1UG ;c AVZ[[r2   c                .    t        | dg|z   |gz   d      S )z Start a container by name or ID.startTr.  r9  )r,   r$  r   s      r0   docker_startrD    s!     $	G 3|n DdSSr2   c                    	 t        | ddd|gd       t        | d|gd       y# t        $ r(}d|j                  j                         vr|Y d}~yd}~ww xY w)	zRemove the specified container.stopz-t0Tr.  rmzno such containerN)r&   r	   r   lowerr,   r$  r   s      r0   	docker_rmrK    s^    
 	tfdC>MtdL14@  biioo&77H 8s   "% 	AAAc                      e Zd ZdZy)DockerErrorzGeneral Docker error.N)r   r   r   r   r   r2   r0   rM  rM    s    r2   rM  c                  $     e Zd ZdZd fdZ xZS )ContainerNotFoundErrorz7The container identified by `identifier` was not found.c                8    t         |   d|z         || _        y )Nz!The container "%s" was not found.)superr7   
identifier)r6   rR  	__class__s     r0   r7   zContainerNotFoundError.__init__  s    <zIJ$r2   )rR  r{   rz   r}   )r   r   r   r   r7   __classcell__)rS  s   @r0   rO  rO    s    A% %r2   rO  c                      e Zd ZdZddZedd       Zedd       Zedd       Zedd       Z	edd       Z
edd       Zedd	       Zedd
       Zedd       Zedd       ZddZddZddZddZy)DockerInspectz7The results of `docker inspect` for a single container.c                     || _         || _        y r4   r,   
inspectionr6   r,   rY  s      r0   r7   zDockerInspect.__init__      	$r2   c                     | j                   d   S )zReturn the ID of the container.IdrY  r@   s    r0   r  zDockerInspect.id  s     t$$r2   c                     | j                   d   S )z6Return a dictionary of the container network settings.NetworkSettingsr^  r@   s    r0   network_settingszDockerInspect.network_settings  s     011r2   c                     | j                   d   S )z+Return a dictionary of the container state.Stater^  r@   s    r0   statezDockerInspect.state   s     w''r2   c                     | j                   d   S )z3Return a dictionary of the container configuration.Configr^  r@   s    r0   configzDockerInspect.config       x((r2   c                     | j                   d   S )z9Return a dictionary of ports the container has published.Ports)ra  r@   s    r0   portszDockerInspect.ports  s     $$W--r2   c                8    | j                   j                  d      S )zReturn a dictionary of the networks the container is attached to, or None if running under podman, which does not support networks.Networks)ra  r)   r@   s    r0   networkszDockerInspect.networks  s     $$((44r2   c                     | j                   d   S )z9Return True if the container is running, otherwise False.Running)rd  r@   s    r0   runningzDockerInspect.running  s     zz)$$r2   c                N    | j                   j                  ry| j                  d   S )z#Return the PID of the init process.r   Pid)r,   r   rd  r@   s    r0   pidzDockerInspect.pid  s#     99zz%  r2   c                     | j                   d   S )zHReturn a list of the environment variables used to create the container.Envrg  r@   s    r0   r   zDockerInspect.env#       {{5!!r2   c                     | j                   d   S )z.Return the image used to create the container.Imagerw  r@   s    r0   r)  zDockerInspect.image(  s     {{7##r2   c           
         t        d | j                  D cg c]  }|j                  dd       c}D              S c c}w )zNReturn a dictionary of the environment variables used to create the container.c              3  0   K   | ]  }|d    |d   f  yw)r   r   Nr   )r   items     r0   r   z)DockerInspect.env_dict.<locals>.<genexpr>1  s     W4T!Wd1g&Ws   ro   r   )rq   r   rH   )r6   es     r0   env_dictzDockerInspect.env_dict/  s/    WTXX3VAGGCO3VWWW3Vs   ;
c                >    | j                   j                  d|z        S )zuReturn a list of the endpoints published by the container for the specified TCP port, or None if it is not published.z%d/tcp)rk  r)   )r6   ports     r0   get_tcp_portzDockerInspect.get_tcp_port3  s    zz~~ho..r2   c                F    | j                   yt        | j                         S )z@Return a list of the network names the container is attached to.N)rn  r4  r@   s    r0   get_network_nameszDockerInspect.get_network_names7  s    == dmm$$r2   c                    | j                         }|st        d| j                  z        t        |      dkD  r*t        d| j                  ddj	                  |            |d   S )zwReturn the network name the container is attached to. Raises an exception if no network, or more than one, is attached.z*No network found for Docker container: %s.r   z-Found multiple networks for Docker container z instead of only one: r   r   )r  r   r  r   r*   )r6   rn  s     r0   get_network_namezDockerInspect.get_network_name>  s~    ))+"#ORVRYRY#YZZx=1"ptpwpwy}  zC  zC  DL  zM  $N  O  O{r2   Nr,   r   rY  r|   rz   r}   r   r~   )rz   zdict[str, list[dict[str, str]]])rz   z't.Optional[dict[str, dict[str, t.Any]]]r   r   rz   	list[str]rz   zdict[str, str])r  rI   rz   z t.Optional[list[dict[str, str]]])rz   t.Optional[list[str]])r   r   r   r   r7   r   r  ra  rd  rg  rk  rn  rq  rt  r   r)  r  r  r  r  r   r2   r0   rV  rV    s    A% % % 2 2 ( ( ) ) . . 5 5 % % ! ! " " $ $X/%
r2   rV  c                   	 t        | dd|gd|      d   }| j                  r|sg }nt	        j
                  |      }t        |      dk(  rt        | |d         S t        |      # t        $ r}|j                  }Y d}~hd}~ww xY w)z
    Return the results of `docker container inspect` for the specified container.
    Raises a ContainerNotFoundError if the container was not found.
    	containerr5  Tr   r   Nr   )	r&   r	   r   r   r'   r(   r   rV  rO  )r,   rR  r    r   r   rr   s         r0   docker_inspectr  K  s    
{Iz&JTXaghijk ||F

6"
5zQT58,,
 
,,  s   A% %	B.A??Bc                (    t        | dd||gd       y)zADisconnect the specified docker container from the given network.network
disconnectTr.  Nr9  )r,   r$  r  s      r0   docker_network_disconnectr  `  s    4)\7LISWXr2   c                  N    e Zd ZdZddZedd       Zedd       Zed	d       Zy)
DockerImageInspectz9The results of `docker image inspect` for a single image.c                     || _         || _        y r4   rX  rZ  s      r0   r7   zDockerImageInspect.__init__h  r[  r2   c                     | j                   d   S )z(Return a dictionary of the image config.rf  r^  r@   s    r0   rg  zDockerImageInspect.confign  rh  r2   c                @    | j                   j                  d      xs i S )z)Return a dictionary of the image volumes.Volumes)rg  r)   r@   s    r0   r3  zDockerImageInspect.volumesu  s     {{y)/R/r2   c                     | j                   d   S )z-The command to run when the container starts.Cmdrw  r@   s    r0   r   zDockerImageInspect.cmdz  rx  r2   Nr  r~   r  )	r   r   r   r   r7   r   rg  r3  r   r   r2   r0   r  r  e  sF    C% ) ) 0 0 " "r2   r  c                   	 t         j                  }|j                  |      x}r|S 	 t	        | dd|gd|      d   }| j                  r|sg }nt        j                  |      }t        |      dkD  r2t        d| d	t        |       d
t        j                  |d             t        |      dk(  rt        | |d         }|||<   |S y# t        $ r i x}t         _        Y w xY w# t
        $ r d}Y w xY w)zs
    Return the results of `docker image inspect` for the specified image or None if the image does not exist.
    r)  r5  Tr   r   r
  r   zInspection of image "z" resulted in z items:
   )indentN)r/  r
   r   r)   r&   r	   r   r'   r(   r   r   dumpsr  )r,   r)  r    inspect_cacheinspect_resultr   rr   s          r0   r/  r/    s   8,22 '**511~1w	5&A4X^_`ab ||F

6"
5zA~!6ug^CPUJ<W`aeakaklqz{a|`}~
5zQ+D%(;-e1  8577,28  s"   C C$ C! C!$C21C2c                      e Zd ZdZddZy)DockerNetworkInspectz=The results of `docker network inspect` for a single network.c                     || _         || _        y r4   rX  rZ  s      r0   r7   zDockerNetworkInspect.__init__  r[  r2   Nr  )r   r   r   r   r7   r   r2   r0   r  r    s
    G%r2   r  c                    	 t        | dd|gd|      d   }| j                  r|sg }nt        j                  |      }t        |      dk(  rt        | |d         S y# t        $ r d}Y Qw xY w)	zy
    Return the results of `docker network inspect` for the specified network or None if the network does not exist.
    r  r5  Tr   r   r
  r   N)r&   r	   r   r'   r(   r   r  )r,   r  r    r   rr   s        r0   docker_network_inspectr    sx    y)W&Et\bcdef ||F

6"
5zQ#D%(33  s   A A)(A)c                    	 t        | d|gd       y# t        $ r(}t        j                  t	        |             Y d}~yd}~ww xY w)zoDisplay logs for the specified container. If an error occurs, it is displayed rather than raising an exception.logsFr.  N)r&   r	   r   errorr{   rJ  s      r0   docker_logsr    s;    tfl3UC c"gs    	A?Ac
           
     t    |sg }|	s|s|r|j                  d       t        | dg|z   |gz   |z   ||||||	      S )z5Execute the given command in the specified container.r   exec)r   stdinr   interactiveoutput_streamr   )rs   r&   )
r,   r$  r   r   r   r  r   r  r  r   s
             r0   docker_execr    sW     ut	7l^+c1#	 	r2   c	                    t               }	t               j                  g}
|
d   dk(  rt               r|
j	                  d       t        | |
|z   |	|||||||
      S )z!Run the specified docker command.r   r   z--remote)r   r   r  r   r  r    r  r   )r   r$   r%   r  rs   r   )r,   r   r   r  r   r  r  r    r   r   r%   s              r0   r&   r&     sj     
C''(GqzX"3"5z"## r2   c            
         t               } dh}ddh}| j                  t        j                  j	                         D ci c]!  \  }|v st        fd|D              s|# c}}       | S c c}}w )z]Return a dictionary of docker related environment variables found in the current environment.XDG_RUNTIME_DIR
CONTAINER_DOCKER_c              3  @   K   | ]  }j                  |        y wr4   )r   )r   prefixr   s     r0   r   z%docker_environment.<locals>.<genexpr>  s+       ^R  {A^b^m^mnt^u  ^Rs   )r   updater   r   rr   any)r   	var_namesvar_prefixesr   rv   s      ` r0   r   r     s    

C 	I
 	L
 JJrzz/?/?/A  S  SeTYEVZ]  ^R  EQ  ^R  [Re  S  TJ Ss     A0
!A0
ry   )r,   r   rz   r   )r,   r   r   r{   rz   r{   r4   )r,   r   r   r{   r   r  r   r  r   t.Optional[str]rz   'tuple[t.Optional[str], t.Optional[str]])rz   r   r   r   r   )rz   r  )r,   r   r)  r{   rz   r}   )
r,   r   r$  r{   r:  r{   r;  r{   rz   r}   )
r,   r   r)  r{   r   r  r   r  rz   r  )NN)r,   r   r)  r{   r   r  r   r  r   r  rz   r  )r,   r   r$  r{   r   r  rz   r  )r,   r   r$  r{   rz   r}   )F)r,   r   rR  r{   r    r   rz   rV  )r,   r   r$  r{   r  r{   rz   r}   )r,   r   r)  r{   r    r   rz   zt.Optional[DockerImageInspect])r,   r   r  r{   r    r   rz   z t.Optional[DockerNetworkInspect])NNNFNN)r,   r   r$  r{   r   r  r   r   r   r  r  t.Optional[t.IO[bytes]]r   r  r  r   r  t.Optional[OutputStream]r   r  rz   r  )NNFNFN)r,   r   r   r  r   r   r  r  r   r  r  r   r  r  r    r   r   r  rz   r  r  )Rr   
__future__r   dataclassesenumr'   r   r   r  r   r1  urllib.parser   typingtutilr   r   r   r   r	   r
   r   r   r   util_commonr   r   rg  r   threadr   r   rn   r   r   r   r   r   r   r   r   r   Enumr   	dataclassr   r`   r   r   r   r$   r   r   r   r   r  r  r  r  r&  r   r(  r<  r?  r   rD  rK  	ExceptionrM  rO  rV  r  r  r  r/  r  r  r  r  r&   r   r   r2   r0   <module>r     s   8 "    	  	    
 
 


  
 G  
  b8 b8J   ;$)) ; d#  $ O Od) !?
?
? 
? 	?
 ? -?. @o " "
&
    $    4  .   , ,^'%:S 	T
TT T 
	T
 -T  \
\\ \ 
	\
 \ -\T
TT T -	T  )  %[ %[ [|-*Y
" "6    F% %( &*%)&*.2 
 
 	
 # # $  ,  -F &*&*.2 
	  #	
 $  ,   -@r2   