
    *                        d Z ddlZddlmZmZ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 ddlmZ ddlmZ  G d d      Z	 d'dee ef   de de de dejB                  de"ddfdZ#d Z$dee ef   de de dejB                  ddf
dZ%de d e de fd!Z&d"ed#e de de de dejB                  ddfd$Z'd% Z(d& Z)y)(zBuild config for Run Compose.    N)AnyDictOptional)encoding)cloudbuild_util)waiter)submit_util)connection_context)	platforms)serverless_operations)tracker)
exceptions)log)
properties)	resources)yaml)progress_tracker)filesc                   j    e Zd ZdZ	 d
dee   dee   fdZedeee	f   dd fd       Z
deee	f   fd	Zy)BuildConfigz1Represents the build configuration for a service.Ncontext
dockerfilec                 .    || _         || _        d | _        y Nr   r   image_id)selfr   r   s      5lib/googlecloudsdk/command_lib/run/compose/builder.py__init__zBuildConfig.__init__)   s     DL DO#'DM    datareturnc                 R     | |j                  d      |j                  d            S )Nr   r   )r   r   )get)clsr!   s     r   	from_dictzBuildConfig.from_dict0   s'    #88L) r    c                 J    | j                   | j                  | j                  dS )z4Serializes the BuildConfig instance to a dictionary.r   r   )r   s    r   to_dictzBuildConfig.to_dict7   s#     <<ooMM r    )NN)__name__
__module____qualname____doc__r   strr   classmethodr   r   r&   r(    r    r   r   r   &   sg    9 HL(c](7?}( 4S> m  tCH~ r    r   source_buildrepoproject_nameregionr   no_buildr"   c           	      r   |rt        | |||       yg }| j                         D ]  \  }}	 t        ||||||      \  }	}
}|j                  |	       t	        |	      }|rV|d   dk7  rN|j                  t        j                  j                  j                  |      ddj                  |
             nd}|j                  d      r|j                  d      j                  d	      rl|j                  d      j                  d	      D ]I  }|j                  d
      |k(  s|j                  d      }|s,|j                  d      \  }}}|dz   |z   } n |r!||_        t        j                  d| d       n ||_        t        j                  d| d       |j!                  t        j                  j                  j                  |              y# t"        j$                  $ r!}t        j&                  d| d|         d}~wt(        j*                  $ r}t        j&                  d|         d}~ww xY w)zUPerforms source build across containers subsequetially mentioned in the compose file.NstatusSUCCESS	containerzCContainer build failed and logs are available at [{build_log_url}].build_log_url)messageresultsimagesnamedigest:@zImage 'z
' created.zBuild failed for container z: z+An error occurred during build submission: )_handle_no_builditems_build_from_sourceappend_poll_until_build_completes	FailStagetracker_stagesStagedProgressTrackerStageBUILDget_keyformatr$   
rpartitionr   r   debugCompleteStager	   FailedBuildExceptionerrorr   Error)r0   r1   r2   r3   r   r4   build_opr9   build_configbuild_op_refr;   	image_tagresponse_dictimage_with_digestimgr@   image_name_without_tag_es                      r   handler^   @   sF    \<A
(!-!3!3!5i2/A
	4vw0,lM9 ool#1,?m	=2i?55;;CC# D  ;;A6"/ <B < 	 	
 !Y'M,=,=i,H,L,L-
 #&&y155h?cwwv)+wwx(f/8/C/CC/H,&1*S069 "  @ "3,

))g/0
;
<"+,

))gi[
3
455;;CC# D 	
S "6\ ++ 	ii-i[1#>? 	ii=aSABs1   C2GG3BGH6&HH6H11H6c                 v   t         j                  j                  j                  j	                  d      }t        j                  dt        j                  |      }t        j                  j                  | d|id      }t        j                  |      5 }|j                  |      cddd       S # 1 sw Y   yxY w)z+Get service if it exists, else return None.TrequiredN)region_labelnamespacesIdrun.namespaces.servicesparams
collection)r   VALUEScoreprojectGetr
   GetConnectionContextr   PLATFORM_MANAGEDr   REGISTRYParser   Connect
GetService)service_namer3   rj   conn_contextservice_refclients         r   _get_servicerv      s    ""**...='#88
  ,
 ""((g&* ) +
 $$\2f[) 322s   B//B8c                 $   | syt         j                  j                  j                  j	                  d      }t
        j                  j                  |d|id      }t        j                  dt        j                  |      }t        j                  |      5 }|j                  |      }ddd       st        j                   d| d	      i }	|r^|j"                  j$                  j&                  r>|j"                  j$                  j&                  D ]  }
|
j(                  |	|
j*                  <    | j-                         D ]  \  }}t.        j0                  j2                  j5                  |
      }	 |j7                  |       |	j9                  |      }|st        j                   d| d| d	      ||_        |j=                  |d| d       |j?                  |        y# 1 sw Y   9xY w# t@        $ r}|jC                  ||d        d}~ww xY w)zHandles --no-build flag.NTr`   rc   rd   re   )platformrb   z?--no-build cannot be used for the first deployment of service 'z'.r8   z$Could not find image for container 'z' in service 'zUsing image [z] from deployed service.zImage retrieval failed.)"r   rh   ri   rj   rk   r   rn   ro   r
   rl   r   rm   r   rp   rq   r   rS   templatespec
containersimager?   rD   rI   rJ   rK   rL   
StartStager$   r   UpdateStagerP   	ExceptionrH   )r0   r2   r3   r   rj   rt   rs   ru   existing_servicecontainer_to_image_mapcr9   rU   	stage_keyr|   r]   s                   r   rC   rC      s    

""**...='""((g&* ) +
 $88
)),
 $$\2f((5 3	


	N"	  *3388CC&&++66'(wwQVV$ 7 ".!3!3!5i99??GG H I#$((3e29+ >b"
 	
 $l
}UG+CD I&! "6 32:  	1&?@s%   G /A.G- G*-	H6H

Hr   rW   c                    d| ddddd|dgdg|gd	}t         j                  j                  | d
      }t         j                  j                  |d      }	 t        j                  |       t        j
                  |      5 }t        j                  ||       ddd       t        j                  d| d       |S # 1 sw Y   $xY w# t        $ r }t        j                  d| d|       d}~ww xY w)zWrites a cloudbuild.yaml file to the service source directory.

  Args:
    context: The build context directory.
    image_tag: The full tag for the image to be built.

  Returns:
    The path to the written cloudbuild.yaml file.
  zBuild Docker Image: zgcr.io/cloud-builders/dockerbuildxbuildz--loadz-t.)idr?   args)stepsr>   outzcloudbuild.yamlNzWrote Cloud Build config to ''z'Failed to write Cloud Build config to 'z': )ospathjoinr   MakeDir
FileWriterr   dumpr   rO   r   r   rS   )r   rW   config_dataout_dir	file_pathfr]   s          r   _write_cloudbuild_configr      s     'yk20WhiE 	
 + GGLL%('ggll7$56)		MM'			)	$
iiQ 
%II-i[:; 
%	$ 
 



1)CsC s0   *C  B:"C :C?C 	C/C**C/configr9   c                    | j                   }|t        d      dj                  |||d      }t        ||      }t	        j
                         }	t        j                  d| d| d       t        j                  d!i d	dd
dd|	ddd|ddddd|dddddddddddddddddddddddddd}
t        j                  d        t        |||	|
|      \  }}|||fS )"z?Performs source build for a given container using build config.Nz+Build context is required for source build.z'{repo}/{project_name}_{container}:{tag}latest)r1   r2   r9   tagz!Creating build config for image 'z' from source 'r   r   no_cacheFmessagessubstitutions
arg_configis_specified_sourceT	no_sourcesourcegcs_source_staging_dirignore_filearg_gcs_log_dirarg_machine_typearg_disk_sizearg_worker_poolarg_dirarg_revisionarg_git_source_dirarg_git_source_revisionarg_service_account	buildpack	hide_logsz&Submitting build to Google Cloud Buildr/   )r   
ValueErrorrM   r   r   GetMessagesModuler   rO   r	   CreateBuildConfig_build_using_cloud_build)r   r9   r1   r2   r3   r   source_pathrW   config_pathr   rU   rV   r;   s                r   rE   rE      s    +
B
CC7>>liX ? )
 )i@+ ..0( )))) 5-q .. 
  	
     "        !" #$ #%& '( )* +,2 ))45 8(L&!, 
}i	//r    c                    t        j                  |d|d|      \  }}d|j                   d| d|j                   }t        j
                  j                  |d      }|j                  }	t        j                  j                  j                  |       }
|j                  |
       |j                  |
d	j                  |	
             ||	fS )zGBuild an image from source if a user specifies a source when deploying.T)r   build_regionz	projects/z/locations/z/operations/z(cloudbuild.projects.locations.operations)rg   r8   z(Logs are available at [{build_log_url}].r:   )r	   Build	projectIdr   r   rn   ParseRelativeNamelogUrlrI   rJ   rK   rL   r}   r~   rM   )r9   r   build_messagesrU   r3   r   r\   rT   rV   r;   r   s              r   r   r   )  s     
(% 	%//"+fX\%((L  ##55E 6 , ,,-77==EE F ) 
Y	077% 8  
}	$$r    c                     t        j                         }t        j                  |j                  |j
                        }t        j                  ||       }t        j                  |j                        S r   )
r   GetClientInstancer   CloudOperationPollerprojects_builds
operationsPollUntilDoner   MessageToPyValueresponse)rV   ru   poller	operations       r   rG   rG   H  s\    ,,.&&&f//& ""6<8)		"	"9#5#5	66r    )F)*r,   r   typingr   r   r   apitools.base.pyr   !googlecloudsdk.api_lib.cloudbuildr   googlecloudsdk.api_lib.utilr   !googlecloudsdk.command_lib.buildsr	   googlecloudsdk.command_lib.runr
   r   r   &googlecloudsdk.command_lib.run.composer   rI   googlecloudsdk.corer   r   r   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r   r-   StagedProgressTrackerboolr^   rv   rC   r   rE   r   rG   r/   r    r   <module>r      sl   $ 	 & & % = . 9 = 4 @ L * # * ) $ 8 * @ @sK'(@
@ @ 	@
 33@ @ 
@F*"4sK'(44 4 33	4
 
4nc c c B:0:0:0 :0 	:0
 :0 33:0 
:0z%>7r    