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

adding preptask sample #266

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
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
6 changes: 6 additions & 0 deletions Python/Batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ path invocation. This sample can be run on Windows with an appropriate openssl
binary and modified openssl invocations (i.e., `openssl.exe` instead of
`openssl`).

#### [sample4\_preparationtask.py](./sample4_preparationtask.py)
This sample demostrates preparation task with batch job to show environment
manishshar marked this conversation as resolved.
Show resolved Hide resolved
variables using command line as an example before executing normal batch
manishshar marked this conversation as resolved.
Show resolved Hide resolved
task on each nodes. It spin-up a fixed pool of windows paas cloud service
manishshar marked this conversation as resolved.
Show resolved Hide resolved
and submits a simple python script as the only task of the job.
manishshar marked this conversation as resolved.
Show resolved Hide resolved

## Azure Batch on Linux Best Practices

Although some of the Python samples are not specific to Linux, the Azure Batch
Expand Down
4 changes: 4 additions & 0 deletions Python/Batch/sample4_preparationtask.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[DEFAULT]
shoulddeletejob=true
poolvmsize=small
poolvmcount=1
manishshar marked this conversation as resolved.
Show resolved Hide resolved
126 changes: 126 additions & 0 deletions Python/Batch/sample4_preparationtask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Sample4_preparationtask.py Code Sample

from __future__ import print_function
try:
import configparser
except ImportError:
import ConfigParser as configparser
import datetime
import os

import azure.batch.batch_service_client as batch
import azure.batch.batch_auth as batchauth
import azure.batch.models as batchmodels

import common.helpers

preptaskcommand = 'cmd /c set'
manishshar marked this conversation as resolved.
Show resolved Hide resolved


def submit_job_and_add_task(batch_client, job_id, vm_size, vm_count):
"""Submits a job to the Azure Batch service
and adds a simple task with preparation task
:param batch_client: The batch client to use.
:type batch_client: `batchserviceclient.BatchServiceClient`
:param str job_id: The id of the job to create.
"""

pool_info = batchmodels.PoolInformation(
auto_pool_specification=batchmodels.AutoPoolSpecification(
auto_pool_id_prefix="Helloworld_jobprep",
pool=batchmodels.PoolSpecification(
vm_size=vm_size,
target_dedicated_nodes=vm_count,
cloud_service_configuration={'os_family': "4"}),
keep_alive=False,
pool_lifetime_option=batchmodels.PoolLifetimeOption.job))

job = batchmodels.JobAddParameter(
id=job_id,
pool_info=pool_info,
job_preparation_task=batch.models.JobPreparationTask(
command_line=preptaskcommand,
manishshar marked this conversation as resolved.
Show resolved Hide resolved
wait_for_success=True)
)

batch_client.job.add(job)

task = batchmodels.TaskAddParameter(
id="HelloWorld_Task",
command_line=common.helpers.wrap_commands_in_shell(
'windows', ['echo Hello world from the Batch Hello world sample!'])
)

batch_client.task.add(job_id=job.id, task=task)


def execute_sample(global_config, sample_config):
"""Executes the sample with the specified configurations.

:param global_config: The global configuration to use.
:type global_config: `configparser.ConfigParser`
:param sample_config: The sample specific configuration to use.
:type sample_config: `configparser.ConfigParser`
"""
# Set up the configuration
batch_account_key = global_config.get('Batch', 'batchaccountkey')
batch_account_name = global_config.get('Batch', 'batchaccountname')
batch_service_url = global_config.get('Batch', 'batchserviceurl')

should_delete_job = sample_config.getboolean(
'DEFAULT',
'shoulddeletejob')
pool_vm_size = sample_config.get(
'DEFAULT',
'poolvmsize')
pool_vm_count = sample_config.getint(
'DEFAULT',
'poolvmcount')

# Print the settings we are running with
common.helpers.print_configuration(global_config)
common.helpers.print_configuration(sample_config)

credentials = batchauth.SharedKeyCredentials(
batch_account_name,
batch_account_key)

batch_client = batch.BatchServiceClient(
credentials,
base_url=batch_service_url)

# Retry 5 times -- default is 3
batch_client.config.retry_policy.retries = 5
job_id = common.helpers.generate_unique_resource_name("samplePrepJob")

try:
submit_job_and_add_task(
batch_client,
job_id,
pool_vm_size,
pool_vm_count)

common.helpers.wait_for_tasks_to_complete(
batch_client,
job_id,
datetime.timedelta(minutes=25))

tasks = batch_client.task.list(job_id)
task_ids = [task.id for task in tasks]

common.helpers.print_task_output(batch_client, job_id, task_ids)
finally:
if should_delete_job:
print("Deleting job: ", job_id)
batch_client.job.delete(job_id)


if __name__ == '__main__':
global_config = configparser.ConfigParser()
global_config.read(common.helpers._SAMPLES_CONFIG_FILE_NAME)

sample_config = configparser.ConfigParser()
sample_config.read(
os.path.splitext(os.path.basename(__file__))[0] + '.cfg')

execute_sample(global_config, sample_config)