
    &`iV                     h   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mZ d dl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 d dlmZ d d	lmZ d d
lmZ d dlmZmZmZ dZdZ ej         e!          Z"dede#defdZ$d Z%d Z&d Z'dede#de#ddfdZ(ddddedef         de#de#d e)d!e)de*fd"Z+d#ede#fd$Z,dS )%    N)md5sha256)Path)AnyCallable)UUID)get_cli_profile)HttpResponseErrorResourceNotFoundError)AzureCliCredential)ResourceManagementClient)DeploymentMode)generate_rsa_key_pairgenerate_ssh_key_namegenerate_ssh_key_pathsz$b24988ac-6180-42a0-ab88-20f7382dd24c   clientfunction_namereturnc           	          t          | |t          | d| d                    }|*t          d                    | j        h|                    |S )a2  Retrieve a callable function from Azure SDK client object.

    Newer versions of the various client SDKs renamed function names to
    have a begin_ prefix. This function supports both the old and new
    versions of the SDK by first trying the old name and falling back to
    the prefixed new name.
    begin_Nz6'{obj}' object has no {func} or begin_{func} attribute)objfunc)getattrAttributeErrorformat__name__)r   r   r   s      y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/_private/_azure/config.pyget_azure_sdk_functionr      sr     wv/G/G/GNN D |DKK_%M L  
 
 	

 K    c                 B    t          |           } t          |           } | S N)_configure_key_pair_configure_resource_group)configs    r   bootstrap_azurer&   3   s!     ((F&v..FMr    c           
         | d                              d          }| t                                                      }t          t	                      |          }|| d         d<   t
                              d|           d| d         v s
J d            | d         d         }d| d         v s
J d            d| d         d         i}d| d         v r| d         d         |d<   t
                              d	|           t          |j        d
          } |||           t          t                    j        }|                    d          }t          |d          5 }t          j        |          }	d d d            n# 1 swxY w Y   t
                              d| d                    | d                              d          }
|
dt!                      }|                    | d         d                             d                     |                                d t(                   }
nt+          |
          }
|
| d         d<   t
                              d|
           d                    | d         |
          }d| d}t/          |          }d| d| d| }| d                              d          }|<t1          j        |
           d                    t1          j        dd                    }t
                              d|           t          |j        d          }t9           ||d |
 d!                    }t;          |          d"k    r|d"         j        }t          |j        d#          }d$}	 |j                             d%          }tA          d& |j!        D             d           }|rR|j"        rKd' |j"        D             }|p|j"        }tG          |          d(         }t
                              d)|           nt
          $                    d*|           n?# tJ          $ r2}t
          $                    d+t+          |                     Y d }~nd }~ww xY w |||          j&        d,         d"         }tA          d- |	d.         D             d           }|D|d/                              d,          }|'|d"         d/                             |d/                    d0| d         v o	d1| d         v }| d                              d1|          }| d                              d0d| d2          } t
                              d3| |           d }!|sNd| d| d4|  }"	 t          |j        d#          }# |#|"d5          }$tO          |$d/i                                d6          }!n@# tP          $ r d }!Y n2tJ          $ r&}%t
          $                    d7| |%           Y d }%~%nd }%~%ww xY w|!r,t
                              d8|!           tS          |||!           t          |j        d9          }&t          |j        d#          }'d:}(d:})	  |'|d;           n]# tP          $ r  d<}(t
          *                    d=|           Y n4tJ          $ r(}%t
          $                    d>||%           d<})Y d }%~%nd }%~%ww xY w|(s	  |&|d;?          }*tW          |*d@          r|*,                                 t
                              dA|           |)rt
          *                    dB|           t[          |'||          st
          $                    dC|           nY# tP          $ r t
          *                    dD|           Y n2tJ          $ r&}t
          $                    dE||           Y d }~nd }~ww xY wd/t\          j/        |	dF|idF|idF| idF|idF| idF|idGdHi}+t          |j0        d
          }, |,|dI|+J          1                                j&        j2        }-|-dK         dF         | d         dK<   |-dL         dF         | d         dL<   |-dM         dF         | d         dM<   | S )NNprovidersubscription_idzUsing subscription id: %sresource_groupz1Provider config must include resource_group fieldlocationz+Provider config must include location fieldtagsz$Creating/Updating resource group: %screate_or_updater   r   )resource_group_name
parameterszazure-config-template.jsonrzUsing cluster name: %scluster_name	unique_idutf-8zUsing unique id: %sz{}-{}zray-z-raz/subscriptions/z/resourceGroups/z3/providers/Microsoft.Authorization/roleAssignments/subnet_maskz10.{}.0.0/16      zUsing subnet mask: %slist_by_resource_groupzsubstringof('z@', name) and resourceType eq 'Microsoft.Network/virtualNetworks'r   	get_by_idz
2024-10-01zMicrosoft.Networkc              3   0   K   | ]}|j         d k    |V  dS )virtualNetworksN)resource_type).0rts     r   	<genexpr>z,_configure_resource_group.<locals>.<genexpr>   s=        '+<<< <<<< r    c                     g | ]}d |v|	S )preview r=   vs     r   
<listcomp>z-_configure_resource_group.<locals>.<listcomp>   s*     # # #)STBTBTABTBTBTr    z)Using API version: %s for virtualNetworkszYCould not determine supported API versions for virtualNetworks, using fallback version %szUFailed to query Microsoft.Network provider: %s. Using fallback API version 2024-10-01subnetsc              3   2   K   | ]}|d          dk    |V  dS )typez!Microsoft.Network/virtualNetworksNrB   )r=   rss     r   r?   z,_configure_resource_group.<locals>.<genexpr>   s>        f:!DDD DDDD r    	resources
propertiesmsi_namemsi_resource_groupz-msiz.Using msi_name: %s from msi_resource_group: %sz</providers/Microsoft.ManagedIdentity/userAssignedIdentities/z
2023-01-31principalIdz1Failed to query MSI %s for existing principal: %szIRemoving existing role assignments for MSI principal %s before recreationdelete_by_idF
2022-04-01TzCRole assignment %s not found before MSI creation; skipping deletionz6Failed to query role assignment %s before deletion: %sresource_idapi_versionwaitz9Deleted existing role assignment %s before recreating MSIz,Retrying verification for role assignment %sz(Role assignment %s not confirmed deletedzBRole assignment %s disappeared before deletion attempt; continuingz;Failed to delete role assignment %s before MSI creation: %svalue)subnet	clusterIdmsiNamemsiResourceGroup	createMsiroleAssignmentGuid)modetemplater0   z
ray-config)r/   deployment_namer0   msinsgrW   )3getr	   get_subscription_idr   r   loggerinfor   resource_groupsr   __file__parentjoinpathopenjsonloadr   updateencode	hexdigestUNIQUE_ID_LENstrr   _generate_arm_guidrandomseedrandintrK   listlenid	providersnextresource_typesapi_versionssortedwarning	ExceptionrL   r   r   &_delete_role_assignments_for_principaldebughasattrrU   "_wait_for_role_assignment_deletionr   incrementaldeploymentsresultoutputs).r%   r)   resource_clientr*   paramsrg_create_or_updatecurrent_pathtemplate_pathtemplate_fpr^   r3   hasher
cluster_idrole_assignment_namerole_assignment_guidrole_assignment_resource_idr5   
list_by_rgexisting_vnetsvnidr9   vnet_api_versionry   vnet_resource_typestable_versionsversions_to_considererW   template_vnettemplate_subnetsuse_existing_msirN   rM   existing_principal_idmsi_resource_idget_identityexisting_msiexcdelete_role_assignmentget_role_assignmentrole_assignment_known_missinginitial_query_failed
delete_lror0   r-   r   s.                                                 r   r$   r$   9   sM    Z(,,->??O)++??AA./A/C/C_UUO,;F:()
KK+_=== 	F:....: 	/..J'(89N 	fZ((((4 	)((&,Z89F
###
+F3v
KK6GGG0.>P   NvNNNN >>(L ))*FGGM	mS	!	! *[9[))* * * * * * * * * * * * * * * KK(&*@AAA z"&&{33IfZ()9:AA'JJKKK$$&&~~6			NN	&/F:{#
KK%y111~ 6	BBJ1*111-.BCC	K/ 	K 	K> 	K 	K4H	K 	K  
 $((77KI$++FN1c,B,BCC
KK'555 ((8P  J 
BI B B B	
 	
 N >Qa #*",K
 
 
	 (	'1556IJJI!% '6  
 " " " &8&E # #1># # # $F'9'F % $**>#?#?#C ?AQ    o$    	 	 	NNgA       	 4!122=iHK ";/  
 
 
 $,\:>>yII+ #L1889MNNN
 	fZ((W-AVJEW-W   
+//0DnUUj!%%j2I2I2I2IJJH
KK8(DV   ! gVo V V?Q V VKSV V 		1&0  L (<FFL$+L,$K$K$O$O% %!! % 	) 	) 	)$(!!! 	 	 	NNC       	 ! 		KK[%   3%   "8",N"
 "
 "
 5",K
 
 
 ).%$	(+    % 	 	 	,0)LLU$      	( 	( 	(NNH$  
 $(      	( - &	%33 ; ,  
 :v.. &OO%%%O(  
 ( LLF,  
 :'/(  
 NNB,   )   X(        Q(        	". "K0%z2#X.%,.@$A%+;';<'.0D&E 
 
J .*:L   	 .(!	
 	
 	

 
	G  !(w 7F:u 'w 7F:u#*8#4W#=F:x Ms   E>>FF=B(P& &
Q"0(QQ"%AV, ,W):	W)W$$W)Y 'Z2	Z2
Z--Z28B] %^$5	^$>^^$c                 r   | d         d         }d}d| d         v }d| d         v }||k    r"|rd\  }}nd\  }}t          | d| d	          |rZ|rWt          | d         d                                                   }t          | d         d                                                   }g }	|                                s|	                    d
|            |                                s|	                    d|            |	r5t          d                    d                    |	                              t                              d                    ||                     t          |d          5 }
|

                                }ddd           n# 1 swxY w Y   n| d         d         }| d         d         }t          dd|||          }t          |          \  }}t          j                            |          rt          j                            |          rkt                              d                    ||                     t          |d          5 }
|

                                }ddd           n# 1 swxY w Y   nt                              d                    ||                     t          j        t          j                            |          d           t%                      \  }}t          |dd           5 }
|
                    |           ddd           n# 1 swxY w Y   t          |d          5 }
|
                    |           ddd           n# 1 swxY w Y   t          j                            |          sJ d                    ||                      t)          |          | d         d<   | d                             dd           | d                                         D ](}|d                             di           }||d<   ||d <   )| S )!z
    Configure SSH keypair. Use user specified custom paths, otherwise,
    generate Ray-specific keypair in this format: "ray-autoscaler_azure_{region}_{resource_group}_{ssh_user}_{index}"
    authssh_userNssh_private_keyssh_public_key)r   r   )r   r   z is specified but zu is missing. Both SSH key paths must be specified together, or omit both from your config to use auto-generated keys.zssh_private_key: zssh_public_key: zSSH key files from config do not exist: {}. Please create the keys or remove the custom paths from your config to use auto-generated keys.z, z/Using specified SSH keys from config: {} and {}r1   r(   r+   r*   azurez/Using existing Ray-specific SSH keys: {} and {}z5Generating new Ray-specific SSH key pair at {} and {}T)exist_okwc                 .    t          j        | |d          S )Ni  )osrj   )pathflagss     r   <lambda>z%_configure_key_pair.<locals>.<lambda>  s    274+F+F r    )openerz)Private key file {} not found for user {}available_node_typesnode_configazure_arm_parametersadminUsername	publicKey)
ValueErrorr   
expanduseris_fileappendr   joinrd   re   rj   readr   r   r   r   existsmakedirsdirnamer   writerq   popvalues
setdefault)r%   r   
public_keyuser_specified_private_keyuser_specified_public_keymissing_keyspecified_keyprivate_key_pathpublic_key_pathmissing_keysfregionr*   key_nameprivate_key	node_typer   s                    r   r#   r#   Z  s   
 f~j)HJ "3fVn!D 0F6N B "%>>>% 	M)L&K)L&K 6 6 6 6 6
 
 	
 " D
&? D
v/@ ABBMMOOvf~.>?@@KKMM '')) 	H F4D F FGGG&&(( 	F D? D DEEE 	..4fTYY|5L5L.M.M  
 	=DD / 	
 	
 	
 /3'' 	"1J	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 
#J/
+,<= )T6>8
 
 -C8,L,L)) 7>>*++ 	$0O0O 	$KKAHH$o   
 os++ &qVVXX
& & & & & & & & & & & & & & & KKGNN$o   
 K(899DIIII&;&=&=#J FF   % $$$% % % % % % % % % % % % % % % os++ $q
###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ w~~
 
 	
 	
6==h
 
	
 	
 
 ),,<(=(=F6N$%
6N'...23::<< 7 7	(7BB"B 
  
 19_-,6[))MsH   <FF!$F!'JJJL;;L?L?M88M<?M<r   r*   principal_idc           	         |sdS t          | j        d          }t          | j        d          }	 t           ||d                    }t                              dt          |          ||           n4# t          $ r'}t                              d||           Y d}~dS d}~ww xY w|D ]@}t          |di           pi }t                              d	t          |d
d          |	                    d          |	                    d                     |	                    d          |k    r	  ||j
        d          }	t          |	d          r|	                                 t                              d|j        |           # t          $ r% t                              d|j        |           Y 	t           $ r-}t                              d|j        ||           Y d}~:d}~ww xY wdS )a  Delete all role assignments in the resource group for the given principal.

    Uses the generic ResourceManagementClient so we avoid depending on
    azure-mgmt-authorization. All role assignments associated with the
    provided principal ID are removed.
    Nr8   r.   rP   z9resourceType eq 'Microsoft.Authorization/roleAssignments'zMFound %d role assignments in resource group %s while cleaning up principal %sz>Failed to enumerate role assignments for resource group %s: %srL   zEInspecting role assignment %s with principalId=%s roleDefinitionId=%snamez	<unknown>rO   roleDefinitionIdrQ   rR   rU   z4Deleted existing role assignment %s for principal %sz:Role assignment %s not found while processing principal %sz8Failed to delete role assignment %s for principal %s: %s)r   rK   rv   rd   r   rw   r
   r~   r   rb   rx   r   rU   re   r   r   r   )
r   r*   r   r   r   assignmentsr   
assignmentrL   r   s
             r   r   r     s     '(8P  J 4(  JK 
 
 	[		
 	
 	
 	
    L	
 	
 	

 	 " # #
Zr::@b
SJ44NN=))NN-..		
 	
 	
 >>-((L88	//&M(  J z6** "!!!KKF   
 % 	 	 	LLL    
  	 	 	NNJ	       	;# #s7   AA6 6
B' B""B'/AF+G+5	G+>"G&&G+
      )max_attemptsdelay_secondsr   .rS   r   r   r   c          	      @   t          d|dz             D ]}	  | |d           t                              d|||           nB# t          $ r Y  dS t          $ r(}t                              d||||           Y d}~nd}~ww xY w||k     rt          j        |           dS )zPoll until a role assignment disappears after deletion.

    Returns True if the assignment is confirmed deleted, False otherwise.
    Logs detailed progress to aid troubleshooting when transient errors occur.
    r6   rQ   z?Role assignment %s still present after deletion (attempt %d/%d)Tz@Attempt %d/%d to verify removal of role assignment %s failed: %sNF)rangerd   r   r   r   timesleep)r   rS   r   r   r   attemptr   s          r   r   r     s     L1,-- & &	\::: LLQ$	    % 	 	 	444 	 	 	LLR$       	  \!!J}%%%5s   A
B 	B A;;B r   c            	          d                     d | D                       }t          t          t          |                    d                                                                        S )zGReplicates ARM template guid() function for creating deterministic IDs. c              3   4   K   | ]}t          |          V  d S r"   )rq   rC   s     r   r?   z%_generate_arm_guid.<locals>.<genexpr>C  s(      22a3q66222222r    r4   )r   rq   r   r   rn   ro   )r   concatenateds     r   rr   rr   @  s\     7722622222LtC++G4455??AABBCCCr    )-rk   loggingr   rs   r   hashlibr   r   pathlibr   typingr   r   uuidr   azure.common.credentialsr	   azure.core.exceptionsr
   r   azure.identityr   azure.mgmt.resourcer   $azure.mgmt.resource.resources.modelsr   ray.autoscaler._private.utilr   r   r   CONTRIBUTOR_ROLE_DEFINITION_IDrp   	getLoggerr   rd   rq   r   r&   r$   r#   r   intboolr   rr   rB   r    r   <module>r      ss     				                                       4 4 4 4 4 4 J J J J J J J J - - - - - - 8 8 8 8 8 8 ? ? ? ? ? ?          "H 		8	$	$3 s x    (  ^ ^ ^B	i i iXN-NN N 
	N N N Nl & & &!#s(+&& &
 & & 
& & & &RD D D D D D D Dr    