
    tnir                         d Z ddlZddlZddl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  ej        d	            G d
 d          ZdS )zH
Runpod | CLI | Utils | SSH Command

Connect and run commands over SSH.
    N)
STOP_EVENT   )get_pod_ssh_ip_port)get_ignore_list)find_ssh_key_fileT)	autoresetc                   V    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZddZd ZdS )SSHConnectionz"Connect and run commands over SSH.c                 b   || _         	 t          |          \  | _        | _        t	          | j        | j                  | _        t          j                    | _        | j        	                    t          j
                               | j                            | j        | j        d| j                   nV# t          j        $ rD}t          t          j        j        d| dz   |           t#          j        d           Y d }~nd }~ww xY wt'          j        t&          j        | j                   d S )Nroot)portusernamekey_filename[]r   )pod_idr   pod_ippod_portr   key_fileparamiko	SSHClientsshset_missing_host_key_policyAutoAddPolicyconnectSSHExceptionprintcoloramaForeREDsysexitsignalSIGINT_signal_handler)selfr   errs      U/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/runpod/cli/utils/ssh_cmd.py__init__zSSHConnection.__init__   s   	)<V)D)D&DK-dk4=IIDM)++DHH001G1I1IJJJH]!]	      $ 	 	 	(-#m&mmm3S999HQKKKKKKKK	 	fmT%9:::::s   B+B5 5D:DDc                     | S N r&   s    r(   	__enter__zSSHConnection.__enter__0   s        c                 4    ~~~|                                   d S r+   )close)r&   exc_type	exc_value	tracebacks       r(   __exit__zSSHConnection.__exit__3   s    i

r/   c                 D    dddddt          | j                  d| j        gS )z.Get the SSH options for connecting to the pod.z-ozStrictHostKeyChecking=nozLogLevel=ERRORz-pz-i)strr   r   r-   s    r(   _get_ssh_optionszSSHConnection._get_ssh_options7   s2     &M	
 		
r/   c                     ~~|                                   t          t          j        j        d| j         dz   d           t          j                     t          j	        d           dS )zHandle signals.r   r   zSSH Connection Closedr   N)
r1   r   r   r   BLUEr   r   setr!   r"   )r&   signumframes      r(   r%   zSSHConnection._signal_handlerD   s\    E

hm #5t{#5#5#557NOOOr/   c                    d }|D ]}d                     ddd|g          }| j                            |          \  }}}t          j        ||t
          j        j        | j        fd          }t          j        ||t
          j        j	        | j        fd          }	|
                                 |	
                                 |                                  |	                                  dS )	z&Runs a list of bash commands over SSH.c                 f    | D ]-}|r)t          |d| dz   |                                           .d S )Nr   r   )r   strip)streamcolorprefixlines       r(   handle_streamz1SSHConnection.run_commands.<locals>.handle_streamO   sI     ? ? ?%-f---/>>>? ?r/   z && zsource /root/.bashrczsource /etc/rp_environmentzHwhile IFS= read -r -d '' line; do export "$line"; done < /proc/1/environT)targetargsdaemonN)joinr   exec_command	threadingThreadr   r   GREENr   r    start)
r&   commandsrE   commandfull_command_stdoutstderrstdout_threadstderr_threads
             r(   run_commandszSSHConnection.run_commandsL   s   	? 	? 	?
   	! 	!G!;;*0`	 L !% 5 5l C CAvv%,$hm14;?  M
 &,$hm/=  M !!!!!!       5	! 	!r/   c                     | j                                         5 }|                    ||           ddd           dS # 1 swxY w Y   dS )z+Copy local file to remote machine over SSH.N)r   	open_sftpput)r&   
local_pathremote_pathsftps       r(   put_filezSSHConnection.put_filep   s    X!! 	.THHZ---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.   >AAc                     | j                                         5 }|                    ||           ddd           dS # 1 swxY w Y   dS )z.Fetch a remote file to local machine over SSH.N)r   rY   get)r&   r\   r[   r]   s       r(   get_filezSSHConnection.get_fileu   s    X!! 	.THH[*---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.r_   c                 z    dg|                                  z   d| j         gz   }t          j        |d           dS )z(Launch an interactive terminal over SSH.r   root@TcheckN)r8   r   
subprocessrun)r&   cmds     r(   launch_terminalzSSHConnection.launch_terminalz   sH    g--///3H4;3H3H2IIs$''''''r/   Fc           	      L   g d}t                      D ]}|                    d|g           |r|                    d           |                    ddd                    |                                            |d| j         d| g           t          j        |d	
          S )a`  Sync a local directory to a remote directory over SSH.

        A .runpodignore file can be used to ignore files and directories.
        This file should be placed in the root of the local directory to sync.

        Args:
            local_path (str): The local directory to sync.
            remote_path (str): The remote directory to sync.
        )rsyncz-avzz
--no-ownerz
--no-groupz	--excludez--quietz-ezssh  rd   :Tre   )r   extendappendrI   r8   r   rg   rh   )r&   r[   r\   quiet	rsync_cmdpatterns         r(   rl   zSSHConnection.rsync   s     BAA	&(( 	5 	5Gk734444 	(Y''':sxx 5 5 7 788::333k33		
 	
 	
 ~it4444r/   c                 8    | j                                          dS )zClose the SSH connection.N)r   r1   r-   s    r(   r1   zSSHConnection.close   s    r/   N)F)__name__
__module____qualname____doc__r)   r.   r5   r8   r%   rW   r^   rb   rj   rl   r1   r,   r/   r(   r
   r
      s        ,,; ; ;*    
 
 
  "! "! "!H. . .
. . .
( ( (5 5 5 5:    r/   r
   )rx   r#   rg   r!   rK   r   r   
runpod.clir   rp_infor   rp_runpodignorer   rp_userspacer   initr
   r,   r/   r(   <module>r~      s          



       ! ! ! ! ! ! ( ( ( ( ( ( , , , , , , + + + + + +     G G G G G G G G G Gr/   