
    C                     @   d Z ddlmZ ddlmZ ddlm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 Zd Zej                    ej"                  ej$                  j&                  ej$                  j(                         G d dej*                                      Zy)z?Implementation of gcloud dataflow flex_template build command.
    )absolute_import)division)unicode_literalsN)apis)actions)arg_parsers)base)
propertiesc           	      ~
   | j                  d      }|j                         }|j                         }| j                  dddt        j                  dd             |j                  d	d
       | j                  ddt        j                  dd             | j                  ddt        j                  dd             | j                  ddt        j                  dd             | j                  ddg dd       | j                  ddt        j
                                | j                  dddt        j                  t        j                  j                  j                               | j                  dd t        j                  dd             | j                  d!d"t        j                  dd             | j                  d#t        j                  d$d%      d&'       | j                  d(t        j                  d$d%      d)'       | j                  d*t        d+'       | j                  d,t        j                  t        j                  j                  j                        d-.       | j                  d/t        d0'       | j                  d1d2       | j                  d3d4       | j                  d5d6       | j                  d7d8       | j                         }|j                  d9d:       |j                  d;d<       | j                  d=t        j                  t        j                  j                  j                        d>.       | j                  d?d@t        j                  dd      dAB       | j                  dCdDt        j                         t        j                   dEF       | j                  dGdHt        j"                         t        j                   dIF       |j                  dJdKt        j                  dLdM      dN       |j                  d      }|j                  dOdPt        j                         t        j                   dQF       |j                  dRdSt        j                         t        j                   dTF       |j                  dUdVdWX       |j                  dYddZt        j
                         d[\       |j                  d]d^d_X       |j                  d`dat        j                  dbdc      dN       |j                  dddet        j"                         t        j                   dfdg       yA)hzmRegisters flags for this command.

  Args:
    parser: argparse.ArgumentParser to register arguments with.
  T)requiredtemplate_file_gcs_pathTEMPLATE_FILE_GCS_PATHz]The Google Cloud Storage location of the flex template file.Overrides if file already exists.z^gs://.*zMust begin with 'gs://')metavarhelptypez--imagezLPath to the any image registry location of the prebuilt flex template image.)r   z%--image-repository-username-secret-ida  Secret Manager secret id for the username to authenticate to private registry. Should be in the format projects/{project}/secrets/{secret}/versions/{secret_version} or projects/{project}/secrets/{secret}. If the version is not provided latest version will be used.zD^projects\/[^\n\r\/]+\/secrets\/[^\n\r\/]+(\/versions\/[^\n\r\/]+)?$z~Must be in the format 'projects/{project}/secrets/{secret}' or'projects/{project}/secrets/{secret}/versions/{secret_version}'.)r   r   z%--image-repository-password-secret-ida  Secret Manager secret id for the password to authenticate to private registry. Should be in the format projects/{project}/secrets/{secret}/versions/{secret_version} or projects/{project}/secrets/{secret}. If the version is not provided latest version will be used.z--image-repository-cert-patha)  The full URL to self-signed certificate of private registry in Cloud Storage. For example, gs://mybucket/mycerts/selfsigned.crt. The certificate provided in Cloud Storage must be DER-encoded and may be supplied in binary or printable (Base64) encoding. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by -----END CERTIFICATE-----. If this parameter is provided, the docker daemon in the template launcher will be instructed to trust that certificate. z--sdk-languagez&SDK language of the flex template job.)JAVAPYTHONGOYAML)r   choicesr   z--metadata-filez;Local path to the metadata json file for the flex template.z--print-onlyzKPrints the container spec to stdout. Does not save in Google Cloud Storage.F)r   defaultactionz--staging-locationzbDefault Google Cloud Storage location to stage local files.(Must be a URL beginning with 'gs://'.)z--temp-locationzDefault Google Cloud Storage location to stage temporary files. If not set, defaults to the value for --staging-location.(Must be a URL beginning with 'gs://'.)z--service-account-emailz	.*@.*\..*z"must provide a valid email addressz.Default service account to run the workers as.)r   r   z--cloud-build-service-accounta  Service account to run the Cloud Build in the format projects/{project}/serviceAccounts/{service_account}. Ensure that the account you are using to run 'gcloud dataflow flex-template build' has 'ServiceAccountUser' role on the specified Cloud Build service account you provide with the --cloud-build-service-account flag. The specified service account must have required permissions to build the image. If the specified service account is in a project that is different from the project where you are starting builds, see https://cloud.google.com/build/docs/securing-builds/configure-user-specified-service-accounts#cross-project_set_up to grant the necessary access.z--max-workersz)Default maximum number of workers to run.z--disable-public-ipsz8Cloud Dataflow workers must not use public IP addresses.)r   r   z--num-workersz,Initial number of workers to use by default.z--worker-machine-typezIDefault type of machine to use for workers. Defaults to server-specified.z--subnetworkzODefault Compute Engine subnetwork for launching instances to run your pipeline.z	--networkzLDefault Compute Engine network for launching instances to run your pipeline.z--dataflow-kms-keyz3Default Cloud KMS key to protect the job resources.z--worker-regionz%Default region to run the workers in.z--worker-zonez#Default zone to run the workers in.z--enable-streaming-enginez9Enable Streaming Engine for the streaming job by default.z--gcs-log-dirzYGoogle Cloud Storage directory to save build logs.(Must be a URL beginning with 'gs://'.)N)r   r   r   z--additional-experimentsADDITIONAL_EXPERIMENTSz'Default experiments to pass to the job.)r   r   r   r   z--additional-user-labelsADDITIONAL_USER_LABELSz]Default user labels to pass to the job. Example: --additional-user-labels='{"key1":"value1"}'z--image-gcr-pathzpThe Google Container Registry or Google Artifact Registry location to store the flex template image to be built.z0^(.*\.){0,1}gcr.io/.*|^(.){2,}-docker.pkg.dev/.*a  Must begin with '[multi-region.]gcr.io/' or '[region.]-docker.pkg.dev/'. Please check https://cloud.google.com/container-registry/docs/overview for available multi-regions in GCR or https://cloud.google.com/artifact-registry/docs/repo-organize#locations for available location in GAR)r   r   r   z--jarJARa  Local path to your dataflow pipeline jar file and all their dependent jar files required for the flex template classpath. You can pass them as a comma separated list or repeat individually with --jar flag. Ex: --jar="code.jar,dep.jar" or --jar code.jar, --jar dep.jar.z	--py-pathPY_PATHa<  Local path to your dataflow pipeline python files and all their dependent files required for the flex template classpath. You can pass them as a comma separated list or repeat individually with --py-path flag. Ex: --py-path="path/pipleline/,path/dependency/" or --py-path path/pipleline/, --py-path path/dependency/.z--go-binary-pathGO_BINARY_PATHzLocal path to your compiled dataflow pipeline Go binary. The binary should be compiled to run on the target worker architecture (usually linux-amd64). See https://beam.apache.org/documentation/sdks/go-cross-compilation/ for more information.)r   r   z--yaml-pipeline-pathYAML_PIPELINE_PATHz&Local path to your YAML pipeline file.)r   r   r   r   z--yaml-image
YAML_IMAGEzLPath to the any image registry location of the prebuilt yaml template image.z--flex-template-base-imagea  Flex template base image to be used while building the container image. Allowed choices are allowed labels (JAVA11, JAVA17, JAVA21, PYTHON3, GO), supported distroless images (JAVA11_DISTROLESS, JAVA17_DISTROLESS, JAVA21_DISTROLESS, GO_DISTROLESS), or full gcr.io path of the specific version of the base image. For labels, we use the latest base image version to build the container. You can also provide a specific version from this link  https://gcr.io/dataflow-templates-base/zF^(JAVA11|JAVA17|JAVA21|GO)(_DISTROLESS)?$|^JAVA8$|^PYTHON3$|^gcr.io/.*zMust be JAVA11, JAVA17, JAVA21, PYTHON3, GO, (or with `_DISTROLESS` suffix for supported distroless variants), or begin with 'gcr.io/'z--envENVaX  Environment variables to create for the Dockerfile. You can pass them as a comma separated list or repeat individually with --env flag. Ex: --env="A=B,C=D" or --env A=B, --env C=D.When you reference files/dir in env variables, please specify relative path to the paths passed via --py-path.Ex: if you pass. --py-path="path/pipleline/" then set FLEX_TEMPLATE_PYTHON_PY_FILE="pipeline/pipeline.py" You can find the list of supported environment variables in this link. https://cloud.google.com/dataflow/docs/guides/templates/configuring-flex-templates#setting_required_dockerfile_environment_variables.)r   r   r   r   r   )add_mutually_exclusive_groupadd_argument_groupadd_argumentr   RegexpValidatorFileContentsr   StoreBooleanPropertyr
   VALUESdataflow
print_onlyintdisable_public_ipsenable_streaming_engineArgListUpdateActionArgDict)parser
image_argsimage_building_args	yaml_argsregion_grouppipeline_argss         +lib/surface/dataflow/flex_template/build.py_CommonArgsr7      s%    22D2A*"557++-)&0&&{'BD  E    
 	-4
 &&
QOP  Q 	-4
 &&
QOP  Q 	$5 &&{'BD  E 	3.	   	H##%  '
 	$))



$
$
/
/1  2 	6&&{'BD	  E 	6 &&{'BD  E 	&&|'KM;	  = 	%&&
<
,  ( 	6   	))



$
$
7
79E	  G 	9  ;
 	  
 	  
 	  
 	@  B 446,2  4 0  2 	!))



$
$
<
<>F	  H 	6&&{'BD   	 & %%0  3 	 & %%;  	 ""E&&
=57  #  &BB C - %%-  	/  %%E  
G $  & "##%3   	   """5 &&
S ) # . "" %%
= # #     c                 >   t        j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  t        j                  j                  j                  j                         | j                  | j                   | j"                  t        j                  j                  j$                  j                         | j&                  | j(                        }| j*                  dk(  r| j,                  st/        d      t1        j2                  | j4                        }| j,                  |d<   t1        j6                  |d      | _        t         j8                  j;                  |       }n| j<                  }|s| j>                  }t         j8                  jA                  | j>                  | jB                  | jD                  | jF                  | jH                  | jJ                  | j*                  | jL                  | jN                  	       t         j8                  jQ                  | jR                  || j4                  | j*                  | jT                  || jV                  | jX                  | jZ                  	      S )zRuns the command.

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

  Returns:
    A Job message.
  )max_workersnum_workersnetwork
subnetworkworker_machine_typekms_key_namestaging_locationtemp_locationr+   service_account_emailworker_regionworker_zoner,   additional_experimentsadditional_user_labelsr   zyaml_pipeline_path is required.yamlDefinition   )indent).r   TemplateArgumentsr:   r;   r<   r=   r>   dataflow_kms_keyr@   rA   r
   r'   r(   r+   GetBoolrB   rC   rD   r,   rE   rF   sdk_languageyaml_pipeline_path
ValueErrorjsonloadsmetadata_filedumps	TemplatesGetYamlTemplateImageimageimage_gcr_pathBuildAndStoreFlexTemplateImageflex_template_base_imagejarpy_pathgo_binary_pathenvgcs_log_dircloud_build_service_accountBuildAndStoreFlexTemplateFiler   r)   #image_repository_username_secret_id#image_repository_password_secret_idimage_repository_cert_path)argstemplate_argsmetadata
image_paths       r6   
_CommonRunrh   S  s    ((""""ll22((,,&&#**33FFNN  66&&""(//88PPwy!88!88#:-& 
& ""899zz$,,-H!%!8!8HHQ7D44T:JJ	$$JNN11%%((
 
	5	5
!!:t/A/A
-
..
..0O0O	
Q Qr8   c                   0    e Zd ZdZdddZed        Zd Zy)Buildz:Builds a flex template file from the specified parameters.az            To build and store a flex template JSON file, run:

            $ {command} gs://template-file-gcs-path --image=gcr://image-path               --metadata-file=/local/path/to/metadata.json --sdk-language=JAVA

          If using prebuilt template image from private registry, run:

            $ {command} gs://template-file-gcs-path               --image=private.registry.com:3000/image-path               --image-repository-username-secret-id="projects/test-project/secrets/username-secret"
              --image-repository-password-secret-id="projects/test-project/secrets/password-secret/versions/latest"
              --metadata-file=metadata.json
              --sdk-language=JAVA

          To build the template image and flex template JSON file, run:

            $ {command} gs://template-file-gcs-path               --image-gcr-path=gcr://path-to-store-image               --jar=path/to/pipeline.jar --jar=path/to/dependency.jar               --env=FLEX_TEMPLATE_JAVA_MAIN_CLASS=classpath               --flex-template-base-image=JAVA11               --metadata-file=/local/path/to/metadata.json --sdk-language=JAVA
          )DESCRIPTIONEXAMPLESc                     t        |        y N)r7   )r0   s    r6   Argsz
Build.Args  s
    r8   c                     t        |      S rn   )rh   )selfrd   s     r6   Runz	Build.Run  s    dr8   N)__name__
__module____qualname____doc__detailed_helpstaticmethodro   rr    r8   r6   rj   rj     s3     C G	-<  r8   rj   )rv   
__future__r   r   r   rP   googlecloudsdk.api_lib.dataflowr   googlecloudsdk.callioper   r   r	   googlecloudsdk.corer
   r7   rh   DefaultUniverseOnlyReleaseTracksReleaseTrackGABETACommandrj   ry   r8   r6   <module>r      s    &  '  0 + / ( *rj	8Qv D%%(($*;*;*@*@A&DLL & B &r8   