Skip to content

Commit 8a3f6f5

Browse files
committed
adding a new sample
1 parent 8e90e33 commit 8a3f6f5

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-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: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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_and_add_task(
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(command_line=common.helpers.wrap_commands_in_shell(
77+
'linux', ['echo job preparation task!']))
78+
79+
job.job_release_task = batchmodels.JobReleaseTask(command_line=common.helpers.wrap_commands_in_shell(
80+
'linux', ['echo job release task!']))
81+
82+
batch_client.job.add(job)
83+
84+
task = batchmodels.TaskAddParameter(
85+
id="JobPrepAndRelease",
86+
command_line=common.helpers.wrap_commands_in_shell(
87+
'linux', ['echo Hello world from the Batch Job prep and release sample!'])
88+
)
89+
90+
batch_client.task.add(job_id=job.id, task=task)
91+
92+
93+
def execute_sample(global_config: ConfigParser, sample_config: ConfigParser):
94+
"""Executes the sample with the specified configurations.
95+
96+
:param global_config: The global configuration to use.
97+
:param sample_config: The sample specific configuration to use.
98+
"""
99+
# Set up the configuration
100+
batch_account_key = global_config.get('Batch', 'batchaccountkey')
101+
batch_account_name = global_config.get('Batch', 'batchaccountname')
102+
batch_service_url = global_config.get('Batch', 'batchserviceurl')
103+
104+
should_delete_job = sample_config.getboolean(
105+
'DEFAULT',
106+
'shoulddeletejob')
107+
pool_vm_size = sample_config.get(
108+
'DEFAULT',
109+
'poolvmsize')
110+
pool_vm_count = sample_config.getint(
111+
'DEFAULT',
112+
'poolvmcount')
113+
114+
# Print the settings we are running with
115+
common.helpers.print_configuration(global_config)
116+
common.helpers.print_configuration(sample_config)
117+
118+
credentials = SharedKeyCredentials(
119+
batch_account_name,
120+
batch_account_key)
121+
122+
batch_client = BatchServiceClient(
123+
credentials,
124+
batch_url=batch_service_url)
125+
126+
# Retry 5 times -- default is 3
127+
batch_client.config.retry_policy.retries = 5
128+
job_id = common.helpers.generate_unique_resource_name("JobPrepRelease")
129+
130+
try:
131+
submit_job_and_add_task(
132+
batch_client,
133+
job_id,
134+
pool_vm_size,
135+
pool_vm_count)
136+
137+
common.helpers.wait_for_tasks_to_complete(
138+
batch_client,
139+
job_id,
140+
datetime.timedelta(minutes=25))
141+
142+
tasks = batch_client.task.list(job_id)
143+
task_ids = [task.id for task in tasks]
144+
145+
common.helpers.print_task_output(batch_client, job_id, task_ids)
146+
finally:
147+
if should_delete_job:
148+
print("Deleting job: ", job_id)
149+
batch_client.job.delete(job_id)
150+
151+
152+
if __name__ == '__main__':
153+
global_cfg = ConfigParser()
154+
global_cfg.read(common.helpers.SAMPLES_CONFIG_FILE_NAME)
155+
156+
sample_cfg = ConfigParser()
157+
sample_cfg.read(
158+
os.path.splitext(os.path.basename(__file__))[0] + '.cfg')
159+
160+
execute_sample(global_cfg, sample_cfg)

0 commit comments

Comments
 (0)