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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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m Z  ddl!m"Z"m#Z# ddl$m%Z% e&e'Z(G dd deZ)dS )z%Implementation of the docker builder.    )annotationsN)Any)JobAndRunStatusTracker)AbstractBuilderregistry_from_uri)AbstractEnvironment)AbstractRegistry   )
EntryPointLaunchProject)LaunchDockerErrorLaunchError)AnonynmousRegistry)LocalRegistry)
LOG_PREFIXevent_loop_thread_exec$warn_failed_packages_from_build_logs   )_WANDB_DOCKERFILE_NAMEvalidate_docker_installation)BuildContextManagerc                   @  sR   e Zd ZdZdZdZdd
dZedddZd ddZ	d ddZ
	d!d"ddZdS )#DockerBuilderzxBuilds a docker image for a project.

    Attributes:
        builder_config (Dict[str, Any]): The builder config.

    dockerzlinux/amd64environmentr   registryr   configdict[str, Any]c                 C  s   || _ || _|| _dS )a  Initialize a DockerBuilder.

        Arguments:
            environment (AbstractEnvironment): The environment to use.
            registry (AbstractRegistry): The registry to use.

        Raises:
            LaunchError: If docker is not installed
        N)r   r   r   )selfr   r   r    r   [/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/builder/docker_builder.py__init__*   s   
zDockerBuilder.__init__returnc                 C  s@   | d}|r|durtt d| d t|}| |||S )a  Create a DockerBuilder from a config.

        Arguments:
            config (Dict[str, Any]): The config.
            registry (AbstractRegistry): The registry to use.
            verify (bool, optional): Whether to verify the functionality of the builder.
            login (bool, optional): Whether to login to the registry.

        Returns:
            DockerBuilder: The DockerBuilder.
        destinationNz.Overriding registry from registry config with z from builder config.)getwandbtermwarnr   r   )clsr   r   r   	image_urir   r   r   from_config=   s   
zDockerBuilder.from_configNonec                   s   t  I dH  dS )zVerify the builder.N)r   )r   r   r   r   verify\   s   zDockerBuilder.verifyc                   sz   t | jtrtt d dS t | jtr!tt d dS | j I dH \}}tt	j
}|||| jjI dH  dS )zLogin to the registry.z'No registry configured, skipping login.z#Anonymous registry, skipping login.N)
isinstancer   r   _loggerinfor   r   get_username_passwordr   r   loginuri)r   usernamepasswordr/   r   r   r   r/   `   s   
zDockerBuilder.loginNlaunch_projectr   
entrypointr
   job_trackerJobAndRunStatusTracker | Nonestrc              
     s  |   I dH  |  I dH  t|d}|d\}}tj|t}| js'dn| j	 I dH }|r9| d| }	n|j
 d| }	| sP| j|	I dH rP|	S td|	 d zttj|	g||| jddI dH }
t|
|	|j| W n tjy } z|r|d	 td
| d}~ww zt| W n ty   t d| d}t| Y nw |r|	d\}}tt d|	  ttj||I dH }|du rt d|j!dkrd| d|vrt d| |	S )zBuild the image for the given project.

        Arguments:
            launch_project (LaunchProject): The project to build.
            entrypoint (EntryPoint): The entrypoint to use.
        N)r3   r   :zimage z0 does not already exist in repository, building.platform)tagsfilecontext_pathr9   buildz(Error communicating with docker client: zTemporary docker context file z was not deleted.zPushing image z"Failed to push image to repository	sagemakerzThe push refers to repository []z'Unable to push image to ECR, response: )"r*   r/   r   create_build_contextospathjoinr   r   get_repo_uri
image_namebuild_requiredcheck_image_existsr,   r-   r   r   r=   r   r#   r   apiDockerErrorset_err_stager   remove	Exceptionr   splitr$   termlogpushr   resource)r   r3   r4   r5   build_context_managerbuild_ctx_path	image_tag
dockerfile
repositoryr'   outpute_msgregtag	push_respr   r   r   build_imagek   sd   





zDockerBuilder.build_image)r   r   r   r   r   r   )r   r   r   r   r   r   r!   r   )r!   r)   )N)r3   r   r4   r
   r5   r6   r!   r7   )__name__
__module____qualname____doc__builder_typetarget_platformr    classmethodr(   r*   r/   r\   r   r   r   r   r      s    


r   )*r`   
__future__r   loggingrA   typingr   r$   wandb.dockerr   )wandb.sdk.launch.agent.job_status_trackerr   !wandb.sdk.launch.builder.abstractr   r   %wandb.sdk.launch.environment.abstractr   "wandb.sdk.launch.registry.abstractr   _project_specr
   r   errorsr   r   registry.anonr   registry.local_registryr   utilsr   r   r   r=   r   r   context_managerr   	getLoggerr]   r,   r   r   r   r   r   <module>   s(    
