gc_batch
gc-batch: Google Cloud Batch Job Management CLI and Python Client.
This package provides a command-line interface and Python library for managing Google Cloud Batch jobs with support for job creation, monitoring, logging, and filtering by labels.
Example
Using the CLI:
gc-batch create --job-name my-job --docker-image python:3.12 --command "python main.py"
gc-batch list_my_jobs
gc-batch status --job-name my-job
Using the Python client:
from gc_batch import GCBatchClient, BatchClientConfig, JobRequest, BatchJobConfig
# Create a client
config = BatchClientConfig(project_id="my-project", location="us-central1")
client = GCBatchClient(config)
# Create a job
job_config = BatchJobConfig(machine_type="e2-standard-2", boot_disk_type="pd-balanced")
request = JobRequest(
job_name="my-job",
docker_image="python:3.12",
command="python main.py",
config=job_config,
)
job = client.create_job(request)
# List jobs
jobs = client.list_jobs(labels={"team": "data-science"})
Modules:
| Name | Description |
|---|---|
client |
Main GCBatchClient class for interacting with Google Cloud Batch API. |
models |
Pydantic models for job configuration and requests. |
batch_logging |
Logging utilities for viewing job logs in Cloud Logging. |
gcs_logging |
Reader for job logs written to a GCS bucket (--logs-bucket). |
utils |
Utility functions for working with jobs. |
__all__ = ['__version__', 'GCBatchClient', 'BatchClientConfig', 'BatchBootDiskType', 'BatchJobConfig', 'JobRequest', 'MachineTypeHelper', 'GCBatchSettings', 'JobProfile']
module-attribute
__version__ = '0.2.0'
module-attribute
BatchBootDiskType
Bases: CustomStrEnum
Supported boot disk type strings for Batch jobs.
This is the union of disk types supported on at least one machine generation;
use :meth:MachineTypeHelper.disk_type_supported to check compatibility with
a specific machine type.
Source code in src/gc_batch/models/job_request.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
HYPERDISK_BALANCED = 'hyperdisk-balanced'
class-attribute
instance-attribute
HYPERDISK_BALANCED_HIGH_AVAILABILITY = 'hyperdisk-balanced-high-availability'
class-attribute
instance-attribute
HYPERDISK_EXTREME = 'hyperdisk-extreme'
class-attribute
instance-attribute
PD_BALANCED = 'pd-balanced'
class-attribute
instance-attribute
PD_SSD = 'pd-ssd'
class-attribute
instance-attribute
PD_STANDARD = 'pd-standard'
class-attribute
instance-attribute
BatchClientConfig
Bases: BaseModel
Configuration for the GCBatchClient.
This configuration is used to initialize a GCBatchClient instance with the necessary GCP project and location settings.
Attributes:
| Name | Type | Description |
|---|---|---|
location |
str
|
The GCP region where Batch jobs will be created (e.g., "us-central1"). |
project_id |
str
|
The GCP project ID where Batch jobs will be managed. |
settings |
GCBatchSettings
|
Deployment-specific configuration (job-name prefix, job
profiles, etc.). Defaults to :class: |
Example
from gc_batch import BatchClientConfig, GCBatchClient
config = BatchClientConfig(
location="us-central1",
project_id="my-gcp-project"
)
client = GCBatchClient(config)
Source code in src/gc_batch/models/batch_config.py
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 | |
location
instance-attribute
project_id
instance-attribute
settings = Field(default_factory=GCBatchSettings)
class-attribute
instance-attribute
BatchJobConfig
Bases: BaseModel
Configuration for a Google Cloud Batch job.
This model contains all the configuration options for creating a Batch job, including machine specifications, storage, networking, and environment settings.
Attributes:
| Name | Type | Description |
|---|---|---|
default_task_count |
int
|
Number of tasks in the job (default: 1). |
default_parallelism |
int
|
Maximum tasks to run in parallel (default: 1). |
machine_type |
str
|
GCP machine type (e.g., "e2-standard-2", "n2-standard-4"). |
boot_disk_size |
int
|
Boot disk size in GB (default: 30). |
boot_disk_type |
BatchBootDiskType
|
Boot disk type (:class: |
input_bucket |
str | None
|
GCS bucket path for input data (without gs:// prefix). |
input_dir |
str | None
|
Mount point for input data in container (default: "/mnt/input"). |
input_billing_project |
str | None
|
Billing project for requester-pays input buckets. |
output_bucket |
str | None
|
GCS bucket path for output data (without gs:// prefix). |
output_dir |
str | None
|
Mount point for output data in container (default: "/mnt/output"). |
logs_bucket |
str | None
|
GCS bucket path (without gs:// prefix) to write job logs to. When set, logs go to this bucket path instead of Cloud Logging. Include a subfolder in the path to separate logs per job. |
logs_billing_project |
str | None
|
Billing project for requester-pays logs buckets. |
local_ssd_size_gb |
int | None
|
Size of local SSD in GB (must be multiple of 375). |
local_ssd_device_name |
str | None
|
Device name for local SSD (default: "local-ssd-0"). |
local_ssd_mount_path |
str | None
|
Mount path for local SSD (default: "/mnt/local_ssd"). |
provisioning_model |
BatchProvisioningModel | None
|
VM provisioning model (STANDARD, SPOT, or PREEMPTIBLE). |
network |
str | None
|
VPC network path for the VM. |
subnetwork |
str | None
|
Subnetwork path for the VM. |
service_account |
str | None
|
Service account email to use for the job. |
use_private_address |
bool
|
Whether to use private IP (no external IP). |
regions |
list[str] | None
|
List of allowed regions for job placement. |
zones |
list[str] | None
|
List of allowed zones for job placement. |
user_env_dict |
dict[str, str] | None
|
Custom environment variables for the container. |
Example
from gc_batch import BatchJobConfig
# Basic configuration
config = BatchJobConfig(
machine_type="n2-standard-4",
boot_disk_type="pd-balanced",
boot_disk_size=50,
)
# Configuration with GCS mounts
config = BatchJobConfig(
machine_type="n2-standard-8",
boot_disk_type="pd-ssd",
input_bucket="my-bucket/input-data",
output_bucket="my-bucket/output-data",
)
# Configuration with local SSD
config = BatchJobConfig(
machine_type="n2-standard-4",
boot_disk_type="pd-balanced",
local_ssd_size_gb=375,
local_ssd_mount_path="/mnt/fast",
)
Source code in src/gc_batch/models/job_request.py
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 | |
boot_disk_size = Field(default=30)
class-attribute
instance-attribute
boot_disk_type
instance-attribute
cores = Field(default=1)
class-attribute
instance-attribute
default_parallelism = Field(default=1)
class-attribute
instance-attribute
default_task_count = Field(default=1)
class-attribute
instance-attribute
input_billing_project = Field(default=None, description='Billing project for input bucket access')
class-attribute
instance-attribute
input_bucket = Field(default=None)
class-attribute
instance-attribute
input_dir = Field(default=(Constants.INPUT_DIR))
class-attribute
instance-attribute
local_ssd_device_name = Field(default='local-ssd-0', description="Device name for the local SSD (default: 'local-ssd-0')")
class-attribute
instance-attribute
local_ssd_mount_path = Field(default=None, description="Mount path for the local SSD in the container (default: '/mnt/local_ssd')")
class-attribute
instance-attribute
local_ssd_size_gb = Field(default=None, description='Size of local SSD in GB (must be multiple of 375 GB). If specified, a local SSD will be attached.')
class-attribute
instance-attribute
logs_billing_project = Field(default=None, description='Billing project for requester-pays logs bucket access')
class-attribute
instance-attribute
logs_bucket = Field(default=None, description="GCS bucket path (without the gs:// prefix) where job logs will be written, e.g. 'my-bucket/batch-logs'. When set, logs are written to this bucket path instead of Cloud Logging; include a subfolder to separate logs per job. Useful when Cloud Logging is not accessible. Note: Batch supports a single log destination, so enabling this disables Cloud Logging for the job.")
class-attribute
instance-attribute
machine_type
instance-attribute
network = Field(default=None, description="Network to use (e.g., 'global/networks/network')")
class-attribute
instance-attribute
output_bucket = Field(default=None)
class-attribute
instance-attribute
output_dir = Field(default=(Constants.OUTPUT_DIR))
class-attribute
instance-attribute
output_location = Field(default=None)
class-attribute
instance-attribute
provisioning_model = Field(default=None, description="Provisioning model: 'STANDARD', 'SPOT', or 'PREEMPTIBLE'. SPOT is recommended for cost savings. Accepts string input which is converted to enum.")
class-attribute
instance-attribute
ram_gb = Field(default=4)
class-attribute
instance-attribute
regions = Field(default=None, description='List of regions to use')
class-attribute
instance-attribute
service_account = Field(default=None, description='Service account email to use')
class-attribute
instance-attribute
subnetwork = Field(default=None, description="Subnetwork to use (e.g., 'regions/us-central1/subnetworks/subnetwork')")
class-attribute
instance-attribute
use_private_address = Field(default=False, description='Use private IP address (no external IP)')
class-attribute
instance-attribute
user_env_dict = Field(default=None)
class-attribute
instance-attribute
zones = Field(default=None, description='List of zones to use')
class-attribute
instance-attribute
apply_profile(profile)
Apply a job profile's networking and VM settings to this config.
This is what gc-batch create --job-profile does, exposed for library
callers. Fields the profile leaves unset are left untouched, so a profile
can be applied over a config that already has other settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
JobProfile
|
The :class: |
required |
Returns:
| Type | Description |
|---|---|
BatchJobConfig
|
This config, mutated in place, to allow chaining after construction. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the profile sets |
Example
from gc_batch import BatchJobConfig, GCBatchSettings
settings = GCBatchSettings.load()
config = BatchJobConfig(
machine_type="n2-standard-4",
boot_disk_type="pd-balanced",
).apply_profile(settings.job_profiles["all-of-us"])
Source code in src/gc_batch/models/job_request.py
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 | |
validate_boot_disk_type(v)
classmethod
Convert string input to BatchBootDiskType enum before validation.
Source code in src/gc_batch/models/job_request.py
321 322 323 324 325 326 327 | |
validate_provisioning_model(v)
classmethod
Convert string input to BatchProvisioningModel enum before validation.
Source code in src/gc_batch/models/job_request.py
311 312 313 314 315 316 317 318 319 | |
GCBatchClient
Client for managing Google Cloud Batch jobs.
This is the main entry point for interacting with Google Cloud Batch. It provides methods for creating jobs, listing jobs with filters, checking job status, and retrieving job logs.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
The BatchClientConfig with project and location settings. |
|
client |
The underlying Google Cloud Batch API client. |
|
batch_logging |
Helper for Cloud Logging operations. |
|
gcs_logging |
Helper for reading logs written to a GCS bucket. |
Example
from gc_batch import GCBatchClient, BatchClientConfig, JobRequest, BatchJobConfig
# Initialize the client
config = BatchClientConfig(project_id="my-project", location="us-central1")
client = GCBatchClient(config)
# Create a job
job_config = BatchJobConfig(machine_type="e2-standard-2", boot_disk_type="pd-balanced")
request = JobRequest(
job_name="my-analysis",
docker_image="python:3.12",
command="python /app/main.py",
config=job_config,
)
job = client.create_job(request)
print(f"Created job: {job.name}")
# List jobs by label
jobs = client.list_jobs(labels={"team": "data-science"})
# Get job status
job = client.get_job("my-analysis-1234567890")
print(f"Status: {job.status.state.name}")
# Cancel a job
client.cancel_job("projects/my-project/locations/us-central1/jobs/my-job")
Source code in src/gc_batch/client.py
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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
batch_logging = BatchLogging(config=batch_client_config, logger=(self.logging))
instance-attribute
client = batch_v1.BatchServiceClient()
instance-attribute
config = batch_client_config
instance-attribute
gcs_logging = GCSLogReader(config=batch_client_config, logger=(self.logging))
instance-attribute
logging = logger or get_logger(name='GCBatchClient', level=log_level)
instance-attribute
__init__(batch_client_config, logger=None, log_level=logging.INFO)
Initialize the GCBatchClient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_client_config
|
BatchClientConfig
|
Configuration with project_id and location settings. |
required |
Source code in src/gc_batch/client.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
cancel_job(job_name)
Cancel a running or queued job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The full job resource path (e.g., "projects/my-project/locations/us-central1/jobs/my-job"). |
required |
Example
job = client.get_job("my-job-name")
client.cancel_job(job.name) # Use the full name from the job object
Source code in src/gc_batch/client.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
create_job(job_request)
Create a new Google Cloud Batch job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_request
|
JobRequest
|
The JobRequest containing job configuration and settings. |
required |
Returns:
| Type | Description |
|---|---|
Job
|
The created GCSBatchJob object with job details. |
Raises:
| Type | Description |
|---|---|
Exception
|
If job creation fails. |
Example
config = BatchJobConfig(machine_type="e2-standard-2", boot_disk_type="pd-balanced")
request = JobRequest(
job_name="my-job",
docker_image="python:3.12",
command="python main.py",
config=config,
)
job = client.create_job(request)
print(f"Job created: {job.name}")
Source code in src/gc_batch/client.py
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 | |
get_cloud_logging_url(job, severity='DEFAULT', agent_logs=False, custom_label_filters=None)
Get a URL to the Google Cloud Logging console filtered for this job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The GCSBatchJob object to generate a URL for |
required |
severity
|
str
|
Minimum severity level (e.g., "DEFAULT", "INFO", "WARNING", "ERROR") |
'DEFAULT'
|
agent_logs
|
bool
|
Whether to include agent logs (default: False) |
False
|
custom_label_filters
|
dict[str, str] | None
|
Optional mapping of label key to label value to filter the query by. If provided, overrides the job_uid filter. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
URL to the Cloud Logging console with filters applied |
Source code in src/gc_batch/client.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
get_failure_message(job)
Get concise failure information for a job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The failed Google Cloud Batch job |
required |
Returns:
| Type | Description |
|---|---|
str
|
Concise failure message with essential error details |
Source code in src/gc_batch/client.py
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 | |
get_job(job_name)
Get a specific job by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The short job name (not the full resource path). |
required |
Returns:
| Type | Description |
|---|---|
Job
|
The GCSBatchJob object with full job details. |
Example
job = client.get_job("my-analysis-1234567890")
print(f"Status: {job.status.state.name}")
print(f"Created: {job.create_time}")
Source code in src/gc_batch/client.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
list_jobs(labels=None, name=None, since_time=None, status=None, page_size=100)
List Batch jobs filtered by specific labels, name, since time, and status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
dict[str, str] | None
|
Dictionary of label key-value pairs to filter by |
None
|
name
|
str | None
|
Filter by job name |
None
|
since_time
|
datetime | None
|
Filter by update time |
None
|
status
|
str | None
|
Filter by job status (RUNNING, SUCCEEDED, FAILED, QUEUED, etc.) |
None
|
page_size
|
int
|
Number of jobs to return per page (max 1000) |
100
|
Returns:
| Type | Description |
|---|---|
list[Job]
|
List of Batch jobs with matching labels, name, since time, and status |
Examples:
List jobs with specific label
jobs = client.list_jobs({"environment": "production"})
List jobs with multiple labels and name
jobs = client.list_jobs({ "team": "data-science", "project": "genomics", }, "genome-sequencing")
List jobs created in the last day
jobs = client.list_jobs(since_time=datetime.now() - timedelta(days=1))
List only failed jobs
jobs = client.list_jobs(status="FAILED")
List running jobs with specific label
jobs = client.list_jobs({"team": "research"}, status="RUNNING")
List all jobs (no filters)
jobs = client.list_jobs()
Source code in src/gc_batch/client.py
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 | |
setup_gcs_volume(bucket_name, mount_path, disk_mount_path, read_only=False, billing_project=None, implicit_dirs=False)
Configure a GCS bucket as a mounted volume.
Creates a Volume configuration that mounts a Google Cloud Storage bucket path into the container, allowing the job to read/write files directly to GCS as if they were local files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_name
|
str
|
GCS bucket path (e.g., "my-bucket/data/input"). |
required |
mount_path
|
str
|
Mount path inside the container (e.g., "/mnt/input"). |
required |
disk_mount_path
|
str
|
Physical mount path on the VM. |
required |
read_only
|
bool
|
If True, pass gcsfuse's |
False
|
billing_project
|
str | None
|
Billing project for requester-pays buckets. |
None
|
implicit_dirs
|
bool
|
If True, pass |
False
|
Returns:
| Type | Description |
|---|---|
Volume
|
Tuple of (Volume, volume_config_string) where volume_config_string |
str
|
is the Docker volume mount format "host_path:container_path". |
Example
volume, config = client.setup_gcs_volume(
bucket_name="my-bucket/input-data",
mount_path="/mnt/input",
disk_mount_path="/mnt/disks/input",
billing_project="my-billing-project",
)
Source code in src/gc_batch/client.py
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
setup_local_ssd(config)
Configure a local SSD for high-performance storage.
Creates the configuration to attach a local SSD to the job VM for high-performance ephemeral storage. Local SSDs provide very high IOPS and low latency, ideal for temporary data processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BatchJobConfig
|
BatchJobConfig with local_ssd_size_gb, local_ssd_device_name, and local_ssd_mount_path settings. |
required |
Returns:
| Type | Description |
|---|---|
tuple[AttachedDisk, Volume, str]
|
Tuple of (AttachedDisk, Volume, volume_config_string). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If local_ssd_size_gb is None or not a multiple of 375 GB. |
Note
- Local SSD size must be a multiple of 375 GB.
- Data on local SSDs is ephemeral and lost when the VM terminates.
- Local SSDs are automatically formatted and mounted by Batch.
Example
config = BatchJobConfig(
machine_type="n2-standard-4",
boot_disk_type="pd-balanced",
local_ssd_size_gb=375,
local_ssd_mount_path="/mnt/fast",
)
attached_disk, volume, volume_config = client.setup_local_ssd(config)
Source code in src/gc_batch/client.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
setup_volume(disk_type, disk_size, device_name, mount_path)
Create an attached disk and volume configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disk_type
|
str
|
The disk type (e.g., "pd-balanced", "pd-ssd"). |
required |
disk_size
|
int
|
Disk size in GB. |
required |
device_name
|
str
|
Device name for the disk. |
required |
mount_path
|
str
|
Path where the disk will be mounted in the VM. |
required |
Returns:
| Type | Description |
|---|---|
tuple[AttachedDisk, Disk]
|
Tuple of (AttachedDisk, Disk) configurations. |
Source code in src/gc_batch/client.py
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
GCBatchSettings
Bases: BaseSettings
Top-level configuration for gc-batch.
All fields have neutral defaults. Deployment-specific behavior is supplied
through GC_BATCH_* environment variables or a TOML config file loaded via
:meth:load.
Attributes:
| Name | Type | Description |
|---|---|---|
job_name_prefix |
str
|
Prefix prepended to every created job's name. |
created_using_label |
str
|
Value of the "created-using" label set on every job. |
owner_email_env_vars |
list[str]
|
Environment variables checked, in order, to
determine the "created-by" label value. The default list covers the
All of Us Researcher Workbench, which does not set |
default_project_id |
str | None
|
Fallback GCP project id when none is given on the
command line or via |
job_profiles |
dict[str, JobProfile]
|
Named bundles of networking/VM settings selectable via
|
Source code in src/gc_batch/settings.py
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 | |
created_using_label = 'gc-batch'
class-attribute
instance-attribute
default_project_id = None
class-attribute
instance-attribute
job_name_prefix = ''
class-attribute
instance-attribute
job_profiles = {None: _built_in_job_profiles(), None: self.job_profiles}
class-attribute
instance-attribute
model_config = SettingsConfigDict(env_prefix='GC_BATCH_', env_nested_delimiter='__', extra='ignore')
class-attribute
instance-attribute
owner_email_env_vars = Field(default_factory=(lambda: ['OWNER_EMAIL', 'WORKBENCH_USER_EMAIL', 'TERRA_USER_EMAIL', 'USER']))
class-attribute
instance-attribute
__init__(**data)
Initialize settings and merge in the built-in job profiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**data
|
Any
|
Field overrides, following the standard pydantic-settings precedence (constructor arguments > environment variables > configured TOML source > defaults). |
{}
|
Source code in src/gc_batch/settings.py
173 174 175 176 177 178 179 180 181 182 | |
load(config_file=None, **overrides)
classmethod
Load settings, including values from a TOML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_file
|
str | Path | None
|
An explicit path to a TOML config file. When omitted,
the standard discovery order is used: |
None
|
**overrides
|
Any
|
Explicit field overrides, which take precedence over everything else (environment variables, the TOML file, and defaults). |
{}
|
Returns:
| Type | Description |
|---|---|
GCBatchSettings
|
A fully resolved |
Source code in src/gc_batch/settings.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
settings_customise_sources(settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings)
classmethod
Insert a TOML file source between env vars and the default sources.
Returns:
| Type | Description |
|---|---|
PydanticBaseSettingsSource
|
The settings sources in priority order (highest first): constructor |
...
|
arguments, environment variables, the TOML file (if one was |
tuple[PydanticBaseSettingsSource, ...]
|
requested via :meth: |
tuple[PydanticBaseSettingsSource, ...]
|
sources that fall through to field defaults. |
Source code in src/gc_batch/settings.py
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 | |
JobProfile
Bases: BaseModel
A named bundle of networking/VM settings applied via --job-profile.
Attributes:
| Name | Type | Description |
|---|---|---|
network |
str | None
|
VPC network path for the VM (e.g. "global/networks/network"). |
subnetwork |
str | None
|
Subnetwork path for the VM. |
use_private_address |
bool
|
Whether to use a private IP (no external IP). |
regions |
list[str] | None
|
List of allowed regions for job placement. |
service_account_from_gcloud |
bool
|
Whether to resolve the job's service account
from the current |
cloud_logging_unreadable |
bool
|
Whether callers in this environment are expected
to be unable to read Cloud Logging. When |
Source code in src/gc_batch/settings.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
cloud_logging_unreadable = False
class-attribute
instance-attribute
network = None
class-attribute
instance-attribute
regions = None
class-attribute
instance-attribute
service_account_from_gcloud = False
class-attribute
instance-attribute
subnetwork = None
class-attribute
instance-attribute
use_private_address = False
class-attribute
instance-attribute
JobRequest
Bases: BaseModel
Request to create a new Google Cloud Batch job.
This model represents a complete job creation request, combining the job metadata with the job configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
job_name |
str
|
Name for the job (will be timestamped, and prefixed if
|
docker_image |
str
|
Docker image URI to use for the job container. |
command |
str
|
Command to run inside the container. |
args |
str
|
Additional arguments to pass to the command (optional). |
config |
BatchJobConfig
|
BatchJobConfig with machine and storage settings. |
labels |
dict[str, str] | None
|
Custom labels to attach to the job for filtering and organization. |
Example
from gc_batch import JobRequest, BatchJobConfig
config = BatchJobConfig(
machine_type="n2-standard-4",
boot_disk_type="pd-balanced",
input_bucket="my-bucket/data",
output_bucket="my-bucket/results",
)
request = JobRequest(
job_name="data-analysis",
docker_image="gcr.io/my-project/analyzer:latest",
command="python /app/analyze.py",
args="--input /mnt/input --output /mnt/output",
config=config,
labels={"team": "data-science", "environment": "production"},
)
Source code in src/gc_batch/models/job_request.py
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 | |
args = Field(default='')
class-attribute
instance-attribute
command = Field(default="echo 'Hello, World!'")
class-attribute
instance-attribute
config
instance-attribute
docker_image = Field(default='ubuntu:latest')
class-attribute
instance-attribute
job_name = Field(default='default')
class-attribute
instance-attribute
labels = Field(default=None, description='Custom labels for the Batch job')
class-attribute
instance-attribute
MachineTypeHelper
Helper class for working with GCP machine types.
Provides utilities for determining compatible disk types, checking machine type generations, and identifying LSSD (Local SSD) machine types.
Example
from gc_batch import MachineTypeHelper
# Check if a machine type is LSSD
is_lssd = MachineTypeHelper.is_lssd_machine_type("c4-standard-8-lssd")
# Returns: True
# Get supported disk types for a machine type
disk_types = MachineTypeHelper.supported_disk_types("n2-standard-4")
# Returns: ["hyperdisk-balanced", "hyperdisk-balanced-high-availability", ...]
# Get the default disk type
default = MachineTypeHelper.get_default_disk_type("e2-standard-2")
# Returns: "pd-standard"
Source code in src/gc_batch/models/job_request.py
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 | |
disk_type_supported(disk_type, type_name)
staticmethod
Check if a disk type is supported for a given machine type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disk_type
|
str
|
The disk type to check (e.g., "pd-balanced"). |
required |
type_name
|
str
|
The machine type name (e.g., "n2-standard-4"). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the disk type is supported, False otherwise. |
Source code in src/gc_batch/models/job_request.py
93 94 95 96 97 98 99 100 101 102 103 104 | |
get_default_disk_type(type_name)
staticmethod
Get the default disk type for a machine type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The machine type name (e.g., "n2-standard-4"). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The default (first) supported disk type for this machine type. |
Source code in src/gc_batch/models/job_request.py
121 122 123 124 125 126 127 128 129 130 131 | |
is_lssd_machine_type(type_name)
staticmethod
Check if a machine type has pre-attached Local SSDs.
LSSD machine types (e.g., "c4-standard-8-lssd") come with Local SSDs automatically attached and cannot have additional SSDs manually attached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The machine type name to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the machine type is an LSSD type, False otherwise. |
Source code in src/gc_batch/models/job_request.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
is_mid_generation_machine_type(type_name)
staticmethod
Check if a machine type is generation 3 or newer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The machine type name (e.g., "n3-standard-4"). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if generation 3+, False otherwise. |
Source code in src/gc_batch/models/job_request.py
145 146 147 148 149 150 151 152 153 154 155 | |
is_new_generation_machine_type(type_name)
staticmethod
Check if a machine type is generation 4 or newer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The machine type name (e.g., "c4-standard-8"). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if generation 4+, False otherwise. |
Source code in src/gc_batch/models/job_request.py
133 134 135 136 137 138 139 140 141 142 143 | |
supported_disk_types(type_name)
staticmethod
Get the list of supported disk types for a machine type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The GCP machine type name (e.g., "n2-standard-4"). |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of supported disk type strings for this machine type. |
Note
- Generation 4+ machines (c4, m4, etc.) only support hyperdisk types.
- Generation 3 machines support both hyperdisk and pd-* types.
- Older generations only support pd-* types.
Source code in src/gc_batch/models/job_request.py
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 | |