Skip to content

Commit 079a7d2

Browse files
authored
Merge pull request #313 from Azure-Samples/pythonjobprep
adding in a sample for job prep and release taks
2 parents 8e90e33 + e7992e2 commit 079a7d2

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

Python/Batch/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
azure-batch==11.0.0
1+
azure-batch==12.0.0
22
azure-storage-blob==12.8.1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[DEFAULT]
2+
shoulddeletejob=true
3+
poolvmsize=STANDARD_DS1_V2
4+
poolvmcount=1
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# sample1_jobprep_and_release.py Code Sample
2+
#
3+
# Copyright (c) Microsoft Corporation
4+
#
5+
# All rights reserved.
6+
#
7+
# MIT License
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a
10+
# copy of this software and associated documentation files (the "Software"),
11+
# to deal in the Software without restriction, including without limitation
12+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
13+
# and/or sell copies of the Software, and to permit persons to whom the
14+
# Software is furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in
17+
# all copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25+
# DEALINGS IN THE SOFTWARE.
26+
27+
"""
28+
Create a pool and submit job with a preparation and release task
29+
"""
30+
31+
import datetime
32+
import os
33+
from configparser import ConfigParser
34+
35+
from azure.batch import BatchServiceClient
36+
from azure.batch.batch_auth import SharedKeyCredentials
37+
import azure.batch.models as batchmodels
38+
39+
import common.helpers
40+
41+
42+
def submit_job_with_prep_and_release_tasks(
43+
batch_client: BatchServiceClient,
44+
job_id: str,
45+
vm_size: str,
46+
node_count: int
47+
):
48+
"""Submits a job to the Azure Batch service and adds a simple task.
49+
50+
:param batch_client: The batch client to use.
51+
:param job_id: The id of the job to create.
52+
:param vm_size: The VM size to use.
53+
:param node_count: The number of dedicated nodes to start.
54+
"""
55+
56+
vm_config = batchmodels.VirtualMachineConfiguration(
57+
image_reference=batchmodels.ImageReference(
58+
publisher="canonical",
59+
offer="ubuntuserver",
60+
sku="18.04-lts"
61+
),
62+
node_agent_sku_id="batch.node.ubuntu 18.04"
63+
)
64+
pool_info = batchmodels.PoolInformation(
65+
auto_pool_specification=batchmodels.AutoPoolSpecification(
66+
auto_pool_id_prefix="JobPrepAndRelease",
67+
pool=batchmodels.PoolSpecification(
68+
vm_size=vm_size,
69+
target_dedicated_nodes=node_count,
70+
virtual_machine_configuration=vm_config),
71+
keep_alive=False,
72+
pool_lifetime_option=batchmodels.PoolLifetimeOption.job))
73+
74+
job = batchmodels.JobAddParameter(id=job_id, pool_info=pool_info)
75+
76+
job.job_preparation_task = batchmodels.JobPreparationTask(
77+
command_line=common.helpers.wrap_commands_in_shell(
78+
'linux', ['echo job preparation task!']))
79+
80+
job.job_release_task = batchmodels.JobReleaseTask(
81+
command_line=common.helpers.wrap_commands_in_shell(
82+
'linux', ['echo job release task!']))
83+
84+
batch_client.job.add(job)
85+
86+
task = batchmodels.TaskAddParameter(
87+
id="JobPrepAndRelease",
88+
command_line=common.helpers.wrap_commands_in_shell(
89+
'linux',
90+
['echo Hello world from the Batch Job prep and release sample!'])
91+
)
92+
93+
batch_client.task.add(job_id=job.id, task=task)
94+
95+
96+
def execute_sample(global_config: ConfigParser, sample_config: ConfigParser):
97+
"""Executes the sample with the specified configurations.
98+
99+
:param global_config: The global configuration to use.
100+
:param sample_config: The sample specific configuration to use.
101+
"""
102+
# Set up the configuration
103+
batch_account_key = global_config.get('Batch', 'batchaccountkey')
104+
batch_account_name = global_config.get('Batch', 'batchaccountname')
105+
batch_service_url = global_config.get('Batch', 'batchserviceurl')
106+
107+
should_delete_job = sample_config.getboolean(
108+
'DEFAULT',
109+
'shoulddeletejob')
110+
pool_vm_size = sample_config.get(
111+
'DEFAULT',
112+
'poolvmsize')
113+
pool_vm_count = sample_config.getint(
114+
'DEFAULT',
115+
'poolvmcount')
116+
117+
# Print the settings we are running with
118+
common.helpers.print_configuration(global_config)
119+
common.helpers.print_configuration(sample_config)
120+
121+
credentials = SharedKeyCredentials(
122+
batch_account_name,
123+
batch_account_key)
124+
125+
batch_client = BatchServiceClient(
126+
credentials,
127+
batch_url=batch_service_url)
128+
129+
# Retry 5 times -- default is 3
130+
batch_client.config.retry_policy.retries = 5
131+
job_id = common.helpers.generate_unique_resource_name("JobPrepRelease")
132+
133+
try:
134+
submit_job_with_prep_and_release_tasks(
135+
batch_client,
136+
job_id,
137+
pool_vm_size,
138+
pool_vm_count)
139+
140+
common.helpers.wait_for_tasks_to_complete(
141+
batch_client,
142+
job_id,
143+
datetime.timedelta(minutes=25))
144+
145+
tasks = batch_client.task.list(job_id)
146+
task_ids = [task.id for task in tasks]
147+
148+
common.helpers.print_task_output(batch_client, job_id, task_ids)
149+
finally:
150+
if should_delete_job:
151+
print("Deleting job: ", job_id)
152+
batch_client.job.delete(job_id)
153+
154+
155+
if __name__ == '__main__':
156+
global_cfg = ConfigParser()
157+
global_cfg.read(common.helpers.SAMPLES_CONFIG_FILE_NAME)
158+
159+
sample_cfg = ConfigParser()
160+
sample_cfg.read(
161+
os.path.splitext(os.path.basename(__file__))[0] + '.cfg')
162+
163+
execute_sample(global_cfg, sample_cfg)

0 commit comments

Comments
 (0)