2424# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2525# DEALINGS IN THE SOFTWARE.
2626
27- from __future__ import print_function
28- try :
29- import configparser
30- except ImportError :
31- import ConfigParser as configparser
27+ """
28+ Create a pool and submit a task which use resource files.
29+ """
30+
3231import datetime
3332import os
33+ from configparser import ConfigParser
34+
35+ from azure .core .exceptions import ResourceExistsError
3436
35- import azure .storage .blob as azureblob
36- import azure .batch . _batch_service_client as batch
37- import azure .batch .batch_auth as batchauth
37+ from azure .storage .blob import BlobServiceClient
38+ from azure .batch import BatchServiceClient
39+ from azure .batch .batch_auth import SharedKeyCredentials
3840import azure .batch .models as batchmodels
3941
4042import common .helpers
4446_SIMPLE_TASK_PATH = os .path .join ('resources' , 'simple_task.py' )
4547
4648
47- def create_pool (batch_client , block_blob_client , pool_id , vm_size , vm_count ):
49+ def create_pool (
50+ batch_client : BatchServiceClient ,
51+ blob_service_client : BlobServiceClient ,
52+ pool_id : str ,
53+ vm_size : str ,
54+ vm_count : int ):
4855 """Creates an Azure Batch pool with the specified id.
4956
5057 :param batch_client: The batch client to use.
51- :type batch_client: `batchserviceclient.BatchServiceClient`
52- :param block_blob_client: The storage block blob client to use.
53- :type block_blob_client: `azure.storage.blob.BlockBlobService`
54- :param str pool_id: The id of the pool to create.
55- :param str vm_size: vm size (sku)
56- :param int vm_count: number of vms to allocate
58+ :param blob_service_client: The storage block blob client to use.
59+ :param pool_id: The id of the pool to create.
60+ :param vm_size: vm size (sku)
61+ :param vm_count: number of vms to allocate
5762 """
5863 # pick the latest supported 16.04 sku for UbuntuServer
5964 sku_to_use , image_ref_to_use = \
6065 common .helpers .select_latest_verified_vm_image_with_node_agent_sku (
61- batch_client , 'Canonical ' , 'UbuntuServer ' , '16 .04' )
66+ batch_client , 'canonical ' , 'ubuntuserver ' , '18 .04' )
6267
63- block_blob_client .create_container (
64- _CONTAINER_NAME ,
65- fail_on_exist = False )
68+ try :
69+ blob_service_client .create_container (_CONTAINER_NAME )
70+ except ResourceExistsError :
71+ pass
6672
6773 sas_url = common .helpers .upload_blob_and_create_sas (
68- block_blob_client ,
74+ blob_service_client ,
6975 _CONTAINER_NAME ,
7076 _SIMPLE_TASK_NAME ,
7177 _SIMPLE_TASK_PATH ,
@@ -87,29 +93,32 @@ def create_pool(batch_client, block_blob_client, pool_id, vm_size, vm_count):
8793 common .helpers .create_pool_if_not_exist (batch_client , pool )
8894
8995
90- def submit_job_and_add_task (batch_client , block_blob_client , job_id , pool_id ):
96+ def submit_job_and_add_task (
97+ batch_client : BatchServiceClient ,
98+ blob_service_client : BlobServiceClient ,
99+ job_id : str ,
100+ pool_id : str ):
91101 """Submits a job to the Azure Batch service and adds
92102 a task that runs a python script.
93103
94104 :param batch_client: The batch client to use.
95- :type batch_client: `batchserviceclient.BatchServiceClient`
96- :param block_blob_client: The storage block blob client to use.
97- :type block_blob_client: `azure.storage.blob.BlockBlobService`
98- :param str job_id: The id of the job to create.
99- :param str pool_id: The id of the pool to use.
105+ :param blob_service_client: The storage block blob client to use.
106+ :param job_id: The id of the job to create.
107+ :param pool_id: The id of the pool to use.
100108 """
101109 job = batchmodels .JobAddParameter (
102110 id = job_id ,
103111 pool_info = batchmodels .PoolInformation (pool_id = pool_id ))
104112
105113 batch_client .job .add (job )
106114
107- block_blob_client .create_container (
108- _CONTAINER_NAME ,
109- fail_on_exist = False )
115+ try :
116+ blob_service_client .create_container (_CONTAINER_NAME )
117+ except ResourceExistsError :
118+ pass
110119
111120 sas_url = common .helpers .upload_blob_and_create_sas (
112- block_blob_client ,
121+ blob_service_client ,
113122 _CONTAINER_NAME ,
114123 _SIMPLE_TASK_NAME ,
115124 _SIMPLE_TASK_PATH ,
@@ -125,24 +134,19 @@ def submit_job_and_add_task(batch_client, block_blob_client, job_id, pool_id):
125134 batch_client .task .add (job_id = job .id , task = task )
126135
127136
128- def execute_sample (global_config , sample_config ):
137+ def execute_sample (global_config : ConfigParser , sample_config : ConfigParser ):
129138 """Executes the sample with the specified configurations.
130139
131140 :param global_config: The global configuration to use.
132- :type global_config: `configparser.ConfigParser`
133141 :param sample_config: The sample specific configuration to use.
134- :type sample_config: `configparser.ConfigParser`
135142 """
136143 # Set up the configuration
137144 batch_account_key = global_config .get ('Batch' , 'batchaccountkey' )
138145 batch_account_name = global_config .get ('Batch' , 'batchaccountname' )
139146 batch_service_url = global_config .get ('Batch' , 'batchserviceurl' )
140147
148+ storage_account_url = global_config .get ('Storage' , 'storageaccounturl' )
141149 storage_account_key = global_config .get ('Storage' , 'storageaccountkey' )
142- storage_account_name = global_config .get ('Storage' , 'storageaccountname' )
143- storage_account_suffix = global_config .get (
144- 'Storage' ,
145- 'storageaccountsuffix' )
146150
147151 should_delete_container = sample_config .getboolean (
148152 'DEFAULT' ,
@@ -164,35 +168,35 @@ def execute_sample(global_config, sample_config):
164168 common .helpers .print_configuration (global_config )
165169 common .helpers .print_configuration (sample_config )
166170
167- credentials = batchauth . SharedKeyCredentials (
171+ credentials = SharedKeyCredentials (
168172 batch_account_name ,
169173 batch_account_key )
170- batch_client = batch .BatchServiceClient (
174+
175+ batch_client = BatchServiceClient (
171176 credentials ,
172177 batch_url = batch_service_url )
173178
174179 # Retry 5 times -- default is 3
175180 batch_client .config .retry_policy .retries = 5
176181
177- block_blob_client = azureblob .BlockBlobService (
178- account_name = storage_account_name ,
179- account_key = storage_account_key ,
180- endpoint_suffix = storage_account_suffix )
182+ blob_service_client = BlobServiceClient (
183+ account_url = storage_account_url ,
184+ credential = storage_account_key )
181185
182186 job_id = common .helpers .generate_unique_resource_name (
183187 "PoolsAndResourceFilesJob" )
184188 pool_id = "PoolsAndResourceFilesPool"
185189 try :
186190 create_pool (
187191 batch_client ,
188- block_blob_client ,
192+ blob_service_client ,
189193 pool_id ,
190194 pool_vm_size ,
191195 pool_vm_count )
192196
193197 submit_job_and_add_task (
194198 batch_client ,
195- block_blob_client ,
199+ blob_service_client ,
196200 job_id , pool_id )
197201
198202 common .helpers .wait_for_tasks_to_complete (
@@ -207,9 +211,7 @@ def execute_sample(global_config, sample_config):
207211 finally :
208212 # clean up
209213 if should_delete_container :
210- block_blob_client .delete_container (
211- _CONTAINER_NAME ,
212- fail_not_exist = False )
214+ blob_service_client .delete_container (_CONTAINER_NAME )
213215 if should_delete_job :
214216 print ("Deleting job: " , job_id )
215217 batch_client .job .delete (job_id )
@@ -219,11 +221,11 @@ def execute_sample(global_config, sample_config):
219221
220222
221223if __name__ == '__main__' :
222- global_config = configparser . ConfigParser ()
223- global_config .read (common .helpers ._SAMPLES_CONFIG_FILE_NAME )
224+ global_cfg = ConfigParser ()
225+ global_cfg .read (common .helpers .SAMPLES_CONFIG_FILE_NAME )
224226
225- sample_config = configparser . ConfigParser ()
226- sample_config .read (
227+ sample_cfg = ConfigParser ()
228+ sample_cfg .read (
227229 os .path .splitext (os .path .basename (__file__ ))[0 ] + '.cfg' )
228230
229- execute_sample (global_config , sample_config )
231+ execute_sample (global_cfg , sample_cfg )
0 commit comments