
    ZN                        S r SSKrSSKrSSK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rSrSr " S S\R2                  5      r " S S\R2                  5      r\R8                  " S/ SQ5      r " S S\5      rS r S r! " S S\5      r" " S S\"5      r# " S S\"5      r$ " S S\RJ                  5      r& " S  S!\5      r'g)"z/Manage and stream build logs from Cloud Builds.    N)
exceptions)cloudbuild_util)common)log)
properties)	resources)console_attr_os)requests)encodingz
To live stream log output for this build, please ensure the grpc module is installed. Run:
  pip install grpcio
and set:
  export CLOUDSDK_PYTHON_SITEPACKAGES=1
a  
The build is running, and logs are being written to the default logs bucket.
This tool can only stream logs if you are Viewer/Owner of the project and, if applicable, allowed by your VPC-SC security policy.

The default logs bucket is always outside any VPC-SC security perimeter.
If you want your logs saved inside your VPC-SC perimeter, use your own bucket.
See https://cloud.google.com/build/docs/securing-builds/store-manage-build-logs.
c                   (   ^  \ rS rSrU 4S jrSrU =r$ )NoLogsBucketException6   c                 0   > Sn[         [        U ]  U5        g )Nz8Build does not specify logsBucket, unable to stream logs)superr   __init__)selfmsg	__class__s     -lib/googlecloudsdk/api_lib/cloudbuild/logs.pyr   NoLogsBucketException.__init__8   s    
DC	
/4     __name__
__module____qualname____firstlineno__r   __static_attributes____classcell__r   s   @r   r   r   6   s    5 5r   r   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )4DefaultLogsBucketIsOutsideSecurityPerimeterException=   c                 4   > [         [        U ]  [        5        g N)r   r"   r   6DEFAULT_LOGS_BUCKET_IS_OUTSIDE_SECURITY_PERIMETER_TEXT)r   r   s    r   r   =DefaultLogsBucketIsOutsideSecurityPerimeterException.__init__?   s    	
>
OPr   r   r   r    s   @r   r"   r"   =   s    Q Qr   r"   Response)statusheadersbodyc                   $    \ rS rSrSrS rS rSrg)RequestsLogTailerG   z9LogTailer transport to make HTTP requests using requests.c                 8    [         R                  " 5       U l        g r%   )creds_requests
GetSessionsessionr   s    r   r   RequestsLogTailer.__init__J   s    !,,.DLr   c                 0    U R                   R                  SUSSR                  U5      0S9n[        UR                  UR
                  UR                  5      $ ! [        R                  R                   a  n[        R                  " SU-  5      eS nAff = f)NGETRangez
bytes={0}-)r*   zFailed to connect: %s)r2   requestformatr(   status_coder*   contentr
   r   RequestExceptionapi_exceptionsCommunicationError)r   urlcursorresponsees        r   RequestRequestsLogTailer.RequestM   s    K%%
w(;(;F(CD & Fhh**H,<,<h>N>NOO// K--.E.IJJKs   AA B7BB)r2   N)r   r   r   r   __doc__r   rC   r   r   r   r   r-   r-   G   s    A/Kr   r-   c                       SSK Jn   U R                  5       $ ! [         a&    [        R                  R                  [        5         gf = f)zReturn a GCL LogTailer.r   tailingN)googlecloudsdk.api_lib.loggingrH   ImportErrorr   outPrintLOG_STREAM_HELP_TEXT	LogTailerrG   s    r   GetGCLLogTailerrO   V   s@    6 
				 
 GGMM&'s    -AAc                 6   U R                   (       a  U R                   R                  (       a)  [        U R                   R                  R                  5      $ U R                   R                  (       a)  [        U R                   R                  R
                  5      $ g)z/Separate CB4A requests to print different logs.F)optionsclusterboolnameanthosCluster
membership)builds    r   IsCB4ArX   c   s\    
]]}}%--'',,--		$	$%----8899	r   c                   B    \ rS rSrSrSrSrS rS r\4S jr	SS jr
S	rg
)
TailerBasem   z"Base class for log tailer classes.z REMOTE BUILD OUTPUT -c                     [         R                  R                  R                  R	                  5       nU(       a  [
        R                  " SSU5      $ U$ )z2Modify output for better screen reader experience.z--->  )r   VALUESaccessibilityscreen_readerGetBoolresub)r   textra   s      r   _ValidateScreenReader TailerBase._ValidateScreenReaderr   s=    %%33AAIIKMVVGR&&Kr   c                     U R                   (       a2  U(       a*  U R                   R                  UR                  5       5        ggg)z@Testing Hook: This method enables better verification of output.N)rK   rL   rstrip)r   re   s     r   _PrintLogLineTailerBase._PrintLogLiney   s(    xxD
hhnnT[[]# xr   c                     [         R                  " 5       u  p#U R                  UR                  X R                  5      5        g)zDPrint a pretty starting line to identify start of build output logs.Nr	   GetTermSizerj   centerOUTPUT_LINE_CHARr   r   width_s       r   _PrintFirstLineTailerBase._PrintFirstLine~   s/    **,HEszz%)>)>?@r   c                     [         R                  " 5       u  p#U R                  UR                  X R                  5      S-   5        g)z@Print a pretty ending line to identify end of build output logs.
Nrm   rq   s       r   _PrintLastLineTailerBase._PrintLastLine   s4    **,HEszz%)>)>?$FGr   r   N)r^   )r   r   r   r   rE   LOG_OUTPUT_BEGINrp   rf   rj   rt   rx   r   r   r   r   rZ   rZ   m   s,    *,$
 !1 A
Hr   rZ   c                   x    \ rS rSrSrS\R                  S4S jr\\R                  4S j5       r
S rS rS	 rS
rg)GCLLogTailer   zBHelper class to tail logs from GCL, printing content as available.NFc                     [        5       U l        Xl        X l        X0l        XPl        SU l        X@l        SU l        X`l	        g )N   F)
rO   tailerbuild_id
project_id	timestamprK   buffer_window_secondslog_urlstopis_cb4a)r   buildId	projectIdr   logUrlrK   r   s          r   r   GCLLogTailer.__init__   s;     "#DKMONH!"DLDILr   c           
      z    U " UR                   UR                  UR                  UR                  U[	        U5      S9$ )zBuild a GCLLogTailer from a build resource.

Args:
  build: Build resource, The build whose logs shall be streamed.
  out: The output stream to write the logs to.

Returns:
  GCLLogTailer, the tailer of this build's logs.
)r   r   r   r   rK   r   )idr   
createTimer   rX   )clsrW   rK   s      r   	FromBuildGCLLogTailer.FromBuild   s9     //""||u r   c                    U R                   (       d  gU R                  (       a  gSR                  U R                  S9nSR                  U R                  U R                  S9nU R
                  (       a  SR                  U R                  S9nU R                   R                  U/X R                  S9nU R                  5         U H/  nU R                  UR                  5      nU R                  U5        M1     U R                  S	5        U R                  (       a(  U R                  S
R                  U R                  S95        g)z9Tail the GCL logs and print any new bytes to the console.Nprojects/{project_id}r   zslogName="projects/{project_id}/logs/cloudbuild" AND resource.type="build" AND resource.labels.build_id="{build_id}")r   r   zdlabels."k8s-pod/tekton.dev/taskRun"="{build_id}" OR labels."k8s-pod/tekton_dev/taskRun"="{build_id}")r   )r   z( BUILD FINISHED; TRUNCATING OUTPUT LOGS z"Logs are available at [{log_url}].)r   )r   r   r9   r   r   r   TailLogsr   rt   rf   text_payloadrj   rx   r   r   parent
log_filteroutput_logsoutputre   s         r   TailGCLLogTailer.Tail   s*    ;;yy$++t+GF:;A6!%4== <B <J  ||
GHN!% IO I0  ++&&	*4N4N ' PK 	''(;(;<d
  	BC||

.
5
5dll
5
KM r   c                     SU l         [        R                  " U R                  5        U R                  (       a  U R                  R                  5         ggzStop log tailing.TN)r   timesleepr   r   Stopr3   s    r   r   GCLLogTailer.Stop   s7    DIJJt))*{{
kk r   c                    SR                  U R                  S9nSR                  U R                  U R                  U R                  S9nU R                  (       a$  SR                  U R                  U R                  S9n[
        R                  " USUS9nU R                  5         U H/  nU R                  UR                  5      nU R                  U5        M1     U R                  5         g	)
zPrint GCL logs to the console.r   r   zlogName="projects/{project_id}/logs/cloudbuild" AND resource.type="build" AND timestamp>="{timestamp}" AND resource.labels.build_id="{build_id}")r   r   r   z(labels."k8s-pod/tekton.dev/taskRun"="{build_id}" OR labels."k8s-pod/tekton_dev/taskRun"="{build_id}") AND timestamp>="{timestamp}")r   r   asc)r   order_byr   N)r9   r   r   r   r   r   	FetchLogsrt   rf   textPayloadrj   rx   r   s         r   rL   GCLLogTailer.Print   s    $++t+GF	0 28nn]] 28 2$  ||
/06!%$.. 17 1J 
 ""f>K 	''(:(:;d
  	r   )	r   r   r   r   rK   r   r   r   r   )r   r   r   r   rE   r   r)   r   classmethodrK   r   r   r   rL   r   r   r   r   r|   r|      sD    J ::"  #  $&P r   r|   c                       \ rS rSrSrSr\R                  4S jrS r	\
\R                  4S j5       rSS jrS rS	 rS
 rSrg)GCSLogTaileri  zBHelper class to tail a GCS logfile, printing content as available.z (possibly incomplete) c                     [        5       U l        U R                  X5      U l        [        R
                  " SU R                  -   5        SU l        X0l        SU l        g )NzGCS logfile url is r   F)	r-   	transport_StorageUrlr?   r   debugr@   rK   r   )r   bucketobjrK   s       r   r   GCSLogTailer.__init__	  sG    &(DN,DHII#dhh./DKHDIr   c                     [         R                  R                  R                  R	                  5       nSn[         R                  R
                  R                  R                  5       (       a  SnUR                  X1US9$ )Nz0https://storage.{universe_domain}/{bucket}/{obj}z5https://storage.mtls.{universe_domain}/{bucket}/{obj})universe_domainr   r   )	r   r_   corer   Getcontext_awareuse_client_certificaterb   r9   )r   r   r   r   url_patterns        r   r   GCSLogTailer._StorageUrl  sl     '',,<<@@BODK&&==EEGGKk'C  A Ar   c                 (   UR                   (       d
  [        5       eUR                   nSnUR                  U5      (       a  U[        U5      S nSU;  a  UnSnOUR	                  SS5      u  pVUS-  nSR                  UUR                  S9nU " UUUS9$ )	a,  Build a GCSLogTailer from a build resource.

Args:
  build: Build resource, The build whose logs shall be streamed.
  out: The output stream to write the logs to.

Raises:
  NoLogsBucketException: If the build does not specify a logsBucket.

Returns:
  GCSLogTailer, the tailer of this build's logs.
zgs://N/r^      z{object}log-{id}.txt)objectr   )r   r   rK   )
logsBucketr   
startswithlensplitr9   r   )r   rW   rK   log_stripped
gcs_prefix
log_bucketlog_object_dir
log_objects           r   r   GCSLogTailer.FromBuild  s     !## ##LJz**!#j/"23l
,jn%1%7%7Q%?"zn'..88 / J
  r   c                     U R                   R                  U R                  U R                  5      nUR                  S:X  a  [        R                  " S5        gUR                  S:X  a.  [        R                  " S5        U(       a  U R                  5         gUR                  S:X  d  UR                  S:X  a  [        R                  " SR                  UR                  [        UR                  5      S	95        U R                  S
:X  a  U R                  5         U =R                  [        UR                  5      -  sl        [        R                  " UR                  5      nUb  U R!                  U5      nU R#                  UR%                  S5      5        U(       a  U R                  5         gUR                  S:X  a9  [        R&                  " S5        U(       a  U R                  U R(                  5        gUR                  S:  ab  UR                  S:  aR  [        R&                  " SR                  UR                  5      5        U(       a  U R                  U R(                  5        g[+        UR,                  5      nUR                  US'   [        R.                  " XBR                  U R                  5      e! [        R
                   a    U(       a  e  gf = f)a)  Poll the GCS object and print any new bytes to the console.

Args:
  is_last: True if this is the final poll operation.

Raises:
  api_exceptions.HttpError: if there is trouble connecting to GCS.
  api_exceptions.CommunicationError: if there is trouble reaching the server
      and is_last=True.
Ni  z3Reading GCS logfile: 404 (no log yet; keep polling)i  z7Reading GCS logfile: 416 (no new content; keep polling)      z0Reading GCS logfile: {code} (read {count} bytes))codecountr   rw   i  z2Reading GCS logfile: 429 (server is throttling us)i  iX  z&Reading GCS logfile: got {0}, retryingr)   )r   rC   r?   r@   r=   r>   r)   r   r   rx   r9   r   r+   rt   r   Decoderf   rj   ri   warningLOG_OUTPUT_INCOMPLETEdictr*   	HttpError)r   is_lastresdecodedr*   s        r   PollGCSLogTailer.PollC  s   	NN""488T[[9c zzS	iiEF
zzS	iiIJ	
zzSCJJ#-	iiBIIzzSXX J 0 1		
kkS]"k)g		,,W5
-.	 zzS	kkFG	D667
zzSSZZ#-	kk:AA#**MN	D667 3;;G

GH

"
"7HHdhh
??q ,, 
 
s   0J6 6KKc                     U R                   (       d9  U R                  5         [        R                  " S5        U R                   (       d  M9  U R                  SS9  g)z;Tail the GCS object and print any new bytes to the console.r   Tr   N)r   r   r   r   r3   s    r   r   GCSLogTailer.Tail  s8    ii
iik
jjm iii 	IIdIr   c                     SU l         gr   )r   r3   s    r   r   GCSLogTailer.Stop  s	    DIr   c                 "    U R                  SS9  g)zPrint GCS logs to the console.Tr   N)r   r3   s    r   rL   GCSLogTailer.Print  s    IIdIr   )r@   rK   r   r   r?   N)F)r   r   r   r   rE   r   r   r)   r   r   r   rK   r   r   r   r   rL   r   r   r   r   r   r     sP    J3&)jj A  # % %NE@N
r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )ThreadInterceptori  z'Wrapper to intercept thread exceptions.c                 D   > [         [        U ]  5         Xl        S U l        g r%   )r   r   r   target	exception)r   r   r   s     r   r   ThreadInterceptor.__init__  s    	
T+-KDNr   c                      U R                  5         g ! [        R                   a4  nUR                  S:X  a  [	        5       U l         S nAg Xl         S nAg S nAf[        R                   a  nXl         S nAg S nAff = f)Ni  )r   r=   r   r:   r"   r   r>   )r   rB   s     r   runThreadInterceptor.run  s^    
kkm## 	
#	 NO,, nns&    A=AAA=-A88A=)r   r   )	r   r   r   r   rE   r   r   r   r   r    s   @r   r   r     s    /
 r   r   c                   \    \ rS rSrSr    S
S jrS rS r\R                  4S jr
S rS	rg)CloudBuildClienti  zGClient for interacting with the Cloud Build API (and Cloud Build logs).Nc                     U=(       d    [         R                  " 5       U l        U=(       d    [         R                  " 5       U l        X0l        X@l        g r%   )r   GetClientInstanceclientGetMessagesModulemessagessupport_gclpolling_interval)r   r   r   r   r   s        r   r   CloudBuildClient.__init__  s:    
 ?O==?DKC A A CDM",r   c                 B   UR                  5       S:X  aB  [        R                  R                  SUR                  [
        R                  UR                  S9nU R                  R                  R                  U R                  R                  UR                  5       S95      $ )zGet a Build message.

Args:
  build_ref: Build reference. Expects a cloudbuild.projects.locations.builds
    but also supports cloudbuild.projects.builds.

Returns:
  Build resource
zcloudbuild.projects.buildsz$cloudbuild.projects.locations.builds)
collection
projectsIdlocationsIdbuildsId)rT   )
Collectionr   REGISTRYCreater   r   DEFAULT_REGIONr   r   projects_locations_buildsr   r   +CloudbuildProjectsLocationsBuildsGetRequestRelativeName)r   	build_refs     r   GetBuildCloudBuildClient.GetBuild  s     !==$$++;((%44<<	 , !i ;;0044AA'') 	B 	+, ,r   c                 J   [         R                  R                  S[        U R                  5      -   S-   5        UR                  U;   aC  U R                  U5      n[        R                  " U R                  5        UR                  U;   a  MC  U(       a  UR                  5         U$ )ah  Checks whether a log tailer should be stopped.

Args:
  build: Build object, containing build status
  build_ref: Build reference, The build whose logs shall be streamed.
  log_tailer: Specific log tailer object
  working_statuses: Valid working statuses that define we should continue
    tailing

Returns:
  Build message, the completed or terminated build.
z1Waiting for build to complete. Polling interval: z second(s).)	r   r)   rL   strr   r   r   r   r   )r   rW   r   
log_tailerworking_statusess        r   ShouldStopTailer!CloudBuildClient.ShouldStopTailer  s     JJH../02?@ A
,,*
*mmI&e
jj&&' ,,*
* ooLr   c                    U R                  U5      nUR                  (       a  UR                  R                  U R                  R                  R
                  R                  U R                  R                  R
                  R                  U R                  R                  R
                  R                  4;  a  [        R                  X2S9nGOUR                  R                  U R                  R                  R
                  R                  U R                  R                  R
                  R                  4;   a^  U R                  (       aM  [        R                  " SR                  UR                  R                  5      5        [        R                  X2S9nO;[        R                  " SR                  UR                  R                  5      5        SnU R                  R                   R"                  nUR$                  UR&                  /nSnU(       a#  [)        UR*                  S9nUR-                  5         U R/                  X1UU5      nU(       a)  UR1                  5         UR2                  b  UR2                  eU$ )a  Streams the logs for a build if available.

Regardless of whether logs are available for streaming, awaits build
completion before returning.

Args:
  build_ref: Build reference, The build whose logs shall be streamed.
  out: The output stream to write the logs to.

Raises:
  NoLogsBucketException: If the build is expected to specify a logsBucket
  but does not.

Returns:
  Build message, the completed or terminated build.
)rK   z7Streaming logs from GCL: requested logging mode is {0}.z2Not streaming logs: requested logging mode is {0}.N)r   )r   rQ   loggingr   BuildOptionsLoggingValueValuesEnumNONESTACKDRIVER_ONLYCLOUD_LOGGING_ONLYr   r   r   r   infor9   r|   BuildStatusValueValuesEnumQUEUEDWORKINGr   r   startr  joinr   )r   r   rK   rW   r  statusesr  ts           r   StreamCloudBuildClient.Stream  s   " MM)$E==EMM11""99>>""99JJ""99LL: 
  ))%)9j			""99JJ""99LL# 
 

	hhHOO
--

! "))%)9j	hhCJJ
--

! "j}}""88H
 	A
:??
3aggi!!%J"24Effh	
	 kkLr   c                    U R                  U5      nUR                  (       a  UR                  R                  U R                  R                  R
                  R                  U R                  R                  R
                  R                  U R                  R                  R
                  R                  4;  a  [        R                  U5      nOUR                  R                  U R                  R                  R
                  R                  U R                  R                  R
                  R                  4;   aO  [        R                  " SR                  UR                  R                  5      5        [        R                  U5      nO;[        R                  " SR                  UR                  R                  5      5        SnU(       a  UR                  5         gg)zPrint the logs for a build.

Args:
  build_ref: Build reference, The build whose logs shall be streamed.

Raises:
  NoLogsBucketException: If the build does not specify a logsBucket.
z6Printing logs from GCL: requested logging mode is {0}.z.Logs not available: build logging mode is {0}.N)r   rQ   r	  r   r
  r  r  r  r  r   r   r   r  r9   r|   rL   )r   r   rW   r  s       r   PrintLogCloudBuildClient.PrintLog)  sN    MM)$E==EMM11""99>>""99JJ""99LL: 
  ))%0j			""99JJ""99LL# 
 
hhGNN
--

! "))%0j	hh?FF
--

! "j r   )r   r   r   r   )NNFr   )r   r   r   r   rE   r   r   r  r   rK   r  r  r   r   r   r   r   r     s6    O   !	-,.0 #&'' 5nr   r   )(rE   collectionsrc   	threadingr   apitools.base.pyr   r=   !googlecloudsdk.api_lib.cloudbuildr   rI   r   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler	   googlecloudsdk.core.credentialsr
   r0   googlecloudsdk.core.utilr   rM   r&   Errorr   r"   
namedtupler(   r   r-   rO   rX   rZ   r|   r   Threadr   r   r   r   r   <module>r(     s    6  	   9 = 1 * # * ) 7 F -  : 65J,, 5Q:;K;K Q !!*.KLK K
H H:w: wtX: Xv	(( .Qv Qr   