|
| 1 | +# sample4_job_scheduler.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 | +from __future__ import print_function |
| 28 | + |
| 29 | +try: |
| 30 | + import configparser |
| 31 | +except ImportError: |
| 32 | + import ConfigParser as configparser |
| 33 | + |
| 34 | +import datetime |
| 35 | +import os |
| 36 | + |
| 37 | +import azure.batch.batch_service_client as batch |
| 38 | +import azure.storage.blob as azureblob |
| 39 | +import azure.batch.batch_auth as batchauth |
| 40 | +import azure.batch.models as batchmodels |
| 41 | + |
| 42 | +import common.helpers |
| 43 | + |
| 44 | +_CONTAINER_NAME = 'jobscheduler' |
| 45 | +_SIMPLE_TASK_NAME = 'simple_task.py' |
| 46 | +_SIMPLE_TASK_PATH = os.path.join('resources', 'simple_task.py') |
| 47 | +_PYTHON_DOWNLOAD = \ |
| 48 | + 'https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64.exe' |
| 49 | +_PYTHON_INSTALL = \ |
| 50 | + r'.\python373.exe /passive InstallAllUsers=1 PrependPath=1 Include_test=0' |
| 51 | +_USER_ELEVATION_LEVEL = 'admin' |
| 52 | +_START_TIME = datetime.datetime.utcnow() |
| 53 | +_END_TIME = _START_TIME + datetime.timedelta(minutes=30) |
| 54 | + |
| 55 | + |
| 56 | +def create_job_schedule(batch_client, job_schedule_id, vm_size, vm_count, |
| 57 | + block_blob_client): |
| 58 | + """Creates an Azure Batch pool and job schedule with the specified ids. |
| 59 | +
|
| 60 | + :param batch_client: The batch client to use. |
| 61 | + :type batch_client: `batchserviceclient.BatchServiceClient` |
| 62 | + :param str job_schedule_id: The id of the job schedule to create |
| 63 | + :param str vm_size: vm size (sku) |
| 64 | + :param int vm_count: number of vms to allocate |
| 65 | + :param block_blob_client: The storage block blob client to use. |
| 66 | + :type block_blob_client: `azure.storage.blob.BlockBlobService` |
| 67 | + """ |
| 68 | + cloud_service_config = batchmodels.CloudServiceConfiguration(os_family='6') |
| 69 | + |
| 70 | + user_id = batchmodels.UserIdentity( |
| 71 | + auto_user=batchmodels.AutoUserSpecification( |
| 72 | + elevation_level=_USER_ELEVATION_LEVEL)) |
| 73 | + |
| 74 | + python_download = batchmodels.ResourceFile( |
| 75 | + http_url=_PYTHON_DOWNLOAD, |
| 76 | + file_path='python373.exe') |
| 77 | + |
| 78 | + pool_info = batchmodels.PoolInformation( |
| 79 | + auto_pool_specification=batchmodels.AutoPoolSpecification( |
| 80 | + auto_pool_id_prefix="JobScheduler", |
| 81 | + pool=batchmodels.PoolSpecification( |
| 82 | + vm_size=vm_size, |
| 83 | + target_dedicated_nodes=vm_count, |
| 84 | + cloud_service_configuration=cloud_service_config, |
| 85 | + start_task=batchmodels.StartTask( |
| 86 | + command_line=common.helpers.wrap_commands_in_shell( |
| 87 | + 'windows', ['{}'.format(_PYTHON_INSTALL)]), |
| 88 | + resource_files=[python_download], |
| 89 | + wait_for_success=True, |
| 90 | + user_identity=user_id)), |
| 91 | + keep_alive=False, |
| 92 | + pool_lifetime_option=batchmodels.PoolLifetimeOption.job)) |
| 93 | + |
| 94 | + sas_url = common.helpers.upload_blob_and_create_sas( |
| 95 | + block_blob_client, |
| 96 | + _CONTAINER_NAME, |
| 97 | + _SIMPLE_TASK_NAME, |
| 98 | + _SIMPLE_TASK_PATH, |
| 99 | + datetime.datetime.utcnow() + datetime.timedelta(minutes=30)) |
| 100 | + |
| 101 | + job_spec = batchmodels.JobSpecification( |
| 102 | + pool_info=pool_info, |
| 103 | + # Terminate job once all tasks under it are complete to allow for a new |
| 104 | + # job to be created under the schedule |
| 105 | + on_all_tasks_complete=batchmodels.OnAllTasksComplete.terminate_job, |
| 106 | + job_manager_task=batchmodels.JobManagerTask( |
| 107 | + id="JobManagerTask", |
| 108 | + command_line=common.helpers.wrap_commands_in_shell( |
| 109 | + 'windows', ['python {}'.format(_SIMPLE_TASK_NAME)]), |
| 110 | + resource_files=[batchmodels.ResourceFile( |
| 111 | + file_path=_SIMPLE_TASK_NAME, |
| 112 | + http_url=sas_url)])) |
| 113 | + |
| 114 | + do_not_run_after = datetime.datetime.utcnow() \ |
| 115 | + + datetime.timedelta(minutes=30) |
| 116 | + |
| 117 | + schedule = batchmodels.Schedule( |
| 118 | + do_not_run_after=do_not_run_after, |
| 119 | + recurrence_interval=datetime.timedelta(minutes=10)) |
| 120 | + |
| 121 | + scheduled_job = batchmodels.JobScheduleAddParameter( |
| 122 | + id=job_schedule_id, |
| 123 | + schedule=schedule, |
| 124 | + job_specification=job_spec) |
| 125 | + |
| 126 | + batch_client.job_schedule.add(cloud_job_schedule=scheduled_job) |
| 127 | + |
| 128 | + |
| 129 | +def execute_sample(global_config, sample_config): |
| 130 | + """Executes the sample with the specified configurations. |
| 131 | +
|
| 132 | + :param global_config: The global configuration to use. |
| 133 | + :type global_config: `configparser.ConfigParser` |
| 134 | + :param sample_config: The sample specific configuration to use. |
| 135 | + :type sample_config: `configparser.ConfigParser` |
| 136 | + """ |
| 137 | + # Set up the configuration |
| 138 | + batch_account_key = global_config.get('Batch', 'batchaccountkey') |
| 139 | + batch_account_name = global_config.get('Batch', 'batchaccountname') |
| 140 | + batch_service_url = global_config.get('Batch', 'batchserviceurl') |
| 141 | + |
| 142 | + storage_account_key = global_config.get('Storage', 'storageaccountkey') |
| 143 | + storage_account_name = global_config.get('Storage', 'storageaccountname') |
| 144 | + storage_account_suffix = global_config.get( |
| 145 | + 'Storage', |
| 146 | + 'storageaccountsuffix') |
| 147 | + |
| 148 | + should_delete_container = sample_config.getboolean( |
| 149 | + 'DEFAULT', |
| 150 | + 'shoulddeletecontainer') |
| 151 | + should_delete_job_schedule = sample_config.getboolean( |
| 152 | + 'DEFAULT', |
| 153 | + 'shoulddeletejobschedule') |
| 154 | + pool_vm_size = sample_config.get( |
| 155 | + 'DEFAULT', |
| 156 | + 'poolvmsize') |
| 157 | + pool_vm_count = sample_config.getint( |
| 158 | + 'DEFAULT', |
| 159 | + 'poolvmcount') |
| 160 | + |
| 161 | + # Print the settings we are running with |
| 162 | + common.helpers.print_configuration(global_config) |
| 163 | + common.helpers.print_configuration(sample_config) |
| 164 | + |
| 165 | + credentials = batchauth.SharedKeyCredentials( |
| 166 | + batch_account_name, |
| 167 | + batch_account_key) |
| 168 | + |
| 169 | + batch_client = batch.BatchServiceClient( |
| 170 | + credentials, |
| 171 | + batch_url=batch_service_url) |
| 172 | + |
| 173 | + block_blob_client = azureblob.BlockBlobService( |
| 174 | + account_name=storage_account_name, |
| 175 | + account_key=storage_account_key, |
| 176 | + endpoint_suffix=storage_account_suffix) |
| 177 | + |
| 178 | + batch_client.config.retry_policy.retries = 5 |
| 179 | + job_schedule_id = common.helpers.generate_unique_resource_name( |
| 180 | + "JobScheduler") |
| 181 | + |
| 182 | + try: |
| 183 | + create_job_schedule( |
| 184 | + batch_client, |
| 185 | + job_schedule_id, |
| 186 | + pool_vm_size, |
| 187 | + pool_vm_count, |
| 188 | + block_blob_client) |
| 189 | + |
| 190 | + print("Start time: ", _START_TIME) |
| 191 | + print("Delete time: ", _END_TIME) |
| 192 | + |
| 193 | + recent_job = common.helpers.wait_for_job_under_job_schedule( |
| 194 | + batch_client, |
| 195 | + job_schedule_id, |
| 196 | + datetime.timedelta(minutes=5)) |
| 197 | + |
| 198 | + common.helpers.wait_for_tasks_to_complete( |
| 199 | + batch_client, |
| 200 | + recent_job, |
| 201 | + datetime.timedelta(minutes=25)) |
| 202 | + |
| 203 | + tasks = batch_client.task.list(recent_job) |
| 204 | + task_ids = [task.id for task in tasks] |
| 205 | + |
| 206 | + common.helpers.print_task_output( |
| 207 | + batch_client, |
| 208 | + recent_job, |
| 209 | + task_ids) |
| 210 | + |
| 211 | + common.helpers.wait_for_job_schedule_to_complete( |
| 212 | + batch_client, |
| 213 | + job_schedule_id, |
| 214 | + _END_TIME + datetime.timedelta(minutes=10)) |
| 215 | + |
| 216 | + except batchmodels.BatchErrorException as e: |
| 217 | + for x in e.error.values: |
| 218 | + print("BatchErrorException: ", x) |
| 219 | + |
| 220 | + finally: |
| 221 | + if should_delete_job_schedule: |
| 222 | + print("Deleting job schedule: ", job_schedule_id) |
| 223 | + batch_client.job_schedule.delete(job_schedule_id) |
| 224 | + if should_delete_container: |
| 225 | + block_blob_client.delete_container( |
| 226 | + _CONTAINER_NAME, |
| 227 | + fail_not_exist=False) |
| 228 | + |
| 229 | + |
| 230 | +if __name__ == '__main__': |
| 231 | + global_config = configparser.ConfigParser() |
| 232 | + global_config.read(common.helpers._SAMPLES_CONFIG_FILE_NAME) |
| 233 | + |
| 234 | + sample_config = configparser.ConfigParser() |
| 235 | + sample_config.read( |
| 236 | + os.path.splitext(os.path.basename(__file__))[0] + '.cfg') |
| 237 | + |
| 238 | + execute_sample(global_config, sample_config) |
0 commit comments