Skip to content

Commit 7d83c38

Browse files
authored
Batch Samples (#46786)
* adding code snippits for microsoft learn
1 parent 0fbcbab commit 7d83c38

9 files changed

Lines changed: 779 additions & 0 deletions
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
"""Snippets extracted from articles/batch/batch-automatic-scaling.md.
9+
"""
10+
11+
import datetime
12+
13+
from azure.batch import BatchClient, models
14+
15+
16+
def autoscale_create_and_enable(batch_client: BatchClient, pool_id: str) -> None:
17+
# [START autoscale_pool_create_enable_python]
18+
# Create a pool; specify configuration
19+
new_pool = models.BatchPoolCreateOptions(
20+
id=pool_id,
21+
virtual_machine_configuration=models.VirtualMachineConfiguration(
22+
image_reference=models.BatchVmImageReference(
23+
publisher="Canonical",
24+
offer="UbuntuServer",
25+
sku="20.04-LTS",
26+
version="latest",
27+
),
28+
node_agent_sku_id="batch.node.ubuntu 20.04",
29+
),
30+
vm_size="STANDARD_D1_v2",
31+
target_dedicated_nodes=0,
32+
target_low_priority_nodes=0,
33+
)
34+
batch_client.create_pool(pool=new_pool) # Add the pool to the service client
35+
36+
formula = (
37+
"$curTime = time();\n"
38+
"$workHours = $curTime.hour >= 8 && $curTime.hour < 18;\n"
39+
"$isWeekday = $curTime.weekday >= 1 && $curTime.weekday <= 5;\n"
40+
"$isWorkingWeekdayHour = $workHours && $isWeekday;\n"
41+
"$TargetDedicated = $isWorkingWeekdayHour ? 20:10;"
42+
)
43+
44+
# Enable autoscale; specify the formula
45+
enable_options = models.BatchPoolEnableAutoScaleOptions(
46+
auto_scale_formula=formula,
47+
auto_scale_evaluation_interval=datetime.timedelta(minutes=10),
48+
)
49+
batch_client.enable_pool_auto_scale(pool_id=pool_id, enable_auto_scale_options=enable_options)
50+
# [END autoscale_pool_create_enable_python]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
# Snippets extracted from articles/batch/batch-docker-container-workloads.md (Python only).
10+
11+
12+
from azure.batch import models
13+
14+
15+
def make_pool_no_prefetch(pool_id: str):
16+
# [START docker_pool_no_prefetch_python]
17+
image_ref_to_use = models.BatchVmImageReference(
18+
publisher='microsoft-dsvm',
19+
offer='ubuntu-hpc',
20+
sku='2204',
21+
version='latest')
22+
23+
# Specify container configuration. This is required even though there are no prefetched images.
24+
container_conf = models.BatchContainerConfiguration(type='dockerCompatible')
25+
26+
new_pool = models.BatchPoolCreateOptions(
27+
id=pool_id,
28+
virtual_machine_configuration=models.VirtualMachineConfiguration(
29+
image_reference=image_ref_to_use,
30+
container_configuration=container_conf,
31+
node_agent_sku_id='batch.node.ubuntu 22.04'),
32+
vm_size='STANDARD_D2S_V3',
33+
target_dedicated_nodes=1)
34+
# [END docker_pool_no_prefetch_python]
35+
return new_pool
36+
37+
38+
def make_pool_dockerhub_prefetch(pool_id: str):
39+
# [START docker_pool_dockerhub_prefetch_python]
40+
image_ref_to_use = models.BatchVmImageReference(
41+
publisher='microsoft-dsvm',
42+
offer='ubuntu-hpc',
43+
sku='2204',
44+
version='latest')
45+
46+
# Specify container configuration, fetching the official Ubuntu container image from Docker Hub.
47+
container_conf = models.BatchContainerConfiguration(
48+
type='dockerCompatible',
49+
container_image_names=['ubuntu'])
50+
51+
new_pool = models.BatchPoolCreateOptions(
52+
id=pool_id,
53+
virtual_machine_configuration=models.VirtualMachineConfiguration(
54+
image_reference=image_ref_to_use,
55+
container_configuration=container_conf,
56+
node_agent_sku_id='batch.node.ubuntu 22.04'),
57+
vm_size='STANDARD_D2S_V3',
58+
target_dedicated_nodes=1)
59+
# [END docker_pool_dockerhub_prefetch_python]
60+
return new_pool
61+
62+
63+
def make_pool_acr_prefetch():
64+
# [START docker_pool_acr_prefetch_python]
65+
image_ref_to_use = models.BatchVmImageReference(
66+
publisher='microsoft-dsvm',
67+
offer='ubuntu-hpc',
68+
sku='2204',
69+
version='latest')
70+
71+
# Specify a container registry
72+
subscription_id = "yyyy-yyy-yyy-yyy-yyy"
73+
resource_group_name = "TestRG"
74+
user_assigned_identity_name = "testUMI"
75+
resource_id = (
76+
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}"
77+
f"/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{user_assigned_identity_name}"
78+
)
79+
80+
container_registry = models.ContainerRegistryReference(
81+
registry_server="myRegistry.azurecr.io",
82+
identity_reference=models.BatchNodeIdentityReference(resource_id=resource_id))
83+
84+
# Create container configuration, prefetching Docker images from the container registry
85+
container_conf = models.BatchContainerConfiguration(
86+
type='dockerCompatible',
87+
container_image_names=["myRegistry.azurecr.io/samples/myImage"],
88+
container_registries=[container_registry])
89+
90+
new_pool = models.BatchPoolCreateOptions(
91+
id="myPool",
92+
virtual_machine_configuration=models.VirtualMachineConfiguration(
93+
image_reference=image_ref_to_use,
94+
container_configuration=container_conf,
95+
node_agent_sku_id='batch.node.ubuntu 22.04'),
96+
vm_size='STANDARD_D2S_V3',
97+
target_dedicated_nodes=1)
98+
# [END docker_pool_acr_prefetch_python]
99+
return new_pool
100+
101+
102+
def make_container_task():
103+
# [START docker_container_task_python]
104+
task_id = 'sampletask'
105+
task_container_settings = models.BatchTaskContainerSettings(
106+
image_name='myimage',
107+
container_run_options='--rm --workdir /')
108+
task = models.BatchTaskCreateOptions(
109+
id=task_id,
110+
command_line='/bin/sh -c "echo \'hello world\' > $AZ_BATCH_TASK_WORKING_DIR/output.txt"',
111+
container_settings=task_container_settings
112+
)
113+
# [END docker_container_task_python]
114+
return task
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
# Snippets extracted from articles/batch/batch-linux-nodes.md (Python only).
10+
11+
import datetime
12+
import getpass
13+
14+
from azure.batch import BatchClient, models
15+
from azure.core.credentials import AzureNamedKeyCredential
16+
17+
18+
def create_pool_explicit_image():
19+
# [START linux_nodes_pool_create_python]
20+
# Specify Batch account credentials
21+
account = "<batch-account-name>"
22+
key = "<batch-account-key>"
23+
account_endpoint = "<batch-account-url>"
24+
25+
# Pool settings
26+
pool_id = "LinuxNodesSamplePoolPython"
27+
vm_size = "STANDARD_D2_V3"
28+
node_count = 1
29+
30+
# Initialize the Batch client
31+
creds = AzureNamedKeyCredential(account, key)
32+
client = BatchClient(endpoint=account_endpoint, credential=creds)
33+
34+
# Configure the start task for the pool
35+
start_task = models.BatchStartTask(
36+
command_line="printenv AZ_BATCH_NODE_STARTUP_DIR",
37+
user_identity=models.UserIdentity(
38+
auto_user=models.AutoUserSpecification(
39+
elevation_level=models.ElevationLevel.ADMIN,
40+
scope=models.AutoUserScope.POOL,
41+
)
42+
),
43+
)
44+
45+
# Create an ImageReference which specifies the Marketplace
46+
# virtual machine image to install on the nodes
47+
ir = models.BatchVmImageReference(
48+
publisher="canonical",
49+
offer="0001-com-ubuntu-server-focal",
50+
sku="20_04-lts",
51+
version="latest")
52+
53+
# Create the VirtualMachineConfiguration, specifying
54+
# the VM image reference and the Batch node agent
55+
# to install on the node
56+
vmc = models.VirtualMachineConfiguration(
57+
image_reference=ir,
58+
node_agent_sku_id="batch.node.ubuntu 20.04")
59+
60+
# Create the unbound pool
61+
new_pool = models.BatchPoolCreateOptions(
62+
id=pool_id,
63+
vm_size=vm_size,
64+
target_dedicated_nodes=node_count,
65+
virtual_machine_configuration=vmc,
66+
start_task=start_task,
67+
)
68+
69+
# Create pool in the Batch service
70+
client.create_pool(pool=new_pool)
71+
# [END linux_nodes_pool_create_python]
72+
return client
73+
74+
75+
def vm_config_from_supported_images(client: BatchClient):
76+
# [START linux_nodes_image_reference_python]
77+
# Get the list of supported images from the Batch service
78+
images = list(client.list_supported_images())
79+
80+
# Obtain the desired image reference
81+
image = None
82+
for img in images:
83+
if (img.image_reference.publisher and img.image_reference.publisher.lower() == "canonical" and
84+
img.image_reference.offer and img.image_reference.offer.lower() == "0001-com-ubuntu-server-focal" and
85+
img.image_reference.sku and img.image_reference.sku.lower() == "20_04-lts"):
86+
image = img
87+
break
88+
89+
if image is None:
90+
raise RuntimeError('invalid image reference for desired configuration')
91+
92+
# Create the VirtualMachineConfiguration, specifying the VM image
93+
# reference and the Batch node agent to be installed on the node
94+
vmc = models.VirtualMachineConfiguration(
95+
image_reference=image.image_reference,
96+
node_agent_sku_id=image.node_agent_sku_id)
97+
# [END linux_nodes_image_reference_python]
98+
return vmc
99+
100+
101+
def ssh_create_user_demo_inputs():
102+
# [START linux_nodes_ssh_user_python]
103+
# Specify your own account credentials
104+
batch_account_name = ''
105+
batch_account_key = ''
106+
batch_account_url = ''
107+
108+
# Specify the ID of an existing pool containing Linux nodes
109+
# currently in the 'idle' state
110+
pool_id = ''
111+
112+
# Specify the username and prompt for a password
113+
username = 'linuxuser'
114+
password = getpass.getpass()
115+
116+
# Create a BatchClient
117+
creds = AzureNamedKeyCredential(batch_account_name, batch_account_key)
118+
batch_client = BatchClient(endpoint=batch_account_url, credential=creds)
119+
120+
# Create the user that will be added to each node in the pool
121+
expiry = datetime.datetime.utcnow() + datetime.timedelta(days=30)
122+
user = models.BatchNodeUserCreateOptions(
123+
name=username,
124+
password=password,
125+
is_admin=True,
126+
expiry_time=expiry,
127+
)
128+
129+
# Get the list of nodes in the pool and add the user to each
130+
nodes = batch_client.list_nodes(pool_id=pool_id)
131+
for node in nodes:
132+
batch_client.create_node_user(pool_id=pool_id, node_id=node.id, user=user)
133+
login_settings = batch_client.get_node_remote_login_settings(pool_id=pool_id, node_id=node.id)
134+
print(f"{node.id}: ssh {username}@{login_settings.remote_login_ip_address} -p {login_settings.remote_login_port}")
135+
# [END linux_nodes_ssh_user_python]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
# Snippets extracted from articles/batch/batch-sig-images.md.
10+
11+
from azure.batch import BatchClient, models
12+
from azure.identity import DefaultAzureCredential
13+
14+
15+
def create_sig_pool():
16+
# [START sig_create_pool_python]
17+
# Specify Batch account credentials
18+
account_endpoint = "https://{batch-account-name}.{region}.batch.azure.com"
19+
20+
# Pool settings
21+
pool_id = "LinuxNodesSamplePoolPython"
22+
vm_size = "STANDARD_D2_V3"
23+
node_count = 1
24+
25+
# Initialize the Batch client with Microsoft Entra ID authentication
26+
client = BatchClient(endpoint=account_endpoint, credential=DefaultAzureCredential())
27+
28+
# Configure the start task for the pool
29+
start_task = models.BatchStartTask(
30+
command_line="printenv AZ_BATCH_NODE_STARTUP_DIR",
31+
user_identity=models.UserIdentity(
32+
auto_user=models.AutoUserSpecification(
33+
elevation_level=models.ElevationLevel.ADMIN,
34+
scope=models.AutoUserScope.POOL,
35+
)
36+
),
37+
)
38+
39+
# Create an image reference that points to an Azure Compute Gallery image.
40+
ir = models.BatchVmImageReference(
41+
virtual_machine_image_id=(
42+
"/subscriptions/{sub id}/resourceGroups/{resource group name}"
43+
"/providers/Microsoft.Compute/galleries/{gallery name}"
44+
"/images/{image definition name}/versions/{version id}"
45+
)
46+
)
47+
48+
# Create the VirtualMachineConfiguration
49+
vmc = models.VirtualMachineConfiguration(
50+
image_reference=ir,
51+
node_agent_sku_id="batch.node.ubuntu 22.04",
52+
)
53+
54+
# Create the unbound pool
55+
new_pool = models.BatchPoolCreateOptions(
56+
id=pool_id,
57+
vm_size=vm_size,
58+
target_dedicated_nodes=node_count,
59+
virtual_machine_configuration=vmc,
60+
start_task=start_task,
61+
)
62+
63+
# Create pool in the Batch service
64+
client.create_pool(pool=new_pool)
65+
# [END sig_create_pool_python]
66+
67+
68+
if __name__ == "__main__":
69+
create_sig_pool()

0 commit comments

Comments
 (0)