
                            d Z ddlmZ ddlmZ ddlmZ 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 ddlZ ej                  d	      Z ej"                  d
      Z ej"                  d      ZdZdZdZddddddddZ G d de
j0                        Z G d de
j0                        Z G d de
j0                        Z G d de
j0                        Zd Zd  Zd! Zd" Z d# Z!d$ Z"d% Z#d& Z$y)'z5Utilities used by gcloud functions local development.    )absolute_import)division)unicode_literalsN)
exceptions)execution_utils)filesz    You must install Docker and Pack to run this command.
    To install Docker and Pack, please follow this guide:
    https://cloud.google.com/functions/1stgendocs/running/functions-emulatordockerpackz4gcr.io/serverless-runtimes/google-{}-full/builder/{}zgcr.io/buildpacks/builder:v1z#gcr.io/buildpacks/builder:google-22i6     t      R          )pythonnodejsgojavaphprubydotnetc                       e Zd ZdZy)MissingExecutablesExceptionz0Executables for local development are not found.N__name__
__module____qualname____doc__     6lib/googlecloudsdk/command_lib/functions/local/util.pyr   r   ,   s    8r    r   c                       e Zd ZdZy)ContainerNotFoundExceptionzDocker container is not found.Nr   r   r    r!   r#   r#   0   s    &r    r#   c                       e Zd ZdZy)DockerExecutionExceptionz,Docker executable exited with non-zero code.Nr   r   r    r!   r%   r%   4   s    4r    r%   c                       e Zd ZdZy)PackExecutionExceptionz*Pack executable exited with non-zero code.Nr   r   r    r!   r'   r'   8   s    2r    r'   c                  :    t         t        t        t              y N)_DOCKER_PACKr   _INSTALLATION_GUIDEr   r    r!   ValidateDependenciesr-   <   s    _
%&9
:: &r    c                    t         ddg}|sqt        j                  d|      \  }}t        |      t        |   k\  r"|dk(  rt
        nt        j                  d|      }n!|dk(  rt        nt        j                  d|      }|j                  |       |rt        ||       |j                  dd|z   g       |j                  d	|g       |j                  d
| g       t        j                  |d      }	|	rt        |	d      y)a  Runs Pack Build with the command built from arguments of the command parser.

  Args:
    name: Name of the image build.
    builder: Name of the builder by the flag.
    runtime: Runtime specified by flag.
    entry_point: Entry point of the function specified by flag.
    path: Source of the zip file.
    build_env_vars: Build environment variables.
  Raises:
    PackExecutionException: if the exit code of the execution is non-zero.
  buildz	--builderz	(\D+|\d+)r      r   --envzGOOGLE_FUNCTION_TARGET=z--path-qTno_exitz)Pack failed to build the container image.N)r+   refindallint_RUNTIME_MINVERSION_UBUNTU_22_GOOGLE_22_BUILDER_APPENGINE_BUILDERformat_V1_BUILDERappend_AddEnvVarsextendr   Execr'   )
namebuilderruntimeentry_pointpathbuild_env_varspack_cmdlanguageversionstatuss
             r!   RunPackrK   A   s     Wk*( 
**\7;Xw
7|4X>>'/8';#(//H=  !)H 4(//H= 
//'.)
//75CDE
//8T"#
//4,$7&
 ;= = r    c           
         t        |       rt        |        t        ddg}|j                  dt	        j
                  |      dz   g       |rt        ||       |j                         D ];  \  }}|j                  ddj                  |t        j                  |            g       = |j                  d| | g       t        j                  |d	      }|rt        |d
| z         y)a  Runs the Docker container (detached mode) with specified port and name.

  If the name already exists, it will be removed.

  Args:
    name: The name of the container to run.
    port: The port for the container to run on.
    env_vars: The container environment variables.
    labels: Docker labels to store flags and environment variables.

  Raises:
    DockerExecutionException: if the exit code of the execution is non-zero.
  runz-dz-pz:8080z--labelz{}={}z--nameTr3   zDocker failed to run container N)ContainerExistsRemoveDockerContainerr*   r?   six	text_typer>   itemsr;   jsondumpsr   r@   r%   )rA   portenv_varslabels
docker_cmdkvrJ   s           r!   RunDockerContainerr[   g   s     T$%*T3==.89:
H%llnday'..DJJqM"BCD XtT*+
D9&
"1D8: : r    c                 h    t         dd| g}t        j                  |d      }|rt        |d| z         y)zRemoves the Docker container with specified name.

  Args:
    name: The name of the Docker container to delete.

  Raises:
    DockerExecutionException: if the exit code of the execution is non-zero.
  rm-fTr3   z5Docker failed to execute: failed to remove container Nr*   r   r@   r%   )rA   
delete_cmdrJ   s      r!   rO   rO      sF     tT**
D9&
"G$NP P r    c                     t         dddd| z   g}g fd}t        j                  ||d      }|rt        |d| z         t	        d	         S )
a  Returns True if the Docker container with specified name exists.

  Args:
    name: The name of the Docker container.

  Returns:
    bool: True if the container exists, False otherwise.

  Raises:
    DockerExecutionException: if the exit code of the execution is non-zero.
  psr2   r^   zname=c                 B    j                  | j                               S r)   r=   stripstdoutouts    r!   <lambda>z!ContainerExists.<locals>.<lambda>       szz&,,.9r    Tout_funcr4   z3Docker failed to execute: failed to list container r   )r*   r   r@   r%   bool)rA   list_cmdcapture_outrJ   rh   s       @r!   rN   rN      sb     tT448(
#9+;M&
"ELN N	c!fr    c                     d}t         dd|z   | g}g fd}t        j                  ||d      }|rt        |d| z         d   S )	a>  Returns the port of the Docker container with specified name.

  Args:
    name: The name of the Docker container.

  Returns:
    str: The port number of the Docker container.

  Raises:
    DockerExecutionException: if the exit code of the execution is non-zero
    or if the port of the container does not exist.
  zV{{range $p, $conf := .NetworkSettings.Ports}}      {{(index $conf 0).HostPort}}{{end}}inspectz	--format=c                 B    j                  | j                               S r)   rd   rf   s    r!   ri   z#FindContainerPort.<locals>.<lambda>   rj   r    Trk   z2Docker failed to execute: failed to find port for r   r_   )rA   mapping	find_portro   rJ   rh   s        @r!   FindContainerPortru      sa    -'	;#8$?)
#9+	KN&
"DtKM M	Q-r    c                     t        |       si S t        dd| g}g fd}t        j                  ||d      }|rt	        |d| z         t        j                  d         S )aL  Returns the labels of the Docker container with specified name.

  Args:
    name: The name of the Docker container.

  Returns:
    dict: The labels for the docker container in json format.

  Raises:
    DockerExecutionException: if the exit code of the execution is non-zero
    or if the port of the container does not exist.
  rq   z --format={{json .Config.Labels}}c                 B    j                  | j                               S r)   rd   rf   s    r!   ri   z*GetDockerContainerLabels.<locals>.<lambda>   rj   r    Trk   z/Docker failed to execute: failed to labels for r   )rN   r*   r   r@   r%   rS   loads)rA   find_labelsro   rJ   rh   s       @r!   GetDockerContainerLabelsrz      so     
	I)%GN+
#9+k4P&
"ADHJ J	CF	r    c                 f    |j                         D ]  \  }}| j                  d|dz   |z   g         y )Nr1   =)rR   r?   )cmd_argsrV   keyvalues       r!   r>   r>      s1    NN$jc5OOWcCi%/01 %r    )%r   
__future__r   r   r   rS   r5   textwrapgooglecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.utilr   rP   dedentr,   FindExecutableOnPathr*   r+   r:   r<   r9   r8   Errorr   r#   r%   r'   r-   rK   r[   rO   rN   ru   rz   r>   r   r    r!   <module>r      s   < &  '  	  = / * 
%hoo 'P Q  %%
$
$X
."""6*K ,: +."C)+B+,!. 
9/"7"7 9'!6!6 '544 53_22 3;
#=L::P ,222r    