
    AE                        S r SSK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  \R>                  " S5      r Sr!         S'S\"4S jjr#S r$S r%S\"S\"S\"SS4S jr& S(S jr'S r(S  r)S! r*S"\RV                  S\,\"\4   4S# jr-S$ r.S\"S\/4S% jr0S& r1g))Creates an image from Source.    N)Any)encoding)
exceptions)cloudbuild_util)global_methods)apis)waiter)base)submit_util)artifact_registry)stages)sources)types)
properties)	resources)retryzSprojects/(?P<projectId>[^/]*)/locations/(?P<location>[^/]*)/builds/(?P<build>[^/]*)z/cloud-run-source-deployregionc                    U(       ae  U R                  [        R                  5        U R                  S5        [        R
                  " XF5        U R                  [        R                  5        SnSnSnU R                  [        R                  5        U(       a4  U R                  S5        [        X!U5        [        R                  " U5      nO)U R                  S5        [        R                  " X'UU5      nU R                  [        R                  5        [        UUUU
UUUUUUUUU5      n [        U U5      u  nnnU(       a6  US   S:w  a-  U R#                  [        R$                  SS	R'                  US
9S9  gU R                  [        R$                  5        US   S   S   S   UUS   UUS   4$ ! [        R                   a+  nU
(       d  U	(       a  Ue[!        U UUUUUS9u  nn SnANSnAff = f)r   zCreating Container Repository.Ngcloudz+Using the source from the specified bucket.Uploading sources.)remote_sourcestatusSUCCESSzCContainer build failed and logs are available at [{build_log_url}].build_log_url)message)NNNNNresultsimagesr   digestidname)
StartStager   CREATE_REPOUpdateHeaderMessager   CreateRepositoryCompleteStageUPLOAD_SOURCE_ValidateCmekDeploymentr   GetGcsObjectUpload_PrepareSubmitBuildRequest_SubmitBuildapitools_exceptionsHttpNotFoundError_CreateImageWithoutSubmitBuild	FailStageBUILD_READYformat)trackerbuild_imagebuild_source
build_packrepo_to_createrelease_trackalready_activated_servicesr   resource_refdelegate_builds
base_imageservice_accountbuild_worker_poolbuild_machine_typebuild_env_varsenable_automatic_updatessource_bucketkms_keybase_image_from_buildsourceclientsubmit_build_requestresponse_dictr   es                            <lib/googlecloudsdk/command_lib/run/sourcedeploys/deployer.pyCreateImagerL   /   s   * v))* @A&& &,,-&&	V))* MNLw?!!,/F 45^^L,NF	,,-3:F;7M="7, }X.);77=v+ 8> 8  	 (&,,-i *1-h7df A 
	.	. _ g $B"$ M=s   -F2 2G1!G,,G1c                 F    [        U UUUU5      u  pg[        U UUUS9u  pX4$ )zSCreates an image from source by calling GCB direcly, bypassing the SubmitBuild API.)skip_activation_prompt)_PrepareBuildConfig_BuildFromSource)
r4   r5   r6   r7   r:   r   build_messagesbuild_configrI   r   s
             rK   r0   r0      sF     "5". "27	"- 
	%%    c                    [         R                  " 5       nU(       Gau  [        R                  " U5      nUb?  US   R	                  S/ 5      nUR                  SU 35        US   R                  SU05        [        R                  R                  R                  R                  S5        [        R                  " U40 SS_SU_SS_S	S_S
S_SS_SU_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SU_SS_SS_SS_6nUcL  UR                  S   R                  S:X  d   eUR                  S   R                   R#                  SS U 3/5        UR%                  UR'                  UR(                  UR                  UR*                  S!9S"9Ul        XX4$ U R/                  [0        R2                  5        U R5                  S#5        [        R                  R                  R                  R                  S5        [        R                  " U40 SS_SU_SS_S	S_S
S_SS_SU_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SU_SS_SS_6nU R7                  [0        R2                  5        XX4$ )$z%Prepare build config for cloud build.Nr   envszGOOGLE_LABEL_SOURCE=Fno_cachemessagessubstitutions
arg_configis_specified_sourceT	no_sourcerF   gcs_source_staging_dirignore_filearg_gcs_log_dirarg_machine_typearg_disk_sizearg_worker_poolarg_dirarg_revisionarg_git_source_dirarg_git_source_revisionarg_service_account	buildpack	hide_logsskip_set_source
client_tag	gcloudrunz"gcr.io/cloud-builders/gcb-internalz--labelzgoogle.source=bucketobject
generation)storageSourcer   )r   GetMessagesModuler   GetGsutilUrigetappendupdater   VALUESbuilds
use_kanikoSetr   CreateBuildConfigstepsr"   argsextendSourceStorageSourcerm   ro   rF   r#   r   r(   r%   r'   )	r4   r5   r6   r7   r   rQ   urirU   rR   s	            rK   rO   rO      sh    #446. 

}
-C]vr*d
kk(./mFD>* ''++E200   	
  !    $        !"  #$ !%%& !'( )* +, -. /L6 "''+OOOO  ''nSE4J(KL(//$22 '' %%$// 3 
 0 LL 
	%%= v++, 45''++E200   	
  !    $        !"  #$ !%%& !'( )* +, -L0 &../		%%rS   rF   image_repositoryrD   returnc                    U(       d  g[         R                  " U 5      (       d  [        R                  " SU  S35      eU(       d  [        R                  " S5      e[	        U5      (       a  [        R                  " S5      eg)z/Validate the CMEK parameters of the deployment.NzInvalid source location: z. Deployments encrypted with a customer-managed encryption key (CMEK) expect the source to be passed in a pre-configured Cloud Storage bucket. See https://cloud.google.com/run/docs/securing/using-cmek#source-deploy for more details.zDeployments encrypted with a customer-managed encryption key (CMEK) require a pre-configured Artifact Registry repository to be passed via the `--image` flag. See https://cloud.google.com/run/docs/securing/using-cmek#source-deploy for more details.a  The default Artifact Registry repository can not be used when deploying with a customer-managed encryption key (CMEK). Please provide a pre-configured repository using the `--image` flag. See https://cloud.google.com/run/docs/securing/using-cmek#source-deploy for more details.)r   IsGcsObjectr   ArgumentError_IsDefaultImageRepository)rF   r   rD   s      rK   r)   r)     s     

			V	$	$

"
"
#F8 , 	  


"
"	  /00

"
"	  1rS   c           	         [         R                  n[        R                  " USUSUUS9u  pVSUR                   SU SUR
                   3n[        R                  R                  USS9nUR                  n	U R                  [        R                  5        U R                  S5        U R                  [        R                  S	R                  U	S
95        [!        U5      n
X4$ )zGBuild an image from source if a user specifies a source when deploying.T)rh   build_regionrN   	projects//locations//operations/(cloudbuild.projects.locations.operations
collectionBuilding Container.(Logs are available at [{build_log_url}].r   )r   DEFAULT_REGIONr   Build	projectIdr!   r   REGISTRYParseRelativeNamelogUrlr#   r   r2   r%   UpdateStager3   _PollUntilBuildCompletes)r4   rQ   rR   rN   r   build_build_opbuild_op_refr   rI   s              rK   rP   rP   0  s     !//,
3(% )\N,uxxjY(##55E 6 , ,,-	V''(	34	077% 8  +<8-		%%rS   c                    [         R                  " [        R                  S5      nSR	                  [
        R                  R                  R                  R                  SS9US9nUR                  UR                  UR                  UR                  S9n[        U5      nU(       Ga  SnUS   R                  S	S5      nUS   R                  S
/ 5       H/  nUR!                  S5      (       d  M  UR#                  S5      S   nM1     U	bh  UR$                  R'                  [)        U	R+                  5       5       VVs/ s H*  u  nnUR$                  R&                  R-                  UUS9PM,     snnS9n	UR/                  UUR1                  UUS   R                  S5      UR%                  UUU	U
US9SUUUU[3        X5      US9
S9$ UR/                  UUR1                  UU SUR5                  5       UUUU[3        X5      US9
S9$ s  snnf )zBUpload the provided build source and prepare submit build request.v2z%projects/{project}/locations/{region}T)required)projectr   rl   Nr   project_descriptorrU   GOOGLE_FUNCTION_TARGET=   )keyvalue)additionalPropertiesimage)	baseImagefunctionTargetenvironmentVariablesenableAutomaticUpdatesprojectDescriptor)
rp   imageUribuildpackBuilddockerBuildtagsserviceAccount
workerPoolmachineTypereleaseTrackrG   )parent"googleCloudRunV2SubmitBuildRequest)r	   rq   r   SERVERLESS_API_NAMEr3   r   rv   corer   GetGoogleCloudRunV2StorageSourcerm   r"   ro   _GetBuildTagsrs   
startswithsplitGoogleCloudRunV2BuildpacksBuildEnvironmentVariablesValuesorteditemsAdditionalProperty'RunProjectsLocationsBuildsSubmitRequest"GoogleCloudRunV2SubmitBuildRequest_MapToReleaseTrackEnumGoogleCloudRunV2DockerBuild)docker_imager7   r   r=   rF   r;   r>   r?   r@   rA   rB   r9   rG   rW   r   storage_sourcer   function_targetr   envr   r   s                         rK   r,   r,   O  sK     ##N$F$FM(299$$,,00$0? : & 99]]6;;6;L;L : . 
|	$$O#A**+?F!}  ,	0	1	1))C.+ - !??YY
 !'~';';'= >	  !?*#u 66PPcc d  !?	  Z n ;;+3+V+V(]&&w/#CC$.%3'?"4 D  *(*/H! ,W ,
 <  . 
	9	9)1)T)T&::<(&(-mF *U * 
: 
 = s   :1H 
c                 V    [         R                  " U 5       SU R                  5        3/$ )Nr   )r   GetKindName)r;   s    rK   r   r     s*    ]]<()<+<+<+>*?
@	AArS   c                 Z   [         R                  " [        R                  S5      n[        R
                  " 5       nUR                  R                  U5      nUR                  (       a  U R                  UR                  5        UR                  n[        R                  " UR                  5      n[        R                  " UR                  U5      R                   n[#        UR$                  5      nSUR&                   SU SUR(                   3n	[*        R,                  R/                  U	SS9n
UR0                  nU R3                  [4        R6                  5        U R9                  S5        U R;                  [4        R6                  SR=                  US	95        [?        U
5      nXUR@                  4$ )
a  Call Build API to submit a build.

Arguments:
  tracker: StagedProgressTracker, to report on the progress of releasing.
  submit_build_request: SubmitBuildRequest, the request to submit build.

Returns:
  response_dict: Build resource returned by Cloud build.
  build_log_url: The url to build log
  build_response.baseImageUri: The rectified uri of the base image that should
  be used in automatic base image update.
r   r   r   r   r   r   r   r   r   )!r	   GetClientInstancer   r   r   rq   projects_locations_buildsSubmitbaseImageWarning
AddWarningbuildOperationr   MessageToJsonmetadataJsonToMessageBuildOperationMetadatar   _GetBuildRegionr"   r   r!   r   r   r   r   r#   r   r2   r%   r   r3   r   baseImageUri)r4   rH   
run_clientrQ   build_responser   jsonr   r   r"   r   r   rI   s                rK   r-   r-     sp     %%n&H&H$O*"446.77>>. $$~667**(			 1 1	2$

 
 ++T	E  !,,U__%[l588*	U$##55
A 6 , ,,-	V''(	34	077% 8  +<8-	~'B'B	BBrS   r   c           
      ^   [         R                  " 5       n[        R                  " UR                  UR
                  5      n [        R                  " UU SSSSSS9n[        R                  " UR                  5      $ ! [        R                   a    [        R                  " UU SSSSS	9n NOf = f)
a:  Poll the build operation until it completes.

Args:
  build_op_ref: The resource reference for the Cloud Build operation.

Returns:
  A dictionary representation of the completed build operation's response.

Raises:
  waiter.TimeoutError: If the build operation does not complete within the
    maximum wait time.
i  r   r   i`  )sleep_ms	jitter_msexponential_sleep_multiplierwait_ceiling_msmax_wait_msi  g?i'  )r   r   r   r   )r   r   r
   CloudOperationPollerprojects_builds
operationsPollUntilDoner   RetryExceptionr   MessageToPyValueresponse)r   rG   poller	operations       rK   r   r     s     ,,.&&&f//&$$%&I& 
	"	"9#5#5	66 
		 	$$%(I	s   A< <-B,+B,c                 x    [         R                  U 5      nU(       a  UR                  S5      $ [        SU  35      e)NlocationzInvalid build name: )_BUILD_NAME_PATTERNmatchgroup
ValueError)
build_namer   s     rK   r   r      s7    

#
#J
/%
;;z"")*677rS   c                     [         U ;   $ )z2Checks if the image repository is the default one.)_DEFAULT_IMAGE_REPOSITORY_NAME)r   s    rK   r   r     s    	'+;	;;rS   c                     SnU (       aF  U [         R                  R                  :w  a(  UR                  R                  nU" U R
                  5      nU$ )z-Returns the enum value for the release track.N)calliope_baseReleaseTrackGAr   ReleaseTrackValueValuesEnumr"   )r9   rW   release_track_enum_valuerelease_track_enum_clss       rK   r   r     sK    !}(B(B(E(EE33OO   6m6H6HI	!!rS   )	FNNNNNFNN)F)2__doc__retypingr   apitools.base.pyr   r   r.   !googlecloudsdk.api_lib.cloudbuildr   googlecloudsdk.api_lib.runr   googlecloudsdk.api_lib.utilr	   r
   googlecloudsdk.callioper   r   !googlecloudsdk.command_lib.buildsr   googlecloudsdk.command_lib.runr   r   ,googlecloudsdk.command_lib.run.sourcedeploysr   r   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   compiler   r   strrL   r0   rO   r)   rP   r,   r   r-   Resourcedictr   r   boolr   r    rS   rK   <module>r     s   $ 	  % > = 5 , . 9 9 < 5 1 @ > * ) * jjY  "<   "%d dN&2\&~#&14	F CH&>OdB-C`(7$$(7	#s(^(7V8< < <
"rS   