
    A                         d 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ej                         Zy)z1Utilities for building the dataproc clusters CLI.    )absolute_import)division)unicode_literals)dataproc)util)arg_parsers)base)labels_util)logc                   6    e Zd ZdZed        Zd Zed        Zy)JobSubmitterzSubmit a job to a cluster.c                    t        j                  |       |j                  dt        d       |j                  dt        d       |j                  dt        d       |j                  dt        d	       |j                  d
dt	        j
                         d       |j                  d      }|j                  dd       t        j                  dd      j                  |       y)z Register flags for this command.z--max-failures-per-hourzSpecifies the maximum number of times a job can be restarted per hour in event of failure. Default is 0 (no retries after job failure).)typehelpz--max-failures-totalzSpecifies the maximum total number of times a job can be restarted after the job fails. Default is 0 (no retries after job failure).z--driver-required-memory-mbzThe memory allocation requested by the job driver in megabytes (MB) for execution on the driver node group (it is used only by clusters with a driver node group).z--driver-required-vcoreszThe vCPU allocation requested by the job driver for execution on the driver node group (it is used only by clusters with a driver node group).z--ttlTam  The maximum duration this job is allowed to run before being killed automatically. Specified using a s, m, h, or d (seconds, minutes, hours, or days) suffix. The minimum value is 10 minutes (10m), and the maximum value is 14 days (14d) Run [gcloud topic datetimes] (https://cloud.google.com/sdk/gcloud/reference/topic/datetimes) for information on duration formats.)hiddenr   r   )requiredz	--clusterz*The Dataproc cluster to submit the job to.)r   z5Labels of Dataproc cluster on which to place the job.zcluster-labelsN)	r
   AddCreateLabelsFlagsadd_argumentintr   Durationadd_mutually_exclusive_groupGetCreateLabelsFlagAddToParser)clsparsercluster_placements      9lib/googlecloudsdk/command_lib/dataproc/jobs/submitter.pyArgszJobSubmitter.Args    s    $$V,
!=  ? =  ? %3	   "	   !!#4   ;;T;J""F #  ##? k#$    c           	      f   t        j                  | j                               }t        j                         }|j
                  r|j
                  n|}t        j                  ||      }| j                  |       d}|j                  t        j                  |j                  |      }|j                  j                  |j                  |j                  |j                        }|j                  j                   j#                  |      }d}	|j$                  d|j$                  v r|j$                  d   }	| j'                  ||	|j(                  |j*                        | _        | j/                          |j                  j1                  |j                  j3                  |j                  |j(                        |j                  j5                  |j                              }
| j7                  |j                  |
|       |j8                  rD|j:                  r8|j                  j=                  |j8                  |j:                        }||
_        |j@                  s|jB                  s|jD                  ry|j                  jG                  |j@                  r|j@                  nd|jB                  r|jB                  nd|jD                  rtI        |jD                        d	z   nd
      }||
_%        |j                  jM                  |j                  |j                  |j                  jO                  |
|            }|j                  jP                  jS                  |      }
tT        jV                  jY                  dj[                  |             |j\                  st        j^                  ||
|d|j                  j`                  jb                  jd                  |j                  j`                  jb                  jf                  d      }
tT        jV                  jY                  dj[                  |             |
S )z9This is what gets called when the user runs this command.N)	projectIdregionclusterNamezcluster-pool)bucket)r!   jobId)r#   )	reference	placement)memoryMbvcoress)maxFailuresPerHourmaxFailuresTotalttl)job	requestId)r!   r"   submitJobRequestzJob [{0}] submitted.zWaiting for job completionT)message
goal_stateerror_statestream_driver_logz Job [{0}] finished successfully.)4dpDataprocReleaseTrackr   GetUniqueIdidParseJobPopulateFilesByTypeclusterParseClustermessages)DataprocProjectsRegionsClustersGetRequestr!   r"   r#   clientprojects_regions_clustersGetcluster_labelsGetStagingDirr%   r$   _staging_dirValidateAndStageFilesJobJobReferenceJobPlacementConfigureJobdriver_required_memory_mbdriver_required_vcoresDriverSchedulingConfigdriverSchedulingConfigmax_failures_per_hourmax_failures_totalr-   JobSchedulingstr
scheduling(DataprocProjectsRegionsJobsSubmitRequestSubmitJobRequestprojects_regions_jobsSubmitr   statusPrintformatasync_WaitForJobTermination	JobStatusStateValueValuesEnumDONEERROR)selfargsr   
request_idjob_idjob_refr<   cluster_refrequestcluster_poolr.   driver_scheduling_configrS   s                r   RunzJobSubmitter.RunZ   s   {{4,,./H!!#JTWWZF mmFH-GT"G||%%dllH=k!!KK))##!-- L /g
 99==gFgL&	4..	.**>:**w}}T[[ + BD 




##00''w}} 1 >##00T\\0J   LC 	h''d3%%$*E*E!)!2!2!I!I11,, "J ". $<c !!T%<%<$$22'' "77$$  22%)XXc$((mc!4 3 j "cnHH##~~!**;;  < " I #G //
/
/
6
6w
?CJJ+226:;;;&&


.&&00EEJJ''11FFLL "c 
jj9@@HIJr   c                     t        j                  || j                  j                        |_        t        j                  || j
                  j                  d      |j                  _        y)z3Add type-specific job configuration to job message.rC   )labels_destN)	r
   ParseCreateArgsrG   LabelsValuelabelsrI   ClusterLabelsValuer'   clusterLabels)r>   r.   rb   s      r   rJ   zJobSubmitter.ConfigureJob   sN     ,,T8<<3K3KLCJ"-"="=00$#&CMMr   N)	__name__
__module____qualname____doc__classmethodr   rj   staticmethodrJ    r   r   r   r      s3    "7% 7%rJX & &r   r   N)ru   
__future__r   r   r   googlecloudsdk.api_lib.dataprocr   r5   r   googlecloudsdk.callioper   r	   $googlecloudsdk.command_lib.util.argsr
   googlecloudsdk.corer   Commandr   rx   r   r   <module>r      s7    8 &  ' : 0 / ( < #Q&4<< Q&r   