o
    ni                     @   s   d Z ddlZddlZddlmZmZ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.    N)AnyDictOptional)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                	   @   s   e Zd ZdZdZdZdededee	e
f fddZedee	e
f deded	d fd
dZdddZdddZ	ddededee d	e	fddZdS )DockerBuilderzxBuilds a docker image for a project.

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

    dockerzlinux/amd64environmentregistryconfigc                 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   e/home/ubuntu/SoloSpeech/.venv/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_configNc                    s   t  I dH  dS )zVerify the builder.N)r   )r   r   r   r   verifyZ   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.loginlaunch_project
entrypointjob_trackerc              
      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)r2   r   :zimage z0 does not already exist in repository, building.platform)tagsfilecontext_pathr6   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   r2   r3   r4   build_context_managerbuild_ctx_path	image_tag
dockerfile
repositoryr'   outpute_msgregtag	push_respr   r   r   build_imagei   sd   





zDockerBuilder.build_image)r!   N)N)__name__
__module____qualname____doc__builder_typetarget_platformr   r	   r   strr   r    classmethodr(   r)   r.   r   r   r   r   rY   r   r   r   r   r      sB    




r   )*r]   loggingr>   typingr   r   r   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   	getLoggerrZ   r+   r   r   r   r   r   <module>   s&    
