o
    bi                     @   s   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m	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d	ZeeZd
edede	fddZdd Zdd Zdd ZdS )    N)sha256)Path)AnyCallable)get_cli_profile)AzureCliCredential)ResourceManagementClient)DeploymentMode   clientfunction_namereturnc                 C   s<   t | |t | d| d}|du rtdj| jh|d|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    r   Y/home/ubuntu/.local/lib/python3.10/site-packages/ray/autoscaler/_private/_azure/config.pyget_azure_sdk_function   s   r   c                 C   s   t | } t| } | S )N)_configure_key_pair_configure_resource_group)configr   r   r   bootstrap_azure&   s   r   c              	   C   sn  | d  d}|d u r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 rR| d d |d< td	| t|jd
d}|||d t	t
j}|d}t|d}t|}	W d    n1 sw   Y  td| d  | d  d}
|
d u rt }|| d d d | d t }
nt|
}
|
| d d< td|
 d| d |
}| d  d}|d u rt|
 dtdd}td| t|jdd}t||d|
 d}t|dkrC|d j}t|jdd}|||jjd d }t d d! |	d" D d }|d urC|d#  d}|d urC|d d# |d#  d$| d v oOd%| d v }| d  d%|}| d  d$d&| d'}td(|| d#t!j"|	d)|id)|id)|id)|id)| id*d+i}t|j#d
d}||d,|d-$ jj%}|d. d) | d d.< |d/ d) | d d/< |d0 d) | d d0< | S )1N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_idzutf-8zUsing unique id: %sz{}-{}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_idsubnetsc                 s   s     | ]}|d  dkr|V  qdS )typez!Microsoft.Network/virtualNetworksNr   ).0rsr   r   r   	<genexpr>}   s    z,_configure_resource_group.<locals>.<genexpr>	resources
propertiesmsi_namemsi_resource_groupzray-z-msiz.Using msi_name: %s from msi_resource_group: %svalue)subnet	clusterIdmsiNamemsiResourceGroup	createMsi)modetemplater#   z
ray-config)r"   deployment_namer#   msinsgr6   )&getr   get_subscription_idr   r   loggerinfor   resource_groupsr   __file__parentjoinpathopenjsonloadr   updateencode	hexdigestUNIQUE_ID_LENstrr   randomseedrandintr1   listlenidDEFAULT_API_VERSIONr2   nextr	   incrementaldeploymentsresultoutputs)r   r   resource_clientr   paramsrg_create_or_updatecurrent_pathtemplate_pathtemplate_fpr<   r&   hasher
cluster_idr'   
list_by_rgexisting_vnetsvnidr+   r6   template_vnettemplate_subnetsuse_existing_msir4   r3   r#   r!   r[   r   r   r   r   ,   s   







r   c              	   C   s   | d d }d }dD ]R}zt | d |  }W n ty&   td| ty3   td|w | s?J d||dkr\t|d}| }W d    n1 sWw   Y  q
| d	 	 D ]}|d
 
di }||d< ||d< qc| S )Nauthssh_user)ssh_private_keyssh_public_keyzConfig must define {}zInvalid config value for {}zCould not find ssh key: {}rm   r$   available_node_typesnode_configazure_arm_parametersadminUsername	publicKey)r   
expanduserKeyError	Exceptionr   	TypeErroris_filerH   readvalues
setdefault)r   rk   
public_keykey_typekey_pathf	node_typerp   r   r   r   r      s.   

r   )rI   loggingrP   hashlibr   pathlibr   typingr   r   azure.common.credentialsr   azure.identityr   azure.mgmt.resourcer   $azure.mgmt.resource.resources.modelsr	   rN   	getLoggerr   rB   rO   r   r   r   r   r   r   r   r   <module>   s"    
 