
    6+                        d Z ddlm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m	Z	 ddlm
Z dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  e	j<                  dd      ZdZ  G d dejB                        Z"d Z#d Z$ G d dejJ                        Z&d Z'd Z(d Z)d  Z*d! Z+d" Z,y)#z0Library for configuring cloud-based development.    )absolute_import)division)print_function)unicode_literalsN)apis)messages)docker_util)builders)common)
dataobject)yaml_helper)
exceptions)flags)log)
properties)yaml)filesrunv1zgcr.io/buildpacks/builderc                   "     e Zd ZdZ fdZ xZS )ImageFormatErrorz:An error thrown when the provided image has a tag or hash.c                 N    t         t        |   dj                  ||             y )NzTImage {} has a {} included. To use locally built image, do not include digest or tag)message)superr   __init__format)selfimagefmt	__class__s      2lib/googlecloudsdk/command_lib/code/cloud/cloud.pyr   zImageFormatError.__init__/   s)    	
D*(
&
	 +     )__name__
__module____qualname____doc__r   __classcell__)r    s   @r!   r   r   ,   s    B r"   r   c                     | t         k(  S )zReturn true if the builder is the GCP base builder.

  Args:
    bldr: Name of the builder.

  Returns:
    True if the builder is the GCP base builder.
  )_DEFAULT_BUILDPACK_BUILDER)bldrs    r!   _IsGcpBaseBuilderr+   8   s     
+	++r"   c                 H    t        |       }t        j                  | |d      S )NF)buildertrustdevmode)r+   r
   BuildpackBuilder)builder_argis_gcp_base_builders     r!   _BuilderFromArgr3   D   s(    )+6		"	"!4e
 r"   c                   :    e Zd ZdZg dZed        Zd Zd Zd Z	y)Settingsa-  Settings for a Cloud dev deployment.

  Attributes:
    image: image to deploy from local sources
    project: the gcp project to deploy to
    region: the Cloud Run region to deploy to
    service_name: the name of the Cloud Run service to deploy
    builder: the build configuration. Docker and Buildpacks are supported.
    context: the folder in which the build will be executed
    service: the base service to build off of. Using this allows any field not
      explicitly supported by code dev --cloud to still propagate
    cpu: the amount of CPU to be used
    memory: the amount of memory to be specified.
    ar_repo: the Artifact Registry Docker repo to deploy to.
    local_port: the local port to forward the request for.
    service_account: the service identity to use for the deployed service.
  )r   projectregionr-   service_nameservicecontextcpumemoryar_repo
local_portservice_accountc           	         t         j                  j                  t        j                               }|j                  dd      j                  dd      j                         }t        j                  dd      }d}t        j                  |      } | |||t         j                  j                  t        j                               	      S )
N_- zserving.knative.dev/v1Service)
apiVersionkind
Dockerfile
dockerfile)r8   r9   r-   r:   )ospathbasenamer   GetCWDreplacelowerRUN_MESSAGES_MODULErD   r
   DockerfileBuilderabspath)clsdir_namer8   r9   dockerfile_arg_defaultr*   s         r!   DefaultszSettings.Defaultsm   s    ww/H##C-55c3?EEGL!))+) * G *%%1GHD!/	 r"   c                    t        j                  |      }d|v r|d= t        j                  |dg      }|j	                  dd      }|!t        |t              st        |      |d   d<   	 t        j                  |t        j                        }| j                  r$t        | j                        j                  _        di}|j"                  j$                  j"                  j&                  d   }|j(                  |d	<   |j*                  rT|j*                  j,                  r>|j*                  j,                  j.                  D ]  }	|	j0                  ||	j2                  <    |j                  j4                  r|j                  j4                  |d
<    | j6                  di |S # t        j                  $ r"}t        j                  |d       Y d}~Ad}~ww xY w)z.Use a pre-written service yaml for deployment.statusmetadata	namespaceNzPlease make sure that the YAML file matches the Knative service definition spec in https://kubernetes.io/docs/reference/kubernetes-api/service-resources/service-v1/#Service.)	help_textr9   r   r   r8    )r   	load_pathr   GetOrCreateget
isinstancestrmessages_utilDictToMessageWithErrorCheckrP   rD   ScalarTypeMismatchErrorr   MaybeRaiseCustomFieldMismatchr6   rY   rZ   spectemplate
containersr   	resourceslimitsadditionalPropertiesvaluekeynamerN   )
r   	yaml_pathservice_dictrY   rZ   r9   ereplacements	containerlimits
             r!   WithServiceYamlzSettings.WithServiceYaml~   s    >>),L
 <
x
  &&|j\BH[$/IZ	3%?.1)nl:{+99
+33g ||#&t||#4g w'L%%**55a8I%OOLy2299&&--BB%"'++UYY C%,%5%5%:%:l>"4<<',''+ 00 	..
 	s   *$F G2GGc                    t         j                  j                  j                  j	                         }t        j                  |d      }||d}dD ]#  }|j                  |      st        ||      ||<   % | j                  }|j                  r)t        j                  j                  |j                        }||d<   |j                  d      rt        |j                        |d<   n}|j                  d      r$t!        j"                  |j$                        |d<   nHt'        | j                  t         j"                        r$	 | j                  |d<   |d   j)                  |        | j4                  d
i |S # t         j*                  $ r4 t,        j.                  j1                  d	       t        t2              |d<   Y Xw xY w)z%Update parameters based on arguments.T)prompt)r6   r7   )r>   r<   r;   r   r8   r?   r:   r-   rI   rH   zCNo Dockerfile detected. Using GCP buildpacks to build the containerr\   )r   VALUEScorer6   Get	run_flags	GetRegionIsKnownAndSpecifiedgetattrr:   sourcerJ   rK   rR   r3   r-   r
   rQ   rI   r`   ValidateInvalidLocationErrorr   rX   Printr)   rN   )r   argsr6   r7   rr   override_argr:   s          r!   WithArgszSettings.WithArgs   s}   $$,,002G  d3F&&9L 
	!	!,	/%,T<%@\" llG{{,g%L	* / =l9		!	!,	/ ( : :__!l9 
DLL("<"<	=	P$(LL,y
!
y
!
*
*7
3 4<<','' ,, 	P
**

< %44N$O,y
!	Ps   #E6 6AF=<F=c                     i }| j                   sJt        j                  | j                  | j                  d      }||d<   t        || j                        |d<    | j                  di |S )Nzcloud-run-source-deploy)
project_idlocation_idrepo_idr=   r   r\   )r   r	   
DockerRepor6   r7   _DefaultImageNamer8   rN   )r   rr   r=   s      r!   BuildzSettings.Build   sf    L::&&\\kk+g
 !(l9/9J9JKl74<<',''r"   N)
r#   r$   r%   r&   NAMESclassmethodrV   ru   r   r   r\   r"   r!   r5   r5   K   s3    $%   .(`'(R(r"   r5   c                 *   t         j                         }t        | dd       xs t        j                  j
                  }t        | dd       }t        j                  ||      }|r|j                  |      }|j                  |       }|j                         S )Nr   service_config)r5   rV   r~   rJ   rK   curdirr   ChooseExistingServiceYamlru   r   r   )r   settingscontext_dirr   	yaml_files        r!   AssembleSettingsr      s|     (h-?+4!148...{NK)''	2Ht$(		r"   c                    t        j                  | j                        }|j                  xs t        j                         }| j                  |_        t        | j                        |_
        ||_        t        |       | j                  r/| j                  |j                  j                  j                  _        |j                  j                  j                  j                   d   }| j"                  |_        t%        ||        |S )zEGenerate a service configuration from a Cloud Settings configuration.r   )copydeepcopyr9   rY   rP   
ObjectMetar8   rn   ra   r6   rZ   _BuildSpecTemplater?   rf   rg   serviceAccountNamerh   r   _FillContainerRequirements)r   r9   rY   rs   s       r!   GenerateServicer      s    MM(**+'A!4!?!?!A(''(-8++,('W4<4L4LGLL1ll##((33A6)NN)/Y1	.r"   c                     | j                   st        j                         | _         | j                   j                  s#t        j	                         | j                   _        | j                   j                  j                   s-t        j                         | j                   j                  _         | j                   j                  j                   j                  s9t        j                         g| j                   j                  j                   _        y y )N)rf   rP   ServiceSpecrg   RevisionTemplateRevisionSpecrh   	Container)r9   s    r!   r   r      s    	&224GL			/@@BGLL				#	#!4!A!A!CGLL				#	#	.	.-@-J-J-L,MGLL) 
/r"   c                 D    dj                  | j                         |      S )Nz{repo}/{service})repor9   )r   GetDockerString)r=   r8   s     r!   r   r     s(    		"	"""$l 
# 
 r"   c                 ~   t               }| j                  xs t        j                         }|j                  xs t        j                  j                         }|j                  D ]v  }|j                  dk(  r|j                  r|j                  |_	        n,|j                  dk(  r|j                  r|j                  |_	        |j                  |j                         x d|vre|j                  rYt        j                  j
                  j                  dt        |j                              }|j                  j                  |       d|vre|j                  rYt        j                  j
                  j                  dt        |j                              }|j                  j                  |       ||_        || _        y)z:Set the container CPU and memory limits based on settings.r;   r<   )rm   rl   N)setri   rP   ResourceRequirementsrj   LimitsValuerk   rm   r;   rl   r<   addAdditionalPropertyra   append)rs   r   foundri   rj   rt   r;   mems           r!   r   r     sb   
%%!!O%8%M%M%O)P-BBNNP 	 **eyyEhllLLek	h	8??OOek	IIeii + %HLL00<<OOS. 	P 	
 
 &&s+Ux00<<OOHOO 4 	P 	
 
 &&s+)!)r"   c                     d| j                   v rt        | j                   d      d| j                   v rt        | j                   d      y )N@digest:tag)r   r   )r   s    r!   ValidateSettingsr   2  sA    HNN
8>>8
44
hnn
8>>5
11 r"   )-r&   
__future__r   r   r   r   r   rJ   googlecloudsdk.api_lib.utilr   r   rb   $googlecloudsdk.command_lib.artifactsr	   googlecloudsdk.command_lib.coder
   r   r   r   googlecloudsdk.command_lib.runr   r   r{   googlecloudsdk.corecore_exceptionsr   r   r   googlecloudsdk.core.utilr   GetMessagesModulerP   r)   Errorr   r+   r3   
DataObjectr5   r   r   r   r   r   r   r\   r"   r!   <module>r      s    7 &  % '  	 , A < 4 2 6 7 5 = = # * $ *,d,,UD9 8 	,, 		,W(z$$ W(t N"B2r"   