
                            S r SSKJr  SSKJr  SSKJ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\
R0                  5      r " S S\
R0                  5      r " S S\
R0                  5      r " S S\
R0                  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.    )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                       \ 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)ContainerNotFoundException0   zDocker container is not found.r   Nr   r   r#   r$   r&   r&   0   s    &r#   r&   c                       \ rS rSrSrSrg)DockerExecutionException4   z,Docker executable exited with non-zero code.r   Nr   r   r#   r$   r)   r)   4   s    4r#   r)   c                       \ rS rSrSrSrg)PackExecutionException8   z*Pack executable exited with non-zero code.r   Nr   r   r#   r$   r,   r,   8   s    2r#   r,   c                  >    [         b  [        c  [        [        5      eg N)_DOCKER_PACKr   _INSTALLATION_GUIDEr   r#   r$   ValidateDependenciesr3   <   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)r1   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$   RunPackrQ   A   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--nameTr9   zDocker failed to run container N)ContainerExistsRemoveDockerContainerr0   rE   six	text_typerD   itemsrA   jsondumpsr   rF   r)   )rG   portenv_varslabels
docker_cmdkvrP   s           r$   RunDockerContainerra   g   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-fTr9   z5Docker failed to execute: failed to remove container Nr0   r   rF   r)   )rG   
delete_cmdrP   s      r$   rU   rU      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.
psr8   rd   zname=c                 B   > TR                  U R                  5       5      $ r/   rC   stripstdoutouts    r$   <lambda>!ContainerExists.<locals>.<lambda>       szz&,,.9r#   Tout_funcr:   z3Docker failed to execute: failed to list container r   )r0   r   rF   r)   bool)rG   list_cmdcapture_outrP   rn   s       @r$   rT   rT      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/   rj   rl   s    r$   ro   #FindContainerPort.<locals>.<lambda>   rq   r#   Trr   z2Docker failed to execute: failed to find port for r   re   )rG   mapping	find_portrv   rP   rn   s        @r$   FindContainerPortr}      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.
rx   z --format={{json .Config.Labels}}c                 B   > TR                  U R                  5       5      $ r/   rj   rl   s    r$   ro   *GetDockerContainerLabels.<locals>.<lambda>   rq   r#   Trr   z/Docker failed to execute: failed to labels for r   )rT   r0   r   rF   r)   rY   loads)rG   find_labelsrv   rP   rn   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 )Nr7   =)rX   rE   )cmd_argsr\   keyvalues       r$   rD   rD      s/    NN$jcOOWcCi%/01 %r#   )%r!   
__future__r   r   r   rY   r;   textwrapgooglecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.utilr   rV   dedentr2   FindExecutableOnPathr0   r1   r@   rB   r?   r>   Errorr   r&   r)   r,   r3   rQ   ra   rU   rT   r}   r   rD   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#   