
    ci                          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	 d dlm
Z
 ddlmZ dd	lmZ dd
lmZ ej                   Z G d de
j$                        Z G d de
j(                        Zy)    N)Dict)Optional)
aiplatform)initializer)client_builder   )VertexRayTemplate)_validation_utils)_gapic_utilsc                   X     e Zd ZdZdedeeef   dej                  ddf fdZd Z	 xZ
S )	_VertexRayClientContextzCustom ClientContext.persistent_resource_idray_head_urisray_client_contextreturnNc           	      h   |j                  d      }|t        d|d      t        j                  dv rHt        |   ||j                  |j                  |j                  |j                  |j                         nt        j                  dk(  rSt        |   ||j                  |j                  |j                  |j                  |j                  |j                         n t        dt        j                   d	d
z         || _        t        t              | _        |j                  d      | _        y )NRAY_DASHBOARD_URIzRay Cluster z$ failed to start Head node properly.)z2.47.1z2.42.0z2.33.0)dashboard_urlpython_versionray_version
ray_commit_num_clients_context_to_restorez2.9.3)r   r   r   r   protocol_versionr   r   z([Ray on Vertex AI]: Unsupported version .z5Only 2.47.1, 2.42.0, 2.33.0, and 2.9.3 are supported.#RAY_HEAD_NODE_INTERACTIVE_SHELL_URI)get
ValueErrorray__version__super__init__r   r   r   r   r   r   ImportErrorr   strVERTEX_SDK_VERSIONvertex_sdk_version	shell_uri)selfr   r   r   dashboard_uri	__class__s        @/tmp/pip-target-z3e9_cxr/lib/python/vertex_ray/client_builder.pyr"   z _VertexRayClientContext.__init__%   s0    &))*=> &6 
 ??<<G+1@@.::-88/<<$6$J$J   __'G+1@@.::-88!3!D!D/<<$6$J$J   :3??:K1MIJ  '=#"%&8"9&**+PQ    c                    d }| j                   %t        d      j                  | j                         }t        d      j                  | j                  | j                  | j
                  | j                  | j                  |      S )Nzcontext_shellurirow.html.j2)r'   zcontext_table.html.j2)r   r   r&   r   r   shell_uri_row)r'   r	   renderr   r   r&   r   r   )r(   r.   s     r+   _context_table_templatez/_VertexRayClientContext._context_table_templateN   s    >>%-.KLSS.. T M !!89@@..((#66,,#'#>#>' A 
 	
r,   )__name__
__module____qualname____doc__r$   r   r   ClientContextr"   r0   __classcell__r*   s   @r+   r   r   "   sJ    'R #'R CH~'R +88	'R
 
'RR
r,   r   c                   B     e Zd ZdZdee   ddf fdZdef fdZ xZ	S )VertexRayClientBuilderzAClass to initialize a Ray client with vertex on ray capabilities.addressr   Nc           	         t        j                  |      }t        j                  |       d | _        d | _        || _        t        j                  d       || _        t        j                  | j                        | _        | j                  j                  j                  j                  d      }| j                  j                  j                  j                  d      }| j                  j                  j                   j"                  }||}|rt%        d|d      |}|+| j                  j'                  d      d   }t%        d|d      t        j(                  d	|       t        j*                  | j                  
      }|t%        d      t        j,                         }|j.                  |k7  r|j0                  j2                  St         j4                  j                  |j.                        }t        j                  d|d|j.                  d|d       nt        j                  d|z         t6        	| q  |       y )NzU[Ray on Vertex AI]: Using cluster resource name to access head address with GAPIC APIRAY_HEAD_NODE_INTERNAL_IPRAY_CLIENT_ENDPOINTz [Ray on Vertex AI]: Ray Cluster z failed to start Head node properly because custom service account isn't supported in peered VPC network. Use public endpoint instead (createa a cluster without specifying VPC network)./   zW Head node is not reachable. Please ensure that a valid VPC network has been specified.z-[Ray on Vertex AI]: Resolved head node ip: %s)persistent_resourcez[Ray on Vertex AI]: Please delete and recreate the cluster (The cluster is not a Ray cluster or the cluster image is outdated).z/[Ray on Vertex]: Local runtime has Ray version z(, but the requested cluster runtime has z. Please ensure that the Ray versions match for client connectivity. You may "pip install --user --force-reinstall ray[default]==z0" and restart runtime before cluster connection.zt[Ray on Vertex]: Local runtime has Ray version %s.Please ensure that the Ray versions match for client connectivity.)r
   maybe_reconstruct_resource_namevalid_resource_name_credentials	_metadatavertex_addresslogginginforesource_namer   get_persistent_resourceresponseresource_runtimeaccess_urisr   resource_runtime_specservice_account_specservice_accountr   splitdebugpersistent_resource_to_clusterget_local_ray_versionr   head_node_typecustom_imageSUPPORTED_RAY_VERSIONSr!   r"   )
r(   r:   private_addresspublic_addressrO   r   clusterlocal_ray_verioninstall_ray_versionr*   s
            r+   r"   zVertexRayClientBuilder.__init__b   s   #CCGL--g6 %c	
 %$<<T=O=OP--88DDHH'
 77CCGG!
 MM//DDTT 	 !%G 6%  %G?%)%7%7%=%=c%B1%E"2&i  	EwO== $
 ? R  -BBD"22%%22:&7&N&N&R&R'''#  ()<)<>QS Y&'
 	!r,   c                    t        j                  d       | j                  j                  j                  j                  d      }| j                  j                  j                  j                  d      }|rr|spt        j                         | _        t        j                         }ddj                  |      fddj                  t        j                  j                        fg| _        t         | E         }| j                  j                  j                  }| j$                  j'                  d      d	   }t)        |||
      S )Nz![Ray on Vertex AI]: Connecting...r=   r<   authorizationz	Bearer {}zx-goog-user-projectz{}r>   r?   )r   r   r   )rF   rG   rJ   rK   rL   r   grpcssl_channel_credentialsrC   r
   get_bearer_tokenformatr   global_configprojectrD   r!   connectrH   rP   r   )r(   rX   rW   bearer_tokenr   r   r   r*   s          r+   rd   zVertexRayClientBuilder.connect   s   8977CCGG!
 --88DDHH'
 / $ < < >D,==?L +"4"4\"BC&K4M4M4U4U(VWDN #W_.66BB "&!3!3!9!9#!>q!A&#9'1
 	
r,   )
r1   r2   r3   r4   r   r$   r"   r   rd   r6   r7   s   @r+   r9   r9   _   s3    KI" I"$ I"V
0 
 
r,   r9   )r^   rF   r   typingr   r   google.cloudr   google.cloud.aiplatformr   r   r/   r	   utilr
   r   r    r%   r5   r   ClientBuilderr9    r,   r+   <module>rl      s]   "   
   # /  % #   ++ :
n:: :
zj
^99 j
r,   