
    B                    
   S r SSKJr  SSKrSSKrSSKrSSKJrJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK J!r!  SSK"J#r#  SSK"J$r$  Sr%Sr&Sr' " S S5      r(  S%               S&S jjr)      S'S jr*S(S jr+S)S jr,              S*S jr-S  r.          S+S! jr/              S,S" jr0S# r1S$ r2g)-zBuild config for Run Compose.    )annotationsN)AnyDict)encoding)cloudbuild_util)waiter)submit_util)connection_context)	platforms)serverless_operations)
exceptions)
exit_codes)tracker)config)log)
properties)	resources)progress_tracker)files)parallelzfingerprints.jsonfingerprintimage_idc                  L    \ rS rSrSr S   S	S jjr\S
S j5       rSS jrSr	g)BuildConfig0   z1Represents the build configuration for a service.Nc                *    Xl         X l        S U l        g Ncontext
dockerfiler   )selfr   r    s      5lib/googlecloudsdk/command_lib/run/compose/builder.py__init__BuildConfig.__init__3   s     L O $DM    c                L    U " UR                  S5      UR                  S5      S9$ )Nr   r    )r   r    )get)clsdatas     r"   	from_dictBuildConfig.from_dict:   s'    #88L) r%   c                J    U R                   U R                  U R                  S.$ )z4Serializes the BuildConfig instance to a dictionary.r   r   )r!   s    r"   to_dictBuildConfig.to_dictA   s#     <<ooMM r%   r   )NN)r   
str | Noner    r/   )r)   Dict[str, Any]returnz'BuildConfig')r1   r0   )
__name__
__module____qualname____firstlineno____doc__r#   classmethodr*   r-   __static_attributes__ r%   r"   r   r   0   s:    9 BF%%4>%  r%   r   c                  ^ U(       a  [        XUT5        g[        U5      n0 n/ n	U R                  5        H  u  p[        U5      nU(       d  UR	                  U
0 5      R	                  [
        5      U:X  a  Xz   [           nXl        Xz   X'   [        R                  R                  R                  U
S9nTR                  U5        TR                  USU S35        TR                  U5        [        R                   " SU
 S35        M   [#        XXUT5      u  nnnU	R%                  U
UUUUU45        M     U	(       d  [=        X5        gU4S
 jnU	 V
VVVVVs/ s H  u  n
nnnnnU
UUUUU4PM     nnnnnn
n[?        [A        U5      S5      n[B        RD                  " U5       nURG                  UU5      nSSS5        SnW H'  nU(       d  M  US-  nUu  n
nn[
        U[        U0X'   M)     [=        X5        U[A        U	5      :w  a%  [,        R.                  " S[2        R4                  5      eg! [&        R(                   aP  n[        R*                  " SU
 SU 35        [,        R.                  " [1        U5      [2        R4                  5      UeSnAf[6        R8                   aG  n[        R*                  " S	U 35        [,        R.                  " S	U 3[2        R:                  5      UeSnAff = fs  snnnnnn
f ! , (       d  f       GNH= f)z6Performs source builds for all containers in parallel.N	containerzSkipping build, using image [z] from cache.zSkipping build for container z, using image from cache.zBuild failed for container : z+An error occurred during build submission: c                ^   > U u  nnnnnn[        XX4TU5      nU(       a  XUR                  4$ g r   )_poll_and_handle_build_resultr   )	argsr<   build_configbuild_op_refbuild_log_url	image_tagr   successr   s	           r"   
_run_buildhandle.<locals>._run_build   sK     	+gyG \%:%:::r%   
   r      z$One or more container builds failed.)$_handle_no_build_load_source_fingerprintsitems_calculate_source_fingerprintr'   _FINGERPRINT_KEY_IMAGE_ID_KEYr   tracker_stagesStagedProgressTrackerStageBUILDget_key
StartStageUpdateStageCompleteStager   debug_build_from_sourceappendr	   FailedBuildExceptionerrorcompose_exceptions
BuildErrorstrr   BUILD_FAILEDr   ErrorBUILD_SUBMISSION_ERROR_save_source_fingerprintsminlenr   GetPoolMap)source_buildrepoproject_nameregionr   no_buildbuildloaded_fingerprintsfingerprints_to_save	build_opsr<   rA   current_fingerprintr   	stage_keyrB   rC   rD   erF   r   	task_argsnum_threadspoolbuild_resultsnum_build_successesresults       `                      r"   handlery   J   s3    \A
1,?)!-!3!3!5i7E(,,Y;??	 %/>h&(;(F% ;;AAII J i #
4XJmL I&	ii))4M
N /A
4vw0,lM9 





 	3 "6Z 
2A
B !   





 




 !  & C	NB'+$HHZ3M % fvQ)/&ih
K
)%	  0?C	N*

'
'.
0G0G  +I ++ 	ii-i[1#>?))
a&*))  	ii=aSAB))7s
;

+
+ 8( %$s8   	)H-K-#K7-K*AJK*#AK%%K*7
Lc                6   [         R                  " 5       R                  n[        R                  R                  USSSU5      n[        R                  " U5        [        R                  R                  U[        5      n [        R                  " U5       n[        R                  " XSS9  SSS5        [        R                  " SU S35        g! , (       d  f       N)= f! [        R                   a&  n[        R                  " S	U S
U 35         SnAgSnAff = f)zSaves the source fingerprint map to a JSON file.

Args:
  source_fingerprint_map: A dictionary mapping container names to a
    dictionary containing their 'fingerprint' and 'image_id'.
  project_name: The name of the project.
surfaceruncompose   )indentNz$Successfully saved fingerprints to ''.z"Could not write fingerprint file '': )r   Pathsglobal_config_dirospathjoinr   MakeDir_FINGERPRINTS_FILE_NAME
FileWriterjsondumpr   rW   r`   warning)source_fingerprint_mapri   cfg_dirout_dirfingerprint_filefrr   s          r"   rb   rb      s     LLN,,'GGLL)UI|L'--WW\\'+BCO			*	+q
ii&!4 
,II45E4FbIJ 
,	+ 
 OKK45E4Fc!MNNOs0   >C C*"C 
CC D2DDc                   [         R                  " 5       R                  n[        R                  R                  USSSU 5      n[        R                  R                  U[        5      n[        R                  R                  U5      (       a7   [        R                  " U5       n[        R                  " U5      sSSS5        $ [        R                   " S	U S
35        0 $ ! , (       d  f       g= f! [        R                   a)  n[        R                  " SU SU S35        0 s SnA$ SnAf[        R                   a)  n[        R                  " SU SU S35        0 s SnA$ SnAff = f)aJ  Loads the source fingerprint map from a JSON file.

Args:
  project_name: The name of the project.

Returns:
  A dictionary mapping container names to a dictionary containing their
  'fingerprint' and 'image_id'. Returns an empty map if the file is not
  found or cannot be parsed, as this loading is done on a best-effort basis.
r{   r|   r}   Nz#Could not decode fingerprint file 'r   z. Starting with an empty map.z!Could not read fingerprint file 'zFingerprint file 'z(' not found. Starting with an empty map.)r   r   r   r   r   r   r   existsr   
FileReaderr   loadJSONDecodeErrorr   r   r`   rW   )ri   r   r   r   r   rr   s         r"   rK   rK      s/    LLN,,'GGLL)UI|L'WW\\'+BCWW^^$%%,-yy| .- II
-. /& 	& I' .- 	kk/0@/AQC H( ( i;; 	kk-.>-?s1# F( ( isN   C/ "C8	C/ 
C,(C/ ,C/ /E'D'!E''E'>E"E'"E'c           	        U R                   c  g[        R                  R                  U R                   5      (       d2  [        R
                  " SU R                    3[        R                  5      e[        R                  " 5       n[        R                  " U R                   SS9 H  u  p#nUR                  5         UR                  5         U H  n[        R                  R                  X%5      n[        R                  R                  X`R                   5      nUR                  UR                  S5      5         [         R"                  " U5       nUR%                  S5      =n	(       a+  UR                  U	5        UR%                  S5      =n	(       a  M+  SSS5        M     M     UR+                  5       $ ! , (       d  f       M  = f! [         R&                   a!  n
[(        R&                  " SU S	U
 35      eSn
A
ff = f)
z.Calculates the fingerprint of the source code.N z'Build context path is not a directory: T)topdownzutf-8i    zCould not read file z for fingerprinting: )r   r   r   isdirr\   r]   r   BUILD_CONTEXT_INVALIDhashlibsha256walksortr   relpathupdateencoder   BinaryFileReaderreadr`   r   	hexdigest)rA   sha1rootdirs	filenamesfilenamefilepathr   r   chunkrr   s              r"   rM   rM     s   !	|++	,	,

'
'
1,2F2F1GH(( 
 
	$!ww|';';TJd)IIKNNd-h*>*>?g
kk'..)*
##H-%%KK %% .-   K 
	 .- [[ 
"8*,A!E
 	

s7   7GAF1G1
G ;G GG8G33G8c           
     $    [        U5      nU(       aM  US   S:w  aD  UR                  [        R                  R                  R                  U S9SSR                  US9S9  gSnUR                  S	5      (       a  UR                  S	5      R                  S
5      (       au  UR                  S	5      R                  S
5       HQ  nUR                  S5      U:X  d  M  UR                  S5      n	U	(       d  M4  UR                  S5      u  n
  nU
S-   U	-   n  O   U(       a!  Xql	        [        R                  " SU S35        O XQl	        [        R                  " SU S35        UR                  [        R                  R                  R                  U S95        g! [        R                   ai  n[        R                  " SU  SU 35        UR                  [        R                  R                  R                  U S9SSR                  XS9S9   SnAgSnAff = f)z0Polls a build operation and updates the tracker.statusSUCCESSr;   NzCContainer build failed and logs are available at [{build_log_url}].rC   )messageFresultsimagesnamedigest:@zImage 'z
' created.Tz-An error occurred while waiting for build of r=   zRError waiting for build to complete: {e}. Logs are available at [{build_log_url}].)rr   rC   )_poll_until_build_completes	FailStagerP   rQ   rR   rS   formatr'   
rpartitionr   r   rW   rV   r   r`   r[   )r<   rA   rB   rC   r   rD   response_dictimage_with_digestimgr   image_name_without_tag_rr   s                r"   r?   r?   "  s   3/=Mx0I=

3
3
9
9
A
A! B  $$*FF$G  	 			9	%	%-*;*;I*F*J*J
+ + !$$Y/33H=CWWV_	)WWX&Fv-6-A-A#-F*$a"83">"G > 
 1		G-.j9: )		GI;j12

3
3
9
9
A
A! B 
 			 II=i[1#NO1177?? 	@ 	
 	""(&1&"J  	 s-   AF !A3F F 2BF H&AH

Hc                r   [         R                  R                  R                  R	                  SS9n[
        R                  " S[        R                  US9n[        R                  R                  U SU0SS9n[        R                  " U5       nUR                  U5      sSSS5        $ ! , (       d  f       g= f)z+Get service if it exists, else return None.TrequiredN)region_labelnamespacesIdrun.namespaces.servicesparams
collection)r   VALUEScoreprojectGetr
   GetConnectionContextr   PLATFORM_MANAGEDr   REGISTRYParser   Connect
GetService)service_namerj   r   conn_contextservice_refclients         r"   _get_servicer   a  s    ""**...='#88
  ,
 ""((g&* ) +
 $$\2f[) 322s   B((
B6c                p   U (       d  g[         R                  R                  R                  R	                  SS9n[
        R                  R                  USU0SS9n[        R                  " S[        R                  US9n[        R                  " U5       nUR                  U5      nSSS5        W(       d)  [        R                   " SU S	3["        R$                  5      e0 n	U(       ad  UR&                  R(                  R*                  (       a?  UR&                  R(                  R*                   H  n
U
R,                  XR.                  '   M     U R1                  5        H  u  p[2        R4                  R6                  R9                  US
9n UR;                  U5        U	R=                  U5      nU(       d  [>        R@                  " SU SU S	35      eXl!        URE                  USU S35        URG                  U5        M     g! , (       d  f       GNa= f! [H         a  nURK                  XS5        e SnAff = f)zHandles --no-build flag.NTr   r   r   r   )platformr   z?--no-build cannot be used for the first deployment of service 'r   r;   z$Could not find image for container 'z' in service 'zUsing image [z] from deployed service.zImage retrieval failed.)&r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r\   r]   r   BUILD_NO_BUILD_INVALIDtemplatespec
containersimager   rL   rP   rQ   rR   rS   rT   r'   r   r`   r   rU   rV   	Exceptionr   )rg   ri   rj   r   r   r   r   r   existing_servicecontainer_to_image_mapcr<   rA   rq   r   rr   s                   r"   rJ   rJ   r  s    

""**...='""((g&* ) +
 $88
)),
 $$\2f((5 3	

'
'	N"	)) 
 *3388CC&&++66'(wwVV$ 7 ".!3!3!5i99??GG H I#$((3e29+ >b"
 	
 $
}UG+CD I&! "6 32<  	&?@s%   H	A3H
H
H5H00H5c                   U R                   nUc  [        S5      eSR                  X#USS9n[        R                  " 5       n[
        R                  " SU SU S35        UR                  UR                  S	U 3S
SSSSUS/S9/U/SS9n	[        R                  " U	USSUSSSSSSSS9n	[
        R                  " S5        [        XXU5      u  pXU4$ )z?Performs source build for a given container using build config.Nz+Build context is required for source build.z'{repo}/{project_name}_{container}:{tag}latest)rh   ri   r<   tagz!Creating build config for image 'z' from source ''zBuild Docker Image: zgcr.io/cloud-builders/dockerbuildxrl   z--loadz-t.)idr   r@   3600s)stepsr   timeoutTF)
is_specified_source	no_sourcesourcegcs_source_staging_dirarg_dirarg_revisionarg_git_source_dirarg_git_source_revisionignore_file	hide_logsz&Submitting build to Google Cloud Build)r   
ValueErrorr   r   GetMessagesModuler   rW   Build	BuildStepr	   	SetSource_build_using_cloud_build)	build_cfgr<   rh   ri   rj   r   source_pathrD   messagesrA   rB   rC   s               r"   rX   rX     s-    !!+
B
CC7>>iX ? )
 ..0( )))) 5-q 


'	{31gxy#F   [   
, &&!", ))45 8(&!, 
i	//r%   c                |   [         R                  " USUSUS9u  pVSUR                   SU SUR                   3n[        R
                  R                  USS9nUR                  n	[        R                  R                  R                  U S9n
UR                  U
5        UR                  U
S	R                  U	S
95        X4$ )zGBuild an image from source if a user specifies a source when deploying.T)r   build_regionz	projects/z/locations/z/operations/z(cloudbuild.projects.locations.operations)r   r;   z(Logs are available at [{build_log_url}].r   )r	   r   	projectIdr   r   r   ParseRelativeNamelogUrlrP   rQ   rR   rS   rT   rU   r   )r<   r   build_messagesrA   rj   rl   r   build_oprB   rC   rq   s              r"   r   r     s     
(% 	%//"+fX\%((L  ##55E 6 , ,,-77==EE F ) 
Y	077% 8  
	$$r%   c                    [         R                  " 5       n[        R                  " UR                  UR
                  5      n[        R                  " X 5      n[        R                  " UR                  5      $ r   )
r   GetClientInstancer   CloudOperationPollerprojects_builds
operationsPollUntilDoner   MessageToPyValueresponse)rB   r   poller	operations       r"   r   r     sZ    ,,.&&&f//& ""68)		"	"9#5#5	66r%   )FF)rg   Dict[str, BuildConfig]rh   r^   ri   r^   rj   r^   r   &progress_tracker.StagedProgressTrackerrk   boolrl   r  r1   None)r   Dict[str, Dict[str, str]]ri   r^   r1   r  )ri   r^   r1   r  )rA   r   r1   r^   )r<   r^   rA   r   rB   zresources.ResourcerC   r^   r   r  rD   r^   r1   r  )
rg   r  ri   r^   rj   r^   r   r  r1   r  )r   r   r<   r^   rh   r^   ri   r^   rj   r^   r   r  r1   z#tuple[resources.Resource, str, str])3r6   
__future__r   r   r   r   typingr   r   apitools.base.pyr   !googlecloudsdk.api_lib.cloudbuildr   googlecloudsdk.api_lib.utilr   !googlecloudsdk.command_lib.buildsr	   googlecloudsdk.command_lib.runr
   r   r   &googlecloudsdk.command_lib.run.composer   r\   r   r   rP   googlecloudsdk.corer   r   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r   r   rN   rO   r   ry   rb   rK   rM   r?   r   rJ   rX   r   r   r9   r%   r"   <module>r"     s   $ "   	  % = . 9 = 4 @ S = L & * # * ) 8 * --    @ z(z
z z 	z
 4z z z 
zzO5OEHO	O.$N:<<< %< 	<
 4< < 
<~*"5(55 5 4	5
 
5p909090 90 	90
 90 490 )90x%>7r%   