o
    `۷i                     @   s   d dl mZ d dlm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mZ d d	lmZ G d
d deZdd Zdd Ze efdefddZe efde
fddZdS )    )defaultdict)	lru_cacheN)ResourceNotExistsError)ServiceResource)
BaseClient)Config)cf
cli_logger)BOTO_MAX_RETRIESc                   @   s   e Zd ZdZdd ZdS )LazyDefaultDicta  
    LazyDefaultDict(default_factory[, ...]) --> dict with default factory

    The default factory is call with the key argument to produce
    a new value when a key is not present, in __getitem__ only.
    A LazyDefaultDict compares equal to a dict with the same items.
    All remaining arguments are treated the same as if they were
    passed to the dict constructor, including keyword arguments.
    c                 C   s   |  || |< | | S )z
        __missing__(key) # Called by __getitem__ for missing key; pseudo-code:
          if self.default_factory is None: raise KeyError((key,))
          self[key] = value = self.default_factory(key)
          return value
        )default_factory)selfkey r   W/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/autoscaler/_private/aws/utils.py__missing__   s   zLazyDefaultDict.__missing__N)__name__
__module____qualname____doc__r   r   r   r   r   r      s    
r   c                 O   s  d }d }t | dr| jdd }|d ur|dd }d|j|i |t|g}g d}||v rdtd d td	 d
 td }dtd d }	dtd d }
dtd d }d}tj|  t	t
|  td t  td tt| td tt|	 tt|
 tt| t  td tt| t  tj|  t  td t	dtt
|  tdt|  W d    n1 sw   Y  t  d S )NresponseErrorCodez{}
Error code: {})ExpiredTokenExceptionExpiredTokenRequestExpiredz7aws sts get-session-token --serial-number arn:aws:iam::ROOT_ACCOUNT_IDz:mfa/AWS_USERNAMEz --token-code TWO_FACTOR_AUTH_CODEzexport AWS_SECRET_ACCESS_KEY = 
REPLACE_MEz' # found at Credentials.SecretAccessKeyzexport AWS_SESSION_TOKEN = z$ # found at Credentials.SessionTokenzexport AWS_ACCESS_KEY_ID = z# # found at Credentials.AccessKeyIdzBhttps://gist.github.com/maximsmol/a0284e1d97b25d417bd9ae02e5f450cfzYour AWS session has expired.zYou can request a new one usingz then expose it to Ray by settingz-You can find a script that automates this at:zBoto3 error:z{})hasattrr   getformatr   bold
underlinedr	   verbose_errorverbosevarspanicnewlineabortverbatim_error_ctxstr)excmsgargskwargs
error_code
error_infogeneric_message_argscredentials_expiration_codestoken_commandsecret_key_varsession_token_varaccess_key_id_varaws_session_script_urlr   r   r   handle_boto_error$   s   
		





r:   c                    s   G  fddd}| S )Nc                       s$   e Zd Zdd Z fddZdS )z>boto_exception_handler.<locals>.ExceptionHandlerContextManagerc                 S   s   d S )Nr   )r   r   r   r   	__enter__   s   zHboto_exception_handler.<locals>.ExceptionHandlerContextManager.__enter__c                    s4   dd l }||jju rt|g R i  d S d S )Nr   )botocore
exceptionsClientErrorr:   )r   typevaluetbr<   r/   r0   r.   r   r   __exit__   s   zGboto_exception_handler.<locals>.ExceptionHandlerContextManager.__exit__N)r   r   r   r;   rC   r   rB   r   r   ExceptionHandlerContextManager   s    rD   r   )r.   r/   r0   rD   r   rB   r   boto_exception_handler   s   
rE   returnc                 K   sD   t dt| t| |dtd|id tj| |fi |S )Nz"Creating AWS resource `{}` in `{}`configmax_attemptsretries)r	   r&   r   r#   
setdefaultr   boto3resourcenameregionmax_retriesr0   r   r   r   resource_cache   s   rR   c              
   K   sr   zt | ||fi |jjW S  ty8   tdt| t| |dt	d|id t
j| |fi | Y S w )Nz Creating AWS client `{}` in `{}`rG   rH   rI   )rR   metaclientr   r	   r&   r   r#   rK   r   rL   rN   r   r   r   client_cache   s"   
rU   )collectionsr   	functoolsr   rL   boto3.exceptionsr   boto3.resources.baser   botocore.clientr   botocore.configr   "ray.autoscaler._private.cli_loggerr   r	   !ray.autoscaler._private.constantsr
   r   r:   rE   rR   rU   r   r   r   r   <module>   s&    ^