o
    xi=                     @  s   d Z ddl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 ddlmZ eeZedd	d
Zedd	d
ZG dd deZdS )zDImplementation of Elastic Container Registry class for wandb launch.    )annotationsN)LaunchError)AbstractRegistry)$ELASTIC_CONTAINER_REGISTRY_URI_REGEXevent_loop_thread_exec)
get_modulebotocoreziThe boto3 package is required to use launch with AWS. Please install it with `pip install wandb[launch]`.)requiredboto3c                   @  sP   e Zd ZdZ				ddd
dZedddZdddZdddZdddZ	dS ) ElasticContainerRegistryz!Elastic Container Registry class.Nuri
str | None
account_idregion	repo_namereturnNonec                 C  s   |r4|| _ t|||grtdt| j }|s!td| j  d|d| _|d| _|d| _n"t	|||gs?td|| _|| _|| _| j d| j d| j | _ | jd	u r_td
| jd	u rhtd| jd	u rqtdd	S )a  Initialize the Elastic Container Registry.

        Arguments:
            uri: The uri of the repository.
            account_id: The AWS account id.
            region: The AWS region of the container registry.
            repository: The name of the repository.

        Raises:
            LaunchError: If there is an error initializing the Elastic Container Registry helper.
        z|Could not create ElasticContainerRegistry from config. Either 'uri' or 'account_id', 'region', and 'repo_name' are required.z?Could not create ElasticContainerRegistry from config. The uri z is invalid.accountr   
repository	.dkr.ecr..amazonaws.com/Nz`Could not create ElasticContainerRegistry from config. Either 'uri' or 'account_id' is required.z\Could not create ElasticContainerRegistry from config. Either 'uri' or 'region' is required.z`Could not create ElasticContainerRegistry from config. Either 'uri' or 'repository' is required.)
r   anyr   r   matchgroupr   r   r   all)selfr   r   r   r   r    r   h/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/registry/elastic_container_registry.py__init__   sN   


z!ElasticContainerRegistry.__init__configdict[str, str]c                 C  sZ   h d}t | | }|rtd| d| d| |d|d|d|dd	S )
zCreate an Elastic Container Registry from a config.

        Arguments:
            config (dict): The config.

        Returns:
            ElasticContainerRegistry: The Elastic Container Registry.
        >   r   typer   r   r   zAThe Elastic Container Registry config contains unsupported keys: z5. Please remove these keys. The acceptable keys are: .r   r   r   r   )r   r   r   r   )setkeysr   get)clsr   acceptable_keysunsupported_keysr   r   r   from_config]   s    z$ElasticContainerRegistry.from_configtuple[str, str]c           	   
     s   t d z5tj| jd}t|jdI dH }t|j I dH }t	|d d d 
d\}}|d	|d	fW S  tjjya } z|jd
 d }|jd
 d }td| d| d}~ww )zGet the username and password for the registry.

        Returns:
            (str, str): The username and password.

        Raises:
            RegistryError: If there is an error getting the username and password.
        z=Getting username and password for Elastic Container Registry.region_nameecrNauthorizationDatar   authorizationToken   :zutf-8ErrorCodeMessagez%Error getting username and password:  )_loggerdebugr
   Sessionr   r   clientget_authorization_tokenbase64standard_b64decodesplitdecoder   
exceptionsClientErrorresponser   )	r   sessionr8   r@   usernamepasswordecodemsgr   r   r   get_username_password   s$   
	z.ElasticContainerRegistry.get_username_passwordstrc                   s   | j  d| j d| j S )zeGet the uri of the repository.

        Returns:
            str: The uri of the repository.
        r   r   )r   r   r   )r   r   r   r   get_repo_uri   s   z%ElasticContainerRegistry.get_repo_uri	image_uriboolc              
     s  d|vr|}n| d\}}|  I dH }||kr%td| d| dtd| d| j  z*tj| jd}t	|j
d	I dH }t	|j| jd
|igdI dH }t|d dkW S  tjjy } z#|jd d }	|	dkrwW Y d}~dS |jd d }
td|	 d|
 d}~ww )zCheck if the image tag exists.

        Arguments:
            image_uri (str): The full image_uri.

        Returns:
            bool: True if the image tag exists.
        :Nz
Image uri z/ does not match Elastic Container Registry uri r"   zChecking if image tag z exists in repository r+   r-   imageTag)repositoryNameimageIdsimageDetailsr   r1   r2   ImageNotFoundExceptionFr3   z$Error checking if image tag exists: r4   )r<   rI   r   r5   r6   r   r
   r7   r   r   r8   describe_imagesr   lenr   r>   r?   r@   )r   rJ   tagr   repo_urirA   r8   r@   rD   rE   rF   r   r   r   check_image_exists   s2   	z+ElasticContainerRegistry.check_image_exists)NNNN)
r   r   r   r   r   r   r   r   r   r   )r   r    r   r   )r   r*   )r   rH   )rJ   rH   r   rK   )
__name__
__module____qualname____doc__r   classmethodr)   rG   rI   rV   r   r   r   r   r      s    >
"
r   )rZ   
__future__r   r:   loggingwandb.sdk.launch.errorsr   "wandb.sdk.launch.registry.abstractr   wandb.sdk.launch.utilsr   r   
wandb.utilr   	getLoggerrW   r5   r   r
   r   r   r   r   r   <module>   s$    
