
    0                        d 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 ddlmZ ddlmZ dddZdZdZd Z ej<                  ej>                  j@                  ej>                  jB                  ej>                  jD                        ejF                   G d dejH                                      Z%y)zCreate a release.    N)client_util)release)base)
exceptions)delivery_pipeline_util)deploy_policy_util)deploy_util)flags)promote_util)release_util)resource_args)log)	resources)files)timesz{description}a       To create a release with source located at storage URL `gs://bucket/object.zip`
    and the first rollout in the first target of the promotion sequence:

       $ {command} my-release --source=`gs://bucket/object.zip` --delivery-pipeline=my-pipeline --region=us-central1

    To create a release with source located at current directory
    and deploy a rollout to target prod :

      $ {command} my-release --delivery-pipeline=my-pipeline --region=us-central1 --to-target=prod

    The following command creates a release without a `skaffold.yaml` as input, and generates one
    for you:

      $ {command} my-release --delivery-pipeline=my-pipeline --region=us-central1 --from-k8s-manifest=path/to/kubernetes/k8.yaml

    The current UTC date and time on the machine running the gcloud command can
    also be included in the release name by adding $DATE and $TIME parameters:

      $ {command} 'my-release-$DATE-$TIME' --delivery-pipeline=my-pipeline --region=us-central1

    If the current UTC date and time is set to 2021-12-21 12:02, then the created release
    will have its name set as my-release-20211221-1202.

    When using these parameters, please be sure to wrap the release name in single quotes
    or else the template parameters will be overridden by environment variables.
    )DESCRIPTIONEXAMPLESr      c                    t        j                  | dd       t        j                  |        t        j                  |        t        j
                  |        t        j                  |        t        j                  | d       t        j                  | t               t        j                  | t               t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j                   |        t        j"                  |        t        j$                  |        t        j&                  |        t        j(                  |        y)zRegister flags for this command.

  Args:
    parser: An argparse.ArgumentParser-like object. It is mocked out in order to
      capture some information, but behaves like an ArgumentParser.
  T)
positionalrequiredzDescription of the release.N)r   AddReleaseResourceArgr
   AddGcsSourceStagingDirFlagAddImagesGroupAddIgnoreFileFlagAddToTargetFlagAddDescriptionAddAnnotationsFlag_RELEASEAddLabelsFlagAddDockerVersionAddHelmVersionAddKptVersionAddKubectlVersionAddKustomizeVersionAddSkaffoldVersionAddConfigSourcesGroupAddInitialRolloutGroupAddDeployParametersFlagAddOverrideDeployPoliciesparsers    %lib/surface/deploy/releases/create.py_CommonArgsr.   I   s    %%fM""6*v&!v<=68,fh' vf&!F#6"f%v&'!!&)    c                   0    e Zd ZdZeZed        Zd Zd Z	y)Createz3Creates a new release, delivery pipeline qualified.c                     t        |        y )N)r.   r+   s    r-   ArgszCreate.Argsm   s
    r/   c                 @   d }|j                   }|y|j                  xsD |j                  xs6 |j                  xs( |j                  xs |j
                  xs |j                  }|s|j                  y ||j                  j                        }|p|t        j                         z
  t        j                  t              k  r>t        j                  j!                  dj#                  |j%                  d                   yt&        j(                  j*                  |j                  ft&        j(                  j,                  |j                  ft&        j(                  j.                  |j                  ft&        j(                  j0                  |j                  ft&        j(                  j2                  |j
                  ft&        j(                  j4                  |j                  fg}g }|D ]a  \  }}	|	s	 ||	j                        }||t        j                         z
  t        j                  t              k  sQ|j7                  |       c |rMdj9                  |D cg c]  }|j:                   c}      }
t        j                  j!                  d|
 d	       yyc c}w )
z>Checks to see if a release is close to the maintenance window.c                     | y	 t        j                  |       S # t         j                  t         j                  f$ r Y yw xY w)z8Parses the maintenance dt, returning a datetime or None.N)r   ParseDateTimeDateTimeSyntaxErrorDateTimeValueError)dts    r-   _ParseDtz0Create._CheckIfNearMaintenance.<locals>._ParseDtt   sC    	""2&&'')A)AB s    #A A N)daysa!  WARNING: This release's Skaffold version will be in maintenance mode beginning on {date}. After that you won't be able to create releases using this version of Skaffold.
https://cloud.google.com/deploy/docs/using-skaffold/select-skaffold#skaffold_version_deprecation_and_maintenance_policyz%Y-%m-%d)datez, z'WARNING: The versions used for tools: [z] will be in maintenance mode soon. After that you won't be able to create releases using these versions of the tools.
https://cloud.google.com/deploy/docs/select-tool-version)	conditiondockerVersionSupportedConditionhelmVersionSupportedConditionkptVersionSupportedCondition kubectlVersionSupportedCondition"kustomizeVersionSupportedCondition!skaffoldVersionSupportedConditionskaffoldSupportedConditionmaintenanceModeTimer   Nowdatetime	timedelta_MAINTENANCE_WARNING_DAYSr   statusPrintformatstrftimer   ToolsDOCKERHELMKPTKUBECTL	KUSTOMIZESKAFFOLDappendjoinvalue)selfrelease_objr:   release_conditionhas_tool_versionsmaintenance_dt$tools_supported_condition_to_processtools_almost_in_maintenancetoolr=   joineds              r-   _CheckIfNearMaintenancezCreate._CheckIfNearMaintenanceq   s    $--  	99 	?::	?99	? ==	? ??		?
 >>  		5	5	=

6
6
J
Jn 
$uyy{*%>?@ 	

& '-f#,,Z8 '- '
	
 
 %%==	

 ##;;	

 ""::	

 &&>>	

 ((@@	

 ''??	
+,(4 #%?i	 = =>n

$uyy{*%>?@ 	$**40 @ #yy1LM1L$**1LMNf	jj3F8 <E E #Ms   Jc                    |j                   r"|j                  rt        j                  dd      t	        j
                  |j                  j                  j                        |j                  j                  _        |j                  j                  j                         }t        j                  |j                         j                               }dj                  |j                               }t        j                  ||       |j                   rt"        j$                  j'                  |j                         r|j(                  dk(  rt#        j*                         }d}n|j(                  }d}t-        j.                  ||j                         s/t1        j2                  dj                  |j                   |            t"        j$                  j5                  t"        j$                  j7                  |j                         t"        j$                  j7                  |            |_        |j8                  rt"        j$                  j'                  |j8                        r|j(                  dk(  rt#        j*                         }d}n|j(                  }d}t-        j.                  ||j8                        s/t1        j2                  dj                  |j8                  |            t"        j$                  j5                  t"        j$                  j7                  |j8                        t"        j$                  j7                  |            |_        t        j:                         }t	        j<                  |j(                  |j>                  |j@                  |jB                  |jD                  |jF                  |jH                  |jJ                  |jL                  |jN                  |jP                  |jR                  |j                   |j8                  |jU                         d	   |jV                  |jX                  |jZ                  ||j\                        }t_        j`                  |jb                  |t^        jd                  jf                  |jh                  |jj                         |jm                  ||      }	tn        jp                  js                  |	jt                  d
      }
tw        jx                         j{                  |	|
       t|        j~                  j                  dj                  |j                                      t        j:                         j                  |j                               }| j                  |       |j                   r|S |j                         }t        j                  ||j                        }t        j                  |||j                  d|j                  |j                  |j                  |      }||fS )zThis is what gets called when the user runs this command.

    Args:
      args: All the arguments that were provided to this command invocation.

    Returns:
      The release and rollout created.
    z--disable-initial-rolloutz--to-targetzCannot create release {}..zcurrent working directorysourcez[The skaffold file {} could not be found in the {}. Please enter a valid Skaffold file path.zeThe deploy config file {} could not be found in the {}. Please enter a valid deploy config file path.locationsIdz)clouddeploy.projects.locations.operations)
collectionz Created Cloud Deploy release {}.T)	is_createlabelsannotationsstarting_phase_idoverride_deploy_policies)Ldisable_initial_rollout	to_targetc_exceptionsConflictingArgumentsExceptionr   RenderPatternCONCEPTSparsed_argsr   Parser   GetPipelineParentRelativeNamerL   ThrowIfPipelineSuspendedskaffold_fileospathisabsrd   getcwdr   IsDirAncestorOfcore_exceptionsErrorrelpathabspathdeploy_config_fileReleaseClientCreateReleaseConfiggcs_source_staging_dirignore_fileimagesbuild_artifactsdescriptiondocker_versionhelm_versionkpt_versionkubectl_versionkustomize_versionskaffold_versionAsDictuidfrom_k8s_manifestfrom_run_manifestdeploy_parametersr	   SetMetadatamessagesResourceTypeRELEASEri   rh   r1   r   REGISTRYParseRelativeNamenamer   OperationsClientWaitForOperationr   rJ   rK   NameGetra   r   CreateDeployPolicyNamesFromIDsrk   r   Promoteinitial_rollout_labelsinitial_rollout_annotationsinitial_rollout_phase_id)rX   argsrelease_refpipeline_objfailed_activity_msgrd   source_descriptionclientrelease_config	operationoperation_refrY   pipeline_refpoliciesrollout_resources                  r-   Runz
Create.Run   s    ##66
%}  )5(B(B!!)))DMM% --''--/K)55))+L 6<<  " 33) bggmmD,>,>?		8%""64+=+=>##++16""$6,
 	
 77??
''//$,,
-rwwv/Fd 277==1H1H#I		8%""64+B+BC##66<f'');7
 	
 !#
''//$11
2BGGOOF4K!d ""$F!55##]+)N.   (( k>:I&&88#N 9 M   "33I}MJJ*11+2B2B2DE '')--k.F.F.HIK  -## %%'L!@@d33H $++**4477!)	 (((r/   N)
__name__
__module____qualname____doc___DETAILED_HELPdetailed_helpstaticmethodr3   ra   r    r/   r-   r1   r1   d   s+    
 < - _BC)r/   r1   )&r   rG   os.pathry   "googlecloudsdk.api_lib.clouddeployr   r   googlecloudsdk.callioper   r   rn   !googlecloudsdk.command_lib.deployr   r   r	   r
   r   r   r   googlecloudsdk.corer~   r   r   googlecloudsdk.core.utilr   r   r   r   rI   r.   ReleaseTracksReleaseTrackALPHABETAGADefaultUniverseOnlyCreateCommandr1   r   r/   r-   <module>r      s       : 6 ( > D @ 9 3 : : ; = # ) * * #<  *6 T..33T5F5F5I5I m)T m) m)r/   