
    6+                        S r SSKJ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J	r	  SSKJ
r  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  \	R<                  " SS5      rSr  " S S\RB                  5      r"S r#S r$ " S S\RJ                  5      r&S r'S r(S r)S  r*S! r+S" r,g)#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                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )ImageFormatError,   z:An error thrown when the provided image has a tag or hash.c                 F   > [         [        U ]  SR                  X5      S9  g )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   ImageFormatError.__init__/   s'    	
D*(
&
	 +      )__name__
__module____qualname____firstlineno____doc__r   __static_attributes____classcell__)r!   s   @r"   r   r   ,   s    B r$   r   c                     U [         :H  $ )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"   _IsGcpBaseBuilderr0   8   s     
+	++r$   c                 B    [        U 5      n[        R                  " XSS9$ )NF)buildertrustdevmode)r0   r
   BuildpackBuilder)builder_argis_gcp_base_builders     r"   _BuilderFromArgr8   D   s%    )+6		"	"e
 r$   c                   B    \ rS rSrSr/ SQr\S 5       rS rS r	S r
Srg	)
SettingsK   a  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regionr2   service_nameservicecontextcpumemoryar_repo
local_portservice_accountc           	         [         R                  R                  [        R                  " 5       5      nUR                  SS5      R                  SS5      R                  5       n[        R                  SSS9nSn[        R                  " US9nU " UUU[         R                  R                  [        R                  " 5       5      S	9$ )
N_- zserving.knative.dev/v1Service)
apiVersionkind
Dockerfile
dockerfile)r>   r?   r2   r@   )ospathbasenamer   GetCWDreplacelowerRUN_MESSAGES_MODULErJ   r
   DockerfileBuilderabspath)clsdir_namer>   r?   dockerfile_arg_defaultr/   s         r"   DefaultsSettings.Defaultsm   s    ww/H##C-55c3?EEGL!))+) * G *%%1GHD!/	 r$   c                    [         R                  " U5      nSU;   a  US	 [        R                  " US/5      nUR	                  SS5      nUb&  [        U[        5      (       d  [        U5      US   S'    [        R                  " U[        R                  5      nU R                  (       a$  [        U R                  5      WR                  l        SW0nUR"                  R$                  R"                  R&                  S   nUR(                  US	'   UR*                  (       aZ  UR*                  R,                  (       a?  UR*                  R,                  R.                   H  n	U	R0                  XyR2                  '   M     UR                  R4                  (       a  UR                  R4                  US
'   U R6                  " S0 UD6$ ! [        R                   a   n[        R                  " USS9   SnAGNUSnAff = f)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_textr?   r   r   r>   r%   )r   	load_pathr   GetOrCreateget
isinstancestrmessages_utilDictToMessageWithErrorCheckrV   rJ   ScalarTypeMismatchErrorr   MaybeRaiseCustomFieldMismatchr<   r`   ra   spectemplate
containersr   	resourceslimitsadditionalPropertiesvaluekeynamerT   )
r   	yaml_pathservice_dictr`   ra   r?   ereplacements	containerlimits
             r"   WithServiceYaml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%"'++YY C%,%5%5%:%:l>"<<',''+ 00 	..
 	s   4%F? ?G3G..G3c                    [         R                  R                  R                  R	                  5       n[
        R                  " USS9nX#S.nS H(  nUR                  U5      (       d  M  [        X5      XE'   M*     U R                  nUR                  (       a)  [        R                  R                  UR                  5      nXdS'   UR                  S5      (       a  [        UR                  5      US'   OUR                  S5      (       a"  [         R"                  " UR$                  S9US'   OM['        U R                  [         R"                  5      (       a$   U R                  US'   US   R)                  U5        U R4                  " S
0 UD6$ ! [         R*                   a4    [,        R.                  R1                  S	5        [        [2        5      US'    NYf = f)z%Update parameters based on arguments.T)prompt)r<   r=   )rD   rB   rA   r   r>   rE   r@   r2   rO   rN   zCNo Dockerfile detected. Using GCP buildpacks to build the containerr%   )r   VALUEScorer<   Get	run_flags	GetRegionIsKnownAndSpecifiedgetattrr@   sourcerP   rQ   rX   r8   r2   r
   rW   rO   rf   ValidateInvalidLocationErrorr   r_   Printr.   rT   )r   argsr<   r=   rx   override_argr@   s          r"   WithArgsSettings.WithArgs   s   $$,,002G  d3F&9L 
	!	!,	/	/%,T%@" llG{{,g%	** / =l9		!	!,	/	/ ( : :__!l9 
DLL("<"<	=	=	P$(LL,y
!
y
!
*
*7
3 <<','' ,, 	P
**

< %44N$O,y
!	Ps   #F
 
AGGc                     0 nU R                   (       dF  [        R                  " U R                  U R                  SS9nX!S'   [        X R                  5      US'   U R                  " S0 UD6$ )Nzcloud-run-source-deploy)
project_idlocation_idrepo_idrC   r   r%   )r   r	   
DockerRepor<   r=   _DefaultImageNamer>   rT   )r   rx   rC   s      r"   BuildSettings.Build   s`    L::&&\\kk+g
 !(9/9J9JKl7<<',''r$   r%   N)r&   r'   r(   r)   r*   NAMESclassmethodr\   r{   r   r   r+   r%   r$   r"   r:   r:   K   s3    $%   .(`'(R(r$   r:   c                 >   [         R                  5       n[        U SS 5      =(       d    [        R                  R
                  n[        U SS 5      n[        R                  " X#5      nU(       a  UR                  U5      nUR                  U 5      nUR                  5       $ )Nr   service_config)r:   r\   r   rP   rQ   curdirr   ChooseExistingServiceYamlr{   r   r   )r   settingscontext_dirr   	yaml_files        r"   AssembleSettingsr      sz     (h-?+4!148...{K)''	2Ht$(		r$   c                    [         R                  " U R                  5      nUR                  =(       d    [        R                  5       nU R                  Ul        [        U R                  5      Ul
        X!l        [        U5        U R                  (       a/  U R                  UR                  R                  R                  l        UR                  R                  R                  R                   S   nU R"                  Ul        [%        X05        U$ )zEGenerate a service configuration from a Cloud Settings configuration.r   )copydeepcopyr?   r`   rV   
ObjectMetar>   rt   rg   r<   ra   _BuildSpecTemplaterE   rl   rm   serviceAccountNamern   r   _FillContainerRequirements)r   r?   r`   ry   s       r"   GenerateServicer      s    MM(**+'A!4!?!?!A(''(-8++,(W4<4L4LGLL1ll##((33A6)NN)/Y1	.r$   c                 H   U R                   (       d  [        R                  5       U l         U R                   R                  (       d#  [        R	                  5       U R                   l        U R                   R                  R                   (       d-  [        R                  5       U R                   R                  l         U R                   R                  R                   R                  (       d9  [        R                  5       /U R                   R                  R                   l        g g )N)rl   rV   ServiceSpecrm   RevisionTemplateRevisionSpecrn   	Container)r?   s    r"   r   r      s    	&224GL			/@@BGLL				#	#!4!A!A!CGLL				#	#	.	.-@-J-J-L,MGLL) 
/r$   c                 >    SR                  U R                  5       US9$ )Nz{repo}/{service})repor?   )r   GetDockerString)rC   r>   s     r"   r   r     s(    		"	"""$l 
# 
 r$   c                    [        5       nU R                  =(       d    [        R                  5       nUR                  =(       d    [        R                  R                  5       nUR                   H  nUR                  S:X  a#  UR                  (       a  UR                  Ul	        O2UR                  S:X  a"  UR                  (       a  UR                  Ul	        UR                  UR                  5        M     SU;  ag  UR                  (       aV  [        R                  R
                  R                  S[        UR                  5      S9nUR                  R                  U5        SU;  ag  UR                  (       aV  [        R                  R
                  R                  S[        UR                  5      S9nUR                  R                  U5        XCl        X0l        g)z:Set the container CPU and memory limits based on settings.rA   rB   )rs   rr   N)setro   rV   ResourceRequirementsrp   LimitsValuerq   rs   rA   rr   rB   addAdditionalPropertyrg   append)ry   r   foundro   rp   rz   rA   mems           r"   r   r     s^   
%%!!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                     SU R                   ;   a  [        U R                   S5      eSU R                   ;   a  [        U R                   S5      eg )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   rP   googlecloudsdk.api_lib.utilr   r   rh   $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   GetMessagesModulerV   r.   Errorr   r0   r8   
DataObjectr:   r   r   r   r   r   r   r%   r$   r"   <module>r      s    7 &  % '  	 , A < 4 2 6 7 5 = = # * $ *,,UD9 8 	,, 		,W(z$$ W(t N"B2r$   