Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions sdk/batch/azure-batch/samples/batch_automatic_scaling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""Snippets extracted from articles/batch/batch-automatic-scaling.md.
"""

import datetime

from azure.batch import BatchClient, models


def autoscale_create_and_enable(batch_client: BatchClient, pool_id: str) -> None:
# [START autoscale_pool_create_enable_python]
# Create a pool; specify configuration
new_pool = models.BatchPoolCreateOptions(
id=pool_id,
virtual_machine_configuration=models.VirtualMachineConfiguration(
image_reference=models.BatchVmImageReference(
publisher="Canonical",
offer="UbuntuServer",
sku="20.04-LTS",
version="latest",
),
node_agent_sku_id="batch.node.ubuntu 20.04",
),
vm_size="STANDARD_D1_v2",
target_dedicated_nodes=0,
target_low_priority_nodes=0,
)
batch_client.create_pool(pool=new_pool) # Add the pool to the service client

formula = (
"$curTime = time();\n"
"$workHours = $curTime.hour >= 8 && $curTime.hour < 18;\n"
"$isWeekday = $curTime.weekday >= 1 && $curTime.weekday <= 5;\n"
"$isWorkingWeekdayHour = $workHours && $isWeekday;\n"
"$TargetDedicated = $isWorkingWeekdayHour ? 20:10;"
)

# Enable autoscale; specify the formula
enable_options = models.BatchPoolEnableAutoScaleOptions(
auto_scale_formula=formula,
auto_scale_evaluation_interval=datetime.timedelta(minutes=10),
)
batch_client.enable_pool_auto_scale(pool_id=pool_id, enable_auto_scale_options=enable_options)
# [END autoscale_pool_create_enable_python]
114 changes: 114 additions & 0 deletions sdk/batch/azure-batch/samples/batch_docker_container_workloads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

# Snippets extracted from articles/batch/batch-docker-container-workloads.md (Python only).


from azure.batch import models


def make_pool_no_prefetch(pool_id: str):
# [START docker_pool_no_prefetch_python]
image_ref_to_use = models.BatchVmImageReference(
publisher='microsoft-dsvm',
offer='ubuntu-hpc',
sku='2204',
version='latest')

# Specify container configuration. This is required even though there are no prefetched images.
container_conf = models.BatchContainerConfiguration(type='dockerCompatible')

new_pool = models.BatchPoolCreateOptions(
id=pool_id,
virtual_machine_configuration=models.VirtualMachineConfiguration(
image_reference=image_ref_to_use,
container_configuration=container_conf,
node_agent_sku_id='batch.node.ubuntu 22.04'),
vm_size='STANDARD_D2S_V3',
target_dedicated_nodes=1)
# [END docker_pool_no_prefetch_python]
return new_pool


def make_pool_dockerhub_prefetch(pool_id: str):
# [START docker_pool_dockerhub_prefetch_python]
image_ref_to_use = models.BatchVmImageReference(
publisher='microsoft-dsvm',
offer='ubuntu-hpc',
sku='2204',
version='latest')

# Specify container configuration, fetching the official Ubuntu container image from Docker Hub.
container_conf = models.BatchContainerConfiguration(
type='dockerCompatible',
container_image_names=['ubuntu'])

new_pool = models.BatchPoolCreateOptions(
id=pool_id,
virtual_machine_configuration=models.VirtualMachineConfiguration(
image_reference=image_ref_to_use,
container_configuration=container_conf,
node_agent_sku_id='batch.node.ubuntu 22.04'),
vm_size='STANDARD_D2S_V3',
target_dedicated_nodes=1)
# [END docker_pool_dockerhub_prefetch_python]
return new_pool


def make_pool_acr_prefetch():
# [START docker_pool_acr_prefetch_python]
image_ref_to_use = models.BatchVmImageReference(
publisher='microsoft-dsvm',
offer='ubuntu-hpc',
sku='2204',
version='latest')

# Specify a container registry
subscription_id = "yyyy-yyy-yyy-yyy-yyy"
resource_group_name = "TestRG"
user_assigned_identity_name = "testUMI"
resource_id = (
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}"
f"/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{user_assigned_identity_name}"
)

container_registry = models.ContainerRegistryReference(
registry_server="myRegistry.azurecr.io",
identity_reference=models.BatchNodeIdentityReference(resource_id=resource_id))

# Create container configuration, prefetching Docker images from the container registry
container_conf = models.BatchContainerConfiguration(
type='dockerCompatible',
container_image_names=["myRegistry.azurecr.io/samples/myImage"],
container_registries=[container_registry])

new_pool = models.BatchPoolCreateOptions(
id="myPool",
virtual_machine_configuration=models.VirtualMachineConfiguration(
image_reference=image_ref_to_use,
container_configuration=container_conf,
node_agent_sku_id='batch.node.ubuntu 22.04'),
vm_size='STANDARD_D2S_V3',
target_dedicated_nodes=1)
# [END docker_pool_acr_prefetch_python]
return new_pool


def make_container_task():
# [START docker_container_task_python]
task_id = 'sampletask'
task_container_settings = models.BatchTaskContainerSettings(
image_name='myimage',
container_run_options='--rm --workdir /')
task = models.BatchTaskCreateOptions(
id=task_id,
command_line='/bin/sh -c "echo \'hello world\' > $AZ_BATCH_TASK_WORKING_DIR/output.txt"',
container_settings=task_container_settings
)
# [END docker_container_task_python]
return task
135 changes: 135 additions & 0 deletions sdk/batch/azure-batch/samples/batch_linux_nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

# Snippets extracted from articles/batch/batch-linux-nodes.md (Python only).

import datetime
import getpass

from azure.batch import BatchClient, models
from azure.core.credentials import AzureNamedKeyCredential


def create_pool_explicit_image():
# [START linux_nodes_pool_create_python]
# Specify Batch account credentials
account = "<batch-account-name>"
key = "<batch-account-key>"
account_endpoint = "<batch-account-url>"

# Pool settings
pool_id = "LinuxNodesSamplePoolPython"
vm_size = "STANDARD_D2_V3"
node_count = 1

# Initialize the Batch client
creds = AzureNamedKeyCredential(account, key)
client = BatchClient(endpoint=account_endpoint, credential=creds)

# Configure the start task for the pool
start_task = models.BatchStartTask(
command_line="printenv AZ_BATCH_NODE_STARTUP_DIR",
user_identity=models.UserIdentity(
auto_user=models.AutoUserSpecification(
elevation_level=models.ElevationLevel.ADMIN,
scope=models.AutoUserScope.POOL,
)
),
)

# Create an ImageReference which specifies the Marketplace
# virtual machine image to install on the nodes
ir = models.BatchVmImageReference(
publisher="canonical",
offer="0001-com-ubuntu-server-focal",
sku="20_04-lts",
version="latest")

# Create the VirtualMachineConfiguration, specifying
# the VM image reference and the Batch node agent
# to install on the node
vmc = models.VirtualMachineConfiguration(
image_reference=ir,
node_agent_sku_id="batch.node.ubuntu 20.04")

# Create the unbound pool
new_pool = models.BatchPoolCreateOptions(
id=pool_id,
vm_size=vm_size,
target_dedicated_nodes=node_count,
virtual_machine_configuration=vmc,
start_task=start_task,
)

# Create pool in the Batch service
client.create_pool(pool=new_pool)
# [END linux_nodes_pool_create_python]
return client


def vm_config_from_supported_images(client: BatchClient):
# [START linux_nodes_image_reference_python]
# Get the list of supported images from the Batch service
images = list(client.list_supported_images())

# Obtain the desired image reference
image = None
for img in images:
if (img.image_reference.publisher and img.image_reference.publisher.lower() == "canonical" and
img.image_reference.offer and img.image_reference.offer.lower() == "0001-com-ubuntu-server-focal" and
img.image_reference.sku and img.image_reference.sku.lower() == "20_04-lts"):
image = img
break

if image is None:
raise RuntimeError('invalid image reference for desired configuration')

# Create the VirtualMachineConfiguration, specifying the VM image
# reference and the Batch node agent to be installed on the node
vmc = models.VirtualMachineConfiguration(
image_reference=image.image_reference,
node_agent_sku_id=image.node_agent_sku_id)
# [END linux_nodes_image_reference_python]
return vmc


def ssh_create_user_demo_inputs():
# [START linux_nodes_ssh_user_python]
# Specify your own account credentials
batch_account_name = ''
batch_account_key = ''
batch_account_url = ''

# Specify the ID of an existing pool containing Linux nodes
# currently in the 'idle' state
pool_id = ''

# Specify the username and prompt for a password
username = 'linuxuser'
password = getpass.getpass()

# Create a BatchClient
creds = AzureNamedKeyCredential(batch_account_name, batch_account_key)
batch_client = BatchClient(endpoint=batch_account_url, credential=creds)

# Create the user that will be added to each node in the pool
expiry = datetime.datetime.utcnow() + datetime.timedelta(days=30)
user = models.BatchNodeUserCreateOptions(
name=username,
password=password,
is_admin=True,
expiry_time=expiry,
)

# Get the list of nodes in the pool and add the user to each
nodes = batch_client.list_nodes(pool_id=pool_id)
for node in nodes:
batch_client.create_node_user(pool_id=pool_id, node_id=node.id, user=user)
login_settings = batch_client.get_node_remote_login_settings(pool_id=pool_id, node_id=node.id)
print(f"{node.id}: ssh {username}@{login_settings.remote_login_ip_address} -p {login_settings.remote_login_port}")
# [END linux_nodes_ssh_user_python]
69 changes: 69 additions & 0 deletions sdk/batch/azure-batch/samples/batch_sig_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

# Snippets extracted from articles/batch/batch-sig-images.md.

from azure.batch import BatchClient, models
from azure.identity import DefaultAzureCredential


def create_sig_pool():
# [START sig_create_pool_python]
# Specify Batch account credentials
account_endpoint = "https://{batch-account-name}.{region}.batch.azure.com"

# Pool settings
pool_id = "LinuxNodesSamplePoolPython"
vm_size = "STANDARD_D2_V3"
node_count = 1

# Initialize the Batch client with Microsoft Entra ID authentication
client = BatchClient(endpoint=account_endpoint, credential=DefaultAzureCredential())

# Configure the start task for the pool
start_task = models.BatchStartTask(
command_line="printenv AZ_BATCH_NODE_STARTUP_DIR",
user_identity=models.UserIdentity(
auto_user=models.AutoUserSpecification(
elevation_level=models.ElevationLevel.ADMIN,
scope=models.AutoUserScope.POOL,
)
),
)

# Create an image reference that points to an Azure Compute Gallery image.
ir = models.BatchVmImageReference(
virtual_machine_image_id=(
"/subscriptions/{sub id}/resourceGroups/{resource group name}"
"/providers/Microsoft.Compute/galleries/{gallery name}"
"/images/{image definition name}/versions/{version id}"
)
)

# Create the VirtualMachineConfiguration
vmc = models.VirtualMachineConfiguration(
image_reference=ir,
node_agent_sku_id="batch.node.ubuntu 22.04",
)

# Create the unbound pool
new_pool = models.BatchPoolCreateOptions(
id=pool_id,
vm_size=vm_size,
target_dedicated_nodes=node_count,
virtual_machine_configuration=vmc,
start_task=start_task,
)

# Create pool in the Batch service
client.create_pool(pool=new_pool)
# [END sig_create_pool_python]


if __name__ == "__main__":
create_sig_pool()
Loading