4

I'm using the "new" azure sdk for python: https://github.com/Azure/azure-sdk-for-python

Linked is a usage example to serve as documentation: https://azure-sdk-for-python.readthedocs.org/en/latest/resourcemanagementcomputenetwork.html

In this example, they create an instance from a public image, providing an image publisher, offer, SKU and version. I'd like to create an instance from a custom image (present in "My Images" on the azure portal), for which I only have an image name, no publisher or SKU.

Is this supported? How should I proceed?

Note: I'd like to avoid using the azure CLI command if possible, only relying on the python library.

Thanks!

Gyscos
  • 1,772
  • 17
  • 22

2 Answers2

1

In case anyone else runs into this issue, the SourceImage is actually for the older method (ASM). For ARM, the following will initialize the StorageProfile to provide a reference to a custom image:

storage_profile = azure.mgmt.compute.StorageProfile(
    os_disk=azure.mgmt.compute.OSDisk(
        caching=azure.mgmt.compute.CachingTypes.none,
        create_option=azure.mgmt.compute.DiskCreateOptionTypes.from_image,
        name=OS_DISK_NAME,
        virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
            uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.
            format(STORAGE_NAME, OS_DISK_NAME),
        ),
        operating_system_type='Linux',
        source_image=azure.mgmt.compute.VirtualHardDisk(
            uri='https://{0}.blob.core.windows.net/{1}/{2}'.format(
                STORAGE_NAME, CUSTOM_IMAGE_PATH, CUSTOM_IMAGE_VHD),
        ),
    ),
)

The two very important things above are 'operating_system_type' and the way the source_image is created.

Jeremy
  • 26
  • 1
0

Azure SDK for Python support create a VM with a custom image.

If a custom image has been present in "My Images" on the Azure Portal, you can create VM with the arguments OS_DISK_NAME&STORAGE_NAME of the VHD image on the storage by using Azure Python SDK.

The Azure Python SDK APIs wrap the same function REST APIs. Refer to the REST API doc "Create or update a virtual machine" https://msdn.microsoft.com/en-us/library/azure/mt163591.aspx, to create a VM with image only need the osDisk storage profile element (see the snapshot as below).

enter image description here

So the sample code only modified in partial (remove the image_referencepart) as below:

# 4. Create the virtual machine

result = compute_client.virtual_machines.create_or_update(
    GROUP_NAME,
    azure.mgmt.compute.VirtualMachine(
        location=REGION,
        name=VM_NAME,
        os_profile=azure.mgmt.compute.OSProfile(
            admin_username=ADMIN_USERNAME,
            admin_password=ADMIN_PASSWORD,
            computer_name=COMPUTER_NAME,
        ),
        hardware_profile=azure.mgmt.compute.HardwareProfile(
          virtual_machine_size=azure.mgmt.compute.VirtualMachineSizeTypes.standard_a0
        ),
        network_profile=azure.mgmt.compute.NetworkProfile(
            network_interfaces=[
                azure.mgmt.compute.NetworkInterfaceReference(
                    reference_uri=nic_id,
                ),
            ],
        ),
        storage_profile=azure.mgmt.compute.StorageProfile(
            os_disk=azure.mgmt.compute.OSDisk(
                caching=azure.mgmt.compute.CachingTypes.none,
                create_option=azure.mgmt.compute.DiskCreateOptionTypes.from_image,
                name=OS_DISK_NAME, // Your VHD name
                virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
                    uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.format(
                        STORAGE_NAME, // your storage account name
                        OS_DISK_NAME, // Your VHD name
                    ),
                ),
            )
        ),
    ),
)
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Thank you! So this is basically the query from the example, but with the `ImageReference` part removed? But then, how do I specify the image? Should I clone the image myself into a new VHD disk, and then use this existing disk for the VM creation (in the example query, the disk didn't exist before the call)? Will `DiskCreateOptionTypes.from_image` work if I don't give an `ImageReference` or a `Plan`? – Gyscos Oct 22 '15 at 20:19
  • I just found the `source_image` attribute in `storage_profile`; it's in the python sdk API documentation but not in the web API one... Investigating that. – Gyscos Oct 22 '15 at 23:19
  • @Gyscos The `source_image` attribute for REST API is to `Create Virtual Machine Deployment` for Azure Service Management. You can refer to https://msdn.microsoft.com/en-us/library/azure/jj157194.aspx. There is a C# example at the bottom of the page as a reference. The attribute `SourceImage` in the example provide the name of the platform image that you want to use to provision the Virtual Machine. You can use List OS Images to obtain the names of available platform images in the image repository. – Peter Pan Oct 23 '15 at 02:44
  • I found a test script in the azure sdk: https://github.com/Azure/azure-sdk-for-python/blob/master/azure-mgmt/tests/test_mgmt_compute.py One of the functions (`test_vms_with_source_image`) seems to be doing what I'm looking for... I think? – Gyscos Oct 23 '15 at 18:07
  • @Gyscos It should be works. Its last code `self.compute_client.virtual_machines.create_or_update` seems to be the same as the part 4 `Create the virtual machine` of the sample code. – Peter Pan Oct 26 '15 at 01:52