|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import google.auth |
| 16 | + |
| 17 | +# [START batch_labels_allocation] |
| 18 | +from google.cloud import batch_v1 |
| 19 | + |
| 20 | + |
| 21 | +def create_job_with_custom_allocation_policy_labels( |
| 22 | + project_id: str, region: str, job_name: str, labels: dict |
| 23 | +) -> batch_v1.Job: |
| 24 | + """ |
| 25 | + This method shows the creation of a Batch job with custom labels which describe the allocation policy. |
| 26 | + Args: |
| 27 | + project_id (str): project ID or project number of the Cloud project you want to use. |
| 28 | + region (str): name of the region you want to use to run the job. Regions that are |
| 29 | + available for Batch are listed on: https://cloud.google.com/batch/docs/locations |
| 30 | + job_name (str): the name of the job that will be created. |
| 31 | + labels (dict): a dictionary of key-value pairs that will be used as labels |
| 32 | + E.g., {"label_key1": "label_value2", "label_key2": "label_value2"} |
| 33 | + Returns: |
| 34 | + batch_v1.Job: The created Batch job object containing configuration details. |
| 35 | + """ |
| 36 | + client = batch_v1.BatchServiceClient() |
| 37 | + |
| 38 | + runnable = batch_v1.Runnable() |
| 39 | + runnable.container = batch_v1.Runnable.Container() |
| 40 | + runnable.container.image_uri = "gcr.io/google-containers/busybox" |
| 41 | + runnable.container.entrypoint = "/bin/sh" |
| 42 | + runnable.container.commands = [ |
| 43 | + "-c", |
| 44 | + "echo Hello world!", |
| 45 | + ] |
| 46 | + |
| 47 | + # Create a task specification and assign the runnable and volume to it |
| 48 | + task = batch_v1.TaskSpec() |
| 49 | + task.runnables = [runnable] |
| 50 | + |
| 51 | + # Specify what resources are requested by each task. |
| 52 | + resources = batch_v1.ComputeResource() |
| 53 | + resources.cpu_milli = 2000 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs. |
| 54 | + resources.memory_mib = 16 # in MiB |
| 55 | + task.compute_resource = resources |
| 56 | + |
| 57 | + task.max_retry_count = 2 |
| 58 | + task.max_run_duration = "3600s" |
| 59 | + |
| 60 | + # Create a task group and assign the task specification to it |
| 61 | + group = batch_v1.TaskGroup() |
| 62 | + group.task_count = 3 |
| 63 | + group.task_spec = task |
| 64 | + |
| 65 | + # Policies are used to define on what kind of virtual machines the tasks will run on. |
| 66 | + # In this case, we tell the system to use "e2-standard-4" machine type. |
| 67 | + # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types |
| 68 | + policy = batch_v1.AllocationPolicy.InstancePolicy() |
| 69 | + policy.machine_type = "e2-standard-4" |
| 70 | + instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate() |
| 71 | + instances.policy = policy |
| 72 | + allocation_policy = batch_v1.AllocationPolicy() |
| 73 | + allocation_policy.instances = [instances] |
| 74 | + |
| 75 | + # Assign the provided labels to the allocation policy |
| 76 | + allocation_policy.labels = labels |
| 77 | + |
| 78 | + # Create the job and assign the task group and allocation policy to it |
| 79 | + job = batch_v1.Job() |
| 80 | + job.task_groups = [group] |
| 81 | + job.allocation_policy = allocation_policy |
| 82 | + |
| 83 | + # We use Cloud Logging as it's an out of the box available option |
| 84 | + job.logs_policy = batch_v1.LogsPolicy() |
| 85 | + job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING |
| 86 | + |
| 87 | + # Create the job request and set the job and job ID |
| 88 | + create_request = batch_v1.CreateJobRequest() |
| 89 | + create_request.job = job |
| 90 | + create_request.job_id = job_name |
| 91 | + # The job's parent is the region in which the job will run |
| 92 | + create_request.parent = f"projects/{project_id}/locations/{region}" |
| 93 | + |
| 94 | + return client.create_job(create_request) |
| 95 | + |
| 96 | + |
| 97 | +# [END batch_labels_allocation] |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + PROJECT_ID = google.auth.default()[1] |
| 102 | + REGION = "us-central1" |
| 103 | + job_name = "your-job-name" |
| 104 | + labels = {"label_key1": "label_value2", "label_key2": "label_value2"} |
| 105 | + create_job_with_custom_allocation_policy_labels( |
| 106 | + PROJECT_ID, REGION, job_name, labels |
| 107 | + ) |
0 commit comments