
    $                         d 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 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  G d dej4                        Zy)zCBase class for commands copying files from and to virtual machines.    )absolute_import)division)unicode_literals)FilesCompleter)actions)base)flags)
iap_tunnel)scope)	ssh_utils)ip)ssh)log)
properties)retryc                   n     e Zd ZdZe fd       Zddddddej                  j                  f fd	Z	 xZ
S )BaseScpHelperz>Copy files to and from Google Compute Engine virtual machines.c                 D   t         t        t          |        | j                  dt        ddd       | j                  ddd	       | j                  d
t        j                  t        j                  j                  j                        dt        j                  z          y)z\Set up arguments for this command.

    Args:
      parser: An argparse.ArgumentParser.
    sourceszSpecifies the files to copy.z[[USER@]INSTANCE:]SRC+)	completerhelpmetavarnargsdestinationz-Specifies a destination for the source files.z[[USER@]INSTANCE:]DEST)r   r   z--zonez1The zone of the instance to copy files to/from.

)actionr   N)superr   Argsadd_argumentr   r   StorePropertyr   VALUEScomputezoner	   ZONE_PROPERTY_EXPLANATION)parser	__class__s    3lib/googlecloudsdk/command_lib/compute/scp_utils.pyr   zBaseScpHelper.Args)   s     
-,V4
 +'   <(  *
 $$Z%6%6%>%>%C%CDC--.  0    FNc
                 :   |t         j                  j                  }t        t        |   |       t        j                  j                  |j                        }
|j                  D cg c]!  }t        j                  j                  |      # }}t        j                  j                  ||
d       |
j                  xs |d   j                  }|
j                  s|D ]	  }||_         t        j                  |      \  }}d}d}d}|j                   }t        j"                         }d}|r#t%        j&                  |||j(                        }nt*        j,                  j/                  |j(                  gt0        j2                  j4                  |j6                  |j8                  t+        j:                  |            d   }| j=                  ||      }| j?                  ||j@                        }|jB                  rd}nd}t        jD                  d      |_!        |jF                  rd|_$        nv| jJ                  jM                         jO                  d      }t        jP                  |||jB                  |||||j                   jR                  	      }|jB                  |_!        |jT                  s|jV                  r5t        jX                  |jZ                  |j6                  |j\                        }t        j^                  |      }|jF                  sG|s| jJ                  j`                  }| jc                  t        jd                  |      |jf                        }t%        jh                  |||t        jj                  |d
            }|rt        jd                  |      |_        nQ|	tl        jn                  jp                  u rt        jr                  |      |_        nt        jt                  |      |_        t        j                  ||
|||||||||      }|jv                  rHtx        jz                  j}                  dj                  |j                  | j                                     y|t         j                  j                  k7  rt        j                  |       |jF                  xr t        d |D               }| xrA |j                   xr2 t        j                  j                  j                  j                         }|jF                  s|jH                  rd}n | j                  ||jB                  |      }|rWt        j                  |||||      } tx        j                  j}                  d       	 | j                  | j                  |       |	tl        jn                  jp                  u r| j                  j\                  ||||       |j                  | j                  |       yc c}w # t        j                  $ r t        j                         w xY w)a  SCP files between local and remote GCE instance.

    Run this method from subclasses' Run methods.

    Args:
      compute_holder: The ComputeApiHolder.
      args: argparse.Namespace, the args the command was invoked with.
      on_prem: bool, Whether to connect to an on-prem IP.
      port: str or None, Port number to use for SSH connection.
      recursive: bool, Whether to use recursive copying using -R flag.
      compress: bool, Whether to use compression.
      extra_flags: [str] or None, extra flags to add to command invocation.
      release_track: obj, The current release track.
      ip_type: IpTypeEnum, Specify using internal ip or external ip address.

    Raises:
      ssh_utils.NetworkError: Network issue which likely is due to failure
        of SSH key propagation.
      ssh.CommandError: The SSH command exited with SSH exit code, which
        usually implies that a connection problem occurred.
    NT)single_remoter   )scope_listerF)warn_on_account_user)include_comment)username_requestedmessages)no_raise)	identity_file	cert_fileoptions	recursivecompressportextra_flagsiap_tunnel_argsidentity_list c              3   L   K   | ]  }|d k(  xs |j                  d        yw)z-iz-i=N)
startswith).0fs     r'   	<genexpr>z'BaseScpHelper.RunScp.<locals>.<genexpr>   s&     JkAI4e!44ks   "$)
expiration)r6   z!Waiting for SSH key to propagate.)putty_force_connect)Qr   ReleaseTrackGAr   r   Runr   FileReferenceFromPathr   r   
SCPCommandVerifyremoter   GetSSHKeyExpirationFromArgsclientOsloginStater
   CreateOnPremSshTunnelArgshostinstance_flagsSSH_INSTANCE_RESOLVERResolveResourcescompute_scope	ScopeEnumZONEr#   	resourcesGetInstanceZoneScopeListerGetInstance
GetProjectprojectuserGetDefaultSshUsernameplainoslogin_enabledkeysGetPublicKeyToEntryGetOsloginStater/   third_party_userrequire_certificatesCertFileFromComputeInstancenameidWriteSecurityKeyskey_file	GetConfigHostKeyAliasstrict_host_key_checkingCreateSshTunnelArgsGetExternalInterfacer   
IpTypeEnumINTERNALGetInternalIPAddressGetExternalIPAddressdry_runr   outPrintjoinBuildenvConfirmSecurityKeyStatusanyoslogin_2fa_enabledr   r!   rA   GetBoolEnsureSSHKeyExistsCreateSSHPollerstatusPollr   WaitExceptionNetworkErrorPreliminarilyVerifyInstance)"selfcompute_holderargson_premr6   r4   r5   r7   release_trackip_typedstsrcsrcsrI   r@   expiration_microsr1   identity_file_listr3   compute_clientoslogin_stater2   r8   instance_refinstancerY   r.   
public_keycmdprompt_for_passwordrA   keys_newly_addedpollerr&   s"                                    r'   RunScpzBaseScpHelper.RunScpD   s?   > ''**m	-"4(



$
$T%5%5
6C7;||D|C&&s+|DD NN$48ZZ)47>>F::#
  %.$I$I$$O!J!MG
 $**N$$&MI"<<
v{{,o $99JJ;;-

!
!
&
&
))

"
"%@@ K   !"l !!.,?h0D0DEg	!"//TJ	(-%YY++-55d5K
++KK1#**335 $((

(
(M,N,N33LL,++X[[
	
 00?ZZ!)),,-..!7!7!A!%!>!>@ #66
|

(
(D
ACo 
,,X6bmm,,,44X>44X>
..c)9xd(	*C ||	ggmmCHHSYYtxx012
 )),,,((7 	

 	KJkJJJ    	@111	@!!55==? 
 zz]2200

++

 1 ! (()8tEf 
jj:;'HH 3 	 	5 "--(((
 &&x{{FM'.0CE GG/  1W E~    '$$&&'s   (&W-7W2 2(X)__name__
__module____qualname____doc__staticmethodr   r   rn   EXTERNALr   __classcell__)r&   s   @r'   r   r   &   sE    F0 0: ]]++r1 r1r(   r   N)r   
__future__r   r   r   argcomplete.completersr   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.computer	   r
   r   rR   r   ,googlecloudsdk.command_lib.compute.instancesrO   #googlecloudsdk.command_lib.util.sshr   r   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   BaseSSHCLIHelperr    r(   r'   <module>r      sO     J &  ' 1 + ( 4 9 E 8 P 2 3 # * *P1I.. P1r(   