
    Vh                       d Z ddlm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mZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZm Z m!Z!m"Z"m#Z#m$Z$ dd
l%m&Z&  ejN                  de!      Z( ejN                  de      Z) G d de      Z* ejV                  d       G d d             Z,	 	 	 d	 	 	 	 	 	 	 	 	 ddZ-ddZ.ddZ/ddZ0y)z"Provision hosts for running tests.    )annotationsN   )EnvironmentConfig)ApplicationErrorHostConnectionErrordisplayopen_binary_fileverify_sys_executableversion_to_str
type_guard)ExitHandler)WrappedThread)ControllerHostProfileDockerProfileHostProfileSshConnectionSshTargetHostProfilecreate_host_profile)run_pypi_proxyTHostProfile)boundTEnvironmentConfigc                      e Zd ZdZy)PrimeContainerszAException raised to end execution early after priming containers.N)__name__
__module____qualname____doc__     S/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/provisioning.pyr   r   6   s    Kr    r   T)frozenc                  b    e Zd ZU dZded<   ded<   edd       ZddZedd       Z	dd	Z
dd
Zy)	HostStatezKState of hosts and profiles to be passed to ansible-test during delegation.r   controller_profilelist[HostProfile]target_profilesc                f    t        j                  t        | j                        g| j                  z   S )z"Return all the profiles as a list.)tcastr   r%   r'   )selfs    r!   profileszHostState.profilesA   s)     {D$;$;<=@T@TTTr    c                r    t        |d      5 }t        j                  | |       ddd       y# 1 sw Y   yxY w)z+Serialize the host state to the given path.wbN)r	   pickledump)r+   path
state_files      r!   	serializezHostState.serializeF   s1    dD) 	*ZKKj)	* 	* 	*s   -6c                    t        |      5 }t        j                  |      }ddd       | j                  _        |j
                  D ]	  }| |_         |S # 1 sw Y   4xY w)z4Deserialize host state from the given args and path.N)r	   r/   loadr%   argsr'   )r6   r1   r2   
host_statetargets        r!   deserializezHostState.deserializeK   se     d# 	<z$*KK
$;J	< .2
%%* 00 	FFK	 	< 	<s   AAc                    t        t        j                  j                  | j                  D cg c]"  }t        |t              r|j                         $ c}            S c c}w )zLReturn SSH connection(s) for accessing all target hosts from the controller.)list	itertoolschainfrom_iterabler'   
isinstancer   !get_controller_target_connections)r+   r8   s     r!   r@   z+HostState.get_controller_target_connectionsX   s\    IOO11=A=Q=Q339U_`fh|U} 4:3[3[3] 3 @ A 	A 3s   'Ac                    | j                   st        d      t        | j                   |      sJ t        j                  t
        t           | j                         S )z<The list of target(s), verified to be of the specified type.zNo target profiles found.)r'   	Exceptionr   r)   r*   r;   r   )r+   profile_types     r!   targetszHostState.targets]   sI    ##788$..===vvd<($*>*>??r    N)returnr&   )r1   strrE   None)r6   r   r1   rF   rE   r$   )rE   zlist[SshConnection])rC   zt.Type[THostProfile]rE   zlist[THostProfile])r   r   r   r   __annotations__propertyr,   r3   staticmethodr9   r@   rD   r   r    r!   r$   r$   :   sJ    U--&&U U*
 
 
A
@r    r$   c                *   | j                   r@t        j                  | t        j                  j                  | j                   d            }n@t        | |       t        t        j                  t        t        | | j                  d            | j                  D cg c]  }t        | |d       c}      }| j                  r<|j                  D ]#  }t        |t               s|j#                          % t%               t'        j(                  t+        j,                  t.        |             dfd}t1        |j                  D cg c]#  }|t3        t+        j,                  ||            f% c}       |j4                  j7                          | j8                  sit;        | |       r |j4                         dfd}	t1        |j<                  D cg c]#  }|t3        t+        j,                  |	|            f% c}       |S c c}w c c}w c c}w )z
    Create new profiles, or load existing ones, and return them.
    If a requirements callback was provided, it will be used before configuring hosts if delegation has already been performed.
    z	state.datTF)r%   r'   c                L    | j                          s| j                          yy)zProvision the given profile.N)	provisionsetup)profile
skip_setups    r!   rM   z#prepare_profiles.<locals>.provision   s      r    c                `    | j                          s| j                          r	 |        yy)zConfigure the given profile.N)wait	configure)rO   requirementsrP   s    r!   rS   z#prepare_profiles.<locals>.configure   s+    LLN!!#W% r    )rO   r   rE   rG   )	host_pathr$   r9   osr1   joinr   r)   r*   r   r   
controllerrD   prime_containersr,   r?   r   rM   r   r   register	functoolspartialcleanup_profilesdispatch_jobsr   r%   rS   delegatecheck_controller_pythonr'   )
r6   targets_use_pypirP   rT   r7   r8   host_profilerM   rO   rS   s
     ``      r!   prepare_profilesrc   g   s    ~~**4dnnk1Z[
t-. vv&;=PQUW[WfWfhl=mnTXT`T`a&0vuEa


    * 3 3 -lM: **,- "##Y../?LM	  	fpfyfyz[by/@/@G/T!UVz{%%//1==j1667	& 	fp  gA  gA  B[by/@/@G/T!UV  B  	CS b& {( Bs   H
(H(Hc                *   t        t        j                  dd       }|j                  j                  }t        |j                        x}r t        dt        j                   d| d      |j                  }||k7  rt        d| d| d      || _
        y)	zBCheck the running environment to make sure it is what we expected.N   z"Running under Python interpreter "z" instead of "z".zRunning under Python version z instead of .)r   sysversion_infor%   pythonr
   r1   r   
executableversioncontroller_python)r6   r7   sys_versionrl   expected_executableexpected_versions         r!   r`   r`      s     !1!1"1!56K"55<<34E4J4JKKK!CCNNCSSabuavvxyzz(00;&!>{m<XhWiijkll.Dr    c                F    | j                   D ]  }|j                           y)z'Cleanup provisioned hosts when exiting.N)r,   deprovision)r7   rO   s     r!   r]   r]      s#    && r    c                   | D ]  \  }}d|_         |j                           t        d | D              r(t        j                  d       t        d | D              r(d}d}| D ]  \  }}	 |j                           |rt        d| d      |rt        d      y# t        $ r6}t        j                  d|j                   d|        d}|dz  }Y d}~rd}~wt        $ r1}t        j                  d|j                   d	|        d}Y d}~d}~wt        $ r}|j                  j                  d
k(  rdn|j                  j                  dz    |j                  j                   }t        j                  d|j                   ddj                  t!        j"                  |j$                              j'                          d| d|        d}Y d}~od}~ww xY w)z@Run the given profile job threads and wait for them to complete.Tc              3  B   K   | ]  \  }}|j                           y w)N)is_alive).0rO   threads      r!   	<genexpr>z dispatch_jobs.<locals>.<genexpr>   s     <OGVfoo<s   r   Fr   zHost z connection failed:
Nz job failed:
builtins rf   z0 job failed:
Traceback (most recent call last):

z: zHost job(s) failed, including z: connection failure(s). See previous error(s) for details.z6Host job(s) failed. See previous error(s) for details.)daemonstartanytimesleepwait_for_resultr   r   errorconfigr   rB   	__class__r   r   rW   	traceback	format_tb__traceback__rstrip)jobsrO   rv   failedconnection_failuresexnames          r!   r^   r^      s     <t<
<

1 <t<
< F 	""$  !$BCVBW  XR  #S  T  	TWXX ! # 	%MME'..!11FrdKLF1$ 	MME'..!1tDEF 	LL33zAbr||G^G^adGdefhfrfrff  fA  BDMME'..!11cWWY%8%89I9I%JKRRTUUWX\W]]_`b_ce fF		s1   )B	G#,CG 'DGB3GG)FFN)
r6   r   ra   boolrP   r   rT   z+t.Optional[c.Callable[[HostProfile], None]]rE   r$   )r6   r   r7   r$   rE   rG   )r7   r$   rE   rG   )r   z'list[tuple[HostProfile, WrappedThread]]rE   rG   )1r   
__future__r   collections.abcabccdataclassesr[   r<   rV   r/   rg   r~   r   typingr)   r   r   utilr   r   r   r	   r
   r   r   util_commonr   rv   r   host_profilesr   r   r   r   r   r   
pypi_proxyr   TypeVarr   r   r   	dataclassr$   rc   r`   r]   r^   r   r    r!   <module>r      s   ( "     	  
       qyy{;QYY3;LM L& L d#)@ )@ $)@\ #@D	:
:: : >	:
 :z/  Yr    