|
| 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] |
0 commit comments