o
    biX                     @   sz   d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZ e eZG dd de
ZdS )	    N)AnyDict)ClusterOperatorClient)bootstrap_vsphere)NodeProvider)STATUS_SETTING_UPTAG_RAY_CLUSTER_NAMETAG_RAY_NODE_NAMETAG_RAY_NODE_STATUSc                   @   s   e Zd ZdZdZdd Zedd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdeeef fddZdd Zdd ZdefddZdS )VsphereWcpNodeProvideri  Nc                 C   s2   t | || i | _t | _t||tj| _	d S N)
r   __init__	tag_cache	threadingLocktag_cache_lockr   r   cluster_configclient)selfprovider_configcluster_name r   a/home/ubuntu/.local/lib/python3.10/site-packages/ray/autoscaler/_private/vsphere/node_provider.pyr      s   

zVsphereWcpNodeProvider.__init__c                 C   s   t | }|t_|S r   )r   r   r   )r   configr   r   r   bootstrap_config    s   z'VsphereWcpNodeProvider.bootstrap_configc                 C   s   | j |\}}| j3 |D ](}||  D ]\}}|| j v r)|| j| |< qi | j|< || j| |< qqW d    n1 sAw   Y  td| j  |S )NzNon terminated nodes' tags are )r   list_vmsr   itemsr   keysloggerinfo)r   tag_filtersnodesr   node_idkvr   r   r   non_terminated_nodes&   s   
z+VsphereWcpNodeProvider.non_terminated_nodesc                 C      | j |S r   )r   is_vm_power_onr   r"   r   r   r   
is_running3      z!VsphereWcpNodeProvider.is_runningc                 C   s   | j |rdS | j | S )NF)r   r'   is_vm_creatingr(   r   r   r   is_terminated6   s   z$VsphereWcpNodeProvider.is_terminatedc                 C   s4   | j  | j| W  d    S 1 sw   Y  d S r   )r   r   r(   r   r   r   	node_tags?   s   $z VsphereWcpNodeProvider.node_tagsc                 C   r&   r   r   get_vm_external_ipr(   r   r   r   external_ipC   r*   z"VsphereWcpNodeProvider.external_ipc                 C   r&   r   r.   r(   r   r   r   internal_ipF   s   z"VsphereWcpNodeProvider.internal_ipc                 C   sh   | j ' | D ]\}}|| j| |< qtd| d| j|   W d    d S 1 s-w   Y  d S )NzUpdated tags for z to: )r   r   r   r   r   )r   r"   tagsr#   r$   r   r   r   set_node_tagsK   s
   "z$VsphereWcpNodeProvider.set_node_tagsreturnc              	   C   s   |}i }|dkr| j |||}| j; | D ]-}| | j|< t| j| t< || j| t< | j	| j| t
< td| d| j|   qW d   |S 1 sQw   Y  |S )zuCreates instances.

        Returns dict mapping instance id to VM object for the created
        instances.
        r   zNode z created with tags: N)r   create_nodesr   r   copyr   r   r
   r	   r   r   r   r   )r   node_configr2   countto_be_launched_node_countcreated_nodes_dictr"   r   r   r   create_nodeT   s(   
		z"VsphereWcpNodeProvider.create_nodec                 C   sr   |r| j |r
d S | j | | j || jv r'| j| W d    d S W d    d S 1 s2w   Y  d S r   )r   r+   delete_noder   r   popr(   r   r   r   terminate_nodem   s   
"z%VsphereWcpNodeProvider.terminate_nodec                 C   s    |sd S |D ]}|  | qd S r   )r>   )r   node_idsr"   r   r   r   terminate_nodesv   s
   z&VsphereWcpNodeProvider.terminate_nodesc                 C   s
   | j  S r   )r   safe_to_scale)r   r   r   r   rA   |   s   
z$VsphereWcpNodeProvider.safe_to_scale)__name__
__module____qualname__max_terminate_nodesr   r   staticmethodr   r%   r)   r,   r-   r0   r1   r3   r   strr   r;   r>   r@   boolrA   r   r   r   r   r      s"    
			r   )loggingr   typingr   r   7ray.autoscaler._private.vsphere.cluster_operator_clientr   &ray.autoscaler._private.vsphere.configr   ray.autoscaler.node_providerr   ray.autoscaler.tagsr   r   r	   r
   	getLoggerrB   r   r   r   r   r   r   <module>   s    
