
    "                     n   d Z ddlmZ ddlmZ ddlmZ ddlZddl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 dd
lmZ ddlmZ dZ ej(                  d      Zdj-                  ej.                        Z G d dej2                        Zd Zd Zd Zd Z G d de      Z  G d de
jB                        Z"y)z)Utilities shared by cloud-shell commands.    )absolute_import)division)unicode_literalsN)apis)waiter)ssh)
exceptions)log)storezusers/me/environments/default   )minutesz{}sc                       e Zd ZdZy)UnsupportedPlatformzCRaised when attempting to run a command on an unsupported platform.N)__name__
__module____qualname____doc__     2lib/googlecloudsdk/command_lib/cloud_shell/util.pyr   r   '   s    Kr   r   c                 T    | j                  ddd       | j                  ddd       y)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                 J    | j                  dt        j                  dd       y )Nssh_argszW          Flags and positionals passed to the underlying ssh implementation.
          z"        $ {command} -- -vvv
      )nargsr   example)r   argparse	REMAINDERr   s    r   AddSshArgFlagr%   C   s+    
  r   c                     t        j                  dd      }t        j                  dd      }t        j                  dd      }t        j                  j                         }|j                          t        j                  j                  | j                        }|j                  | j                         |j                  j                  |j                  t                    }|j!                         j#                         }d}|j$                  D ]  }	||	k(  s	d} n |s|j                  j'                  |j)                  t        |j+                  |      	            }
t-        j.                  t1        |j                  |j2                        |
d
dd      }t5        j6                  d       t9        j:                  d       |j<                  |j                  j>                  j@                  k7  rt5        j6                  d       d}| jC                  d      r&| jD                  rtG        jH                  tJ              }|j                  jM                  |jO                  t        |jQ                  |                  }t-        j.                  t1        |j                  |j2                        |ddd      }|jR                  stU        jV                  d      tY        ||jZ                  |jR                  |j\                  |j^                        S )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 startz&The Cloud Shell machine did not start.ssh_envuserhostportr-   )0r   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core_exceptionsErrorConnectionInfosshUsernamesshPortkey_file)argsclientmessagesoperations_clientr:   keysr.   r-   has_key	candidateadd_public_key_operationaccess_tokenstart_operations                r   PrepareEnvironmentrr   O   s    !!,5&##L$7(,,\4@ OO##%'				(9(9		:$!>!>? ))--44' 5 )*+
 	##%#'))i
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9O9O7768l //5588)$,$D$D( %E %* 	9 	+,O ..&33+66	87K 
		


 H
II	""
--
 r   c                     t        j                  dd      } t        j                  dd      }t        j                  t
              }|rA| j                  j                  |j                  t        |j                  |                   yy)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)ri   rj   rp   s      r   AuthorizeEnvironmentrx      s     !!,5&##L$7( 3324,
''<<)(0(L(L( )M )* 	= 	+, r   c                       e Zd Zd Zy)rd   c                 J    || _         || _        || _        || _        || _        y Nr9   )selfr:   r;   r<   r=   r-   s         r   __init__zConnectionInfo.__init__   s%    DLDIDIDIDHr   N)r   r   r   r}   r   r   r   rd   rd      s    r   rd   c                   (    e Zd ZdZd Zd Zd Zd Zy)rS   z"Poller for environment operations.c                      || _         || _        y r{   )environments_serviceoperations_service)r|   r   r   s      r   r}   zEnvironmentPoller.__init__   s     4D0Dr   c                     |j                   S r{   )done)r|   	operations     r   IsDonezEnvironmentPoller.IsDone   s    >>r   c                     | j                   j                  d      }| j                   j                   ||j                              S NrI   r+   )r   GetRequestTyperI   r,   r|   r   request_types      r   PollzEnvironmentPoller.Poll   s8    **99%@L""&&|'HIIr   c                     | j                   j                  d      }| j                   j                   |t                    S r   )r   r   rI   rK   r   s      r   	GetResultzEnvironmentPoller.GetResult   s:    ,,;;EBL$$((235 5r   N)r   r   r   r   r}   r   r   r   r   r   r   rS   rS      s    *1J5r   rS   )#r   
__future__r   r   r   r#   datetimerV   googlecloudsdk.api_lib.utilr   r   #googlecloudsdk.command_lib.util.sshr   googlecloudsdk.corer	   rb   r
   googlecloudsdk.core.credentialsr   rK   	timedeltaMIN_CREDS_EXPIRYformatsecondsr]   rc   r   r   r%   rr   rx   objectrd   OperationPollerrS   r   r   r   <module>r      s    0 &  '    , . 3 = # 1 ; %8%%b1  <<(8(@(@A L/// L0	M`,"V 5.. 5r   