Skip to content

Commit 280ca2e

Browse files
committed
add sample code for Python
1 parent 2a4169f commit 280ca2e

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

articles/batch/batch-sig-images.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,71 @@ private static void CreateBatchPool(BatchClient batchClient, VirtualMachineConfi
125125
}
126126
```
127127

128+
## Create a pool from a Shared Image using Python
129+
130+
You can also create a pool from a Shared Image using Python SDK.
131+
132+
```python
133+
# Import the required modules from the
134+
# Azure Batch Client Library for Python
135+
import azure.batch._batch_service_client as batch
136+
import azure.batch.models as batchmodels
137+
from azure.common.credentials import ServicePrincipalCredentials
138+
139+
# Specify Batch account and service principal account credentials
140+
account = "{batch-account-name}"
141+
batch_url = "{batch-account-url}"
142+
ad_client_id = "{sp-client-id}"
143+
ad_tenant = "{tenant-id}"
144+
ad_secret = "{sp-secret}"
145+
146+
# Pool settings
147+
pool_id = "LinuxNodesSamplePoolPython"
148+
vm_size = "STANDARD_D2_V3"
149+
node_count = 1
150+
151+
# Initialize the Batch client with Azure AD authentication
152+
creds = ServicePrincipalCredentials(
153+
client_id=ad_client_id,
154+
secret=ad_secret,
155+
tenant=ad_tenant,
156+
resource="https://batch.core.windows.net/"
157+
)
158+
config = batch.BatchServiceClientConfiguration(creds, batch_url)
159+
client = batch.BatchServiceClient(creds, batch_url)
160+
161+
# Create the unbound pool
162+
new_pool = batchmodels.PoolAddParameter(id=pool_id, vm_size=vm_size)
163+
new_pool.target_dedicated = node_count
164+
165+
# Configure the start task for the pool
166+
start_task = batchmodels.StartTask(
167+
command_line="printenv AZ_BATCH_NODE_STARTUP_DIR"
168+
)
169+
start_task.run_elevated = True
170+
new_pool.start_task = start_task
171+
172+
# Create an ImageReference which specifies the image from
173+
# Shared Image Gallery to install on the nodes.
174+
ir = batchmodels.ImageReference(
175+
virtual_machine_image_id="/subscriptions/{sub id}/resourceGroups/{resource group name}/providers/Microsoft.Compute/galleries/{gallery name}/images/{image definition name}/versions/{version id}"
176+
)
177+
178+
# Create the VirtualMachineConfiguration, specifying
179+
# the VM image reference and the Batch node agent to
180+
# be installed on the node.
181+
vmc = batchmodels.VirtualMachineConfiguration(
182+
image_reference=ir,
183+
node_agent_sku_id="batch.node.ubuntu 18.04"
184+
)
185+
186+
# Assign the virtual machine configuration to the pool
187+
new_pool.virtual_machine_configuration = vmc
188+
189+
# Create pool in the Batch service
190+
client.pool.add(new_pool)
191+
```
192+
128193
## Create a pool from a Shared Image using the Azure portal
129194

130195
Use the following steps to create a pool from a Shared Image in the Azure portal.

0 commit comments

Comments
 (0)