
    4                        S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
r
\R                  " S5      r\	R                  " S5      r\	R                  " S5      rS	rS
rSrSSSSSSSS.r " S S\R(                  5      r " S S\R(                  5      r " S S\R(                  5      r " S S\R(                  5      rS rS rS rS rS  rS! rS" rS# r g)$z5Utilities used by gcloud functions local development.    N)
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                       \ rS rSrSrSrg)MissingExecutablesException)   z0Executables for local development are not found. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       6lib/googlecloudsdk/command_lib/functions/local/util.pyr   r   )   s    8r    r   c                       \ rS rSrSrSrg)ContainerNotFoundException-   zDocker container is not found.r   Nr   r   r    r!   r#   r#   -   s    &r    r#   c                       \ rS rSrSrSrg)DockerExecutionException1   z,Docker executable exited with non-zero code.r   Nr   r   r    r!   r&   r&   1   s    4r    r&   c                       \ rS rSrSrSrg)PackExecutionException5   z*Pack executable exited with non-zero code.r   Nr   r   r    r!   r)   r)   5   s    2r    r)   c                  >    [         b  [        c  [        [        5      eg N)_DOCKER_PACKr   _INSTALLATION_GUIDEr   r    r!   ValidateDependenciesr0   9   s    _
%&9
:: &r    c                    [         SS/nU(       dt  [        R                  " SU5      u  px[        U5      [        U   :  a#  US:X  a  [
        O[        R                  SU5      nO"US:X  a  [        O[        R                  SU5      nUR                  U5        U(       a  [        Xe5        UR                  SSU-   /5        UR                  S	U/5        UR                  S
U /5        [        R                  " USS9n	U	(       a  [        U	S5      eg)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!   RunPackrN   >   s     Wk*( 
**\7;X
7|4X>>'/8';#(//H=  !)H 4(//H= 
//')
//75CDE
//8T"#
//4,$7&
 ;= = r    c           
         [        U 5      (       a  [        U 5        [        SS/nUR                  S[        R
                  " U5      S-   /5        U(       a  [        XB5        UR                  5        H<  u  pVUR                  SSR                  U[        R                  " U5      5      /5        M>     UR                  SX /5        [        R                  " USS	9nU(       a  [        US
U -   5      eg)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--nameTr6   zDocker failed to run container N)ContainerExistsRemoveDockerContainerr-   rB   six	text_typerA   itemsr>   jsondumpsr   rC   r&   )rD   portenv_varslabels
docker_cmdkvrM   s           r!   RunDockerContainerr^   d   s     T$%*T3==.89:
%llnday'..DJJqM"BCD Xt*+
D9&
"1D8: : r    c                 n    [         SSU /n[        R                  " USS9nU(       a  [        USU -   5      eg)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-fTr6   z5Docker failed to execute: failed to remove container Nr-   r   rC   r&   )rD   
delete_cmdrM   s      r!   rR   rR      sF     tT**
D9&
"G$NP P r    c                    ^ [         SSSSU -   /n/ mU4S jn[        R                  " XSS9nU(       a  [        USU -   5      e[	        TS	   5      $ )
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.
psr5   ra   zname=c                 B   > TR                  U R                  5       5      $ r,   r@   stripstdoutouts    r!   <lambda>!ContainerExists.<locals>.<lambda>       szz&,,.9r    Tout_funcr7   z3Docker failed to execute: failed to list container r   )r-   r   rC   r&   bool)rD   list_cmdcapture_outrM   rk   s       @r!   rQ   rQ      s`     tT448(
#9+M&
"ELN N	c!fr    c                    ^ Sn[         SSU-   U /n/ mU4S jn[        R                  " X#SS9nU(       a  [        USU -   5      eT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   > TR                  U R                  5       5      $ r,   rg   ri   s    r!   rl   #FindContainerPort.<locals>.<lambda>   rn   r    Tro   z2Docker failed to execute: failed to find port for r   rb   )rD   mapping	find_portrs   rM   rk   s        @r!   FindContainerPortrz      s_    -'	;#8$?)
#9+	N&
"DtKM M	Q-r    c                    ^ [        U 5      (       d  0 $ [        SSU /n/ mU4S jn[        R                  " XSS9nU(       a  [	        USU -   5      e[
        R                  " TS   5      $ )a<  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.
ru   z --format={{json .Config.Labels}}c                 B   > TR                  U R                  5       5      $ r,   rg   ri   s    r!   rl   *GetDockerContainerLabels.<locals>.<lambda>   rn   r    Tro   z/Docker failed to execute: failed to labels for r   )rQ   r-   r   rC   r&   rV   loads)rD   find_labelsrs   rM   rk   s       @r!   GetDockerContainerLabelsr      sp     
		I)%GN+
#9+4P&
"ADHJ J	CF	r    c                 h    UR                  5        H  u  p#U R                  SUS-   U-   /5        M      g )Nr4   =)rU   rB   )cmd_argsrY   keyvalues       r!   rA   rA      s/    NN$jcOOWcCi%/01 %r    )!r   rV   r8   textwrapgooglecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.utilr   rS   dedentr/   FindExecutableOnPathr-   r.   r=   r?   r<   r;   Errorr   r#   r&   r)   r0   rN   r^   rR   rQ   rz   r   rA   r   r    r!   <module>r      s    <  	  = / * 
oo '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    