【问题标题】:How to create a VM with a custom image using azure-sdk-for-python?如何使用 azure-sdk-for-python 创建具有自定义映像的 VM?
【发布时间】:2016-01-21 04:10:15
【问题描述】:

我正在为 python 使用“新”的 azure sdk: https://github.com/Azure/azure-sdk-for-python

Linked 是一个用作文档的用法示例: https://azure-sdk-for-python.readthedocs.org/en/latest/resourcemanagementcomputenetwork.html

在此示例中,他们从公共图像创建实例,提供图像发布者、报价、SKU 和版本。 我想从自定义图像(出现在 azure 门户的“我的图像”中)创建一个实例,我只有一个图像名称,没有发布者或 SKU。

支持吗?我应该如何进行?

注意:如果可能,我想避免使用 azure CLI 命令,只依赖 python 库。

谢谢!

【问题讨论】:

    标签: python azure


    【解决方案1】:

    万一其他人遇到此问题,SourceImage 实际上适用于较旧的方法 (ASM)。对于 ARM,以下将初始化 StorageProfile 以提供对自定义映像的引用:

    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),
            ),
        ),
    )
    

    上面两个非常重要的东西是'operating_system_type'和source_image的创建方式。

    【讨论】:

      【解决方案2】:

      Azure SDK for Python 支持使用自定义映像创建 VM。

      如果 Azure 门户的“我的图像”中已存在自定义图像,则可以使用 Azure Python SDK 使用存储上 VHD 图像的参数 OS_DISK_NAME&STORAGE_NAME 创建 VM。

      Azure Python SDK API 封装了相同的函数 REST API。请参阅 REST API 文档“创建或更新虚拟机”https://msdn.microsoft.com/en-us/library/azure/mt163591.aspx,创建带有映像的 VM 只需要 osDisk 存储配置文件元素(参见下面的快照)。

      因此示例代码仅部分修改(删除image_referencepart)如下:

      # 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
                          ),
                      ),
                  )
              ),
          ),
      )
      

      【讨论】:

      • 谢谢!所以这基本上是示例中的查询,但删除了ImageReference 部分?但是,我如何指定图像?我是否应该自己将映像克隆到新的 VHD 磁盘中,然后使用此现有磁盘创建 VM(在示例查询中,调用之前该磁盘不存在)?如果我不提供ImageReferencePlanDiskCreateOptionTypes.from_image 会起作用吗?
      • 我刚刚在storage_profile中找到了source_image属性;它在 python sdk API 文档中,但不在 web API 中……正在调查。
      • @Gyscos REST API 的 source_image 属性对应于 Azure 服务管理的 Create Virtual Machine Deployment。可以参考msdn.microsoft.com/en-us/library/azure/jj157194.aspx。页面底部有一个 C# 示例作为参考。示例中的属性SourceImage 提供了您要用于供应虚拟机的平台映像的名称。您可以使用 List OS Images 获取镜像存储库中可用平台镜像的名称。
      • 我在 azure sdk 中找到了一个测试脚本:github.com/Azure/azure-sdk-for-python/blob/master/azure-mgmt/… 其中一个函数 (test_vms_with_source_image) 似乎正在做我正在寻找的事情......我想?
      • @Gyscos 应该可行。它的最后一个代码self.compute_client.virtual_machines.create_or_update 似乎与示例代码的第4 部分Create the virtual machine 相同。
      猜你喜欢
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多