Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate code from googleapis/python-batch #8453

Merged
merged 41 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
d13f342
feat: generate v1
parthea Jul 4, 2022
0bb14ce
feat: generate v1alpha (#25)
parthea Aug 31, 2022
4b06783
chore: Bump gapic-generator-python version to 1.3.0 (#32)
gcf-owl-bot[bot] Sep 7, 2022
2e41659
docs(samples): Adding first sample code + tests (#22)
m-strzelczyk Sep 15, 2022
8bdfeee
feat: Add support for REST transport (#37)
gcf-owl-bot[bot] Sep 16, 2022
44c0664
chore(deps): update all dependencies (#36)
renovate-bot Sep 16, 2022
612ebbf
chore: regenerate samples (#39)
gcf-owl-bot[bot] Sep 20, 2022
cee794e
chore(deps): update dependency google-cloud-batch to v0.3.0 (#38)
renovate-bot Sep 20, 2022
cf45502
chore: regenerate samples (#40)
gcf-owl-bot[bot] Sep 21, 2022
5a1b6b3
chore: regenerate samples (#42)
gcf-owl-bot[bot] Sep 23, 2022
c4f3b69
docs(samples): Adding sample for bucket mounting (#43)
m-strzelczyk Sep 27, 2022
8695e99
chore: regenerate samples (#45)
gcf-owl-bot[bot] Sep 27, 2022
278d7e1
docs(samples): Adding samples for template usage (#41)
m-strzelczyk Sep 30, 2022
5932f32
chore: regenerate samples (#48)
gcf-owl-bot[bot] Sep 30, 2022
1de1655
docs(samples): Adding samples for list and get tasks (#50)
m-strzelczyk Oct 3, 2022
fdfd474
chore(deps): update dependency google-cloud-resource-manager to v1.6.…
renovate-bot Oct 4, 2022
11d8441
chore(deps): update all dependencies (#51)
renovate-bot Oct 4, 2022
d540ed8
chore(deps): update dependency black to v22.10.0 (#52)
renovate-bot Oct 8, 2022
7bca50d
docs: Refine GPU drivers installation proto description (#57)
gcf-owl-bot[bot] Oct 16, 2022
a746f82
chore(deps): update all dependencies (#55)
renovate-bot Oct 18, 2022
0332217
chore(deps): update dependency google-cloud-batch to v0.4.0 (#61)
renovate-bot Oct 24, 2022
88a0f4f
chore(deps): update dependency pytest to v7.2.0 (#62)
renovate-bot Oct 26, 2022
f0fc663
docs(samples): Adding code samples for log reading (#56)
m-strzelczyk Oct 27, 2022
a099d6c
chore(deps): update dependency google-cloud-batch to v0.4.1 (#64)
renovate-bot Oct 28, 2022
dc449cf
Merge remote-tracking branch 'migration/main' into python-batch-migra…
m-strzelczyk Nov 3, 2022
f18db24
Removing auto-generated files
m-strzelczyk Nov 3, 2022
02d34cf
Removing noxfile
m-strzelczyk Nov 3, 2022
0fb0b71
feat: Tests in subdericteries get properly detected
m-strzelczyk Nov 3, 2022
c8ca8b1
Merge branch 'main' into python-batch-migration
m-strzelczyk Nov 4, 2022
7e59c94
Merge branch 'main' into python-batch-migration
dandhlee Nov 5, 2022
c9a9268
Fixing the test
m-strzelczyk Nov 7, 2022
6e141a2
Merge branch 'main' into python-batch-migration
dandhlee Nov 7, 2022
5c3311c
Merge branch 'main' into python-batch-migration
m-strzelczyk Nov 9, 2022
ec17b47
Merge branch 'main' into python-batch-migration
engelke Nov 9, 2022
65fda72
Reverting noxfile changes and adjusting folder structure.
m-strzelczyk Nov 14, 2022
6302660
Merge remote-tracking branch 'origin/main' into python-batch-migration
m-strzelczyk Nov 14, 2022
162887a
Merge branch 'main' into python-batch-migration
dandhlee Nov 14, 2022
01342c6
Merge branch 'main' into python-batch-migration
m-strzelczyk Nov 15, 2022
a7633f2
Moving batch to separate top level dir
m-strzelczyk Nov 15, 2022
abbb071
Adding batch info
m-strzelczyk Nov 15, 2022
7be0eb5
Merge branch 'main' into python-batch-migration
dandhlee Nov 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions batch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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.
Empty file added batch/create/__init__.py
Empty file.
87 changes: 87 additions & 0 deletions batch/create/create_with_container_no_mounting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 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.

# [START batch_create_container_job]
from google.cloud import batch_v1


def create_container_job(project_id: str, region: str, job_name: str) -> batch_v1.Job:
"""
This method shows how to create a sample Batch Job that will run
a simple command inside a container on Cloud Compute instances.

Args:
project_id: project ID or project number of the Cloud project you want to use.
region: name of the region you want to use to run the job. Regions that are
available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
job_name: the name of the job that will be created.
It needs to be unique for each project and region pair.

Returns:
A job object representing the job created.
"""
client = batch_v1.BatchServiceClient()

# Define what will be done as part of the job.
runnable = batch_v1.Runnable()
runnable.container = batch_v1.Runnable.Container()
runnable.container.image_uri = "gcr.io/google-containers/busybox"
runnable.container.entrypoint = "/bin/sh"
runnable.container.commands = ["-c", "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."]

# Jobs can be divided into tasks. In this case, we have only one task.
task = batch_v1.TaskSpec()
task.runnables = [runnable]

# We can specify what resources are requested by each task.
resources = batch_v1.ComputeResource()
resources.cpu_milli = 2000 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
resources.memory_mib = 16 # in MiB
task.compute_resource = resources

task.max_retry_count = 2
task.max_run_duration = "3600s"

# Tasks are grouped inside a job using TaskGroups.
# Currently, it's possible to have only one task group.
group = batch_v1.TaskGroup()
group.task_count = 4
group.task_spec = task

# Policies are used to define on what kind of virtual machines the tasks will run on.
# In this case, we tell the system to use "e2-standard-4" machine type.
# Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
policy = batch_v1.AllocationPolicy.InstancePolicy()
policy.machine_type = "e2-standard-4"
instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
instances.policy = policy
allocation_policy = batch_v1.AllocationPolicy()
allocation_policy.instances = [instances]

job = batch_v1.Job()
job.task_groups = [group]
job.allocation_policy = allocation_policy
job.labels = {"env": "testing", "type": "container"}
# We use Cloud Logging as it's an out of the box available option
job.logs_policy = batch_v1.LogsPolicy()
job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

create_request = batch_v1.CreateJobRequest()
create_request.job = job
create_request.job_id = job_name
# The job's parent is the region in which the job will run
create_request.parent = f"projects/{project_id}/locations/{region}"

return client.create_job(create_request)
# [END batch_create_container_job]
91 changes: 91 additions & 0 deletions batch/create/create_with_mounted_bucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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.

# [START batch_create_script_job_with_bucket]
from google.cloud import batch_v1


def create_script_job_with_bucket(project_id: str, region: str, job_name: str, bucket_name: str) -> batch_v1.Job:
"""
This method shows how to create a sample Batch Job that will run
a simple command on Cloud Compute instances.

Args:
project_id: project ID or project number of the Cloud project you want to use.
region: name of the region you want to use to run the job. Regions that are
available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
job_name: the name of the job that will be created.
It needs to be unique for each project and region pair.
bucket_name: name of the bucket to be mounted for your Job.

Returns:
A job object representing the job created.
"""
client = batch_v1.BatchServiceClient()

# Define what will be done as part of the job.
task = batch_v1.TaskSpec()
runnable = batch_v1.Runnable()
runnable.script = batch_v1.Runnable.Script()
runnable.script.text = "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt"
task.runnables = [runnable]

gcs_bucket = batch_v1.GCS()
gcs_bucket.remote_path = bucket_name
gcs_volume = batch_v1.Volume()
gcs_volume.gcs = gcs_bucket
gcs_volume.mount_path = '/mnt/share'
task.volumes = [gcs_volume]

# We can specify what resources are requested by each task.
resources = batch_v1.ComputeResource()
resources.cpu_milli = 500 # in milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
resources.memory_mib = 16
task.compute_resource = resources

task.max_retry_count = 2
task.max_run_duration = "3600s"

# Tasks are grouped inside a job using TaskGroups.
# Currently, it's possible to have only one task group.
group = batch_v1.TaskGroup()
group.task_count = 4
group.task_spec = task

# Policies are used to define on what kind of virtual machines the tasks will run on.
# In this case, we tell the system to use "e2-standard-4" machine type.
# Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
allocation_policy = batch_v1.AllocationPolicy()
policy = batch_v1.AllocationPolicy.InstancePolicy()
policy.machine_type = "e2-standard-4"
instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
instances.policy = policy
allocation_policy.instances = [instances]

job = batch_v1.Job()
job.task_groups = [group]
job.allocation_policy = allocation_policy
job.labels = {"env": "testing", "type": "script", "mount": "bucket"}
# We use Cloud Logging as it's an out of the box available option
job.logs_policy = batch_v1.LogsPolicy()
job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

create_request = batch_v1.CreateJobRequest()
create_request.job = job
create_request.job_id = job_name
# The job's parent is the region in which the job will run
create_request.parent = f"projects/{project_id}/locations/{region}"

return client.create_job(create_request)
# [END batch_create_script_job_with_bucket]
87 changes: 87 additions & 0 deletions batch/create/create_with_script_no_mounting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 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.

# [START batch_create_script_job]
from google.cloud import batch_v1


def create_script_job(project_id: str, region: str, job_name: str) -> batch_v1.Job:
"""
This method shows how to create a sample Batch Job that will run
a simple command on Cloud Compute instances.

Args:
project_id: project ID or project number of the Cloud project you want to use.
region: name of the region you want to use to run the job. Regions that are
available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
job_name: the name of the job that will be created.
It needs to be unique for each project and region pair.

Returns:
A job object representing the job created.
"""
client = batch_v1.BatchServiceClient()

# Define what will be done as part of the job.
task = batch_v1.TaskSpec()
runnable = batch_v1.Runnable()
runnable.script = batch_v1.Runnable.Script()
runnable.script.text = "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
# You can also run a script from a file. Just remember, that needs to be a script that's
# already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
# exclusive.
# runnable.script.path = '/tmp/test.sh'
task.runnables = [runnable]

# We can specify what resources are requested by each task.
resources = batch_v1.ComputeResource()
resources.cpu_milli = 2000 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
resources.memory_mib = 16
task.compute_resource = resources

task.max_retry_count = 2
task.max_run_duration = "3600s"

# Tasks are grouped inside a job using TaskGroups.
# Currently, it's possible to have only one task group.
group = batch_v1.TaskGroup()
group.task_count = 4
group.task_spec = task

# Policies are used to define on what kind of virtual machines the tasks will run on.
# In this case, we tell the system to use "e2-standard-4" machine type.
# Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
allocation_policy = batch_v1.AllocationPolicy()
policy = batch_v1.AllocationPolicy.InstancePolicy()
policy.machine_type = "e2-standard-4"
instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
instances.policy = policy
allocation_policy.instances = [instances]

job = batch_v1.Job()
job.task_groups = [group]
job.allocation_policy = allocation_policy
job.labels = {"env": "testing", "type": "script"}
# We use Cloud Logging as it's an out of the box available option
job.logs_policy = batch_v1.LogsPolicy()
job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

create_request = batch_v1.CreateJobRequest()
create_request.job = job
create_request.job_id = job_name
# The job's parent is the region in which the job will run
create_request.parent = f"projects/{project_id}/locations/{region}"

return client.create_job(create_request)
# [END batch_create_script_job]
88 changes: 88 additions & 0 deletions batch/create/create_with_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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.

# [START batch_create_job_with_template]
from google.cloud import batch_v1


def create_script_job_with_template(project_id: str, region: str, job_name: str, template_link: str) -> batch_v1.Job:
"""
This method shows how to create a sample Batch Job that will run
a simple command on Cloud Compute instances created using a provided Template.

Args:
project_id: project ID or project number of the Cloud project you want to use.
region: name of the region you want to use to run the job. Regions that are
available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
job_name: the name of the job that will be created.
It needs to be unique for each project and region pair.
template_link: a link to an existing Instance Template. Acceptable formats:
* "projects/{project_id}/global/instanceTemplates/{template_name}"
* "{template_name}" - if the template is defined in the same project as used to create the Job.

Returns:
A job object representing the job created.
"""
client = batch_v1.BatchServiceClient()

# Define what will be done as part of the job.
task = batch_v1.TaskSpec()
runnable = batch_v1.Runnable()
runnable.script = batch_v1.Runnable.Script()
runnable.script.text = "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
# You can also run a script from a file. Just remember, that needs to be a script that's
# already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
# exclusive.
# runnable.script.path = '/tmp/test.sh'
task.runnables = [runnable]

# We can specify what resources are requested by each task.
resources = batch_v1.ComputeResource()
resources.cpu_milli = 2000 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
resources.memory_mib = 16
task.compute_resource = resources

task.max_retry_count = 2
task.max_run_duration = "3600s"

# Tasks are grouped inside a job using TaskGroups.
# Currently, it's possible to have only one task group.
group = batch_v1.TaskGroup()
group.task_count = 4
group.task_spec = task

# Policies are used to define on what kind of virtual machines the tasks will run on.
# In this case, we tell the system to use an instance template that defines all the
# required parameters.
allocation_policy = batch_v1.AllocationPolicy()
instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
instances.instance_template = template_link
allocation_policy.instances = [instances]

job = batch_v1.Job()
job.task_groups = [group]
job.allocation_policy = allocation_policy
job.labels = {"env": "testing", "type": "script"}
# We use Cloud Logging as it's an out of the box available option
job.logs_policy = batch_v1.LogsPolicy()
job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

create_request = batch_v1.CreateJobRequest()
create_request.job = job
create_request.job_id = job_name
# The job's parent is the region in which the job will run
create_request.parent = f"projects/{project_id}/locations/{region}"

return client.create_job(create_request)
# [END batch_create_job_with_template]
Loading