
    $                     h   d 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Zej2                   ej4                  ej6                  j8                        ej:                   G d dej<                                             Zy)z@Command for deploying containers from Compose file to Cloud Run.    N)base)docker_util)util)artifact_registry)compose_resource)
exceptions)flags)pretty_print)up)tracker)log)
properties)progress_trackerzcloud-run-source-deployc                   d    e Zd ZdZdddZed        Zed        Zd Zd Z	d	 Z
d
 Zd Zd Zd Zy)Upz/Deploy to Cloud Run from compose specification.z"          {description}
          z          To deploy a container from the source Compose file on Cloud Run:

              $ {command} compose.yaml

          To deploy to Cloud Run with unauthenticated access:

              $ {command} compose.yaml --allow-unauthenticated
         )DESCRIPTIONEXAMPLESc                 (   t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j
                  |        t        j                  |        | j                  ddd       y )Nz
--no-build
store_truez(Skip building from source if applicable.)actionhelp)r	   AddDeployFromComposeArgumentAddRegionArgAddDebugFlagAddDryRunFlag
AddDevFlagAddAllowUnauthenticatedFlagadd_argumentparsers    lib/surface/run/compose/up.py
CommonArgszUp.CommonArgs:   sr    	&&v.	v	v		V	%%f-
7      c                 .    t         j                  |        y )N)r   r"   r   s    r!   ArgszUp.ArgsH   s    MM&r#   c           
          |d||j                   |j                  |      }|j                  s7t        j                  d|j
                          t        j                  d      	 t        j                  j                  |j                  d         }t        j                   d       t        j                   d|        t        j                  d| j                  |      d	d
d       5 }	|j                  |||	|j                        }
t        j                   d|
        ddd       
j!                         } |d||||j                   |j                  ||      }|j                  rt        j"                  j                  |j                  d         }t        j                   d       t        j                   dt%        |j&                  j)                               t%        |j*                  j)                               z           |j*                  j)                         D ]  }t        j,                  |||        |j&                  j)                         D ]  }t        j,                  |||        |S t        j                  d|j
                          t        j                  d      # 1 sw Y   xY w# t.        $ r@}t        j                  d|        t        j                  d|j                           d}~ww xY w)z-Handles the resource and translate run logic.resource)commandcompose_filedebugdry_runregionz$Resource command failed with error: z)No resource config found in compose file.r   z+Successfully parsed resources config proto.zResourcesConfig:
zSetting up resources...zSetup failedFc                       y)NzResources setup complete. r.   r#   r!   <lambda>z-Up._ResourceAndTranslateRun.<locals>.<lambda>k   s    (Cr#   )failure_messagesuppress_outputdone_message_callbackzHandled ResourcesConfig:
N	translate)r(   repor)   resources_configr*   r+   project_numberr,   z1Successfully translated resources config to YAML.zYAML files:
)yaml_file_pathr,   argszTranslate failed with error: zOSomething went wrong while translating compose file to Cloud Run service YAMLs.z9Failed to handle resources config and translate to YAML: zRaw output: )r*   r+   stdoutr   errorstderrr   ConfigurationErrorr   ResourcesConfig	from_jsonr   StagedProgressTracker_AddTrackerStageshandle_resourcesno_buildto_jsonTranslateResultlistservicesvaluesmodelsdeploy_application	Exception)selfcommand_executorr)   r4   r6   r,   r8   resource_responseconfigr   r5   resources_config_jsonresponsetranslate_result
model_yamlservice_yamles                    r!   _ResourceAndTranslateRunzUp._ResourceAndTranslateRunL   s    )!jj ## 
ii01B1I1I0J
K ))
5 ;//99

"
"1
%f 
ii=>	ii$VH-.11
#

 
 
(( C !22D'4==
 			./?.@AB /668!#0

,,'	h 
+;;EEOOA
 			EF		$--4467$?O?V?V?]?]?_:``ac	
 +1188:J

-
-'T ; -55<<>L

-
-)&t ? o 			1(//1BCD++
 	
Y d  	iiKA3OP	ii,07789:s8   &BJ; )7J. EJ; 77J; .J83J; ;	L;K??Lc                    t         j                  j                  d       t        j                  |d      }| j                  |       t        j                  j                  j                  j                  d      }t        j                  |      }| j                  ||      }t        j                  ||t               }t#        j$                  |dd      r| j'                  |       t)        j*                         }|j,                  r|j,                  }n| j/                         }|j0                  r| j3                  ||||||      }	|	S  |d|||j4                  |j6                        }	|	S )	z=Deploy a container from the source Compose file to Cloud Run.z&Deploying from Compose to Cloud Run...T)prompt)required)
project_idlocation_idrepo_id)skip_activation_promptskip_console_promptr   )r(   r4   r)   r*   r+   )r   statusPrintr	   	GetRegion_SetRegionConfigr   VALUEScoreprojectGetprojects_utilGetProjectNumber_GenerateRepositoryNamer   
DockerRepoDEFAULT_REPO_NAMEr   ShouldCreateRepository_CreateARRepositoryr   RunComposeWrapperr)   _GetComposeFiledevrU   r*   r+   )
rK   r8   r,   rd   r6   r4   docker_reporL   r)   rP   s
             r!   RunzUp.Run   sF   JJ=>__T$/F&!$$,,00$0?G"33G<N''D ((!K //Dd {+++-&&l))+lxx..
L$h O "#

,,h Or#   c                 r    dj                  |      }dj                  ||j                  dd      t              S )z#Generate a name for the repository.z{}-docker.pkg.devz{}/{}/{}:/)formatreplacerj   )rK   rd   r,   
repositorys       r!   rh   zUp._GenerateRepositoryName   s8    $++F3JGOOC-/@ r#   c                 .   t         j                  j                  j                  j	                         sct
        j                  j                  dj                  |             t         j                  j                  j                  j                  |       yy)zSet the region config.z]Region set to {region}.You can change the region with gcloud config set run/region {region}.
)r,   N)
r   rb   runr,   re   r   r^   r_   ru   Set)rK   r,   s     r!   ra   zUp._SetRegionConfig   sj      ''++-	jj//5vVv/D ""&&v. .r#   c                     dD ]%  }t         j                  j                  |      s#|c S  t        j                  d      )N)zcompose.yamlzcompose.ymlzdocker-compose.yamlzdocker-compose.ymlz5No compose file found. Please provide a compose file.)ospathexistsr   r<   )rK   filenames     r!   rn   zUp._GetComposeFile   s=     
	! 
'
'? r#   c                 v    t        j                  d|j                          t        j                  |d       y)z6Create an Artifact Registry Repository if not present.z&Creating AR Repository in the region: T)r\   N)r
   Successlocationr   CreateRepository)rK   rp   s     r!   rl   zUp._CreateARRepository   s3    
01E1E0FG &&{4Pr#   c           	      8   g }|j                   rc|j                   D ]T  }|j                  t        j                  d| dt        j
                  j                  j                  |                   V |j                  rL|j                  t        j                  dt        j
                  j                  j                                      |j                  j                  s|j                  j                  rL|j                  t        j                  dt        j
                  j                  j                                      |j                  rL|j                  t        j                  dt        j
                  j                  j                                      |S )z&Add a tracker to the progress tracker.zBuilding container z from source...)	container)keyzCreating secrets...zCreating volumes...zCreating configs...)source_buildsappendr   StagestagesStagedProgressTrackerStageBUILDget_keysecretsSECRETSvolumes
bind_mountnamed_volumeVOLUMESconfigsCONFIGS)rK   rN   staged_operationscontainer_names       r!   r@   zUp._AddTrackerStages   sG   "00.  ""%n%5_E55;;CC, D 	
 1 ~~

 
 #33;;CCE ~~  FNN$?$?

 
 #33;;CCE ~~

 
 #33;;CCE r#   N)__name__
__module____qualname____doc__detailed_helpstaticmethodr"   r%   rU   rq   rh   ra   rn   rl   r@   r.   r#   r!   r   r   %   sg     8	-    Ob(T/Q"r#   r   ) r   r|   googlecloudsdk.callioper   $googlecloudsdk.command_lib.artifactsr   #googlecloudsdk.command_lib.projectsr   rf   googlecloudsdk.command_lib.runr   r   r   r	   r
   r   &googlecloudsdk.command_lib.run.composer   r   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   rj   HiddenReleaseTracksReleaseTrackALPHADefaultUniverseOnlyBinaryBackedCommandr   r.   r#   r!   <module>r      s    G 	 ( < E < ; 5 0 7 - D # * 8 .  D%%++,e	!	! e  - er#   