Skip to content

Commit 8e90e33

Browse files
authored
Update Python SDK versions, add type hints (#305)
* Updated CI image to windows-2019 * Update Python SDK versions * Fix linting warnings and added type annotations
1 parent c212f8d commit 8e90e33

12 files changed

+563
-430
lines changed

Python/Batch/common/helpers.py

Lines changed: 275 additions & 189 deletions
Large diffs are not rendered by default.

Python/Batch/configuration.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ batchaccountkey=
44
batchserviceurl=
55

66
[Storage]
7-
storageaccountname=
7+
storageaccounturl=
88
storageaccountkey=
9-
storageaccountsuffix=core.windows.net

Python/Batch/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
azure-batch==8.0.0
2-
azure-storage-blob==1.3.1
1+
azure-batch==11.0.0
2+
azure-storage-blob==12.8.1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[DEFAULT]
22
shoulddeletejob=true
3-
poolvmsize=STANDARD_D1_V2
3+
poolvmsize=STANDARD_DS1_V2
44
poolvmcount=1

Python/Batch/sample1_helloworld.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,50 @@
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+
Submit a simple 'hello world' task
29+
"""
30+
3231
import datetime
3332
import os
33+
from configparser import ConfigParser
3434

35-
import azure.batch._batch_service_client as batch
36-
import azure.batch.batch_auth as batchauth
35+
from azure.batch import BatchServiceClient
36+
from azure.batch.batch_auth import SharedKeyCredentials
3737
import azure.batch.models as batchmodels
3838

3939
import common.helpers
4040

4141

42-
def submit_job_and_add_task(batch_client, job_id, vm_size, vm_count):
42+
def submit_job_and_add_task(
43+
batch_client: BatchServiceClient,
44+
job_id: str,
45+
vm_size: str,
46+
node_count: int
47+
):
4348
"""Submits a job to the Azure Batch service and adds a simple task.
4449
4550
:param batch_client: The batch client to use.
46-
:type batch_client: `batchserviceclient.BatchServiceClient`
47-
:param str job_id: The id of the job to create.
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.
4854
"""
4955

50-
cloud_service_config = batchmodels.CloudServiceConfiguration(
51-
os_family='5')
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+
)
5264
pool_info = batchmodels.PoolInformation(
5365
auto_pool_specification=batchmodels.AutoPoolSpecification(
5466
auto_pool_id_prefix="HelloWorld",
5567
pool=batchmodels.PoolSpecification(
5668
vm_size=vm_size,
57-
target_dedicated_nodes=vm_count,
58-
cloud_service_configuration=cloud_service_config),
69+
target_dedicated_nodes=node_count,
70+
virtual_machine_configuration=vm_config),
5971
keep_alive=False,
6072
pool_lifetime_option=batchmodels.PoolLifetimeOption.job))
6173

@@ -66,19 +78,17 @@ def submit_job_and_add_task(batch_client, job_id, vm_size, vm_count):
6678
task = batchmodels.TaskAddParameter(
6779
id="HelloWorld",
6880
command_line=common.helpers.wrap_commands_in_shell(
69-
'windows', ['echo Hello world from the Batch Hello world sample!'])
81+
'linux', ['echo Hello world from the Batch Hello world sample!'])
7082
)
7183

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

7486

75-
def execute_sample(global_config, sample_config):
87+
def execute_sample(global_config: ConfigParser, sample_config: ConfigParser):
7688
"""Executes the sample with the specified configurations.
7789
7890
:param global_config: The global configuration to use.
79-
:type global_config: `configparser.ConfigParser`
8091
:param sample_config: The sample specific configuration to use.
81-
:type sample_config: `configparser.ConfigParser`
8292
"""
8393
# Set up the configuration
8494
batch_account_key = global_config.get('Batch', 'batchaccountkey')
@@ -99,11 +109,11 @@ def execute_sample(global_config, sample_config):
99109
common.helpers.print_configuration(global_config)
100110
common.helpers.print_configuration(sample_config)
101111

102-
credentials = batchauth.SharedKeyCredentials(
112+
credentials = SharedKeyCredentials(
103113
batch_account_name,
104114
batch_account_key)
105115

106-
batch_client = batch.BatchServiceClient(
116+
batch_client = BatchServiceClient(
107117
credentials,
108118
batch_url=batch_service_url)
109119

@@ -134,11 +144,11 @@ def execute_sample(global_config, sample_config):
134144

135145

136146
if __name__ == '__main__':
137-
global_config = configparser.ConfigParser()
138-
global_config.read(common.helpers._SAMPLES_CONFIG_FILE_NAME)
147+
global_cfg = ConfigParser()
148+
global_cfg.read(common.helpers.SAMPLES_CONFIG_FILE_NAME)
139149

140-
sample_config = configparser.ConfigParser()
141-
sample_config.read(
150+
sample_cfg = ConfigParser()
151+
sample_cfg.read(
142152
os.path.splitext(os.path.basename(__file__))[0] + '.cfg')
143153

144-
execute_sample(global_config, sample_config)
154+
execute_sample(global_cfg, sample_cfg)

Python/Batch/sample2_pools_and_resourcefiles.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
shoulddeletecontainer=true
33
shoulddeletejob=true
44
shoulddeletepool=true
5-
poolvmsize=STANDARD_D1_V2
5+
poolvmsize=STANDARD_DS1_V2
66
poolvmcount=1

Python/Batch/sample2_pools_and_resourcefiles.py

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@
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+
3231
import datetime
3332
import 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
3840
import azure.batch.models as batchmodels
3941

4042
import common.helpers
@@ -44,28 +46,32 @@
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

221223
if __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)

Python/Batch/sample3_encrypted_resourcefiles.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ shoulddeletecontainer=true
33
shoulddeletejob=true
44
shoulddeletepool=true
55
shoulddeletecert=true
6-
poolvmsize=STANDARD_D1_V2
6+
poolvmsize=STANDARD_DS1_V2
77
poolvmcount=1

0 commit comments

Comments
 (0)