
    &                     4   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSK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	rS
r\	R*                  R-                  SS5      rS r\R2                  \R4                   " S S\R6                  5      5       5       rg)z!The `gcloud meta daemon` command.    )absolute_import)division)unicode_literalsN)gcloud_main)base)filesz0.0.0.0i  GCLOUD_DAEMON_PRECOMPUTE_DATACLIc           
      D   [         R                   " 5       n[        R                  " 5       n[        R                  " SUU5        U c  [        R
                  " SU5        SS0$  U R                  U5      nUR                  UR                  UR                  R                  5        VVs0 s H&  u  pVUR                  S5      UR                  S5      _M(     snn[        UR                  [        5      (       a  UR                  R                  S5      OUR                  S.n[        R                  " SU5        U[         R                   " 5       nX-
  n	[        R                  " S	UU	5        $ s  snnf ! [         a  n
[        R                   " S
X:5        [        U
R"                  [$        5      (       a  U
R"                  OSnSU
-  US.s Sn
A
[         R                   " 5       nX-
  n	[        R                  " S	UU	5        $ Sn
A
f[&         ab  n[        R
                  " SU5        SS[)        U5      -  0s SnA[         R                   " 5       nX-
  n	[        R                  " S	UU	5        $ SnAff = f! [         R                   " 5       nX-
  n	[        R                  " S	UU	5        f = f)z=Executes the gcloud command using the precomputed CLI object.z<PID: %s - Starting perform_gcloud_execution with input: '%s'Nz)PID: %s - Precomputed CLI object is None!errorz/Internal Server Error: CLI object not availableutf-8)urimethodheadersbodyzPID: %s - Execution successful.z<PID: %s - Completed perform_gcloud_execution in %.4f secondsz+PID: %s - SystemExit processing request: %s   z"Command failed with SystemExit: %s)r   	exit_codez-PID: %s - Exception during command execution:z&Exception during command execution: %s)timeosgetpidlogginginfor   Executer   r   r   itemsdecode
isinstancer   bytes
SystemExit	exceptioncodeint	Exceptionstr)cli_objcommand_list
start_timepidrequestkvresponse_dataend_timecomputation_timeexcr   es                lib/surface/meta/daemon.pyperform_gcloud_executionr1   )   s0   yy{*
		#	,,D	 _MM=sCFGG"ool+G{{..  --/
/ HHWqxx00/
 ',,.. LL(M LL2C8 yy{H,LLF3
 
 CSN&sxx551I5;  yy{H,LLF 
 HMMA3G=AFGGyy{H,LLFH yy{H,LLFs]   "AE. &-E(
A#E. (E. .
I)8AG:I)I, :I)&I$-I).I, $I))I, ,3Jc                   (    \ rS rSrSrSSS.rS rSrg)	Daemon\   zE(DEBUG MODE) Precomputes gcloud CLI and runs a single-request server.a  
            (DEBUG MODE) Initializes the gcloud CLI environment based on PRECOMPUTE_DATA,
            starts an HTTP server in the FOREGROUND, serves exactly one POST
            request to the root path ('/'), executes the requested gcloud command
            using the precomputed environment, returns the result, and then exits.

            The command will BLOCK until the single request is received and processed.
        a  
            To run the foreground daemon precomputing the basic CLI:

            $ gcloud meta daemon

            (The command will now wait here)

            Open another terminal and send a request (e.g., using curl):

            $ curl -X POST -H "Content-Type: application/json" -d '{"command_list": ["projects", "list", "--limit=1", "--format=json"]}' http://localhost:8080/

            (The original 'gcloud meta daemon' command will process this, print logs, and then exit)
        )DESCRIPTIONEXAMPLESc                   ^
 [         R                  " [         R                  SS9  [         R                  " S5        [        R                  " 5       n S[
        R                  S'   S[
        R                  S'   S[
        R                  S'   S[
        R                  S'   S[
        R                  S	'   S[
        R                  S
'   S[
        R                  S'   S[
        R                  S'   S[
        R                  S'   S[
        R                  S'   [        R                  " / 5      m
[         R                  " S5        [         R                  " S5         / SQnT
R                  U5        [        R                  " 5       n[         R                  " SXB-
  5         " U
4S jS[        R                  R                   5      n[         R                  " S5         S["        R$                  l        ["        R$                  " [(        [*        4U5       nSn[,        R.                  " U5       n	U	R1                  S5        S S S 5        [         R                  " SU5        [         R                  " S [(        [*        [2        5        UR5                  5         [         R                  " S!5        UR7                  5         S S S 5        [         R                  " S$5        [         R8                  " 5         [         R                  " S%5        [        R                  " S&5        g ! [         a     GNf = f! [         a:  n[         R                  " S5        [        R                  " SU-  5         S nAGNS nAff = f! , (       d  f       GN9= f! , (       d  f       N= f! [         a:  n[         R                  " S"5        [        R                  " S#U-  5         S nAGNS nAff = f! [         R                  " S$5        [         R8                  " 5         f = f)'Nz<%(asctime)s - PID: %(process)d - %(levelname)s - %(message)s)levelformatzStarting precomputation...1CLOUDSDK_CORE_DRY_RUN!CLOUDSDK_AUTH_DISABLE_CREDENTIALSCLOUDSDK_CORE_DISABLE_PROMPTS%CLOUDSDK_CORE_DISABLE_USAGE_REPORTING/CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK"CLOUDSDK_CORE_DISABLE_FILE_LOGGINGalways"CLOUDSDK_CORE_SHOW_STRUCTURED_LOGSr   CLOUDSDK_CORE_VERBOSITYgaasCLOUDSDK_METRICS_ENVIRONMENT0!CLOUDSDK_CORE_USER_OUTPUT_ENABLEDzPrecomputing basic CLI...zLoading frequent commands)compute	instanceslistz--project=fake-projectz(Precomputation complete in %.4f seconds.z!Failed during CLI precomputation:z#Error during CLI precomputation: %sc                   ,   > \ rS rSrSrU 4S jrS rSrg)(Daemon.Run.<locals>.SingleRequestHandler   z<Handles ONE incoming POST request and then signals shutdown.c                 j  > [         R                  " 5       n[        R                  " SXR                  5        U R                  S:X  Ga`  [        U R                  S   5      nU R                  R                  U5      n [        R                  " UR                  S5      5      nUR                  S5      nU(       a  [        U[        5      (       d*  [        R                  " SU5        U R!                  SS5        g	 [        R                  " SUU5        [%        TU5      nSnU R'                  U5        U R)                  SS5        U R+                  5         U R,                  R/                  [        R0                  " U5      R3                  S5      5        [        R                  " SU5        O2[        R4                  " SXR                  5        U R!                  S5        [        R                  " SU5        g	! [        R"                   a+    [        R                  " S
USS9  U R!                  SS5         g	f = f)z Handles the single POST request.z&PID: %s - Request received at path: %s/zContent-Lengthr   r%   zGPID: %s - Missing or invalid 'command_list' (must be a list) in requesti  z=Missing or invalid 'command_list' (must be a list) in requestNzPID: %s - Invalid JSON requestT)exc_infozInvalid JSON requestz3PID: %s - Received compute request with command: %s   zContent-typezapplication/jsonz"PID: %s - Response sent to client.z/PID: %s - Received request for unknown path: %si  z5PID: %s - Request handled. Signaling server shutdown.)r   r   r   r   pathr!   r   rfilereadjsonloadsr   getr   rJ   r   
send_errorJSONDecodeErrorr1   send_responsesend_headerend_headerswfilewritedumpsencodewarning)	selfr'   content_length	post_datarequest_datar%   r+   status_codeprecomputed_clis	           r0   do_POST0Daemon.Run.<locals>.SingleRequestHandler.do_POST   s   iik=sIIN99t||,<=>.jjoon5)::i&6&6w&?@L'++N;Lz,'E'Emm 
 oo
  (F" ,,C 3|-
 +


[
)


>+=
>




**

4::m4;;GD
E
,,;S
A //?ii //#
 	CS	
? %% MM:C$OOOC!78s   =A;G3 3<H21H2c                 ^    [         R                  " S[        R                  " 5       X-  5        g)z'Log messages using the standard logger.zPID: %s - HTTP Server: %sN)r   r   r   r   )rb   r9   argss      r0   log_message4Daemon.Run.<locals>.SingleRequestHandler.log_message   s     	0"))+v}M     N)__name__
__module____qualname____firstlineno____doc__rh   rl   __static_attributes__)rg   s   r0   SingleRequestHandlerrL      s    H8
vNrn   rv   z"Starting foreground HTTP server...Tz/tmp/gcloud_daemon.readyreadyz
Created %sz3Server started on http://%s:%s (PRECOMPUTE_DATA=%s)z&Request handled. Server shutting down.z/Unhandled exception occurred in server process:zServer error: %sz Server process function exiting.z7Daemon command finished normally after serving request.r   )r   basicConfigINFOr   r   r   environr   	CreateCLIr   r"   r   sysexithttpserverBaseHTTPRequestHandlersocketserver	TCPServerallow_reuse_addressHOSTPORTr   
FileWriterr^   PRECOMPUTE_DATAhandle_requestserver_closeshutdown)rb   rk   precomputation_start_timecompute_instances_list_commandprecomputation_end_timer/   rv   httpdgcloud_daemon_ready_filefrg   s             @r0   Run
Daemon.Runy   s    llM LL-. $		#:,/bjj()8;bjj4547bjj01<?bjj89FIbjjBC9<bjj569Abjj56.5bjj*+39bjj/08;bjj45#--b1oll./ll./	*
& 	 >? !%		ll
4
!
=BNt{{AA BNJ LL5637l0!!4,0DE#= 671
'''
 8\#;<A		
 	=># F0 ll56LLJKHHQKc    :;<	hh4q899:` 87 FE&  'IJ	hh!A%&&'
 ll56s   D L L '.L 6N M7!M%3A:M7-N 
LL LL 
M"(/MM"%
M4	/M77
NN O N 
O/OO OO -O<ro   N)rp   rq   rr   rs   rt   detailed_helpr   ru   ro   rn   r0   r3   r3   \   s     N-0Urn   r3   )rt   
__future__r   r   r   http.serverr~   rU   r   r   r   r|   r   googlecloudsdkr   googlecloudsdk.callioper   googlecloudsdk.core.utilr   r   r   rz   rW   r   r1   HiddenDefaultUniverseOnlyCommandr3   ro   rn   r0   <module>r      s    ( &  '    	  
  & ( * **..#U
0f pT\\ p  prn   