
    |                     r   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	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SKJr  SSKJr  Sr SSKr\b
  \" \SS5      rSr\R4                  \R6                  \R8                  \4rSrS r " S S\ 5      r!\!" 5       r"SS jr#S r$g! \ a     N[f = f)z+Caching logic for checking if we're on GCE.    )absolute_import)division)unicode_literalsN)config)
properties)gce_read)files)retry)http_client)urllib_errorCertificateErroriX  zName or service not knownc                     A AA[        U[        5      (       d  g[        U[        R                  5      (       a  [        [
        R                  " U5      ;   a  gg)z;Decides if we need to retry the metadata server connection.FT)
isinstance(_POSSIBLE_ERRORS_GCE_METADATA_CONNECTIONr   URLError_DOMAIN_NAME_RESOLVE_ERROR_MSGsix	text_type)exc_type	exc_valueexc_tracebackstates       0lib/googlecloudsdk/core/credentials/gce_cache.py$_ShouldRetryMetadataServerConnectionr   8   sF     u	IG	H	HL1122$i(@@	    c                   ~    \ rS rSrSrSS jrSS jrS rS rS r	S	 r
S
 rS r\R                  " S\S9S 5       rSrg)_OnGCECacheE   a  Logic to check if we're on GCE and cache the result to file or memory.

Checking if we are on GCE is done by issuing an HTTP request to a GCE server.
Since HTTP requests are slow, we cache this information. Because every run
of gcloud is a separate command, the cache is stored in a file in the user's
gcloud config dir. Because within a gcloud run we might check if we're on GCE
multiple times, we also cache this information in memory.
A user can move the gcloud instance to and from a GCE VM, and the GCE server
can sometimes not respond. Therefore the cache has an age and gets refreshed
if more than _GCE_CACHE_MAX_AGE passed since it was updated.
Nc                 P    Xl         X l        [        R                  " 5       U l        g N)	connectedexpiration_time	threadingLock	file_lock)selfr!   r"   s      r   __init___OnGCECache.__init__R   s    N*^^%DNr   c                     U R                  US9nUb  U$ U R                  " U R                  5       6   U R                  US9nUb  U$ U R                  5       $ )a*  Check if we are on a GCE machine.

Checks, in order:
* in-memory cache
* on-disk cache
* metadata server

If we read from one of these sources, update all of the caches above it in
the list.

If check_age is True, then update all caches if the information we have is
older than _GCE_CACHE_MAX_AGE. In most cases, age should be respected. It
was added for reporting metrics.

Args:
  check_age: bool, determines if the cache should be refreshed if more than
    _GCE_CACHE_MAX_AGE time passed since last update.

Returns:
  bool, if we are on GCE or not.
	check_age)_CheckMemory_WriteMemory
_CheckDiskCheckServerRefreshAllCaches)r&   r+   on_gces      r   GetOnGCE_OnGCECache.GetOnGCEW   sd    , 3Fmt()3Fm++--r   c                     U R                  5       nU R                  U5        U R                  U[        R                  " 5       [        -   5        U$ r    )_CheckServerWithRetry
_WriteDiskr-   time_GCE_CACHE_MAX_AGE)r&   r0   s     r   r/   '_OnGCECache.CheckServerRefreshAllCachesx   s=    '')FOOFfdiik,>>?Mr   c                     U(       d  U R                   $ U R                  (       a/  U R                  [        R                  " 5       :  a  U R                   $ g r    )r!   r"   r6   )r&   r+   s     r   r,   _OnGCECache._CheckMemory~   s8    ^^ 4 4		 C^^r   c                     Xl         X l        g r    )r!   r"   )r&   r0   r"   s      r   r-   _OnGCECache._WriteMemory   s    N*r   c                    [         R                  " 5       R                  5       nU R                      [        R
                  " U5      R                  nU[        -   n[        R                  " U5      nU[        R                  " S5      :H  U4sSSS5        $ ! [        [        [        R                  4 a     SSS5        gf = f! , (       d  f       g= f)zReads cache from disk.TNNN)r   PathsGCECachePathr%   osstatst_mtimer7   r	   ReadFileContentsr   r   OSErrorIOErrorError)r&   gce_cache_pathmtimer"   gcecache_file_values        r   r.   _OnGCECache._CheckDisk   s    \\^002N	'00"44#44^D"cmmD&99?J 
 w,   
 
s)   CABB?4C>B??C
Cc                 6   [         R                  " 5       R                  5       nU R                      [        R
                  " U[        R                  " U5      SS9  SSS5        g! [        [        [        R                  4 a     N*f = f! , (       d  f       g= f)zUpdates cache on disk.T)privateN)r   r?   r@   r%   r	   WriteFileContentsr   r   rE   rF   rG   )r&   r0   rH   s      r   r5   _OnGCECache._WriteDisk   sq    \\^002N		CMM&14	A 
 w,  		 
s(   B
*A%%BB
BB


Bc                 D     U R                  5       $ ! [         a     gf = f)NF)_CheckServerr   r&   s    r   r4   !_OnGCECache._CheckServerWithRetry   s(      3 s    
   )max_retrialsshould_retry_ifc                     [         R                  " [         R                  [        R                  R
                  R                  R                  5       5      R                  5       $ r    )	r   ReadNoProxy'GOOGLE_GCE_METADATA_NUMERIC_PROJECT_URIr   VALUEScomputegce_metadata_check_timeout_secGetIntisdigitrR   s    r   rQ   _OnGCECache._CheckServer   sG     88!!@@GGI gir   )r!   r"   r%   r>   T)__name__
__module____qualname____firstlineno____doc__r'   r1   r/   r,   r-   r.   r5   r4   r
   RetryOnExceptionr   rQ   __static_attributes__ r   r   r   r   E   sW    
&
.B+" 	&JLLr   r   c                 ,    [         R                  U 5      $ )zAHelper function to abstract the caching logic of if we're on GCE.)_SINGLETON_ON_GCE_CACHEr1   r*   s    r   r1   r1      s    	 	)	))	44r   c                  *    [         R                  5       $ )z@Force rechecking server status and refreshing of all the caches.)rj   r/   rh   r   r   ForceCacheRefreshrl      s    	 	<	<	>>r   r`   )%re   
__future__r   r   r   rA   socketr#   r6   googlecloudsdk.corer   r   googlecloudsdk.core.credentialsr   googlecloudsdk.core.utilr	   r
   r   	six.movesr   r   SslCertificateErrorsslImportErrorgetattrr7   r   errorHTTPExceptionr   r   r   objectr   rj   r1   rl   rh   r   r   <module>rz      s    2 &  ' 	    & * 4 * * 
 ! "  ?%7> 
 -9,A,A6<<,7,E,E,?,A ( "= 
p& ph &- 5
?u  s   B- -B65B6