
    ;                        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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d Z ej6                  ej8                  j:                         G d dej<                               Z ej6                  ej8                  j@                         G d de             Z! ej6                  ej8                  jD                         G d de!             Z#y)z(Command to submit a specified Batch job.    )absolute_import)division)unicode_literalsN)DecodeError)encoding)jobs)arg_parsers)base)resource_args)	arg_utils)
exceptions)log)yaml)filesc                 X   | j                         }|j                  ddt        d       |j                  ddt        d       |j                  ddd	d
       | j                  d      }|j                  dt        j                         d       |j                  dd      }|j                  dd      }|j                  dd       |j                  dt        d       |j                  d      }|j                  dd       |j                  dd       |j                  dd       | j                  dt        j
                  d d!      d"       | j                  d#d$d%d&t        j                  d'(       | j                  d)t        d*       | j                  d+t        d,       |S )-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.

  Returns:
    network_group flag groups.
  z	--networkTzaThe URL for the network resource.
        Must specify subnetwork as well if network is specified)requiredtypehelpz--subnetworkzdThe URL for the subnetwork resource.
        Must specify network as well if subnetwork is specifiedz--no-external-ip-address
store_trueFzRequired if no external public IP address
        is attached to the VM. If no external public IP address,
        additional configuration is required to allow the VM
        to access Google Services.)actiondefaultr   )r   z--configzThe file path of the job config file in either JSON or YAML format.
        It also supports direct input from stdin with '-' or HereDoc
        (in shells with HereDoc support like Bash) with '- <<DELIMITER'. )r   r   zEither specify the config file for the job or
        the first runnable in the task spec. Specify either a script file or
        container arguments for the first runnable in the task spec.)mutexr   zaEither specify a path to a script file to run or provide
        inline text to execute directly.z--script-file-pathz{Path to script file to run as first runnable in task spec.
        File path should be a valid path on the instance volume.)r   z--script-textz+Text to run as first runnable in task spec.z[Options to specify the container arguments for the first
        runnable in the task spec.z--container-image-uriz)The URI to pull the container image from.z--container-entrypointz6Overrides the `ENTRYPOINT` specified in the container.z--container-commands-filezOverrides the `CMD` specified in the container. If there is an
      ENTRYPOINT (either in the container image or with the entrypoint field
      below) then commands are appended as arguments to the ENTRYPOINT.z
--priorityr   c   z-Job priority [0-99] 0 is the lowest priority.z--provisioning-modelz"The STANDARD VM provisioning modelzmThe SPOT VM provisioning model. Ideal for fault-tolerant
            workloads that can withstand preemption.)STANDARDSPOTz@Specify the allowed provisioning model for the compute instances)choicesr   r   z--machine-typezxSpecify the Compute Engine machine type, for
      example, e2-standard-4. Currently only one machine type is supported.z--job-prefixzSpecify the job prefix. A job ID in the format of
      job prefix + %Y%m%d-%H%M%S will be generated. Note that job prefix
      cannot be specified while JOB ID positional argument is
      specified.)	add_groupadd_argumentstrr	   FileContents
BoundedIntr   ChoiceToEnumName)parsernetwork_grouptask_spec_grouprunnable_groupscript_groupcontainer_groups          lib/surface/batch/jobs/submit.py_CommonArgsr*   #   sJ    ""$-C	   C	    &	   $$d$3/##%M   #,,H - .  )), * ,
 D  
 <   #,,& - / :   G   !K   	!!!R(:  
 	:8
 %%M  	 	O   	   
    c           
      t
   |j                   g |_         |j                   s<|j                   j                  d|j                  |j                  g                    | j                  r]|j                   d   j
                  j                  j                  d|j                  |j                  | j                                     | j                  r]|j                   d   j
                  j                  j                  d|j                  |j                  | j                                     | j                  s| j                  s| j                  rg }| j                  r-t        j                  | j                        j                         }|j                   d   j
                  j                  j                  d|j                  |j!                  | j                  | j                  |      	             | j"                  r| j"                  |_        |j$                  E| j&                  s$| j(                  r| j*                  s| j,                  r|j/                         |_        | j&                  r|j$                  j0                  g |j$                  _        |j$                  j0                  s4|j$                  j0                  j                  d|j3                                |j$                  j0                  d   j4                  ,|j7                         |j$                  j0                  d   _        | j&                  |j$                  j0                  d   j4                  _        | j(                  r| j*                  r|j$                  j(                  !|j;                  g 
      |j$                  _        |j$                  j(                  j<                  j                  d|j?                  | j(                  | j*                  | j@                               | j,                  r|j$                  j0                  g |j$                  _        |j$                  j0                  s4|j$                  j0                  j                  d|j3                                |j$                  j0                  d   j4                  ,|j7                         |j$                  j0                  d   _        tC        jD                  | j,                  |j6                  jF                        |j$                  j0                  d   j4                  _$        yy)zBuild the job API message from the args.

  Args:
    args: the args from the parser.
    job_msg: the output job message.
    batch_msgs: the related version of the batch message.
  Nr   )	runnables)taskSpec)path)script)text)
entrypointimageUricommands)	container)networkInterfaces)network
subnetworknoExternalIpAddress)%
taskGroupsinsert	TaskGroupTaskSpecscript_file_pathr.   r-   RunnableScriptscript_textcontainer_commands_filecontainer_image_uricontainer_entrypointr   ReadFileContents
splitlines	ContainerpriorityallocationPolicymachine_typer7   r8   provisioning_modelAllocationPolicy	instancesInstancePolicyOrTemplatepolicyInstancePolicymachineTypeNetworkPolicyr6   NetworkInterfaceno_external_ip_addressr   ChoiceToEnum ProvisioningModelValueValuesEnumprovisioningModel)argsjob_msg
batch_msgscontainer_cmdss       r)   _BuildJobMsgr\      sS    G				:)<)<r)<)JK 
q"",,33	$$$*?*?$@ 	 	
 
q"",,33	:j&7&7T=M=M&7&NO ""		!	!		"	"N##--

&
&
  q"",,33	 **4411' +  	 	
	 
]]}}G %

,,4??		 	 )::<G	))1+-g(##--((//
Z002 ))!,33;5?5N5N5Pg((+2?C?P?PG&&q)00<	\\doo''/)3)A)A *B *g& $$66==	##LL $ ; ; 	$ 	
 
))1+-g(##--((//
Z002 ))!,33;5?5N5N5Pg((+2##%%FF	
 &&q)00B r+   c                   <    e Zd ZdZed        Zed        Zd Zd Z	y)Submitb  Submit a Batch job.

  This command creates and submits a Batch job. After you create and
  submit the job, Batch automatically queues, schedules, and executes it.

  ## EXAMPLES

  To submit a job with a sample JSON configuration file (config.json) and name
  `projects/foo/locations/us-central1/jobs/bar`, run:

    $ {command} projects/foo/locations/us-central1/jobs/bar --config=config.json

  To submit a job with a sample YAML configuration file (config.yaml) and
  name projects/foo/locations/us-central1/jobs/bar, run:

    $ {command} projects/foo/locations/us-central1/jobs/bar --config=config.yaml

  To submit a job through stdin with a sample job configuration and name
  `projects/foo/locations/us-central1/jobs/bar`, run:

    $ {command} projects/foo/locations/us-central1/jobs/bar --config=-

      then input json job config via stdin
      {
        job config
      }

  To submit a job through HereDoc with a sample job configuration and name
  `projects/foo/locations/us-central1/jobs/bar`, run:

    $ {command} projects/foo/locations/us-central1/jobs/bar --config=- << EOF

      {
        job config
      }
      EOF

  For details about how to define a job's configuration using JSON, see the
  projects.locations.jobs resource in the Batch API Reference.
  If you want to define a job's configuration using YAML, convert the JSON
  syntax to YAML.
  c                 D    t        |        t        j                  |        y )N)r*   r   AddSubmitJobResourceArgs)r#   s    r)   ArgszSubmit.Args,  s    **62r+   c                 p   	 t        j                  |j                  t        j                  |            }|S # t
        t        t        j                  f$ r` 	 t        j                  |j                  |      }Y |S # t
        t        f$ r)}t        j                  dj                  |            d}~ww xY ww xY w)zParse into Job message using the config input.

    Args:
         batch_msgs: Batch defined proto message.
         config: The input content being either YAML or JSON or the HEREDOC
           input.

    Returns:
         The Parsed job message.
    zUnable to parse config file: {}N)r   PyValueToMessageJobr   load
ValueErrorAttributeErrorYAMLParseErrorJsonToMessager   r   Errorformat)clsrZ   configresultes        r)   _CreateJobMessagezSubmit._CreateJobMessage1  s    L((69JKf M (;(;< LL''
? M +& L@GGJKKLLs'   37 B5 A99B1$B,,B11B5c                    |j                   j                  j                         }|j                         }| j	                  ||      }| j                         }t        j                  |      }|j                  }|j                         }|j                  r| j                  ||j                        }t        |||       |j                  |||      }	t        j                  j!                  dj#                  |	j$                               |	S )Nz)Job {jobName} was successfully submitted.)jobName)CONCEPTSjobParseParent	_GetJobIdReleaseTrackr   
JobsClientmessagesre   rn   rq   r\   Creater   statusPrintrl   uid)
selfrX   job_reflocation_refjob_idrelease_trackbatch_clientrZ   rY   resps
             r)   Runz
Submit.RunF  s    mm%%'G>>#L^^GT*F%%'M??=1L&&JnnG{{&&z4;;?gw
+v|W=DJJ3::488:L Kr+   c                 r   |j                         j                  d      d   }|t        j                  k7  r!|j                  rt        j                  d      |j                  r?|j                  dz   t        j                  j                         j                  d      z   }|S |t        j                  k(  rd }|S )N/zM--job-prefix cannot be specified when JOB ID positional argument is specified-z%Y%m%d-%H%M%S)
RelativeNamesplitr   	INVALIDID
job_prefixr   rk   datetimenowstrftime)r   r   rX   r   s       r)   rx   zSubmit._GetJobId\  s    !!#))#.r2F(((T__"  

//!!#,,_=>  M 
=**	*fMr+   N)
__name__
__module____qualname____doc__staticmethodrb   classmethodrq   r   rx    r+   r)   r^   r^      s:    )V 3 3  (,r+   r^   c                       e Zd ZdZy)
SubmitBetar_   Nr   r   r   r   r   r+   r)   r   r   t      )r+   r   c                       e Zd ZdZy)SubmitAlphar_   Nr   r   r+   r)   r   r     r   r+   r   )$r   
__future__r   r   r   r   #apitools.base.protorpclite.messagesr   apitools.base.pyr   googlecloudsdk.api_lib.batchr   googlecloudsdk.callioper	   r
    googlecloudsdk.command_lib.batchr   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr   r*   r\   ReleaseTracksry   GACommandr^   BETAr   ALPHAr   r   r+   r)   <module>r      s    / &  '  ; % - / ( : : * # $ *tnbJ D%%(()qT\\ q *qh D%%**+* * ,*Z D%%++,** * -*r+   