
    1                     (   S 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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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SKJr  SrSr Sr!Sr"Sr#\RH                  \RJ                  " \RL                  RN                  5       " S S\RP                  5      5       5       r)\RJ                  " \RL                  RT                  \RL                  RV                  5       " S S\)5      5       r,g)z,Connects to a serial port gateway using SSH.    )absolute_import)division)unicode_literalsN)base_classes)daisy_utils)arg_parsers)base)
completers)flags)scope)	ssh_utils)ssh)http)log)
properties)encoding)http_clientzOhttps://cloud.google.com/compute/docs/instances/interacting-with-serial-console9600z{0}-ssh-serialport.{1}z2https://www.gstatic.com/vm_serial_port/{0}/{0}.pubz>https://www.gstatic.com/vm_serial_port_public_keys/{0}/{0}.pubc                   J    \ rS rSrSr\R                  rSr\	S 5       r
S rSrg)ConnectToSerialPort;   6  Connect to the serial port of an instance.

*{command}* allows users to connect to, and interact with, a VM's
virtual serial port using ssh as the secure, authenticated transport
protocol.

The user must first enable serial port access to a given VM by setting
the 'serial-port-enable=true' metadata key-value pair. Setting
'serial-port-enable' on the project-level metadata enables serial port
access to all VMs in the project.

This command uses the same SSH key pair as the `gcloud compute ssh`
command and also ensures that the user's public SSH key is present in
the project's metadata. If the user does not have a public SSH key,
one is generated using ssh-keygen.

## EXAMPLES
To connect to the serial port of the instance 'my-instance' in zone
'us-central1-f', run:

  $ {command} my-instance --zone=us-central1-f
Tc           	         [         R                  R                  U 5        U R                  SSSS9  U R                  S[        R
                  SSS9  U R                  S	S
SS9  U R                  S[        R                  " S
S90 SSR                  [        5      S9  U R                  SSS9  [        R                  " U SSS9  [         R                  " U 5        g)zPSet up arguments for this command.

Args:
  parser: An argparse.ArgumentParser.
z	--dry-run
store_truezSIf provided, the ssh command is printed to standard out rather than being executed.)actionhelp	user_hostz[USER@]INSTANCEz        Specifies the user/instance for the serial port connection.

        ``USER'' specifies the username to authenticate as. If omitted,
        the current OS user is selected.
        )	completermetavarr   z--port   a          The number of the requested serial port. Can be 1-4, default is 1.

        Instances can support up to four serial ports. By default, this
        command will connect to the first serial port. Setting this flag
        will connect to the requested serial port.
        )defaultr   z--extra-args)
min_lengthz	KEY=VALUEz        Optional arguments can be passed to the serial port connection by
        passing key-value pairs to this flag, such as max-connections=N or
        replay-lines=N. See {0} for additional options.
        )typer!   r   r   z
--locationz        If provided, the region in which the serial console connection will
        occur. Must be the region of the VM to connect to.
        )r   instancez
connect to)resource_typeoperation_typeN)r   BaseSSHHelperArgsadd_argumentr
   InstancesCompleterr   ArgDictformatSERIAL_PORT_HELPr   AddZoneFlagAddSSHKeyExpirationArgs)parsers    -lib/surface/compute/connect_to_serial_port.pyr(   ConnectToSerialPort.ArgsX   s      (
*	   //!	  
   
   A. F#$  
    
j %%f-    c           
        ^  [         R                  " U R                  5       5      nUR                  n[        R
                  " 5       nUR                  U5        UR                  R                  UR                  SS9  [        R                  R                  UR                  5      nU(       d/  [        R                  " SR                  UR                  5      5      eUR                   (       d  [        R"                  " 5       Ul        UR                  R%                  5       R'                  SS9n[(        R*                  R-                  UR.                  /[0        R2                  R4                  UR6                  UR8                  [(        R:                  " U5      S9S   nUR<                  (       dF  [>        R@                  " UR6                  5      n[B        RD                  " SR                  U5      5        OUR<                  n[F        R                  U[H        RJ                  " 5       5      m [L        R                  U5      n	[N        R                  U5      n
[B        RD                  " SR                  U5      5        S	R                  T [P        5      n[        RR                  RU                  5       nU 4S
 jn/ nU RV                  (       a  URY                  U" U
5      5        URY                  U" U	5      5        U(       ak  U RV                  (       a  UR[                  XSS9  OUR]                  XS   SS9  UR_                  5         [B        RD                  " SR                  T 5      5        OxURa                  U5      (       a1  [B        Rb                  " SR                  T URd                  5      5        O1[B        Rb                  " SR                  T URd                  5      5        gURg                  X75      nURi                  X7Rj                  5      n[        Rl                  " U5      u  nn[        Rn                  " UUUR                   UUU R                  5       UR                  Rp                  S9nUR                   Ul        SR                  URr                  5      nURj                  UR6                  URu                  5       UR                   U/nURv                  (       d"  SR                  S5      nURy                  U5        URz                  (       aE  URz                  R}                  5        H'  u  nnURy                  SR                  UU5      5        M)     SR                  U5      n[        R                  " T US9nUR                  R                  nUR                  USS9nUS	 SUS'   [        R                  " UU[P        US9nURv                  (       aH  [B        R                  R                  SR                  UR                  UR                  5      5      5        gUR                  (       d  UR                  X5R                   UUU5        UR                  (       + =(       a2    [H        R                  R                  R                  R                  5       n UR                  UR                  US9nU(       a  [        R                  " U5        gg! [        R                   a    Sn N7f = f) z!See ssh_utils.BaseSSHCommand.Run.T)allow_passphrasez>Expected argument of the form [USER@]INSTANCE. Received [{0}].)include_comment)scope_listerr   z Determined region from zone: {0}z*Connecting to serialport via server in {0}z	[{0}]:{1}c                 $  > [         R                  " 5       n/ nUR                  U 5      n[        R                  " SR                  U5      5        [        US   S   5      [        R                  :X  ax  [        R                  " US   5      R                  5       R                  S5      nU H  nUR                  U5        M     [        R                  " SR                  TU 5      5        U$ [        R                  " SR                  TU 5      5        U$ )zGet host key from endpoint.zhttp_response: {0}r   statusr    
z.Successfully acquired hostkey for {0} from {1}z*Failed to acquire hostkey for {0} from {1})r   Httprequestr   infor,   inthttplibOKr   Decodestripsplitappendwarning)endpointr   	host_keyshttp_responseretrieved_host_keyshost_keygateways         r1   _GetHostKey,ConnectToSerialPort.Run.<locals>._GetHostKey   s    IIKki!))(3m	hh#**=9:	]1h'	(GJJ	6 OOM!,-335;;DA 	 ,H


8
$ ,<CC	
  	8??	

 r3   )	overwritez$Successfully written hostkey for {0}zUnable to download and update Host Key for [{0}] . Attempting to connect using existing Host Key in [{1}]. If the connection fails, please try again to update the Host Key.zUnable to download Host Key for [{0}]. No Host Key found in known_hosts file [{1}]. gcloud does not have a fallback Host Key and will therefore terminate the connection attempt. If the problem persists, try updating gcloud and connecting again.N)messageszport={0}z
source={0}gcloudz{0}={1}.)useryes)strict_host_key_checkingHostKeyAliasnoneControlPath)identity_fileportoptions )putty_force_connect   )Pr   ComputeApiHolderReleaseTrackclientr   r'   RunkeysEnsureKeysExistforce_key_file_overwriter   RemoteFromArgr   ArgumentErrorr,   rR   GetDefaultSshUsernameGetPublicKeyToEntryinstance_flagsSSH_INSTANCE_RESOLVERResolveResourceshostcompute_scope	ScopeEnumZONEzone	resourcesGetInstanceZoneScopeListerlocationr   GetRegionFromZoner   r=   %REGIONAL_SERIAL_PORT_GATEWAY_TEMPLATEr   GetUniverseDomainREGIONAL_HOST_KEY_URL_TEMPLATE!REGIONAL_HOST_KEY_URL_TEMPLATE_V2CONNECTION_PORT
KnownHostsFromDefaultFileuse_v2_host_key_endpointextendAddMultipleAddWriteContainsAliasrE   	file_pathGetInstance
GetProjectprojectGetSSHKeyExpirationFromArgsGetOsloginStaterO   rY   Namedry_runrD   
extra_argsitemsjoinkey_file	GetConfig
SSHCommandoutPrintBuildenvoslogin_enabledEnsureSSHKeyExistsoslogin_2fa_enabledVALUESr\   GetBoolCommandErrorsysexit)!selfargsholderr`   
ssh_helperremote
public_keyinstance_refru   hostkey_urlhostkey_url_v2hostnameknown_hostsrL   rG   r$   r   
expirationexpiration_micrososlogin_staterY   constructed_username_listsourcekvserial_userserial_remoterX   rZ   cmdr\   return_coderK   s!                                   @r1   ra   ConnectToSerialPort.Run   si   **4+<+<+>?F]]F((*JNN4OO##%% $  ZZ/F##
J6$..!  ;;--/fk--/777MJ!77HH	$$		#>>vF I  	L ==..|/@/@Ah	hh188BCh3::*..0G 177AK6==hGNHH9@@JK!!'?;H..002K6 I$${>23[-.		&	&tDA,$?	hh5<<WEF		"	"8	,	,	kkL 6';001	 
kk  &vg{/D/DE %%f;H##F,@,@AG$-$I$I$$O!J!''''M  $$FK TYY'D! <<""8,f&&v.//'')$!Q!(()9)9!Q)?@ *((45KJJw[9MOO,,M""8e"LG#GM
..#	C ||	ggmmCHHSYYz~~678((##
++x* --- 	@!!55==? GG
...A  k
 	hh{   ks   [
 
[#"[# N)__name__
__module____qualname____firstlineno____doc__r	   TOOLS_CATEGORYcategoryr~   staticmethodr(   ra   __static_attributes__r   r3   r1   r   r   ;   s5    .   (!A. A.Frr3   r   c                   6   ^  \ rS rSrSr\U 4S j5       rSrU =r$ )ConnectToSerialPortAlphaBetaiS  r   c                 :   > SU l         [        [        U ]  U5        g )NT)r~   superr   r(   )clsr0   	__class__s     r1   r(   !ConnectToSerialPortAlphaBeta.Argsl  s    #'C 	
&1&9r3   r   )	r   r   r   r   r   classmethodr(   r   __classcell__)r   s   @r1   r   r   S  s    . : :r3   r   )-r   
__future__r   r   r   r   googlecloudsdk.api_lib.computer   r   googlecloudsdk.callioper   r	   "googlecloudsdk.command_lib.computer
   r   r   ro   r   ,googlecloudsdk.command_lib.compute.instancesrk   #googlecloudsdk.command_lib.util.sshr   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr   	six.movesr   r?   r-   r{   rw   ry   rz   UniverseCompatibleReleaseTracksr_   GACommandr   BETAALPHAr   r   r3   r1   <module>r      s     3 &  ' 
 7 6 / ( 9 4 E 8 P 3 $ # * - ,0   )A % 9  E "
 D%%(()Q$,, Q * Ql D%%**D,=,=,C,CD:#6 : E:r3   