
                             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Zd
 Z	 	 	 ddZd Zy)z0Helpers for running commands external to gcloud.    )absolute_import)division)unicode_literalsN)	ssh_utils)
containers)ssh)logc           	         	 t        j                  |t         j                  t         j                        }t        |j                  j
                  d      D ]?  }t        j                  j                  |       t        j                  j                          A |j                          |j                  dk7  r1t        |j                  j                         j                               y# t        $ ru}t        j                   j#                  d| dt%        j&                  |             dj)                  |      }t        j                   j#                  d|z         Y d}~yd}~ww xY w)	a  Runs a subprocess and prints out the output.

  Args:
    proc_name: The name of the subprocess to call.
      Used for error logging.
    command_list: A list with all the arguments for a subprocess call.
      Must be able to be passed to a subprocess.Popen call.
  )stdoutstderr r   zError running z:  zINVOCATION: %sN)
subprocessPopenPIPEiterr   readliner	   outwriteflushwait
returncodeOSErrorr   readstriperrPrintsix	text_typejoin)	proc_namecommand_listproclecommand_list_strs         Blib/googlecloudsdk/command_lib/compute/diagnose/external_helper.pyRunSubprocessr(      s    7Z__Z__FD$++&&+	ggmmA	ggmmo , 	IIK!DKK$$&,,.//  
 7GGMMIs}}Q7GHIxx-GGMM"%55667s   C&C) )	E'2A+E""E'c                    t        j                  |      }t        j                  |      }	t        j                  ||      }
d}d}|j
                  sE|j                  j                  }|j                  t        j                  |      |j                        }t        j                  ||
||	      }g }t        j                  d|       }t        j                  |
|||||      }|r4t        dj!                  |j#                  |j$                                     y|j'                  |j$                  ||      }t(        j*                  j-                          |S )a  Runs a SSH command to a Google Compute Engine VM.

  Args:
    command_list: List with the ssh command to run.
    instance: The GCE VM object.
    user: The user to be used for the SSH command.
    args: The args used to call the gcloud instance.
    ssh_helper: ssh_utils.BaseSSHCLIHelper instance initialized
      for the command.
    explicit_output_file: Use this file for piping stdout of the SSH command,
                          instead of using stdout. This is useful for
                          capturing the command and analyzing it.
    explicit_error_file: Use this file for piping stdout of the SSH command,
                         instead of using stdout. This is useful for
                         capturing the command and analyzing it.
    dry_run: Whether or not this is a dry-run (only print, not run).
  Returns:
    The exit code of the SSH command
  Raises:
    ssh.CommandError: there was an error running a SSH command
  N)identity_fileoptionsextra_flagsremote_command	remainderr   r   )explicit_output_fileexplicit_error_file)r   GetExternalIPAddressGetInternalIPAddressr   Remoteplainkeyskey_file	GetConfigHostKeyAliasstrict_host_key_checkingParseAndSubstituteSSHFlagsr   GetRemoteCommand
SSHCommand	DryRunLogr    BuildenvRunr	   r   r   )r"   instanceuserargs
ssh_helperr/   r0   dry_runexternal_addressinternal_addressremoter*   r+   r,   r.   r-   cmdreturn_codes                     r'   RunSSHCommandToInstancerK   7   s+   : 33H=33H=::&-&-'	OO,,M""9#9#9(#C#'#@#@BG..tV=M/?A+)..t\B.!#	# chhsyy012nn/-  /+ ''--/	    c                 H    t         j                  j                  d| z         y )Nz[COMMAND TO RUN]: %s
)r	   r   r   )msgs    r'   r=   r=   w   s    ''--(3./rL   )NNF)__doc__
__future__r   r   r   r   "googlecloudsdk.command_lib.computer   #googlecloudsdk.command_lib.util.sshr   r   googlecloudsdk.corer	   r   r(   rK   r=    rL   r'   <module>rU      s@    7 &  '  8 : 3 # 
7> 2604$)=@0rL   