forked from googleapis/python-batch
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.py
More file actions
594 lines (525 loc) · 18.5 KB
/
task.py
File metadata and controls
594 lines (525 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# -*- coding: utf-8 -*-
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import MutableMapping, MutableSequence
from google.protobuf import duration_pb2 # type: ignore
from google.protobuf import timestamp_pb2 # type: ignore
import proto # type: ignore
from google.cloud.batch_v1alpha.types import volume
__protobuf__ = proto.module(
package="google.cloud.batch.v1alpha",
manifest={
"ComputeResource",
"StatusEvent",
"TaskExecution",
"TaskStatus",
"Runnable",
"TaskSpec",
"LifecyclePolicy",
"Task",
"Environment",
},
)
class ComputeResource(proto.Message):
r"""Compute resource requirements
Attributes:
cpu_milli (int):
The milliCPU count.
memory_mib (int):
Memory in MiB.
gpu_count (int):
The GPU count.
Not yet implemented.
boot_disk_mib (int):
Extra boot disk size in MiB for each task.
"""
cpu_milli: int = proto.Field(
proto.INT64,
number=1,
)
memory_mib: int = proto.Field(
proto.INT64,
number=2,
)
gpu_count: int = proto.Field(
proto.INT64,
number=3,
)
boot_disk_mib: int = proto.Field(
proto.INT64,
number=4,
)
class StatusEvent(proto.Message):
r"""Status event
Attributes:
type_ (str):
Type of the event.
description (str):
Description of the event.
event_time (google.protobuf.timestamp_pb2.Timestamp):
The time this event occurred.
task_execution (google.cloud.batch_v1alpha.types.TaskExecution):
Task Execution
"""
type_: str = proto.Field(
proto.STRING,
number=3,
)
description: str = proto.Field(
proto.STRING,
number=1,
)
event_time: timestamp_pb2.Timestamp = proto.Field(
proto.MESSAGE,
number=2,
message=timestamp_pb2.Timestamp,
)
task_execution: "TaskExecution" = proto.Field(
proto.MESSAGE,
number=4,
message="TaskExecution",
)
class TaskExecution(proto.Message):
r"""This Task Execution field includes detail information for
task execution procedures, based on StatusEvent types.
Attributes:
exit_code (int):
When task is completed as the status of
FAILED or SUCCEEDED, exit code is for one task
execution result, default is 0 as success.
"""
exit_code: int = proto.Field(
proto.INT32,
number=1,
)
class TaskStatus(proto.Message):
r"""Status of a task
Attributes:
state (google.cloud.batch_v1alpha.types.TaskStatus.State):
Task state
status_events (MutableSequence[google.cloud.batch_v1alpha.types.StatusEvent]):
Detailed info about why the state is reached.
"""
class State(proto.Enum):
r"""Task states."""
STATE_UNSPECIFIED = 0
PENDING = 1
ASSIGNED = 2
RUNNING = 3
FAILED = 4
SUCCEEDED = 5
state: State = proto.Field(
proto.ENUM,
number=1,
enum=State,
)
status_events: MutableSequence["StatusEvent"] = proto.RepeatedField(
proto.MESSAGE,
number=2,
message="StatusEvent",
)
class Runnable(proto.Message):
r"""Runnable describes instructions for executing a specific
script or container as part of a Task.
This message has `oneof`_ fields (mutually exclusive fields).
For each oneof, at most one member field can be set at the same time.
Setting any member of the oneof automatically clears all other
members.
.. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields
Attributes:
container (google.cloud.batch_v1alpha.types.Runnable.Container):
Container runnable.
This field is a member of `oneof`_ ``executable``.
script (google.cloud.batch_v1alpha.types.Runnable.Script):
Script runnable.
This field is a member of `oneof`_ ``executable``.
barrier (google.cloud.batch_v1alpha.types.Runnable.Barrier):
Barrier runnable.
This field is a member of `oneof`_ ``executable``.
ignore_exit_status (bool):
Normally, a non-zero exit status causes the
Task to fail. This flag allows execution of
other Runnables to continue instead.
background (bool):
This flag allows a Runnable to continue
running in the background while the Task
executes subsequent Runnables. This is useful to
provide services to other Runnables (or to
provide debugging support tools like SSH
servers).
always_run (bool):
By default, after a Runnable fails, no further Runnable are
executed. This flag indicates that this Runnable must be run
even if the Task has already failed. This is useful for
Runnables that copy output files off of the VM or for
debugging.
The always_run flag does not override the Task's overall
max_run_duration. If the max_run_duration has expired then
no further Runnables will execute, not even always_run
Runnables.
environment (google.cloud.batch_v1alpha.types.Environment):
Environment variables for this Runnable
(overrides variables set for the whole Task or
TaskGroup).
timeout (google.protobuf.duration_pb2.Duration):
Timeout for this Runnable.
labels (MutableMapping[str, str]):
Labels for this Runnable.
"""
class Container(proto.Message):
r"""Container runnable.
Attributes:
image_uri (str):
The URI to pull the container image from.
commands (MutableSequence[str]):
Overrides 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.
entrypoint (str):
Overrides the ``ENTRYPOINT`` specified in the container.
volumes (MutableSequence[str]):
Volumes to mount (bind mount) from the host
machine files or directories into the container,
formatted to match docker run's --volume option,
e.g. /foo:/bar, or /foo:/bar:ro
options (str):
Arbitrary additional options to include in
the "docker run" command when running this
container, e.g. "--network host".
block_external_network (bool):
If set to true, external network access to
and from container will be blocked. The
container will use the default internal network
'goog-internal'.
username (str):
Optional username for logging in to a docker registry. If
username matches ``projects/*/secrets/*/versions/*`` then
Batch will read the username from the Secret Manager.
password (str):
Optional password for logging in to a docker registry. If
password matches ``projects/*/secrets/*/versions/*`` then
Batch will read the password from the Secret Manager;
"""
image_uri: str = proto.Field(
proto.STRING,
number=1,
)
commands: MutableSequence[str] = proto.RepeatedField(
proto.STRING,
number=2,
)
entrypoint: str = proto.Field(
proto.STRING,
number=3,
)
volumes: MutableSequence[str] = proto.RepeatedField(
proto.STRING,
number=7,
)
options: str = proto.Field(
proto.STRING,
number=8,
)
block_external_network: bool = proto.Field(
proto.BOOL,
number=9,
)
username: str = proto.Field(
proto.STRING,
number=10,
)
password: str = proto.Field(
proto.STRING,
number=11,
)
class Script(proto.Message):
r"""Script runnable.
This message has `oneof`_ fields (mutually exclusive fields).
For each oneof, at most one member field can be set at the same time.
Setting any member of the oneof automatically clears all other
members.
.. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields
Attributes:
path (str):
Script file path on the host VM.
This field is a member of `oneof`_ ``command``.
text (str):
Shell script text.
This field is a member of `oneof`_ ``command``.
"""
path: str = proto.Field(
proto.STRING,
number=1,
oneof="command",
)
text: str = proto.Field(
proto.STRING,
number=2,
oneof="command",
)
class Barrier(proto.Message):
r"""Barrier runnable blocks until all tasks in a taskgroup reach
it.
Attributes:
name (str):
Barriers are identified by their index in
runnable list. Names are not required, but if
present should be an identifier.
"""
name: str = proto.Field(
proto.STRING,
number=1,
)
container: Container = proto.Field(
proto.MESSAGE,
number=1,
oneof="executable",
message=Container,
)
script: Script = proto.Field(
proto.MESSAGE,
number=2,
oneof="executable",
message=Script,
)
barrier: Barrier = proto.Field(
proto.MESSAGE,
number=6,
oneof="executable",
message=Barrier,
)
ignore_exit_status: bool = proto.Field(
proto.BOOL,
number=3,
)
background: bool = proto.Field(
proto.BOOL,
number=4,
)
always_run: bool = proto.Field(
proto.BOOL,
number=5,
)
environment: "Environment" = proto.Field(
proto.MESSAGE,
number=7,
message="Environment",
)
timeout: duration_pb2.Duration = proto.Field(
proto.MESSAGE,
number=8,
message=duration_pb2.Duration,
)
labels: MutableMapping[str, str] = proto.MapField(
proto.STRING,
proto.STRING,
number=9,
)
class TaskSpec(proto.Message):
r"""Spec of a task
Attributes:
runnables (MutableSequence[google.cloud.batch_v1alpha.types.Runnable]):
The sequence of scripts or containers to run for this Task.
Each Task using this TaskSpec executes its list of runnables
in order. The Task succeeds if all of its runnables either
exit with a zero status or any that exit with a non-zero
status have the ignore_exit_status flag.
Background runnables are killed automatically (if they have
not already exited) a short time after all foreground
runnables have completed. Even though this is likely to
result in a non-zero exit status for the background
runnable, these automatic kills are not treated as Task
failures.
compute_resource (google.cloud.batch_v1alpha.types.ComputeResource):
ComputeResource requirements.
max_run_duration (google.protobuf.duration_pb2.Duration):
Maximum duration the task should run.
The task will be killed and marked as FAILED if
over this limit.
max_retry_count (int):
Maximum number of retries on failures. The default, 0, which
means never retry. The valid value range is [0, 10].
lifecycle_policies (MutableSequence[google.cloud.batch_v1alpha.types.LifecyclePolicy]):
Lifecycle management schema when any task in a task group is
failed. The valid size of lifecycle policies are [0, 10].
For each lifecycle policy, when the condition is met, the
action in that policy will execute. If there are multiple
policies that the task execution result matches, we use the
action from the first matched policy. If task execution
result does not meet with any of the defined lifecycle
policy, we consider it as the default policy. Default policy
means if the exit code is 0, exit task. If task ends with
non-zero exit code, retry the task with max_retry_count.
environments (MutableMapping[str, str]):
Deprecated: please use
environment(non-plural) instead.
volumes (MutableSequence[google.cloud.batch_v1alpha.types.Volume]):
Volumes to mount before running Tasks using
this TaskSpec.
environment (google.cloud.batch_v1alpha.types.Environment):
Environment variables to set before running
the Task.
"""
runnables: MutableSequence["Runnable"] = proto.RepeatedField(
proto.MESSAGE,
number=8,
message="Runnable",
)
compute_resource: "ComputeResource" = proto.Field(
proto.MESSAGE,
number=3,
message="ComputeResource",
)
max_run_duration: duration_pb2.Duration = proto.Field(
proto.MESSAGE,
number=4,
message=duration_pb2.Duration,
)
max_retry_count: int = proto.Field(
proto.INT32,
number=5,
)
lifecycle_policies: MutableSequence["LifecyclePolicy"] = proto.RepeatedField(
proto.MESSAGE,
number=9,
message="LifecyclePolicy",
)
environments: MutableMapping[str, str] = proto.MapField(
proto.STRING,
proto.STRING,
number=6,
)
volumes: MutableSequence[volume.Volume] = proto.RepeatedField(
proto.MESSAGE,
number=7,
message=volume.Volume,
)
environment: "Environment" = proto.Field(
proto.MESSAGE,
number=10,
message="Environment",
)
class LifecyclePolicy(proto.Message):
r"""LifecyclePolicy describes how to deal with task failures
based on different conditions.
Attributes:
action (google.cloud.batch_v1alpha.types.LifecyclePolicy.Action):
Action to execute when ActionCondition is
true.
action_condition (google.cloud.batch_v1alpha.types.LifecyclePolicy.ActionCondition):
Conditions that decide why a task failure is
dealt with a specific action.
"""
class Action(proto.Enum):
r"""Action on task failures based on different conditions."""
ACTION_UNSPECIFIED = 0
RETRY_TASK = 1
FAIL_TASK = 2
class ActionCondition(proto.Message):
r"""Conditions for actions to deal with task failures.
Attributes:
exit_codes (MutableSequence[int]):
Exit codes of a task execution.
If there are more than 1 exit codes,
when task executes with any of the exit code in
the list, the condition is met and the action
will be executed.
"""
exit_codes: MutableSequence[int] = proto.RepeatedField(
proto.INT32,
number=1,
)
action: Action = proto.Field(
proto.ENUM,
number=1,
enum=Action,
)
action_condition: ActionCondition = proto.Field(
proto.MESSAGE,
number=2,
message=ActionCondition,
)
class Task(proto.Message):
r"""A Cloud Batch task.
Attributes:
name (str):
Task name.
The name is generated from the parent TaskGroup
name and 'id' field. For example:
"projects/123456/locations/us-west1/jobs/job01/taskGroups/group01/tasks/task01".
status (google.cloud.batch_v1alpha.types.TaskStatus):
Task Status.
"""
name: str = proto.Field(
proto.STRING,
number=1,
)
status: "TaskStatus" = proto.Field(
proto.MESSAGE,
number=2,
message="TaskStatus",
)
class Environment(proto.Message):
r"""An Environment describes a collection of environment
variables to set when executing Tasks.
Attributes:
variables (MutableMapping[str, str]):
A map of environment variable names to
values.
secret_variables (MutableMapping[str, str]):
A map of environment variable names to Secret
Manager secret names. The VM will access the
named secrets to set the value of each
environment variable.
encrypted_variables (google.cloud.batch_v1alpha.types.Environment.KMSEnvMap):
An encrypted JSON dictionary where the
key/value pairs correspond to environment
variable names and their values.
"""
class KMSEnvMap(proto.Message):
r"""
Attributes:
key_name (str):
The name of the KMS key that will be used to
decrypt the cipher text.
cipher_text (str):
The value of the cipherText response from the ``encrypt``
method.
"""
key_name: str = proto.Field(
proto.STRING,
number=1,
)
cipher_text: str = proto.Field(
proto.STRING,
number=2,
)
variables: MutableMapping[str, str] = proto.MapField(
proto.STRING,
proto.STRING,
number=1,
)
secret_variables: MutableMapping[str, str] = proto.MapField(
proto.STRING,
proto.STRING,
number=2,
)
encrypted_variables: KMSEnvMap = proto.Field(
proto.MESSAGE,
number=3,
message=KMSEnvMap,
)
__all__ = tuple(sorted(__protobuf__.manifest))