
                             d Z ddlmZ ddlmZ ddlm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  G d
 dej                         Z G d de      Zy)zDManage and stream logs in-progress or completed PipelineRun/TaskRun.    )absolute_import)division)unicode_literalsN)logs)client_util)common)logc                   p    e Zd ZdZdZdZej                  fdZe	ej                  fd       Zd Zd Zd Zy	)
GCLLogTailerzBHelper class to tail logs from GCL, printing content as available.
cloudbuild_AllLogsc                 f   t        j                         | _        || _        || _        || _        dj                  | j                  | j                        | _        dj                  | j                  | j
                  | j                  | j                        | _
        || _        || _        d| _        y )NzDprojects/{project_id}/locations/global/buckets/_Default/views/{view})
project_idviewzHprojects/{project_id}/locations/{location}/buckets/{bucket}/views/{view})r   locationbucketr      )v1_logs_utilGetGCLLogTailertailer
log_filterprojectr   formatALL_LOGS_VIEWdefault_log_viewCLOUDBUILD_BUCKETworkerpool_log_viewhas_tipp_pooloutbuffer_window_seconds)selfr   r   r   r   r   s         0lib/googlecloudsdk/api_lib/cloudbuild/v2/logs.py__init__zGCLLogTailer.__init__"   s     ..0DK DODLDMNf4+=+=f> 	  jpp<<%%	  q  !D
 'DDH!"D    c                      | |||||      S )z'Build a GCLLogTailer from a log filter.)r   r   r   r   r    )clsr   r   r   r   r   s         r"   
FromFilterzGCLLogTailer.FromFilter6   s     
 # r$   c                 z   | j                   sy| j                  r| j                  g}n| j                  g}| j                   j	                  || j
                  | j                        }| j                  d       |D ].  }| j                  |j                        }| j                  |       0 | j                  d       y)z9Tail the GCL logs and print any new bytes to the console.N)r     REMOTE RUN OUTPUT z& RUN FINISHED; TRUNCATING OUTPUT LOGS )r   r   r   r   TailLogsr   r    _PrintFirstLine_ValidateScreenReadertext_payload_PrintLogLine_PrintLastLiner!   resource_namesoutput_logsoutputtexts        r"   TailzGCLLogTailer.TailC   s     ;;001n--.n++&&"88 ' K 	./''(;(;<d
  	@A
r$   c                     t        j                  | j                         | j                  r| j                  j	                          yy)zStop log tailing.N)timesleepr    r   Stop)r!   s    r"   r:   zGCLLogTailer.Stop^   s2     	JJt))*{{
kk r$   c                 >   | j                   r| j                  g}n| j                  g}t        j                  | j
                  d|      }| j                  d       |D ].  }| j                  |j                        }| j                  |       0 | j                          y)zPrint GCL logs to the console.asc)r   order_byr2   r*   N)r   r   r   r   	FetchLogsr   r,   r-   textPayloadr/   r0   r1   s        r"   PrintzGCLLogTailer.Printe   s    001n--.n""??%K 	./''(:(:;d
  	r$   N)__name__
__module____qualname____doc__r   r   r	   statusr#   classmethodr   r(   r6   r:   r@   r&   r$   r"   r   r      sI    J"- ?Bjj#( =@WW
 
6r$   r   c                   T    e Zd ZdZd
dZd Zd Zd Zd Ze	j                  fdZd Zy	)CloudBuildLogClientzGClient for interacting with the Cloud Build API (and Cloud Build logs).c                 D    t        j                         | _        || _        y N)v2_client_utilGetClientInstance	v2_client
sleep_time)r!   rN   s     r"   r#   zCloudBuildLogClient.__init__~   s    #557DN DOr$   c                 T    |r| j                  ||||      S | j                  |||      S rJ   )_GetWorkerPoolLogFilter_GetNonWorkerPoolLogFilter)r!   regionrun_idrun_typer   create_times         r"   _GetLogFilterz!CloudBuildLogClient._GetLogFilter   s1    ))+vxPP,,[&&IIr$   c                 *    dj                  |||      S )NzTtimestamp>="{timestamp}" AND labels.location="{region}" AND labels.run_name={run_id})	timestamprR   rS   r   )r!   rU   rS   rR   s       r"   rQ   z.CloudBuildLogClient._GetNonWorkerPoolLogFilter   s    	$f{6&fABr$   c                 >    |dk(  rdnd}dj                  ||||      S )NtaskruntaskRunpipelineRunz(labels."k8s-pod/tekton.dev/{run_label}"="{run_id}" OR labels."k8s-pod/tekton_dev/{run_label}"="{run_id}") AND timestamp>="{timestamp}" AND resource.labels.location="{region}")	run_labelrS   rX   rR   rY   )r!   rU   rS   rT   rR   r^   s         r"   rP   z+CloudBuildLogClient._GetWorkerPoolLogFilter   s5    %2	I	K fFk&  	r$   c                     |j                   :t        j                  ||||      }t        j                  d       |j                   :|r/t        j                  | j
                         |j                          |S )z.Checks whether a log tailer should be stopped.   )completionTimerK   GetRunr8   r9   rN   r:   )r!   
log_tailerrunr   rR   rS   rT   s          r"   ShouldStopTailerz$CloudBuildLogClient.ShouldStopTailer   se     


$!!'668Dc
jjm 


$  jj!ooJr$   c                    t        j                  ||||      }t        |j                        xr d|j                  v}| j	                  |||||j
                        }t        j                  |||||      }	d}
|	r0t        j                  |	j                        }
|
j                          | j                  |	|||||      }|
r(|
j                          |
j                  |
j                  |S )z(Streams the logs for a run if available.workerPoolSecondGen)r   N)target)rK   rb   bool
workerPoolrV   
createTimer   r(   r   ThreadInterceptorr6   startre   join	exception)r!   r   rR   rS   rT   r   rd   r   r   rc   ts              r"   StreamzCloudBuildLogClient.Stream   s    



BC 	S^^L!6cnn!L  ##-J ((] ) J 	A

(
(

@aggi



C&& (*Cffh	
	 kkJr$   c                    t        j                  ||||      }t        |j                        xr d|j                  v}| j	                  |||||j
                        }t        j                  ||||      }|r|j                          yy)zPrint the logs for a run.rg   N)	rK   rb   ri   rj   rV   rk   r   r(   r@   )	r!   r   rR   rS   rT   rd   r   r   rc   s	            r"   PrintLogzCloudBuildLogClient.PrintLog   s     


BCS^^L!6cnn!L  ##-J ((]J  r$   N)<   )rA   rB   rC   rD   r#   rV   rQ   rP   re   r	   r   rq   rs   r&   r$   r"   rH   rH   {   s3    O!JB ;>'' 6r$   rH   )rD   
__future__r   r   r   r8   !googlecloudsdk.api_lib.cloudbuildr   r   $googlecloudsdk.api_lib.cloudbuild.v2r   rK   googlecloudsdk.api_lib.loggingr   googlecloudsdk.corer	   
TailerBaser   objectrH   r&   r$   r"   <module>r|      sF    K &  '  B N 1 #[<** [|[& [r$   