
    &`i;E                     <   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ZddlZddl	Z	ddl
mZ ddlZddlZddlZddlmZ ddlZddlmZ d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Z	 d5dZ e!dk    r# e            \  Z"Z#Z$Z%Z&Z' ee"          Z" ee"            e(de"             e(de#             e(de$             e(de%             ej)        e"*                                          Z+e+d         dz    e, e- ej                                        z   e+d<    e(d            e(de'            e'r ee+e'            e(d            e(de&             ee&          Z. e(de.            e.r ee+e.           e+/                    di           /                    d           Z0d!e+d         d"<   e0d#k    r e             n0e0d$k    r( e             e+d         1                    d%d           ne0d&k    r e              ej2        g d'ej3        d()          j4        5                    d*          6                                Z7 e(d+e7            ej2        g d,ej3        d()          j4        5                    d*          6                                Z8 e(d-e8 d.           e8e+d         d/<   nCe0d0k    r e(d            e(d1           n& e(d            e(d2            ej9        d            ej:        d34          5 Z;e;<                     ej=        e+          >                    d*                     e;?                                  ee;j@                  Z" e e+e"e#e$e%           ddd           dS # 1 swxY w Y   dS dS )6a  
This script automates the process of launching and verifying a Ray cluster using a given
cluster configuration file. It also handles cluster cleanup before and after the
verification process. The script requires one command-line argument: the path to the
cluster configuration file.

Usage:
    python launch_and_verify_cluster.py [--no-config-cache] [--retries NUM_RETRIES]
        [--num-expected-nodes NUM_NODES] [--docker-override DOCKER_OVERRIDE]
        [--wheel-override WHEEL_OVERRIDE]
        <cluster_configuration_file_path>
    N)Path)storage)RAYc                  4   t          j        d          } |                     ddd           |                     dt          dd	
           |                     dt          dd
           |                     dg ddd           |                     dt          dd
           |                     dt          d           |                                 }|j        dk    r|j        dk    r
J d            |j        |j	        |j
        |j        |j        |j        fS )z
    Check command line arguments and return the cluster configuration file path, the
    number of retries, the number of expected nodes, and the value of the
    --no-config-cache flag.
    zLaunch and verify a Ray cluster)description--no-config-cache
store_truez3Pass the --no-config-cache flag to Ray CLI commands)actionhelpz	--retries   z;Number of retries for verifying Ray is running (default: 3))typedefaultr   z--num-expected-nodes   z9Number of nodes for verifying Ray is running (default: 1)z--docker-override)disablelatestnightlycommitr   zAOverride the docker image used for the head node and worker nodes)choicesr   r   z--wheel-override z:Override the wheel used for the head node and worker nodescluster_configz&Path to the cluster configuration file)r   r   z%Cannot override both docker and wheel)argparseArgumentParseradd_argumentintstr
parse_argsdocker_overridewheel_overrider   retriesno_config_cachenum_expected_nodes)parserargss     |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/launch_and_verify_cluster.pycheck_argumentsr%   !   s    $1RSSSF
B    
 J	     H	     :::P	     I	     s)Q     D 		))d.AR.G.G.G. /H.G
 	     c                    | dk    rdS | dk    rdS | dk    rtt          j        dt          j                  r%dt          j         dt          j        d	d
          dS t          dt          j                    t          j        d           d	S )a  
    Get the docker image to use for the head node and worker nodes.

    Args:
        docker_override: The value of the --docker-override flag.

    Returns:
        The docker image to use for the head node and worker nodes, or None if not
        applicable.
    r   zrayproject/ray:latest-py310r   zrayproject/ray:nightly-py310r   z^[0-9]+.[0-9]+.[0-9]+$zrayproject/ray:.N   z-py310zGError: docker image is only available for release version, but we get: r   )rematchray__version__
__commit__printsysexit)r   s    r$   get_docker_imager2   X   s     ("",,	I	%	%--	H	$	$8,co>> 	QS_QQs~bqb7IQQQQB03B B   HQKKK4r&   c                     |                                  rt          j        | t          j                  s(t	          d|             t          j        d           dS dS )z
    Check if the provided file path is valid and readable.

    Args:
        file_path: The path of the file to check.

    Raises:
        SystemExit: If the file is not readable or does not exist.
    z/Error: Cannot read cluster configuration file: r   N)is_fileosaccessR_OKr/   r0   r1   )	file_paths    r$   
check_filer9   s   s_      bi	27&C&C K	KKLLL r&   c                 n    |                      dg           }|                    d| d           || d<   d S )Nsetup_commandsz9pip3 uninstall -y ray && pip3 install -U "ray[default] @ ")getappend)config_yaml	wheel_urlr;   s      r$   override_wheels_urlrA      sO     __%5r::NPIPPP   %3K !!!r&   c                     |                      di           }||d<   d|d<   |                     d          
J d            |                     d          
J d            || d<   d S )	Ndockerimageray_containercontainer_name
head_imagezCannot override head_imageworker_imagezCannot override worker_image)r=   )r?   docker_imagedocker_configs      r$   override_docker_imagerK      s    OOHb11M)M'&5M"#\**224P222^,,446T444)Kr&   c                  h   t          d           t          d           t          j        dd          } |                     d          }|d         }t	          j        |          d	         }t	          j        |          d
         }|                     d          }|d         }t          j                    }t          j	        
                    |d          }t          |d          5 }	|	                    |           ddd           n# 1 swxY w Y   t          j        dddd|d|d|g	           dS )zRGet Azure service principal credentials from AWS Secrets Manager and authenticate.&======================================z5Getting Azure credentials from AWS Secrets Manager...secretsmanager	us-west-2region_namez#azure-service-principal-oss-release)SecretIdSecretString	client_id	tenant_idz#azure-service-principal-certificatezazure_cert.pemwNazloginz--service-principalz
--usernamez--certificatez--tenant)r/   boto3clientget_secret_valuejsonloadstempfilemkdtempr5   pathjoinopenwrite
subprocess
check_call)
secrets_clientsecret_responsesecretrT   rU   cert_responsecerttmp_dir	cert_pathfs
             r$   azure_authenticatern      s   	
2333	
ABBB \"2LLLN %556 6  O ^,F
6"";/I
6"";/I #336 4  M (D   GW&677I	i		 	               !
	
    s   (D

DDc                     t          d           t          d           t          j        dd          } d}d}t          j                            d|           }t          j                            t          j                            |                    s1t          j        t          j                            |                     | 	                    |||           t          j
        |d	           d
S )z=Download the ssh key from the S3 bucket to the local machine.rM   zDownloading ssh key...s3rO   rP   zaws-cluster-launcher-testzray-autoscaler_59_us-west-2.pem~/.ssh/   N)r/   rY   rZ   r5   r`   
expanduserexistsdirnamemakedirsdownload_filechmod)	s3_clientbucket_namekey_namelocal_key_paths       r$   download_ssh_key_awsr}      s    	
2333	
"###T{;;;I .K0H W''(<((<(<==N7>>"'//.99:: 5
BGOON33444K>BBB H^U#####r&   c                  6   t          d           t          d           t          j                    } d}d}|                     |          }|                    |          }t
          j                            d|           }t
          j                            t
          j        	                    |                    s1t          j
        t
          j        	                    |                     |                    |           t          j        |d           dS )zGDownload the ssh key from the google cloud bucket to the local machine.rM   zDownloading ssh key from GCP...z*gcp-cluster-launcher-release-test-ssh-keyszAray-autoscaler_gcp_us-west1_anyscale-bridge-cd812d38_ubuntu_0.pemrq   rr   N)r/   r   Client
get_bucketget_blobr5   r`   rs   rt   ru   rv   download_to_filenamerx   )rZ   rz   r{   bucketblobr|   s         r$   download_ssh_key_gcpr      s    	
2333	
+,,, ^F ?KRH {++F??8$$D W''(<((<(<==N7>>"'//.99:: 5
BGOON33444n--- H^U#####r&   c           
         t          d           t          d           d}d}t          |          D ]8}	 t          j                                        }|                    dd           t          j        dddd	t          |          gd
d
|           t          |             dS # t          j
        $ r}t          d|dz    d| d           t          |j                            d                     t          j                     t          d|j                            d                      t          d|j                            d                      |}Y d}~2d}~ww xY w|)z
    Clean up the cluster using the given cluster configuration file.

    Args:
        cluster_config: The path of the cluster configuration file.
    rM   zCleaning up cluster...Nr   
PYTHONPATHr,   down-v-yTcheckcapture_outputenvzray down fails[r   /z]: utf-8stdout:
stderr:
)r/   ranger5   environcopypoprd   runr   cleanup_security_groupsCalledProcessErroroutputdecode	traceback	print_excstdoutstderr)r?   r   
last_error	num_triesir   es          r$   cleanup_clusterr      s    

2333	
"### JI9  	*//##CGGL$'''NdC,?,?@#	    $K000FF, 	 	 	8AaC88)888999!(//'**+++ !!! 8ahoog66889998ahoog6688999JJJJJJ	 s   A-B&&E)5B)E$$E)c                 `   d}|dk     r	 |                      |           d S # t          j        j        $ rm}|j        d         d         dk    r4d|z  }t          d| d	|            t          j        |           |d
z  }nt          d|            Y d }~d S Y d }~nd }~ww xY w|dk     d S d S )Nr   
   )GroupIdErrorCodeDependencyViolation   zWaiting zGs for the instance to be terminated before deleting the security group r   zError deleting security group: )delete_security_groupbotocore
exceptionsClientErrorresponser/   timesleep)
ec2_clientidretryr   
sleep_times        r$   cleanup_security_groupr     s    E
"**	,,R,888F". 
	 
	 
	z'"6*.CCCX
vzvvrtvv   
:&&&
;;;<<< 
		 "******s   " B#ABB#c                     t          d           t          d           t          j                            d          } t          j                            d          }t          j                            |           r0t          j                            |          rt          d           dS t          d           t          j                            |           }t          j                            |          st          j        |d	           	 t          j        d
ddddd| ddddgdd           t          d           dS # t          j	        $ r}t          d           t          d|j
                            d                      t          d|j                            d                      t          j        d           Y d}~dS d}~ww xY w)zX
    Ensure that the SSH keys for Azure tests exist, and create them if they don't.
    rM   z Ensuring Azure SSH keys exist...z#~/.ssh/ray-autoscaler-tests-ssh-keyz'~/.ssh/ray-autoscaler-tests-ssh-key.pubzAzure SSH keys already exist.Nz.Azure SSH keys not found. Creating new keys...T)exist_okz
ssh-keygenz-trsaz-b4096z-fz-Nr   z-Czray-autoscaler-azure)r   r   z$Successfully created Azure SSH keys.zError creating SSH keys:r   r   r   r   )r/   r5   r`   rs   rt   ru   rv   rd   r   r   r   r   r   r0   r1   )private_key_pathpublic_key_pathssh_dirr   s       r$   ensure_ssh_keys_azurer   /  s    

2333	
,---w))*OPPg(()RSSO	w~~&'' BGNN?,K,K -...	
:;;;goo.//G7>>'"" ,
Gd++++ & 	
 	
 	
 	
" 	455555(   ()))4!(//'22445554!(//'2244555	s   1E GA7GGc                    |                      di                                d          }|dk    rd S 	 t          j        dd          }|                    dt          gdd	| d
         gdg          }|d         D ]}t          ||d                    d S # t          $ r}t          d|            Y d }~d S d }~ww xY w)Nproviderr   awsec2rO   rP   ztag-key)NameValuesztag:ray-cluster-namecluster_name)FiltersSecurityGroupsr   z#Error cleaning up security groups: )r=   rY   rZ   describe_security_groupsr   r   	Exceptionr/   )configprovider_typer   r   security_groupr   s         r$   r   r   [  s   JJz2..226::M9\%[AAA
66 &"e 
 3%n56 	 7 
 
 ''78 	J 	JN":~i/HIIII	J 	J 9 9 97A778888888889s   A"B 
B>!B99B>r   c                    |                      di                                d          }|dk    rt                       t          d           t          d           g d}|r|                    d           |                    t	          |                     t
          j                                        }|                    dd	           	 t          j
        |d
d
|           n# t          j        $ ro}t          |j                   t          d|j                            d                      t          d|j                            d                      |d	}~ww xY wt          d           t          d           d}	d}
|
|k     r	 dddt	          |          dd| dz   g}|r|                    d           t          j
        |d
|           d
}	nJ# t          j        $ r2 |
dz  }
t          d|
 d| d           t!          j        d           Y nw xY w|
|k     |	sdt          d           t          d| d           t%          | |           t          d           t          d            t'          j        d           t          d           t          d!           t%          | |           t          d           t          d"           d	S )#ak  
    Run the necessary Ray commands to start a cluster, verify Ray is running, and clean
    up the cluster.

    Args:
        cluster_config: The path of the cluster configuration file.
        retries: The number of retries for the verification step.
        no_config_cache: Whether to pass the --no-config-cache flag to the ray CLI
            commands.
    r   r   azurerM   zStarting new cluster...)r,   upr   r   r   r   NTr   r   r   r   zVerifying Ray is running...Fr   r,   execr   z2python -c 'import ray; ray.init("localhost:6379");z assert len(ray.nodes()) >= ')r   r   r   z#Verification failed. Retry attempt z of z...<   z!Error: Verification failed after z0 attempts. Cleaning up cluster before exiting...zExiting script.zRay verification successful.z'Finished executing script successfully.)r=   rn   r/   r>   r   r5   r   r   r   rd   r   r   r   r   r   r   r   r   r   r0   r1   )r?   r   r   r    r!   r   cmdr   r   successcounts              r$   run_ray_commandsr   t  s:   "  OOJ3377??M	
2333	
#$$$
#
#
#C (

&'''JJs>""###
*//

CGGL$s$tEEEEE(   ah4!(//'22445554!(//'2244555 

2333	
'(((GE
'//	N##IJ5GJJJK	C  0

.///N3d4444G, 	 	 	QJEOOO7OOOPPPJrNNNNN	# '//,  	6777       	
 	
 	
 	^4446777   	
2333	
()))K000	
2333	
344444s,   C' 'E%6A*E  E%AG >HH__main__z"Using cluster configuration file: z4Number of retries for 'verify ray is running' step: zUsing --no-config-cache flag: z6Number of expected nodes for 'verify ray is running': r   -rM   zOverriding ray wheel...: zOverriding docker image...: zUsing docker image: r   r   Fcache_stopped_nodesr   r   availability_zonegcp)gcloudr   	get-valueaccountT)r   r   r   zActive account email:)r   r   r   projectzInjecting GCP project 'z$' into cluster configuration file...
project_idvspherezVSPHERE provider detected.z-Provider type not recognized. Exiting script.z.yaml)suffix)r   )A__doc__r   r\   r5   r*   rd   r0   r^   r   r   pathlibr   rY   r   yamlgoogle.cloudr   r,   "ray.autoscaler._private.aws.configr   r%   r2   r9   rA   rK   rn   r}   r   r   r   r   r   r   __name__r   r   r    r!   r   r   r/   	safe_load	read_textr?   r   r   docker_override_imager=   r   r   r   PIPEr   r   stripaccount_emailr   r1   NamedTemporaryFiletemprc   dumpencodeflushname r&   r$   <module>r      s#      				 				     



                            



 2 2 2 2 2 24 4 4n  6  3 3 3* * ** * *Z$ $ $*$ $ $4* * *Z  &) ) )X9 9 9< Q5 Q5 Q5 Q5h z 	T.))NJ~	E
?~
?
?@@@	E
J
J
JKKK	E
<?
<
<===	E
WCU
W
WXXX $.!9!9!;!;<<K 	N#c)CCIDIKK0@0@,A,AA  
E
2333	E
6n
6
6777 9K888	E
2333	E
:
:
:;;;,,_==	E
8!6
8
8999 Bk+@AAAOOJ3377??M5:K
12	%		J##$7>>>>	%		 JN<<<!  
 FF7OOUWW 	 	%}555 JN<<<!  
 FF7OOUWW 	 	VjVVV	
 	
 	
 1;J--	)	#	#6777*++++6777=>>> 
%	$G	4	4	4 


949[))0099:::

di/CU	
 	
 	
	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
] s   A*NNN