
    &`i                     l   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	m
Z
 d dlmZmZmZmZmZmZ d dlZd dlZd dlmZ d dlmZ d dlmZmZmZ d dlmZmZ d d	l m!Z!m"Z" d d
l#m$Z$ d dl%m&Z& d dl'm(Z(m)Z)  ej*        e+          Z,dZ-e-dz   Z.e-dz   Z/e-dz   Z0dZ1dddddddddddddddd Z2 eej3                   ed!          k    s
J d"            d# Z4 ej*        d$          5                    ej6                   i Z7d% Z8d& Z9d' Z:d( Z;d)ee<ef         d*dfd+Z=d, Z>d- Z?d. Z@d/e<d*e<fd0ZAd1eee                  d2ee         d3ee<         d4ee<         d5eBd6e<d*eee<         e<f         fd7ZCd8 ZDd9ee<         d)ee<ef         d*e<fd:ZEd; ZFd< ZGd= ZHd> ZId?e<fd@ZJ e	            dAee<         fdB            ZKdC ZLdD ZMdE ZNdF ZOdG ZPdYdHZQdI ZRdJ ZSdK ZTdL ZUdM ZVd)ee<ef         d*ee<ef         fdNZWd)ee<ef         d/ee<ef         d*ee<ef         fdOZXd)ee<ef         dPee<ef         d*ee<ef         fdQZYd)ee<ef         d*ee<ef         fdRZZd/ee<ef         d*ee<ef         fdSZ[dPee<ef         d*ee<ef         fdTZ\d)ee<ef         d*ee<         fdUZ]d)ee<ef         d*eee<                  fdVZ^dW Z_dX Z`dS )Z    N)Counter)	lru_cachepartial)AnyDictListOptionalSetTuple)Version)CloudwatchHelper)LazyDefaultDicthandle_boto_errorresource_cache)cf
cli_logger)CreateClusterEventglobal_event_system)_PROVIDER_PRETTY_NAMES)check_legacy_fields)NODE_TYPE_LEGACY_HEADNODE_TYPE_LEGACY_WORKERzray-autoscalerz-v1z-{}z*AWS Deep Learning AMI (Ubuntu 18.04) V71.0zami-0271ce88f6c03e149zami-0ce21b0ce8e0f5a37zami-030896954b2b72361zami-0a2b85e15b7c0ac34zami-044b98bea12677052zami-004d285ecf53fb335zami-0e17ed40febe794a6zami-0ad9e6b9d4e22df1azami-05698775025146698zami-004b19c7ed8d790bczami-03e45b1f18378ea97zami-095bae3e623e2ff99zami-02fd0ba69c1ef37adzami-03ef1223872072e95zami-0bcf4f75e44e0a866)z	us-east-1z	us-east-2z	us-west-1z	us-west-2zca-central-1zeu-central-1z	eu-west-1z	eu-west-2z	eu-west-3z	sa-east-1zap-northeast-1zap-northeast-2zap-northeast-3zap-southeast-1zap-southeast-2z1.4.8z;Boto3 version >= 1.4.8 required, try `pip install -U boto3`c                    | dk    rS|d                     t          |          n|}|t          j                            d                     |                    fS |d                     t          | |          n|d                     |           z   }|t          j                            d                     |                    fS )z
    If key_name is not None, key_pair will be named after key_name.
    Returns the ith default (aws_key_pair_name, key_pair_path).
    r   Nz{}_{}z~/.ssh/{}.pemz{}_{}_{}z_key-{})formatRAYospath
expanduser)iregionkey_namekey_pair_names       v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/_private/aws/config.pykey_pairr$   L   s    
 	Avv7?7GsF333XG55mDDEE
 	
  	#q&)))	((+++ 
 27--o.D.D].S.STTUU    botocorec                 :    t                               |            d S N)	_log_infoupdate)override_log_infos    r#   reload_log_stater,   f   s    &'''''r%   c                  4    t                                           S r(   )r)   copy r%   r#   get_log_stater0   j   s    >>r%   c                  N    |                                  D ]\  }}|t          |<   dS )z2Record configuration artifacts useful for logging.N)itemsr)   )kwargskvs      r#   _set_config_infor6   n   s4    
   1	! r%   c                 j    |                      d          d                              d          d         S )N:/)split)arns    r#   _arn_to_namer=   w   s*    99S>>"##C((,,r%   configreturnc                     t          j        dd           }t          j        |d ud            d          d                  d         t          j        d|          5  	 	 d)dt
          d	t
          d
t
          dt          t          t
                            dt          dd f fd}dt          d         dk    i}dv sd d         v sJ dv r	d         }nd d         v r d         d         }|                    d          }|rt          |          n|d         }t          j        dd||           t          d  d                                         D                       r |ddd            |dddd             |d!d"d#d             |d$d%d&d'g(           d d d            n# 1 swxY w Y   t          j                     d S )*Nawsz2Could not find a pretty name for the AWS provider.head_node_typeavailable_node_typesnode_configz	{} configFresource_stringkeysrc_keyallowed_tags
list_valuer?   c                 H   |dg}i }t                      }d                                         D ]\  }}i ||<   t          |         |         }	|	|v rd||         |	<   |d                             |          }
|r#|                    t          |
                     l|                    |
           |         }|rt          j        |          }t          |          dk    r)t          j	        | dz   d||d                  	           d S t          j	        | d
 dz   d||         	           d                                         D ]U\  }}|k    r|d         |         }|rt          j        |          }t          j	        | d
| dz   d|||         	           Vd S )NdefaultrC   TrD      z (all available node types){}rB   _tagsz ())
setr2   r)   getaddtupler   render_listlenlabeled_value)rE   rF   rG   rH   rI   	node_tagsunique_settingsnode_type_key	node_typetagsettinghead_value_strworkers_value_strr>   head_node_configrB   s                r#   
print_infozlog_to_cli.<locals>.print_info   s2    # ){I "eeO,23I,J,P,P,R,R 
1 
1(y+-	-((7,&&48Im,S1#M266s;; 1#''g7777#''0000-c2N H!+!7!G!G?##q(( (#&CC"#F+;$<=	      (#&<>&<&<&<<"#N3	    177M0N0T0T0V0V  ,M9$66 (1-(@(E%! V,6,BCT,U,U),'*?}*?*?*??)'6	     r%   rK   head_instance_profile_srcIamInstanceProfile	head_nodeArnNamezIAM ProfilerM   rN   c              3   *   K   | ]}d |d         v V  dS )KeyNamerD   Nr/   ).0r[   s     r#   	<genexpr>zlog_to_cli.<locals>.<genexpr>   s@       
 
 =11
 
 
 
 
 
r%   zEC2 Key pairrh   keypair_srczVPC Subnets	SubnetIds
subnet_srcT)rI   zEC2 Security groupsSecurityGroupIdssecurity_group_srczEC2 AMIImageIdami_srcdlami)rH   )NF)r   rR   r   doassertgroupstrr	   r   boolr)   r=   rW   allvaluesnewline)	r>   provider_namera   tags
IamProfileprofile_arnprofile_namer`   rB   s	   `      @@r#   
log_to_clir   {   s   *.ud;;MT!#W   ,-N45nEmT		+}	5	5 `L `L 15$=	 =	 =	=	 =	 #49-	=	
 =	 =	 =	 =	 =	 =	 =	 =	 =	~ 9%@AYNO
 !$444#vk'::::;  #333)*>?JJ!VK%888,-ABJ nnU++4?W|K000ZPVEW l$OOOO 
 
#$:;BBDD
 
 
 
 
 	A J~y-@@@
=+|MMMM
! 		
 	
 	
 	
 	
9i'KKKKA`L `L `L `L `L `L `L `L `L `L `L `L `L `L `LD s   !EF..F25F2c                    t          j        |           } t          |            i | d<   t          |           } t	          |           } t          |           } t          |           } t          j        t          j
        d| d         d         i           t          |           } t          |           } t          |            | S )Nrd   ssh_key_pathauthssh_private_key)r.   deepcopyr   _configure_from_launch_template"_configure_from_network_interfaces_configure_iam_role_configure_key_pairr   execute_callbackr   ssh_keypair_downloaded_configure_subnet_configure_security_group
_check_amir>   s    r#   bootstrap_awsr      s    ]6""F F; -V44F 077F !((F !((F(1	(9:;   v&&F 'v..F vMr%   c                    | d         }| d         |         d         }d|v rt          d           | S t          d           t          j        | d         t                    }t	          ||           }|qt          j        d	t          j        |                     t          d
|           }|
                    |           t	          ||           }t          j        d           t          j        |d ud           |
J d            |j        s8t          j        | d         t                     }t#          ||           }|t          j        dt          j        |                     t%          d
|           }ddddiddgi}	t          j        | d         |ddg          }
|                    |t+          j        |	                     t#          ||           }t          j        |d ud           |
J d            |
D ]}|                    |           |                    |j                   t          j        d           d|j        i| d         d<   | S )NrB   rC   rD   rc   r>   )rb   rK   providerz<Creating new IAM instance profile {} for use as the default.iam)InstanceProfileName   z"Failed to create instance profile.z!Failed to create instance profilez>Creating new IAM role {} for use as the default instance role.	StatementAllowServicezec2.amazonaws.comzsts:AssumeRole)Effect	PrincipalActionz+arn:aws:iam::aws:policy/AmazonEC2FullAccessz*arn:aws:iam::aws:policy/AmazonS3FullAccess)RoleNameAssumeRolePolicyDocumentzFailed to create role.zFailed to create role)	PolicyArn)r   re   rd   )r6   cwhresolve_instance_profile_nameDEFAULT_RAY_INSTANCE_PROFILE_get_instance_profiler   verboser   bold_clientcreate_instance_profiletimesleeprs   rolesresolve_iam_role_nameDEFAULT_RAY_IAM_ROLE	_get_role	_resourceresolve_policy_arnscreate_rolejsondumpsattach_policyadd_rolenamer<   )r>   rB   r`   instance_profile_nameprofileclient	role_nameroler   
policy_docattach_policy_arns
policy_arns               r#   r   r     s   ,-N45nEmT///8<<<<y9999=z$  $$96BBGJG)**	
 	
 	
 ''&&;P&QQQ'(=vFF
2tA    C= )-fZ.@BVWW	F++<P	""   E6**C")&/1D%E"2 J "%!8z"A@" " OO"TZ
=S=S     Y//DD ":   ##%<###0 9 9
""Z"8888$),,,
2 270DF;,-Mr%   c           	         | d         }i }t          |           d| d         v rM|D ]}d||<   |D ]>}||         d         }d|vr*t          j        d|v t          |                     d|v sJ ?| S |D ]}d	||<   t	          d
|           }t          j        t
          j                            d          d           d}t          |          D ]B}| d         
                    di           
                    d          }	t          || d         d         |	          \  }	}
t          |	|           }|r!t
          j                            |
          r n|st
          j                            |
          st          j        dt          j        |	                     |                    |	          }t%          |
dt'          t
          j        d                    5 }|                    |j                   d d d            n# 1 swxY w Y    nD|st          j        d|	           t          j        t
          j                            |
          dt          j        d          z   dz   t          j        d          z   |
|	           t
          j                            |
          sJ d                    |
|	                      |
| d         d<   |                                D ]}|d         }|	|d<   | S )NrC   )rk   r   r   r>   rD   UserDatarh   rK   ec2z~/.sshT)exist_okiX  r   r$   r!   r    z0Creating new key pair {} for use as the default.)rh   wi  )mode)openerzNo matching local key file for any of the key pairs in this account with ids from 0..{}. Consider deleting some unused keys pairs from your account.zPrivate key file rM   z not found for z$Private key file {} not found for {})r6   r   rs   _key_assert_msgr   r   makedirsr   r   rangerR   r$   _get_keyexistsr   r   r   create_key_pairopenr   writekey_materialabortr   rx   )r>   
node_typeskey_pair_src_inforZ   r[   rD   r   MAX_NUM_KEYSr   r!   key_pathrF   fs                r#   r   r   b  s   ./J !23333F6N**' 	8 	8M/7m,, $ 	0 	0I$Y/>K,,#,oi.H.H   !K////# 5 5+4-((
E6
"
"C K""8,,t<<<< L<    *%))*b99==jII%a
);H)ExPP(x((  	27>>(++ 	E  	27>>(33 	BBGHDUDU   %%h%77C
 hGBG%,H,H,HIII *Q()))* * * * * * * * * * * * * * *E 
J 		
 	
 	
 
x  bgdmm+.??"'$--O	   7>>(##  %K%R%R(& &  # )1F6N$%&&(( * *	.!)IMs   HH		H		r[   c                 B    | t           k    rdS | t          k    rdS d|  dS )Nz#`KeyName` missing for worker nodes.z `KeyName` missing for head node.z7`KeyName` missing from the `node_config` of node type `z`.)r   r   )r[   s    r#   r   r     s<    +++44	+	+	+11 WVVV	
r%   user_specified_subnetsall_subnetsazsvpc_id_of_sguse_internal_ipsrZ   c                     dt           t                   dt          f fd}dt           t                   dt          t                   f fd}	   n|}rfd|D             }t          fd|D             dd	 
          n.# t          j        j        $ r}	t          |	d           |	d}	~	ww xY wst          j        d| d           n. |          r#t          j        d| d |                      |d |                    d          D             }fd|D             st          j        d| d| d           n? |          r4t          j        d| d| dt           |                     d           d         j        }
fdD              |          r%d  D             }t          j        d| d|           |
fS )a  Prunes subnets down to those that meet the following criteria.

    Subnets must be:
    * 'Available' according to AWS.
    * Public, unless `use_internal_ips` is specified.
    * In one of the AZs, if AZs are provided.
    * In the given VPC, if a VPC is specified for Security Groups.

    Returns:
        List[str]: Subnets that are usable.
        str: VPC ID of the first subnet.
    current_subnetsr?   c                 L    d uot          |           t                    k    S r(   )rV   )r   r   s    r#   _are_user_subnets_prunedz4_usable_subnet_ids.<locals>._are_user_subnets_pruned  s5    %T1 
c/6J6Jc"O
 O
 7
 	
r%   c                 >    d | D             }d D             }||z
  S )Nc                     h | ]	}|j         
S r/   	subnet_idri   ss     r#   	<setcomp>zB_usable_subnet_ids.<locals>._get_pruned_subnets.<locals>.<setcomp>  s    CCCaakCCCr%   c                     h | ]	}|j         
S r/   r   r   s     r#   r   zB_usable_subnet_ids.<locals>._get_pruned_subnets.<locals>.<setcomp>  s    $Q$Q$QQQ[$Q$Q$Qr%   r/   )r   current_subnet_idsuser_specified_subnet_idsr   s      r#   _get_pruned_subnetsz/_usable_subnet_ids.<locals>._get_pruned_subnets  s8    CC?CCC$Q$Q:P$Q$Q$Q!(+===r%   Nc                 *    g | ]}|j         k    |S r/   vpc_id)ri   r   r   s     r#   
<listcomp>z&_usable_subnet_ids.<locals>.<listcomp>  s,     ! ! !L0H0H0H0H0Hr%   c              3   D   K   | ]}|j         d k    s|j        |V  dS )	availableN)statemap_public_ip_on_launch)ri   r   r   s     r#   rj   z%_usable_subnet_ids.<locals>.<genexpr>  sO        7k))% *)*)B * )))) r%   Tc                     | j         S r(   availability_zone)subnets    r#   <lambda>z$_usable_subnet_ids.<locals>.<lambda>  s	    v7 r%   )reverserF   z+Failed to fetch available subnets from AWS.z&No usable subnets found for node type z, try manually creating an instance in your specified region to populate the list of subnets and trying this again.
Note that the subnet must map public IPs on instance launch unless you set `use_internal_ips: true` in the `provider` config.z$The specified subnets for node type z are not usable: c                 6    g | ]}|                                 S r/   )strip)ri   azs     r#   r   z&_usable_subnet_ids.<locals>.<listcomp>  s     333brxxzz333r%   ,c                 4    g | ]}D ]}|j         |k    |S r/   r   )ri   r   r   subnetss      r#   r   z&_usable_subnet_ids.<locals>.<listcomp>  sH     
 
 

 
 "b((  )(((r%   z-No usable subnets matching availability zone z found for node type z.
Choose a different availability zone or try manually creating an instance in your specified region to populate the list of subnets and trying this again.zMISMATCH between specified subnets and Availability Zones! The following Availability Zones were specified in the `provider section`: z(.
 The following subnets for node type `z&` have no matching availability zone: .r   c                 J    g | ]}|j         d          j         k    |j         S )r   )r   r   )ri   r   r   s     r#   r   z&_usable_subnet_ids.<locals>.<listcomp>+  s.    MMMqqx71:;L/L/Lq{/L/L/Lr%   c                 (    i | ]}|j         |j        S r/   )r   r   r   s     r#   
<dictcomp>z&_usable_subnet_ids.<locals>.<dictcomp>-  s    MMMq{AHMMMr%   z6Subnets specified in more than one VPC for node type `z\`! Please ensure that all subnets share the same VPC and retry your request. Subnet VPCs: {})r   r   rv   r
   ru   sortedr&   
exceptionsClientErrorr   r   r   r;   listr   )r   r   r   r   r   rZ   r   r   candidate_subnetsexcfirst_subnet_vpc_idsubnet_vpcsr   s   `  ``       @r#   _usable_subnet_idsr    s`   *
$s) 
 
 
 
 
 
 

>T#Y >3s8 > > > > > >
 &1 #" 	
  	! ! ! !,! ! !    *   77	
 	
 	
 *   #LMMM	  
%] % % %	
 	
 	
 	
 
"	!'	*	* 
6= 6 6**7336 6	
 	
 	

 33CIIcNN333
 
 
 

 
 
  	% % %!.% % %    &%g.. 	9'*9 9 '9 9 ++G4455	9 9 9   "!*+MMMMGMMMG(( 
MM6LMMM'] ' ' ' 		
 	
 	
 '''s   5B B0B++B0c                    t          d|           }g }| d                                         D ]3}|d         }|                    |                    dg                      4|rt	          ||           }nd }i }t          |           t          |j                                                  }g }g }	| d         	                                D ]>\  }
}d|d         v r|
                    |
|f           '|	
                    |
|f           ?d }|D ]\  }
}|d         }t          |t          |d                             }t          ||| d                             d          || d                             d	d
          |
          \  }}d||
<   |r|s|}|	D ]b\  }
}|d         }t          d || d                             d          || d                             d	d
          |
          \  }}d||
<   ||d<   c| S )Nr   rC   rD   rn   )rm   rl   r   r   r   F)r   r   r   rZ   r>   rK   )r   rx   extendrR   _get_vpc_id_of_sgr6   r  r   rw   r2   append_get_subnets_or_dierT   r  )r>   r   sg_idsr[   rD   r   subnet_src_infor   node_types_subnetsnode_types_no_subnetsrF   r   user_subnets
subnet_idss                 r#   r   r   7  s{   
E6
"
"C F23::<< ? ?	.koo&8"==>>>> (88 O0000s{(())K !78>>@@ ; ;Y)M222%%sI&67777!((#y)9::::F - ( (Y.*3k+6N0O0OPP/z"&&':;;%#J/334FNN
 
 

F  (  l  0 . .Y./z"&&':;;%#J/334FNN
 
 

F  )#-K  Mr%   r  c                    t          t          |                     } t          d|          }d| dg}|j                            |          }d |D             }t          t          |                    }d}t          j        t          |          dk    |           t          |          dk    s
J |            d}t          j        t          |          d	k    |           t          |          d	k    s
J |            |d	         S )
zReturns the VPC id of the security groups with the provided security
    group ids.

    Errors if the provided security groups belong to multiple VPCs.
    Errors if no security group with any of the provided ids is identified.
    r   zgroup-idrf   ValuesFiltersc                     g | ]	}|j         
S r/   r   ri   sgs     r#   r   z%_get_vpc_id_of_sg.<locals>.<listcomp>  s    333Rry333r%   zRAll security groups specified in the cluster config should belong to the same VPC.rL   zZFailed to detect a security group with id equal to any of the configured SecurityGroupIds.r   )	r   rQ   r   security_groupsfilterr  r   rs   rV   )r  r>   r   filtersr  vpc_idsmultiple_vpc_msg	no_sg_msgs           r#   r
  r
  w  s    CKK  F
E6
"
"C"f556G)000AAO33?333G3w<<  G	)  G)+;<<<w<<1.	+  Gq()444w<<!Y1:r%   c                    i }t          |           | d         D ]}d||<   d | d                                         D             }|s| S | d         }| d         |v r*|                    |           |                    |           t	          | |          }|D ].}| d         |         d         }||         }|j        g|d<   d||<   /| S )	N)ro   rC   r>   c                 ,    g | ]\  }}d |d         v|S )rn   rD   r/   )ri   rZ   r[   s      r#   r   z-_configure_security_group.<locals>.<listcomp>  s6       $M9Y}%=== 	===r%   rB   rD   rn   rK   )r6   r2   remover  _upsert_security_groupsid)r>   security_group_info_srcrZ   node_types_to_configurerB   r  rD   r  s           r#   r   r     s$    (?@@@@ 67 : :19.. (./E(F(L(L(N(N  
 # ,-N#::: 	 &&~666&&~666-f6MNNO0 ; ;34]CMR]++-5'&'1:..Mr%   c                    d | d         D             }t          |           | d         d         }t                              |          }| d                                         D ]a\  }}|d         }|                    dd                                          }|d	v r(|st          j        d
| d| d           W||d<   d||<   bdS )zCProvide helpful message for missing ImageId for node configuration.c                     i | ]}|d S r   r/   )ri   rF   s     r#   r   z_check_ami.<locals>.<dictcomp>  s    LLLcCLLLr%   rC   )rq   r   r    rD   rp    )r*  latest_dlamizNode type `zT` has no ImageId in its node_config and no default AMI is available for the region `z?`. ImageId will need to be set manually in your cluster config.rr   N)r6   DEFAULT_AMIrR   r2   lowerr   r   )r>   ami_src_infor    default_amirF   r[   rD   node_amis           r#   r   r     s    MLV4J-KLLLL\****J)F//&))K !78>>@@ , ,Y.??9b117799+++ , S# S SGMS S S    *5I&$+S!, ,r%   c                 F    t          | |          }t          | |           |S r(   )"_get_or_create_vpc_security_groups_upsert_security_group_rules)r>   r   r  s      r#   r$  r$    s'    8LLO 999r%   c                     t          d            fd|D             } d                             di                               dt                               d                             }d t	           |                                |g          D             }t          t          t           |          |          fd	|	                                D             S )
Nr   c                 n    i | ]1}|t          d          |         d         d         d                   2S )rC   rD   rl   r   )_get_vpc_id_or_die)ri   r[   confr   s     r#   r   z6_get_or_create_vpc_security_groups.<locals>.<dictcomp>  sY       
 	 	%'(3MB;OPQR
 
  r%   r   security_group	GroupNamecluster_namec                     i | ]
}|j         |S r/   r   r  s     r#   r   z6_get_or_create_vpc_security_groups.<locals>.<dictcomp>  s,        		2  r%   
group_namec                 (    i | ]\  }}||         S r/   r/   )ri   r[   r   	vpc_to_sgs      r#   r   z6_get_or_create_vpc_security_groups.<locals>.<dictcomp>  s1       ):F	9V$  r%   )
r   rR   SECURITY_GROUP_TEMPLATEr   _get_security_groupsrx   r   r   _create_security_groupr2   )r7  r   node_type_to_vpcexpected_sg_namevpc_to_existing_sgr   r?  s   `    @@r#   r2  r2    s&   
E4
 
 C    
 $   	Z	r	"	"	[188n9MNN	O	O  &##%%
 
    &9IJJJ I   >N>T>T>V>V   r%   r   c           	          t          | |f          }t          j        t          |          dk    d| dt          |                      |d         j        S )NrL   zExpected 1 subnet with ID `z` but found r   )r  r   rs   rV   r   )r   r   r   s      r#   r6  r6     s^    !#	|44GGKiKKS\\KK   1:r%   r  c                    t          t          |                                                    }t          | j                            dt          |          dg                    }t          j        t          |          t          |          k    d|           t          |          t          |          k    sJ d	                    |                      |S )Nz	subnet-idr  r  zNot all subnet IDs found: {}zSubnet ID not found: {})
rT   r   keysr  r   r  r   rs   rV   r   )r   r  r   s      r#   r  r  	  s     wz**//1122J[DDTDT$U$U#VWW G
 GJ')G   w<<3z??***,E,L,LZ,X,X***Nr%   c                 @    t          | |g|g          }|sd n|d         S )Nr   )rA  )r>   r   r=  r8  s       r#   _get_security_grouprJ    s+    )&6(ZLIIN%<44>!+<<r%   c                     t          t          |                    }t          |          t          d|           }t          |j                            d|dg                    }fd|D             }|S )Nr   zvpc-idr  r  c                 &    g | ]}|j         v |S r/   r<  )ri   r  unique_group_namess     r#   r   z(_get_security_groups.<locals>.<listcomp>(  s-       9K(K(K(K(K(Kr%   )r  rQ   r   r  r  )r>   r  group_namesunique_vpc_idsr   existing_groupsfiltered_groupsrM  s          @r#   rA  rA    s    #g,,''N[))
E6
"
"C""&.AAB 	# 	
 	
 O
   $  O r%   c           
         t          d|           }|                    d||dt          ddd| d         dgdg	           t          | ||          }t	          j        |d
           t	          j        dt          j        |j	                  t          |j                             t	          j        |d
           |s
J d
            |S )Nr   z+Auto-created security group for Ray workerszsecurity-grouptrue)KeyValuezray-cluster-namer:  )ResourceTypeTags)Descriptionr9  VpcIdTagSpecificationszFailed to create security groupzCreated new security group {}r%  rN   )r   create_security_groupr   rJ  r   rs   r   r   r   r=  dictr%  )r>   r   r=  r   r8  s        r#   rB  rB  .  s    UF##F
  A !1&11.9OPP 
	 !    )DDN(IJJJ'
)**n'(((   
 (IJJJ<<<<<>r%   c                 P   d |                                 D             }| d         D ]7}|                    | d         |                             dg                      8t          |                                          D ]%\  }}||         }|j        st          |||            &d S )Nc                     h | ]	}|j         
S r/   r[  r  s     r#   r   z/_upsert_security_group_rules.<locals>.<setcomp>L  s    666rRU666r%   rC   rn   )rx   r*   rR   r   r2   ip_permissions_update_inbound_rules)r7  r  sgidsr[   r  s        r#   r3  r3  K  s    66_3355666E
 01 
 
	'(3778JBOO	
 	
 	
 	
   5 5 7 788 3 3	2Y'  	3!"eT2223 3r%   c                     |d                              di                                dg           }t          ||          }|                     |           d S )Nr   r8  IpPermissions)rd  )rR   _create_default_inbound_rulesauthorize_ingress)target_security_grouprb  r>   extended_rulesr`  s        r#   ra  ra  ^  s^    z/4488"MM  35.IIN++.+IIIIIr%   c                     |g }t          |           }t                      }t          j        |||          }t	          |          S r(   )*_create_default_intracluster_inbound_rules!_create_default_ssh_inbound_rules	itertoolschainr  )rb  rh  intracluster_rules	ssh_rulesmerged_ruless        r#   re  re  f  sP    CEJJ133I? L
 r%   c                 @    dddd t          |           D             dgS )Nr9   z-1c                     g | ]}d |iS )GroupIdr/   )ri   security_group_ids     r#   r   z>_create_default_intracluster_inbound_rules.<locals>.<listcomp>y  s.     ! ! !% -.! ! !r%   )FromPortToPort
IpProtocolUserIdGroupPairs)r   )intracluster_sgidss    r#   rj  rj  s  sF     ! !)/0B)C)C! ! !	
	
 
	
 r%   c                      dddddigdgS )N   tcpCidrIpz	0.0.0.0/0)ru  rv  rw  IpRangesr/   r/   r%   r#   rk  rk    s,     "K01		
 	
 r%   c                 l   t          d|          }|                    |           }	 |                                 |S # t          j        j        $ rb}|j                            di                               d          dk    rY d }~d S t          |dt          j
        |                      |d }~ww xY w)Nr   ErrorCodeNoSuchEntityz.Failed to fetch IAM role data for {} from AWS.)r   Roleloadr&   r   r  responserR   r   r   r   )r   r>   r   r   r  s        r#   r   r     s    
E6
"
"C88ID		* 	 	 	<GR((,,V44FF44444@	""  
 I	   = B32B.	%B..B3c                 l   t          d|          }|                    |           }	 |                                 |S # t          j        j        $ rb}|j                            di                               d          dk    rY d }~d S t          |dt          j
        |                      |d }~ww xY w)Nr   r  r  r  z:Failed to fetch IAM instance profile data for {} from AWS.)r   InstanceProfiler  r&   r   r  r  rR   r   r   r   )r~   r>   r   r   r  s        r#   r   r     s    
E6
"
"C!!,//G* 	 	 	<GR((,,V44FF44444L%%  
 I	r  c                    t          d|          }	 |j                            d| gdg          D ]}|j        | k    r|c S d S # t          j        j        $ r*}t          |dt          j	        |                      |d }~ww xY w)Nr   zkey-namer  r  z)Failed to fetch EC2 key pair {} from AWS.)
r   	key_pairsr  r   r&   r   r  r   r   r   )r!   r>   r   rF   r  s        r#   r   r     s    
E6
"
"C
=''(XJ??@ ( 
 
 	 	C x8##


 $	 	
 *   <bgh>O>O	
 	
 	
 		s   0A A B%A??Bc                     t          j        |           } | d         }t          |                                          D ]\  }}t	          | |          ||<   | S )a  Merges any launch template data referenced by the node config of all
    available node type's into their parent node config. Any parameters
    specified in node config override the same parameters in the launch
    template, in compliance with the behavior of the ec2.create_instances
    API.

    Args:
        config (Dict[str, Any]): config to bootstrap

    Returns:
        config (Dict[str, Any]): The input config with all launch template
        data merged into the node config of all available node types. If no
        launch template data is found, then the config is returned
        unchanged.

    Raises:
        ValueError: When no launch template is found for the given launch template
            [name|id] and version, or more than one launch template is found.
    rC   )r.   r   r   r2   )_configure_node_type_from_launch_templater>   r   r   r[   s       r#   r   r     se    * ]6""F./J "*"2"2"4"455 X XiDVYWW
4Mr%   c                 l    t          j        |          }|d         }d|v rt          | |          |d<   |S )a  Merges any launch template data referenced by the given node type's
    node config into the parent node config. Any parameters specified in
    node config override the same parameters in the launch template.

    Args:
        config (Dict[str, Any]): config to bootstrap
        node_type (Dict[str, Any]): node type config to bootstrap

    Returns:
        node_type (Dict[str, Any]): The input config with all launch template
        data merged into the node config of the input node type. If no
        launch template data is found, then the config is returned
        unchanged.

    Raises:
        ValueError: When no launch template is found for the given launch template
            [name|id] and version, or more than one launch template is found.
    rD   LaunchTemplate)r.   r   (_configure_node_cfg_from_launch_template)r>   r[   node_cfgs      r#   r  r    sI    , i((I'H8###KH$
 $
	-  r%   r  c                    t          j        |          }t          d|           }t          j        |d                   }t          |                    dd                    }||d         d<   |r|gng |d<    |j        di |}|d         }t          |          dk    rt          dt          |                     |d         d	         d
         }|                    |           |                    |           |S )aa  Merges any launch template data referenced by the given node type's
    node config into the parent node config. Any parameters specified in
    node config override the same parameters in the launch template.

    Note that this merge is simply a bidirectional dictionary update, from
    the node config to the launch template data, and from the launch
    template data to the node config. Thus, the final result captures the
    relative complement of launch template data with respect to node config,
    and allows all subsequent config bootstrapping code paths to act as
    if the complement was explicitly specified in the user's node config. A
    deep merge of nested elements like tag specifications isn't required
    here, since the AWSNodeProvider's ec2.create_instances call will do this
    for us after it fetches the referenced launch template data.

    Args:
        config (Dict[str, Any]): config to bootstrap
        node_cfg (Dict[str, Any]): node config to bootstrap

    Returns:
        node_cfg (Dict[str, Any]): The input node config merged with all launch
        template data. If no launch template data is found, then the node
        config is returned unchanged.

    Raises:
        ValueError: When no launch template is found for the given launch template
            [name|id] and version, or more than one launch template is found.
    r   r  r   z$DefaultVersionsLaunchTemplateVersionsrL   z-Expected to find 1 launch template but found r   LaunchTemplateDatar/   )	r.   r   r   ru   pop!describe_launch_template_versionsrV   
ValueErrorr*   )r>   r  r   r3   template_versiontemplatelt_versionslt_datas           r#   r  r    s   > }X&&H
%
 
 C]8$4566F6::i<<== -=Hy)/?G*++RF:4s4>>v>>H34K
;1RK@P@PRR
 
 	
 /034HIGNN8OOGOr%   c                     t          j        |           } | d         }|                                D ]\  }}t          |          ||<   | S )a  Copies all network interface subnet and security group IDs up to their
    parent node config for each available node type.

    Args:
        config (Dict[str, Any]): config to bootstrap

    Returns:
        config (Dict[str, Any]): The input config with all network interface
        subnet and security group IDs copied into the node config of all
        available node types. If no network interfaces are found, then the
        config is returned unchanged.

    Raises:
        ValueError: If [1] subnet and security group IDs exist at both the
            node config and network interface levels, [2] any network interface
            doesn't have a subnet defined, or [3] any network interface doesn't
            have a security group defined.
    rC   )r.   r   r2   +_configure_node_type_from_network_interfacer  s       r#   r   r   8  sY    ( ]6""F./J%++-- R RiFyQQ
4Mr%   c                 j    t          j        |           } | d         }d|v rt          |          | d<   | S )a  Copies all network interface subnet and security group IDs up to the
    parent node config for the given node type.

    Args:
        node_type (Dict[str, Any]): node type config to bootstrap

    Returns:
        node_type (Dict[str, Any]): The input config with all network interface
        subnet and security group IDs copied into the node config of the
        given node type. If no network interfaces are found, then the
        config is returned unchanged.

    Raises:
        ValueError: If [1] subnet and security group IDs exist at both the
            node config and network interface levels, [2] any network interface
            doesn't have a subnet defined, or [3] any network interface doesn't
            have a security group defined.
    rD   NetworkInterfaces)r.   r   5_configure_subnets_and_groups_from_network_interfaces)r[   r  s     r#   r  r  T  sK    , i((I'Hh&& B(KK 		
 r%   c                     t          j                    g d}t           fd|D                       rt          d          t	                     }t          |          st          d          t                     }t          |          st          d          | d<   t          t          j	        |            d<    S )aQ  Copies all network interface subnet and security group IDs into their
    parent node config.

    Args:
        node_cfg (Dict[str, Any]): node config to bootstrap

    Returns:
        node_cfg (Dict[str, Any]): node config with all copied network
        interface subnet and security group IDs

    Raises:
        ValueError: If [1] subnet and security group IDs exist at both the
            node config and network interface levels, [2] any network interface
            doesn't have a subnet defined, or [3] any network interface doesn't
            have a security group defined.
    )SubnetIdrl   rn   c              3       K   | ]}|v V  	d S r(   r/   )ri   conflictr  s     r#   rj   zH_configure_subnets_and_groups_from_network_interfaces.<locals>.<genexpr>  s(      
>
>H8x
>
>
>
>
>
>r%   zjIf NetworkInterfaces are defined, subnets and security groups must ONLY be given in each NetworkInterface.zxNetworkInterfaces are defined but at least one is missing a subnet. Please ensure all interfaces have a subnet assigned.zNetworkInterfaces are defined but at least one is missing a security group. Please ensure all interfaces have a security group assigned.rl   rn   )
r.   r   anyr  _subnets_in_network_configrw   "_security_groups_in_network_configr  rl  rm  )r  conflict_keysr   r  s   `   r#   r  r  t  s    ( }X&&H BAAM

>
>
>
>
>
>
>>> 
;
 
 	
 )22Gw<< 
K
 
 	
 9BBO 

 
 	

 $H[#'	(I#J#JH Or%   c                 B    d |                      dg           D             S )az  
    Returns all subnet IDs found in the given node config's network interfaces.

    Args:
        config (Dict[str, Any]): node config
    Returns:
        subnet_ids (List[str]): List of subnet IDs for all network interfaces,
        or an empty list if no network interfaces are defined. An empty string
        is returned for each missing network interface subnet ID.
    c                 :    g | ]}|                     d d          S )r  r*  rR   ri   nis     r#   r   z._subnets_in_network_config.<locals>.<listcomp>  s&    QQQrBFF:r""QQQr%   r  r  r   s    r#   r  r    s(     RQVZZ8KR-P-PQQQQr%   c                 B    d |                      dg           D             S )a  
    Returns all security group IDs found in the given node config's network
    interfaces.

    Args:
        config (Dict[str, Any]): node config
    Returns:
        security_group_ids (List[List[str]]): List of security group ID lists
        for all network interfaces, or an empty list if no network interfaces
        are defined. An empty list is returned for each missing network
        interface security group list.
    c                 :    g | ]}|                     d g           S )Groupsr  r  s     r#   r   z6_security_groups_in_network_config.<locals>.<listcomp>  s&    OOORBFF8R  OOOr%   r  r  r   s    r#   r  r    s(     PO6::6I2+N+NOOOOr%   c                 6    t          | |          j        j        S r(   )r   metar   )r   r>   s     r#   r   r     s    T6""'..r%   c                 r    |d         d         }|d                              di           }t          | |fi |S )Nr   r    aws_credentials)rR   r   )r   r>   r    r  s       r#   r   r     sD    J)FZ(,,->CCO$::/:::r%   r(   )ar.   rl  r   loggingr   r   collectionsr   	functoolsr   r   typingr   r   r   r	   r
   r   boto3r&   packaging.versionr   8ray.autoscaler._private.aws.cloudwatch.cloudwatch_helperr   r   !ray.autoscaler._private.aws.utilsr   r   r   "ray.autoscaler._private.cli_loggerr   r   $ray.autoscaler._private.event_systemr   r   !ray.autoscaler._private.providersr   ray.autoscaler._private.utilr   ray.autoscaler.tagsr   r   	getLogger__name__loggerr   r   r   r@  DEFAULT_AMI_NAMEr,  __version__r$   setLevelWARNINGr)   r,   r0   r6   r=   ru   r   r   r   r   r   rv   r  r   r
  r   r   r$  r2  r6  r  rJ  rA  rB  r3  ra  re  rj  rk  r   r   r   r   r  r  r   r  r  r  r  r   r   r/   r%   r#   <module>r     s%          				        ( ( ( ( ( ( ( ( 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8   % % % % % %              
 > = = = = = = = X X X X X X X X D D D D D D < < < < < < N N N N N N N N		8	$	$"U{ U{ +  @  )(((++((((----- ( wu !!WW& &   @  
V V V*  *   & &w 7 7 7	( ( (    - - -ltCH~ l$ l l l l^' ' 'TK K K\V V Vr
s 
s 
 
 
 
n($T#Y/n(cn( 
#n( 3-	n(
 n( n( 49c>n( n( n( n(b= = =@d3i c3h C    B  @, , ,2  % % %Ps     s    = = =
     :3 3 3&J J J
 
 
 
       $  $  DcN tCH~    <cN'+CH~	#s(^   @6cN6&*38n6	#s(^6 6 6 6rtCH~ $sCx.    8CH~	#s(^   @.38n.	#s(^. . . .bRtCH~ R$s) R R R RPtCH~ P$tCy/ P P P P / / /; ; ; ; ;r%   