
    tni&              
       T   d 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m	Z	 ddl
mZ  ej        dd	          d
             Ze                    d          d             Ze                    d           ej        dd           ej        ddd           ej        ddd           ej        ddd           ej        ddd          d                                                                         Ze                    d           ej        d          d                         Ze                    d            ej        d!           ej        d"           ej        d#d$%           ej        d&d$%          d'                                                             ZdS )(z
Runpod | CLI | Pod | Commands
    N)PrettyTable)
create_podget_pods   )ssh_cmdpodzManage and interact with pods.)helpc                      dS )z&A collection of CLI functions for Pod.N r       [/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/runpod/cli/groups/pod/commands.pypod_clir      s      r   listc                      t          g d          } t                      D ]3}|                     |d         |d         |d         |d         f           4t          j        |            dS )z.
    Lists the pods for the current user.
    )IDNameStatusImageidnamedesiredStatus	imageNameN)r   r   add_rowclickecho)tabler   s     r   	list_podsr      ss    
 999::Ezz X Xs4y#f+s?/CSEUVWWWW	Jur   creater   F)requiredz--imagezThe image to use for the pod.)defaultr	   z
--gpu-typez The GPU type to use for the pod.z--gpu-count   z&The number of GPUs to use for the pod.z--support-public-ipTz&Whether or not to support a public IP.c                    ||d}| st          j        dd          } t          j        dd          }|rd}d	}d
|d<   t          j        d           t	          | ||fi |}t          j        d|d          d           dS )z
    Creates a pod.
    )	gpu_countsupport_public_ipzEnter pod namezRunPod-CLI-Podr    z%Would you like to launch default pod?T)abortzrunpod/base:0.0.0zNVIDIA GeForce RTX 3090z22/tcpportszLaunching default pod...zPod r   z has been created.N)r   promptconfirmr   r   )r   imagegpu_typer#   r$   kwargsquick_launchnew_pods           r   create_new_podr/   !   s     . F
  H|,6FGGG=!HPTUUUL /#,"w
-...uh99&99G	J7gdm77788888r   connectpod_idc                     t          j        d|  d           t          j        |           }|                                 dS )z
    Connects to a pod.
    zConnecting to pod ...N)r   r   r   SSHConnectionlaunch_terminal)r1   sshs     r   connect_to_podr7   D   sH     
J/F///000


'
'Cr   syncsource_pod_iddest_pod_idsource_workspacez
/workspacer%   dest_workspacec           
         	 ddl m}  |            }|st          j        d           t          j        d           t          j        d           t          j        d           t          j        d           t          j        d           t          j        d           d	S t          j        d
t	          |           d           nO# t
          $ rB}t          j        dt          |                      t          j        d           Y d	}~nd	}~ww xY wt          j        d|  d| d| d|            t          t          j                              d	d         }d| d}d| }		 t          j        d|  d           t          j
        |           5 }
t          j        d| d           |
j                            d| d          \  }}}|                                                                                                }t          j        d| d           |
j                            d| d          \  }}}|                                                                                                }|dk    rWt          j        d| d|             	 d	d	d	           	 d t!                      v rt#          j        |           d	S d	S #  Y d	S xY wt          j        d!| d           d"| }d#t"          j                            |           d$| d%t"          j                            |           }|
                    |g           |
j                            d&| d'          \  }}}|                                                                                                }|d(k    rQt          j        d)           	 d	d	d	           	 d t!                      v rt#          j        |           d	S d	S #  Y d	S xY w|
j                            d*| d+          \  }}}|                                                                                                }t          j        d,| d-           t          j        d.           t/          j        d/d0          5 }|j        }|
                    ||           d	d	d	           n# 1 swxY w Y   |
                    d1| g           d	d	d	           n# 1 swxY w Y   t          j        d2| d           t          j
        |          5 }t          j        d3| d           |                    d4| g           t          j        d5           d"| }|                    ||           t          j        d6| d7|	 d           d#| d8|	 d9|	 d:| d;	}|                    |g           |j                            d| d7|	 d          \  }}}|                                                                                                }t          j        d<| d=           |                    d1| g           t          j        d           t          j        d>           t          j        d?|            t          j        d@| d| d7|	            t          j        d           t          j        dA           t          j        dB|            t          j        dC| d7|	            d	d	d	           n# 1 swxY w Y   n# t
          $ r}t          j        dDt          |                      t          j        d           t          j        dE           t          j        dF           t          j        dG           t          j        dH           t          j        dI           Y d	}~nd	}~ww xY w	 d t!                      v rt#          j        |           d	S d	S #  Y d	S xY w# 	 d t!                      v rt#          j        |           w w #  Y w xY wxY w)JuD  
    Sync data between two pods via SSH.
    
    Transfers files from source_pod_id:source_workspace to dest_pod_id:dest_workspace.
    The workspace will be zipped and transferred to avoid file name conflicts.
    
    📋 PREREQUISITES:
    
    1. SSH Key Setup:
       • You must have an SSH key configured in your Runpod account
       • If you don't have one, create it with: runpod ssh add-key
       • List your keys with: runpod ssh list-keys
    
    2. Pod Configuration:
       • Both pods must have SSH access enabled
       • For running pods using official Runpod templates, you may need to add
         your public key to the PUBLIC_KEY environment variable and restart the pod
    
    ⚠️  IMPORTANT NOTES:
    
    • If a pod was started before adding your SSH key, you'll need to:
      1. Stop the pod
      2. Add PUBLIC_KEY environment variable with your public key
      3. Restart the pod
    
    • The sync creates a unique folder (sync_XXXXXXXX) in the destination to avoid
      file conflicts
    
    📖 EXAMPLES:
    
    Basic sync (uses /workspace as default):
        runpod pod sync pod1 pod2
    
    Custom paths:
        runpod pod sync pod1 pod2 /workspace/data /workspace/backup
    
    Different directories:
        runpod pod sync pod1 pod2 /home/user/files /workspace/imported
    r   )get_user_pub_keysu-   ❌ No SSH keys found in your Runpod account! u   🔑 To create an SSH key, run:z   runpod ssh add-keyu   📖 For more help, see:z   runpod ssh add-key --helpNu
   ✅ Found z SSH key(s) in your accountu,   ⚠️  Warning: Could not verify SSH keys: zContinuing with sync attempt...u   🔄 Syncing from :z to    sync_z.tar.gzu   📡 Connecting to source pod r3   u   📊 Counting files in zfind z -type f | wc -lu   📁 Found z files in source workspaceztest -d z% && echo 'exists' || echo 'not_found'existsu   ❌ Error: Source workspace z does not exist on pod local_temp_pathu   📦 Creating archive of z/tmp/zcd z && tar -czf  ztest -f z# && echo 'created' || echo 'failed'createdu1   ❌ Error: Failed to create archive on source podzdu -h z
 | cut -f1u"   ✅ Archive created successfully ()u/   ⬇️  Downloading archive to local machine...F)deletesuffixzrm -f u#   📡 Connecting to destination pod u%   📂 Preparing destination workspace z	mkdir -p u/   ⬆️  Uploading archive to destination pod...u   📦 Extracting archive to /z && mkdir -p z && cd z && tar -xzf z --strip-components=1u   📁 Extracted z files to destinationu!   🎉 Sync completed successfully!u   📊 Files transferred: u   📍 Destination location: u    💡 To access the synced files:z   runpod ssh z   cd u   ❌ Error during sync: u   🔧 Troubleshooting tips:u,   • Ensure both pods have SSH access enableduQ   • Check that your SSH key is added to your Runpod account: runpod ssh list-keysuH   • For running pods, you may need to add PUBLIC_KEY env var and restartu1   • Verify the source and destination paths exist)groups.ssh.functionsr>   r   r   len	Exceptionstruuiduuid4r   r4   r6   exec_commandreaddecodestriplocalsosunlinkpathdirnamebasenamerun_commandstempfileNamedTemporaryFiler   get_fileput_file)r9   r:   r;   r<   r>   	user_keysetransfer_idtemp_zip_namedest_folder
source_ssh_stdout
file_countstderrresultrD   archive_pathtar_commandarchive_resultarchive_size	temp_filedest_sshdest_archive_pathextract_commanddest_file_counts                             r   	sync_podsrt   O   sI
   ^6======%%''	 
	QJFGGGJrNNNJ8999J.///JrNNNJ1222J5666FJOC	NNOOOPPPP 6 6 6
J#a&&JJKKK
4555555556 
JhMhh4Dhh+hhXfhhiii djll##BQB'K0K000M'+''K_
FMFFFGGG"=11 (	?Z JF1AFFFGGG%>667a?O7a7a7abbLAvq--//5577JJKZKKKLLL !+ ; ;<~GW<~<~<~  Avv[[]]))++1133F!!
r:Jrrcprrsss(	? (	? (	? (	? (	? (	?p	 FHH,,	/***** -,	DDY JH3CHHHIII2=22L D0@ A A  D  DP\  D  D_a_f_o_o  qA  `B  `B  D  DK##[M222 &>667s,7s7s7sttLAvq#[[]]113399;;N**
OPPP5(	? (	? (	? (	? (	? (	?p	 FHH,,	/***** -,	DD &>667X7X7X7XYYLAvq!;;==//117799LJKLKKKLLL JHIII,E)LLL CPY"+.##L/BBBC C C C C C C C C C C C C C C
 ##%<l%<%<$=>>>Q(	? (	? (	? (	? (	? (	? (	? (	? (	? (	? (	? (	? (	? (	? (	?V 	
IIIIJJJ";//  	@8 JR~RRRSSS!!#?~#?#?"@AAA JHIII 7 7 7o/@AAA JV^VVkVVVWWW XN  X  X  X  XU`  X  X  pA  X  X  XO!!?"3444 $<445k^5k5kk5k5k5kllLAvq$kkmm2244::<<OJOOOOPPP !!#?,=#?#?"@AAA JrNNNJ:;;;J>*>>???Ja[aa>aaT_aabbbJrNNNJ9:::J555666J>>>>>???A 	@  	@  	@  	@  	@  	@  	@  	@  	@  	@  	@  	@  	@  	@  	@D  H H H
5SVV55666
2
/000
ABBB
fggg
]^^^
FGGGGGGGGH	 FHH,,	/***** -,	DD		 FHH,,	/**** -	Ds  BC "%C 
D8DD+,\+ DS8\+ )$K KC-S8\+ $O: :O?BS8S<S8S	S8S	S8,\+ 8S<<\+ ?S< /\+ /G$\\+ \##\+ &\#'\+ *` +
_5B_` _` $` ``>$`75`>7`;9`>)__doc__rV   r\   rO   r   prettytabler   runpodr   r   utilsr   groupr   commandr   argumentoptionr/   r7   rt   r   r   r   <module>r}      sq    
			    # # # # # # ' ' ' ' ' ' ' '       U9:::1 1 ;:1 	   	'''i,KLLLlD/QRRRmQ-UVVV4.V  9 9  WV SR ML (' 96 	    	  "L999 ,777b b 87 :9  !  
b b br   