
    r                     l   d Z ddlm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m	Z	 ddlm
Z ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlm
Z ddlmZ ddlmZ ddlmZ ddlm
Z ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddl$m%Z% ddl&Z&dZ'ddgZ( G d dejR                        Z* G d dejR                        Z+ G d dejR                        Z,d Z-d e.fd!Z/d" Z0d# Z1d$ejd                  dfd%Z3d& Z4d' Z5d( Z6d) Z7d* Z8	 d2d+Z9d, Z:	 	 	 	 d3d-Z;	 	 	 	 d3d.Z<d2d/Z=d$ejd                  d$d$d$d0fd1Z>y)4z+Support library to handle the build submit.    )absolute_import)division)unicode_literalsN)encoding)
exceptions)cloudbuild_exceptions)cloudbuild_util)config)logs)utils)storage_api)flags)staging_bucket_util)	execution)execution_utils)log)
properties)	resources)timesz?projects/{project}/locations/{location}/clusters/{cluster_name}zgcr.iozpkg.devc                       e Zd ZdZy)BucketForbiddenErrorz8Error raised when the user is forbidden to use a bucket.N)__name__
__module____qualname____doc__     4lib/googlecloudsdk/command_lib/builds/submit_util.pyr   r   5   s    @r   r   c                   "     e Zd ZdZ fdZ xZS )FailedBuildExceptionz*Exception for builds that did not succeed.c                 v    t         t        |   dj                  |j                  |j
                               y )Nz+build {id} completed with status "{status}")idstatus)superr    __init__formatr"   r#   )selfbuild	__class__s     r   r%   zFailedBuildException.__init__<   s3    	
.5<<xx 	= 	
r   r   r   r   r   r%   __classcell__r)   s   @r   r    r    9   s    2 r   r    c                   "     e Zd ZdZ fdZ xZS )RegionMismatchErrorzBUser-specified build region does not match the worker pool region.c                 >    d|d|d}t         t        |   |       y)zAlert that build_region does not match wp_region.

    Args:
      build_region: str, The region specified in the build config.
      wp_region: str, The region where the worker pool is.
    zYBuilds that run in a worker pool can only run in that worker pool's region. You selected z, but your worker pool is in z-. To fix this, simply omit the --region flag.N)r$   r.   r%   )r'   build_region	wp_regionmsgr)   s       r   r%   zRegionMismatchError.__init__G   s(     7CI	O 
 

t-c2r   r*   r,   s   @r   r.   r.   D   s    J3 3r   r.   c                  0   t         j                  j                  j                  j	                         } | &	 t        |       }t        j                  |      dz   }|S d}|S # t        $ r- t        j                  |       }t        |j                        }Y Sw xY w)zGet the build timeout.Ns)r   VALUESbuildstimeoutGetint
ValueErrorr   ParseDurationtotal_secondssix	text_type)build_timeoutbuild_timeout_secsbuild_timeout_durationtimeout_strs       r   _GetBuildTimeoutrC   V   s    ##**22668-E}- -- 23c9K 
 K	  E$22=A5CCDEs   A 3BBreturnc                 8    | dk(  s| dk(  ry| dk(  ry| dk(  ry| yy	)
zGet the builder tag for input builder useful to cloudbuild.

  Args:
    builder: Google owned builder that needs to be tagged. Any other builders
      are marked as other
  Returns:
    Tag identifying the builder being used.
   gcr.io/buildpacks/builder:latestzgcr.io/buildpacks/builderlatestz#gcr.io/buildpacks/builder:google-22google22zgcr.io/buildpacks/builder:v1v1defaultotherr   )builders    r   _GetBuildTagrM   g   s<     33	/	/7700r   c
                  	   	  t         j                  j                  j                  j	                         r.t         fdt        D              st        j                  dd      t         j                  j                  j                  j	                         r|rd}
nAdj                  t         j                  j                  j                  j                               }
|j                  |j                  t         j                  j                  j                  j                         d dd|
d	d
g      g|t!        j"                  ||            }n#|rt        j                  dd      |s\t$        j&                  j)                  |      r=d}t%        j*                  |      D ]  }|dk(  s	d} n |st        j                  dd      |j                   g|j                  ddddddd dg      g|t!        j"                  ||            }ng||st        j                  dd      |d   j-                  d       t        j                  dd      |d   j-                  d       t         j                  j                  j                  j	                         r.t        fd!t        D              st        j                  dd"      |d   j-                  d#      }|d   j-                  d$      }|d   j-                  d%      }g }dddg}t/        |      g}|"|j1                  d&       |j1                  |       |)|D ]$  }|j1                  d&       |j1                  |       & |#|j1                  d'       |j1                  |       n4d(}|j1                  t/        |             |j                  d)d*d+d,|g-      g}|j1                  |j                  d)d*|-             	d.n		t3        t5        	fd/|            }|j                  g||t!        j"                  ||      |0      }nI|rt        j6                  d1d      |st        j                  d1d2      t9        j:                  ||||3      }|r||_        |S )4zSet build steps.c              3   &   K   | ]  }|v  
 y wNr   ).0regtags     r   	<genexpr>z!_SetBuildSteps.<locals>.<genexpr>   s      @3ss
3   z--tagz9Tag value must be in the *gcr.io* or *pkg.dev* namespace.0hz{}hz--destinationz--cachez--cache-ttlz--cache-dir )nameargs)stepsr7   substitutionszno-cachez@Cannot specify --no-cache if builds/use_kaniko property is FalseF
DockerfileTsourcez)Dockerfile required when specifying --tagz"gcr.io/cloud-builders/gcb-internaldockerr(   z	--network
cloudbuildz
--no-cachez-t.)imagesrZ   r7   r[   z--packzImage value must not be empty.r   imagec              3   &   K   | ]  }|v  
 y wrP   r   )rQ   rR   rb   s     r   rT   z!_SetBuildSteps.<locals>.<genexpr>   s      @ 5u 5rU   z:Image value must be in the *gcr.io* or *pkg.dev* namespaceenvenvsrL   z--envz	--builderrF   zgcr.io/k8s-skaffold/packpackr
   zdefault-builder)rX   
entrypointrY   rK   c                     d| z   dz   z   S )Nzgcp-runtimes-builder--r   )x
client_tags    r   <lambda>z _SetBuildSteps.<locals>.<lambda>  s    -1C7*Dr   )ra   rZ   r7   r[   tagsz--configz#Config file path must not be empty.)params	no_source)r   r5   r6   	check_tagGetBoolany_SUPPORTED_REGISTRIESc_exceptionsInvalidArgumentException
use_kanikor&   kaniko_cache_ttlr8   Build	BuildStepkaniko_imager	   EncodeSubstitutionsospathisdirlistdirgetrM   appendlistmapConflictingArgumentsExceptionr
   LoadCloudbuildConfigFromPathr7   )rS   no_cachemessagesr[   
arg_configro   r]   rB   	buildpackrk   ttlbuild_configfoundfilenamerd   re   rL   rZ   	pack_args
build_tagsdefault_buildpacks_buildercloudbuild_tagsrb   s   `        `            @r   _SetBuildStepsr      s    	_))113C @3@ = 11
N  **224	ll:,,33DDHHJK^^  !((//<<@@B%## !  ';;X! $ l* 
33N
 	

 277==0

6*H%E + 55C  ^^  ;!""	 !  ';;X% $ l, 11
4  | (11
4  aLW%E))113C @ 5@ = 11
P  A,

5
!CQ<F#Dly)GE	I w'(J
ws#!  {#w#E %?@A 

-#,  
e 
LL+ 	 	
 '.JJD	
O >>w%998
  " L 66z<PP11
;  66H]iL
 &L	r   c                      t         j                  j                  j                  j	                         t         j                  j                  j                  j
                  k7  S )zAReturns true if the command is running in a non-default universe.)r   r5   coreuniverse_domainr8   rJ   r   r   r   _IsNonDefaultUniverser   8  sG     ,,002						/	/	7	78r   Fc                 Z   d}d}t         j                  }|d}|t         j                  k7  r\t               r|8|Nt        j                  |      |j
                  j                  j                  k(  r|}t        j                  |      }nt        j                         }dj                  |      }t        j                         }|s|rdrt        fddD              r+|j                  |j!                  ||	            | _        | S t%        j&                  d	      r+|j                  |j)                  ||
            | _        | S t%        j&                  d      r+|j                  |j+                  ||            | _        | S d}j-                  d      st.        j0                  j3                        r"t.        j0                  j5                        \  }}dj                  t7        j8                  t7        j:                               t=        j>                         j@                  |      }tB        jD                  jG                  |d      }	 |t         j                  k(  r|jI                  |jJ                  |       n|jI                  |jJ                  ||       |jX                  r|jX                  dz   |z   }tB        jD                  j[                  d|jJ                  |      }t        j\                  |||
|      }|dk(  rI|j                  |j_                  |jJ                  |j`                  |jb                              | _        | S |j                  |je                  |jJ                  |j`                  |jb                               | _        | S |stU        jf                  d!d"      | S # tL        jN                  $ r% tQ        dj                  |jJ                              t        jR                  $ r& tU        jV                  ddj                  |            w xY w)#z$Set the source for the build config.FNTzgs://{}/sourcec              3   @   K   | ]  }j                  |        y wrP   )
startswith)rQ   rj   r]   s     r   rT   zSetSource.<locals>.<genexpr>l  s     
A)@A6Q)@s   )zhttp://zhttps://)urldirrevision)	gitSourcez7projects/.*/locations/.*/connections/.*/repositories/.*)
repositoryr   r   )connectedRepositoryz=projects/.*/locations/.*/connections/.*/gitRepositoryLinks/.*)gitRepositoryLinkr   r   )developerConnectConfigz.tgzgs://z{stamp}-{uuid}{suffix})stampuuidsuffixstorage.objects
collection)check_ownership)locationr   aV  The user is forbidden from accessing the bucket [{}]. Please check your organization's policy or if the user has the "serviceusage.services.use" permission. Giving the user a role with this permission such as Service Usage Admin may fix this issue. Alternatively, use the --no-source option and access your source code via a different method.zgcs-source-staging-dirzvA bucket with name {} already exists and is owned by another project. Specify a bucket using --gcs-source-staging-dir./)r   bucketobject)ignore_file	hide_logsz.json)r   r   
generation)storageSourceManifest)storageSourcez--no-sourcez)To omit source, use the --no-source flag.)4r	   DEFAULT_REGIONr   r   GetDefaultBuckestBehaviorBuildOptions(DefaultLogsBucketBehaviorValueValuesEnumREGIONAL_USER_OWNED_BUCKETr   GetDefaultRegionalStagingBucketGetDefaultStagingBucketr&   r   StorageClientrr   Source	GitSourcer]   rematchConnectedRepositoryDeveloperConnectConfigr   r|   r}   isfilesplitextr   GetTimeStampFromDateTimeNowr   uuid4hexr   REGISTRYParseCreateBucketIfNotExistsr   api_exceptionsHttpForbiddenErrorr   BucketInWrongProjectErrorrt   RequiredArgumentExceptionr   CreateUploadStorageSourceManifestrX   r   StorageSourceru   )r   r   is_specified_sourcero   r]   gcs_source_staging_dirarg_dirarg_revisionarg_git_source_dirarg_git_source_revisionr   r   r0   arg_bucket_behaviordefault_gcs_sourcedefault_bucket_namedefault_bucket_location
gcs_clientr   _staged_objectgcs_source_stagingstaged_source_objs       `                  r   	SetSourcer   @  sP   " +::#555		 %8%@+//0CD$$MMhhi !-/OO
 0GGI-445HI((** 
F

A)@
AA$OO&&$. '  , l 	xxBF %OO&::# ;  , l 	xxH& %OO!)!@!@ &# "A " , l F!RWW^^F%;''""6*ia -33,,UYY[9ZZ\ 4 M
 '//55+< 6 	 O$B$B	B**")). 	+ 	

 	**")),. 	+ 	
0 $$,33c9MIm"++22$%,, 3  ,22 $OO ( > >&--&++*55 !? ! , l, 
 %OO ..&--&++*55 /  , l 
 11
D  
u ,,  *
 +1&1G1N1N*O  00  22
"&&,f-@&A	 s   AN; ;A/P*c                    |rH	 t         j                  j                  |d      }d|j                  z   dz   |j                  z   | _        | S | S # t         j                  $ r Y nw xY w	 t         j                  j                  |d      }d|j                  z   | _        | S # t         j                  $ r1}t        j                  d|j                  |j                        d}~ww xY w)	z8Set a Google Cloud Storage directory to hold build logs.r   r   r   r   zstorage.bucketszstorage.buckets,storage.objects)expectedgotr}   N)	r   r   r   r   r   
logsBucket WrongResourceCollectionExceptionr   r}   )r   arg_gcs_log_dirgcs_log_dires       r   _SetLogsBucketr     s    	&&,,
&7 - k K&&
&
,{/A/A
A   
 55 
&&,,
&7 - k !(+*<*< <l 
 55 664!%%aff s*   AA A#"A#'5B C"1,CC"c                     |Gt        j                  |      }| j                  s|j                         | _        || j                  _        | S )z+Set the machine type used to run the build.)r   GetMachineTypeoptionsr   machineType)r   r   arg_machine_typemachine_types       r   _SetMachineTyper     sG    !''(89L%224l'3L$	r   c                     |Pt        j                  |      }| j                  s|j                         | _        t	        |      | j                  _        | S )z(Set the disk size used to run the build.)compute_utils	BytesToGbr   r   r9   
diskSizeGb)r   r   arg_disk_size	disk_sizes       r   _SetDiskSizer     sJ    ''6I%224l&))nL#	r   c                    |t         j                  j                  |d      }| j                  s|j	                         | _        |j                         | j                  _        |j                         | j                  j                  _        | S )z(Set the worker pool to run the build in.z)cloudbuild.projects.locations.workerPoolsr   )	r   r   r   r   r   
PoolOptionpoolRelativeNamerX   )r   r   arg_worker_poolworker_pools       r   _SetWorkerPoolr     s     $$**$O + K %224l ( 3 3 5L%0%=%=%?L"	r   c                    |t        j                  |       s||>| j                  s|j                         | _        | j                  j                  s|j                         | j                  _        | j                  j                  j                  s)|j                         | j                  j                  _        |:t        j                  |      }|| j                  j                  j                  _
        |:t        j                  |      }|| j                  j                  j                  _        |%|| j                  j                  j                  _        | S )zSet the worker pool config.)r	   WorkerPoolIsSpecifiedr   r   r   r   workerConfig<GoogleDevtoolsCloudbuildV1BuildOptionsPoolOptionWorkerConfigr   r   r   memoryGb	vcpuCount)r   r   r   
arg_memoryarg_vcpu_countr   memorys          r   _SetWorkerPoolConfigr	  &  s!    
#33LA				#%224l$$"*"5"5"7l$$11

O
O
Q ,  ))-8i:Cl,,7((4f8>l,,5!9Gl,,6	r   c                     |lt               r|dk(  rt        j                  dd      t        j                  |      }| j
                  s|j                         | _        || j
                  _        | S )a  Sets the behavior of the default logs bucket on Build options.

  Args:
    build_config: apitools.base.protorpclite.messages.Message, The Build message
      to analyze.
    messages: API messages class. The CloudBuild API messages.
    arg_bucket_behavior: The default buckets behavior flag.

  Returns:
    build_config: apitools.base.protorpclite.messages.Message, The Build message
      to analyze.
  zlegacy-bucketz--default-buckets-behaviorz>The legacy-bucket option is not supported in this environment.)r   rt   ru   r   r   r   r   defaultLogsBucketBehavior)r   r   r   bucket_behaviors       r   _SetDefaultLogsBucketBehaviorr  H  sr     $#6/#I11
&
J  556IJO%224l5DL2	r   c                     ||| _         | S )aG  Sets the service account to run the build in.

  Args:
    build_config: apitools.base.protorpclite.messages.Message, The Build message
      to analyze.
    arg_service_account: The service account behavior flag.

  Returns:
    build_config: apitools.base.protorpclite.messages.Message, The Build message
      to analyze.
  )serviceAccount)r   arg_service_accounts     r   _SetServiceAccountr  e  s     $"5L	r   c                    t               }t        | |||||||||
      }|st        |||||||||||	|      }t        ||
      }t	        |||      }t        |||      }t        |||      }t        |||      }t        ||      }|S zReturns a build config.)r   )	rC   r   r   r   r   r   r   r  r  )rS   r   r   r[   r   r   ro   r]   r   r   r   r   r   r   r   r   r   r   r  r   r   r   skip_set_sourcerk   rB   r   s                             r   CreateBuildConfigr  v  s    8 !"+	, 
L  o>, x9IJ,lHmD,hH,.H1, $L2EF,	r   c                    t               }t        | |||||||||
      }|st        |||||||||||	|      }t        ||
      }t	        |||      }t        |||      }t        |||||      }t        |||      }t        j                  |      r%t        j                  |      st        j                  t        j                  |      st        |||      }|S r  )rC   r   r   r   r   r   r	  r  r	   WorkerPoolConfigIsSpecifiedr  r    WorkerConfigButNoWorkerpoolErrorr   )rS   r   r   r[   r   r   ro   r]   r   r   r   r   r   r  r  r   r   r   r   r   r   r   r   r  rk   rB   r   s                              r   CreateBuildConfigAlphar    s
   8 !"+	, 
L  o>, x9IJ,hH,%HmZ, /H1, 0011,?

@
@@		.	.|	<hFL	r   c                 4   |2t         j                  j                  j                  j	                         }| j
                  }|s|S |j                  r|j                  j                  nd}|s|j                  }|s|S t        j                  |      s|S t        j                  |      }g }| j                  r9|r7t        d | j                  j                  D              }|D cg c]  }||v  }}|s|r|rt        j                   dd      |r||k7  r|S |S c c}w )ak  Determine what region of the GCB service this build should be sent to.

  Args:
    build_config: apitools.base.protorpclite.messages.Message, The Build message
      to analyze.
    desired_region: str, The region requested by the user, if any.

  Raises:
    RegionMismatchError: If the config conflicts with the desired region.

  Returns:
    str, The region that the build should be sent to, or None if it should be
    sent to the global region.

  Note: we do not validate region strings so that old versions of gcloud are
  able to access new regions. This is aligned with the approach used by other
  teams.
  rW   c              3   4   K   | ]  }|j                     y wrP   )key)rQ   ps     r   rT   z'DetermineBuildRegion.<locals>.<genexpr>/  s      F!Fs   z--regionzL--region flag required when workerpool resource includes region substitution)r   r5   r6   regionr8   r   r   rX   
workerPoolr	   IsWorkerPoolWorkerPoolRegionr[   r   additionalPropertiesrt   ru   )r   desired_region
wp_optionswp_resourcer1   matchessubstitution_keysks           r   DetermineBuildRegionr)    s!   * &&--4488:N##*	(2
$$R+	''K			%	%k	2..{;) 'I #11FF  *;;):AY):G;
iG

/
/  )3	 <s   !D   c	           	         t        j                  dt        |      z          t        j                  |      }	t
        j                  j                  dt        j                  j                  j                  j                         |      }
|	j                  j                  | j                  |
j                         |            }t!        j"                  |j$                        }t!        j&                  | j(                  |      j*                  }t
        j                  j-                  ddd|j.                  ||j0                  d	
      }|szt        j2                  |       |j4                  r:t         j6                  j9                  dj;                  |j4                               nt         j6                  j9                  d       |r||fS |s|j<                  ru|j<                  j>                  | j@                  jB                  jD                  | j@                  jB                  jF                  fv rt         j6                  j9                  d       tI        jJ                  tI        jL                  |	| |            }|st         jN                  nd}tQ        jR                  |      5  tU        jV                  |	| ||      jY                  ||      }ddd       |j6                  | jZ                  j\                  j^                  k(  rt         j6                  j9                  d       |j`                  r|j`                  D ]F  }t         j6                  j9                  dj;                  |jb                  |jd                               H t         j6                  j9                  dj;                  tg        |j`                                     |jh                  rCt         j6                  j9                  dj;                  |jh                  jj                               |j6                  | jZ                  j\                  jl                  k7  rto        |      ||fS # 1 sw Y   xY w)zStarts the build.zsubmitting build: )skip_activation_promptzcloudbuild.projects.locations)r   
projectsIdlocationsId)parentr(   Nz$cloudbuild.projects.locations.buildsrI   )r-  r.  buildsId)r   api_versionrn   z$Logs are available at [ {log_url} ].)log_urlz(Logs are available in the Cloud Console.z}
gcloud builds submit only displays logs from Cloud Storage. To view logs from Cloud Logging, run:
gcloud beta builds submit
zXYour build timed out. Use the [--timeout=DURATION] flag to change the timeout threshold.z
{priority}: {text})textpriorityz
{count} message(s) issued.)countz
BUILD FAILURE: {detail})detail)8r   debugreprr	   GetClientInstancer   r   r   r   r5   r   project	GetOrFailprojects_locations_builds.CloudbuildProjectsLocationsBuildsCreateRequestr   r   MessageToJsonmetadataJsonToMessageBuildOperationMetadatar(   r   	projectIdr"   CreatedResourcelogUrlr#   Printr&   r   loggingr   LoggingValueValuesEnumSTACKDRIVER_ONLYCLOUD_LOGGING_ONLYr   MashHandlerGetCancelBuildHandleroutr   CtrlCSectioncb_logsCloudBuildClientStreamrx   StatusValueValuesEnumTIMEOUTwarningsr3  r4  lenfailureInfor6  SUCCESSr    )r   async_r   r   r0   support_gclsuppress_logsr,  polling_intervalclientparent_resourceopjsonr(   	build_refmash_handlerrL  warns                     r   rx   rx   A  st    )) 4#556,,3& &&--0""''//99; . / ''..== --/| > " 
			,$

 
 !@!@$
G
M
M%   &&
7%hh	 ' 	) 
	"||	jj
0
7
7
7
M 
jjAB "9 
--
--





6
6
G
G



6
6
I
I
 JJ	  &&%%fh	B, %$# ##L1$$V%-%0%57 8>vi7M 
 2 \\X^^99AAAJJ	!
 ^^	jj
 
'
'TYY
'
O 
 JJ&--C4G-H JJ#**%2C2C2J2J*K \\X^^99AAA
u
%%	= 21s    )QQrP   )FNFN)?r   
__future__r   r   r   r|   r   r   apitools.base.pyr   r   r   !googlecloudsdk.api_lib.cloudbuildr   r	   r
   r   rN  googlecloudsdk.api_lib.computer   r   googlecloudsdk.api_lib.storager   googlecloudsdk.calliopert   !googlecloudsdk.command_lib.buildsr   r   %googlecloudsdk.command_lib.cloudbuildr   googlecloudsdk.corecore_exceptionsr   r   r   r   googlecloudsdk.core.utilr   r=   _CLUSTER_NAME_FMTrs   Errorr   r    r.   rC   strrM   r   r   r   r   r   r   r   r   r	  r  r  r  r  r)  rx   r   r   r   <module>rp     sT   2 &  ' 	 	  % 9 C = 4 = A 6 > 3 A ; = / # * ) * 
 F  "9- A?00 A?00 3/// 3$"S 0vr(  //bJ:F 15:L 1At 3K\6z  // mr   