
    $                     V   S 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	r\R                   " S
S9rSR%                  \R&                  5      r " S S\R*                  5      rS rS rS rS rS rS rS r " S S\5      r " S S\R@                  5      r!g)z)Utilities shared by cloud-shell commands.    N)apis)waiter)ssh)
exceptions)log)storezusers/me/environments/default   )minutesz{}sc                       \ rS rSrSrSrg)UnsupportedPlatform$   zCRaised when attempting to run a command on an unsupported platform. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       2lib/googlecloudsdk/command_lib/cloud_shell/util.pyr   r   $   s    Kr   r   c                 H    U R                  SSSS9  U R                  SSSS9  g)z4Parses arguments common to all cloud-shell commands.z--force-key-file-overwriteav        If enabled gcloud will regenerate and overwrite the files associated
      with a broken SSH key without asking for confirmation in both
      interactive and non-interactive environment.

      If disabled gcloud will not attempt to regenerate the files associated
      with a broken SSH key and fail in both interactive and non-interactive
      environment.
      
store_true)helpactionz--ssh-key-filezf      The path to the SSH key file. By default, this is
        *~/.ssh/google_compute_engine*.
      N)add_argumentparsers    r   ParseCommonArgsr   (   sF     	"
    	
   r   c                 D    U R                  S[        R                  SSS9  g )Nssh_argszW          Flags and positionals passed to the underlying ssh implementation.
          z"        $ {command} -- -vvv
      )nargsr   example)r   argparse	REMAINDERr   s    r   AddSshArgFlagr%   @   s+    
  r   c           	         [         R                  " SS5      n[         R                  " SS5      n[         R                  " SS5      n[        R                  R                  5       nUR                  5         [        R                  R                  U R                  S9nUR                  U R                  S9  UR                  R                  UR                  [        S95      nUR!                  5       R#                  5       nSnUR$                   H  n	Xy:X  d  M
  Sn  O   U(       d  UR                  R'                  UR)                  [        UR+                  US9S	95      n
[,        R.                  " [1        UR                  UR2                  5      U
S
SSS9n[4        R6                  " S5        [8        R:                  " S5        SnUR<                  UR                  R>                  R@                  :w  a  [4        R6                  " S5        SnU RC                  S5      (       a)  U RD                  (       a  [F        RH                  " [J        S9nUR                  RM                  URO                  [        URQ                  US9S95      n[,        R.                  " [1        UR                  UR2                  5      USSSS9nURR                  (       d  [U        U5        [W        UURX                  URR                  URZ                  UR\                  S9$ )zGEnsures that the user's environment is ready to accept SSH connections.
cloudshellv1)filename)	overwritenameFT)key)environmentaddPublicKeyRequestz&Pushing your public key to Cloud Shelli  N)sleep_msmax_wait_msz+Waiting for your public key to propagate...   z$Starting your Cloud Shell machine...authorize_sessionmin_expiry_durationaccessToken)r,   startEnvironmentRequestz-Waiting for your Cloud Shell machine to startssh_envuserhostportr-   )/r   GetClientInstanceGetMessagesModuler   EnvironmentCurrent
RequireSSHKeysFromFilenamessh_key_fileEnsureKeysExistforce_key_file_overwriteusers_environmentsGet%CloudshellUsersEnvironmentsGetRequestDEFAULT_ENVIRONMENT_NAMEGetPublicKeyToEntry
publicKeysAddPublicKey.CloudshellUsersEnvironmentsAddPublicKeyRequestAddPublicKeyRequestr   WaitForEnvironmentPoller
operationsr   PrinttimesleepstateStateValueValuesEnumRUNNINGIsKnownAndSpecifiedr3   r   GetFreshAccessTokenIfEnabledMIN_CREDS_EXPIRY_SECONDSStart'CloudshellUsersEnvironmentsStartRequestStartEnvironmentRequestsshHost'_RaiseExceptionDueToStartOperationErrorConnectionInfosshUsernamesshPortkey_file)argsclientmessagesoperations_clientr:   keysr.   r-   has_key	candidateadd_public_key_operationstart_operationaccess_tokens                r   PrepareEnvironmentrq   L   s    !!,5&##L$7(,,\4@ OO##%'				(9(9		:$!>!>? ))--44' 5 )*+
 	##%#'))i
g * 
%88EE??0 ( < < < E 	@ 	
  ..&33+66	8 0K II;<JJqM /(..CCKKKII45L 3449O9O776l //5588)$,$D$D( %E % 	9 	
O ..%%'8'C'C	
 	7K 
		+O<	""
--
 r   c                     U c  g U  HH  nUR                    H5  nUR                  S:X  d  M  UR                  R                  U:X  d  M1  Us  s  $    MJ     g )Nz@type)additionalPropertiesr-   valuestring_value)details
type_classdetailadditional_propertys       r   
_GetDetailrz      sQ    _f%::

!
!W
,!''44
B  ;  
r   c                 n    U c  g U R                    H!  nUR                  U:X  d  M  UR                  s  $    g N)rs   r-   rt   )rx   r-   ry   s      r    _GetAdditionalPropertyFromDetailr}      s:    ^#88#% &&& 9 
r   c                    SnU (       a'  U R                   (       a  U R                   R                  n[        US5      n[        US5      nU(       a  UR                  OSnUS:w  a  [
        R                  " S5      eSn[        US5      nU(       a  [        US5      nU(       al  UR                  R                   HR  nU(       a    OJUR                  R                   H,  n	U	R                  S:X  d  M  U	R                  R                  n  MP     MT     U(       a  [
        R                  " S	U S
35      e[
        R                  " S5      e)a=  Raises a core_exceptions.Error based on the start operation's error details.

If the error is due to an unverified account, a detailed error including the
verification URL (if it exists) will be raised. Otherwise, a generic error is
raised.

Args:
  operation: The operation object returned from the Cloud Shell API.
Nz(type.googleapis.com/google.rpc.ErrorInforeasonACCOUNT_UNVERIFIEDz&The Cloud Shell machine did not start.z#type.googleapis.com/google.rpc.Helplinksurlz:Your account is unverified. Please verify your account at z& or at https://shell.cloud.google.com.zYYour account is unverified. Please verify your account at https://shell.cloud.google.com.)errorrv   rz   r}   ru   core_exceptionsErrorarray_valueentriesobject_value
propertiesr-   rt   )
	operationrv   
error_inforeason_propertyerror_reason
verify_urlhelp_messager   r   links
             r   rb   rb      s    '9??oo%%G'#MN*4ZJ/1@--d,))


 H
II*G%JK, ,\7CE&&..'
((33DXX00J 4 / 


	<=	? 
 

	+ r   c            	         [         R                  " SS5      n [         R                  " SS5      n[        R                  " [
        S9nU(       a;  U R                  R                  UR                  [        UR                  US9S95        gg)zFPushes gcloud command-line tool credentials to the user's environment.r'   r(   r4   r6   )r,   authorizeEnvironmentRequestN)r   r>   r?   r   r\   r]   rH   	Authorize+CloudshellUsersEnvironmentsAuthorizeRequestrK   AuthorizeEnvironmentRequest)rh   ri   rp   s      r   AuthorizeEnvironmentr      s     !!,5&##L$7( 3324,
''<<)(0(L(L( )M )* 	= 	+, r   c                       \ rS rSrS rSrg)rc      c                 @    Xl         X l        X0l        X@l        XPl        g r|   r9   )selfr:   r;   r<   r=   r-   s         r   __init__ConnectionInfo.__init__   s    LIIIHr   )r<   r-   r=   r:   r;   N)r   r   r   r   r   r   r   r   r   rc   rc      s    r   rc   c                   0    \ rS rSrSrS rS rS rS rSr	g)	rS   i  z"Poller for environment operations.c                     Xl         X l        g r|   environments_serviceoperations_service)r   r   r   s      r   r   EnvironmentPoller.__init__	  s     40r   c                     UR                   $ r|   )done)r   r   s     r   IsDoneEnvironmentPoller.IsDone  s    >>r   c                     U R                   R                  S5      nU R                   R                  U" UR                  S95      $ NrI   r+   )r   GetRequestTyperI   r,   r   r   request_types      r   PollEnvironmentPoller.Poll  s8    **99%@L""&&|'HIIr   c                 ~    U R                   R                  S5      nU R                   R                  U" [        S95      $ r   )r   r   rI   rK   r   s      r   	GetResultEnvironmentPoller.GetResult  s:    ,,;;EBL$$((235 5r   r   N)
r   r   r   r   r   r   r   r   r   r   r   r   r   rS   rS     s    *1J5r   rS   )"r   r#   datetimerV   googlecloudsdk.api_lib.utilr   r   #googlecloudsdk.command_lib.util.sshr   googlecloudsdk.corer   r   r   googlecloudsdk.core.credentialsr   rK   	timedeltaMIN_CREDS_EXPIRYformatsecondsr]   r   r   r   r%   rq   rz   r}   rb   r   objectrc   OperationPollerrS   r   r   r   <module>r      s    0    , . 3 = # 1 ; %%b1  <<(8(@(@A L/// L0	Tn-`,"V 5.. 5r   