
    1                        d Z ddlmZ ddlmZ ddlm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dlmZ ddlmZ ddlmZ ddlmZ ddlmZ  ej4                  ej6                  j8                        ej:                   G d dej<                                      Z ej4                  ej6                  j@                        ej:                   G d dej<                                      Z!y)z The `app instances ssh` command.    )absolute_import)division)unicode_literalsN)
exceptions)appengine_api_client)base_classes)base)flags)
iap_tunnel)
ssh_common)ssh)
properties)	resourcesc                   j    e Zd ZdZ ej
                  d      ej                  z   ddZe	d        Z
d Zy)ScpGaz9SCP from or to the VM of an App Engine Flexible instance.zd        *{command}* lets you remotely copy files to or from an App Engine
        Flexible instance.            To copy one file from a remote instance to the local machine, run:

              $ {command} --service=s1 --version=v1 i1:remote_file local_file

          To copy several local files to a remote instance, run:

              $ {command} --service=s1 --version=v1 local_1 local_1 i1:remote_dir

          To use recursive copy, run:

              $ {command} --service=s1 --version=v1 --recurse local_dir i1:remote_dir
          DESCRIPTIONEXAMPLESc                     t        j                  |        t        j                  |        | j	                  ddd       | j	                  ddd       | j	                  ddd	d
       | j	                  ddd       y Nz	--recurse
store_truezUpload directories recursively.)actionhelpz
--compresszEnable compression.sourceszSpecifies the files to copy.z[INSTANCE:]SRC+)r   metavarnargsdestinationz-Specifies a destination for the source files.z[INSTANCE:]DEST)r   r   r
   AddServiceVersionSelectArgsr   AddSshTunnelArgsadd_argumentparsers     lib/surface/app/instances/scp.pyArgsz
ScpGa.Args=       	%%f-'
.  0
 "  $
 + 	   <!  #    c                 	   t        j                  | j                               }t        j                  j                         }|j                          t        j                  j                         }|j                  d       t        j                  j                  |j                        }|j                  D cg c]!  }t        j                  j                  |      # }}t        j                  j                  ||d       |j                   xs |d   j                   }|j"                  }	|j                   s|D ]	  }
||
_         	 |j%                  |j&                  |j(                        }t4        j6                  j8                  j:                  j=                         }t>        j@                  jC                  |	||j(                  |	|j&                  dd	      }|jE                         }	 |jG                  |      }tK        jL                  ||| j                         |||
      }djO                  |jP                  jS                  d      dd       }t        jT                         }tW        jX                  | j                               }t        jZ                  d|||j]                         j_                         d| j                         |j`                  |jb                  |jd                  |	|jf                  r|jf                  nd|dti        jj                  | j                               jl                  jn                        }d}|jp                  s|jr                  r6t        jt                  |j`                  |jb                  |jd                  |	      }tW        jv                  ||j&                  |j(                  |j"                  |j]                         |jx                  |jz                        }|j                   j"                  |_        |j                   jx                  |_<        t        j                  |||j|                  ||j~                  |j                  |j                  |      j                  |      S c c}w # t*        j,                  $ r: t/        j0                  dj3                  |j&                  |j(                              w xY w# t*        j,                  $ r t/        jH                  |      w xY w)  Securily copy files from/to a running flex instance.

    Args:
      args: argparse.Namespace, the args the command was invoked with.

    Raises:
      InvalidInstanceTypeError: The instance is not supported for SSH.
      MissingVersionError: The version specified does not exist.
      MissingInstanceError: The instance specified does not exist.
      UnattendedPromptError: Not running in a tty.
      OperationCancelledError: User cancelled the operation.
      ssh.CommandError: The SCP command exited with SCP exit code, which
        usually implies that a connection problem occurred.

    Returns:
      int, The exit code of the SCP command.
    F	overwriteTsingle_remoter   {}/{}appsId
versionsIdinstancesId
servicesId*appengine.apps.services.versions.instancesparams
collection)args
api_clienttrackprojectversioninstance-N r2   r5   r3   r4   serviceAccountregionapp_engine_paramsmessagesidentity_file	cert_filecompress	recursiveoptionsiap_tunnel_argsCr   GetApiClientForTrackReleaseTrackr   EnvironmentCurrent
RequireSSHKeysFromFilenameEnsureKeysExistFileReferenceFromPathr   r   
SCPCommandVerifyremotehostGetVersionResourceservicer>   apitools_exceptionsHttpNotFoundErrorcommand_exceptionsMissingVersionErrorformatr   VALUEScorer=   	GetOrFailr   REGISTRYParseRelativeNameGetInstanceResourceMissingInstanceErrorr   CreateSshTunnelArgsjoin
vmZoneNamesplitGetDefaultSshUsernamer   GetComputeProjectGetOsloginStateGetPublicKeyToEntrynamer5   r3   rD   compute_base_classesComputeApiHolderclientrH   third_party_userrequire_certificatesCertFileFromAppEngineInstancePopulatePublicKeyuseroslogin_enabledkey_filerL   recurserN   Run)selfr:   r;   envkeysdstsourcesrcsr]   r?   srcversion_resourcer=   resinstance_nameinstance_resourcerO   rE   r   oslogin_staterK   connection_detailss                         r&   r   z	ScpGa.RunW   s   $ &::4;L;L;NOJ
//
!
!
#CNN88  "D5) 


$
$T%5%5
6C=A\\J\6C&&v.\DJNN$48ZZ)47>>F{{H::#
 '#66t||7;||E $$,,668G



"
",,#,,	
 @ # AC $$&MC$88= !44! "$O XX'2288=crBCF$$&D**4+<+<+>?G''##%ll....# $22 !//
 &66

&+M0 I%%)K)K33
,,i $55DLL$,,]//1N1NP
 %++00FK$++00FK>>mm,,"**') *-S2c K 00 '227>>
,,4& ' ''" 00 C33MBBCs   9&Q7&Q< +S <AS	)S5N__name__
__module____qualname____doc__textwrapdedentr   DETAILED_HELPdetailed_helpstaticmethodr'   r    r)   r&   r   r   %   sQ     B %X__ & !+!9!9:	-& # #2s2r)   r   c                   j    e Zd ZdZ ej
                  d      ej                  z   ddZe	d        Z
d Zy)ScpBetazESCP from or to the VM of an App Engine Flexible environment instance.zp        *{command}* lets you remotely copy files to or from an App Engine
        Flexible environment instance.r   r   c                     t        j                  |        t        j                  |        | j	                  ddd       | j	                  ddd       | j	                  ddd	d
       | j	                  ddd       y r   r    r$   s    r&   r'   zScpBeta.Args   r(   r)   c                 	   t        j                  | j                               }t        j                  j                         }|j                          t        j                  j                         }|j                  d       t        j                  j                  |j                        }|j                  D cg c]!  }t        j                  j                  |      # }}t        j                  j                  ||d       |j                   xs |d   j                   }|j"                  }	|j                   s|D ]	  }
||
_         	 |j%                  |j&                  |j(                        }t4        j6                  j8                  j:                  j=                         }t>        j@                  jC                  |	||j(                  |	|j&                  dd	      }|jE                         }	 |jG                  |      }tK        jL                  ||| j                         |||      }d
jO                  |jP                  jS                  d
      dd       }t        jT                         }tW        jX                  | j                               }t        jZ                  d|||j]                         j_                         d| j                         |j`                  |jb                  |jd                  |	|jf                  r|jf                  nd|dti        jj                  | j                               jl                  jn                        }d}|jp                  s|jr                  r6t        jt                  |j`                  |jb                  |jd                  |	      }tW        jv                  ||j&                  |j(                  |j"                  |j]                         |jx                  |jz                        }|j                   j"                  |_        |j                   jx                  |_<        t        j                  |||j|                  ||j~                  |j                  |j                  |      }|j                  |      S c c}w # t*        j,                  $ r: t/        j0                  dj3                  |j&                  |j(                              w xY w# t*        j,                  $ r t/        jH                  |      w xY w)r+   Fr,   Tr.   r   r0   r1   r6   r7   r@   NrA   rB   rC   rF   rI   rP   )r   r:   r;   r   r   r   r   r   r]   r?   r   r   r=   r   r   r   rO   rE   r   r   rK   r   cmds                          r&   r   zScpBeta.Run   s   $ &::4;L;L;NOJ
//
!
!
#CNN88  "D5) 


$
$T%5%5
6C=A\\J\6C&&v.\DJNN$48ZZ)47>>F{{H::#
 '#66t||7;||E $$,,668G



"
",,#,,	
 @ # AC $$&MC$88= !44T:595F5F5H5<>N5FHO
 XX'2288=crBCF$$&D**4+<+<+>?G''##%ll....# $22 !//
 &66

&+M0 I%%)K)K33
,,i $55DLL$,,]//1N1NP
 %++00FK$++00FK
..mm,,"**')C 773<m K 00 '227>>
,,4& ' ''" 00 C33MBBCs   9&Q8&Q= +S =AS
)S6Nr   r   r)   r&   r   r      sR     N %X__ &* +-7-E-EF	-& # #2pr)   r   )"r   
__future__r   r   r   r   apitools.base.pyr   ra   googlecloudsdk.api_lib.appr   googlecloudsdk.api_lib.computer   rx   googlecloudsdk.callioper	   googlecloudsdk.command_lib.apprc   r
   r   r   #googlecloudsdk.command_lib.util.sshr   googlecloudsdk.corer   r   ReleaseTracksrR   GADefaultUniverseOnlyCommandr   BETAr   r   r)   r&   <module>r      s     ' &  '  > ; O ( K 0 5 5 3 * ) D%%(()c2DLL c2  *c2L D%%**+`dll `  ,`r)   