简体   繁体   English

通过捕获的图像以编程方式创建Azure VM

[英]Programatically creating an Azure VM from a captured image

Using Azure Powershell the above is relatively straight-forward: 使用Azure Powershell,以上内容相对简单:

$image = "captured-vm-image-xx" 
New-AzureService -ServiceName $servicename -Location $region
$config = New-AzureVMConfig -Name $servicename -InstanceSize ExtraSmall -AvailabilitySetName $avsetname -ImageName $image
New-AzureVM -ServiceName $servicename -VMs $config -VNetName $vnetname -WaitForBoot 

Now I'm trying to port the code above to C# using the Azure 2.4 SDK: 现在,我尝试使用Azure 2.4 SDK将上面的代码移植到C#:

var vmname = ComputeClusterMasterName(clusterName);

var p1 = new VirtualMachineCreateDeploymentParameters
{
    Name = vmname, 
    Roles = new List<Role>
    {  
        new Role() 
        {
            RoleType = "PersistentVMRole",
            RoleName = vmname,
            RoleSize = "ExtraSmall",
            ConfigurationSets = new List<ConfigurationSet>(),
        },
    },
    DeploymentSlot = DeploymentSlot.Production,
    Label = vmname
};

await client.VirtualMachines.CreateDeploymentAsync(clusterName, p1);

var parms = new VirtualMachineCreateParameters();
parms.AvailabilitySetName = clusterName;
parms.RoleName = vmname;
parms.RoleSize = "ExtraSmall";
parms.VMImageName = "captured-vm-image-xx";
await client.VirtualMachines.CreateAsync(cloudServiceName, vmname, parms);

When I run the code above CreateDeploymentAsync fails with: 当我运行上面的代码时,CreateDeploymentAsync失败并显示:

BadRequest: Property 'OSDisk' for entity type 'PersistentVMRole' with name 'c1cmdb' is null or empty. BadRequest:名称为“ c1cmdb”的实体类型“ PersistentVMRole”的属性“ OSDisk”为空或为空。

Yes, I've omitted that property but why do I have to specify it in the first place?? 是的,我省略了该属性,但是为什么我必须首先指定它?

It looks like your CreateDeploymentAsync() call is attempting to create a new VM (aka Role) without specifying some of the required fields. 看起来您的CreateDeploymentAsync()调用正在尝试创建新的VM(也称为Role),而未指定某些必填字段。 When a deployment is created, it also spins up its first virtual machine(s). 创建部署后,它还会启动其第一个虚拟机。

You have to specify the missing information in your original role because there isn't enough information in your request to create a virtual machine. 您必须在原始角色中指定缺少的信息,因为您的请求中没有足够的信息来创建虚拟机。 If you compare the interfaces of Role and VirtualMachineCreateParameters , you should notice a lot of parallels- they share more or less the same responsibility. 如果比较RoleVirtualMachineCreateParameters的接口,您会注意到很多相似之处-它们或多或少地承担着相同的责任。 Have you tried specifying your actual VM from a VmImage in the Role in your initial call instead? 您是否尝试过在初始调用中从Role中的VmImage指定您的实际VM? You might have more luck that way. 这样您可能会有更多的运气。 :) :)

Just for anyone struggling with this problem. 只适合那些在这个问题上苦苦挣扎的人。 Turns out Greg answer hinted the right approach that's why I've marked as the correct answer. 事实证明,格雷格(Greg)的答案暗示了正确的方法,这就是为什么我将其标记为正确的答案。

When creating a VM from an captured image, all you have to do is to create a deployment using that image: 从捕获的映像创建VM时,您要做的就是使用该映像创建部署:

var args = new VirtualMachineCreateDeploymentParameters
{
    Name = vmname,
    Roles = new List<Role>
    {  
        new Role
        {
            RoleType = "PersistentVMRole",
            RoleName = vmname,
            RoleSize = "ExtraSmall",
            ConfigurationSets = new List<ConfigurationSet>(),
            VMImageName = "captured-vm-image-xx",
            ProvisionGuestAgent = true,
        },
    },
    DeploymentSlot = DeploymentSlot.Production,
    Label = vmname,
};

await client.VirtualMachines.CreateDeploymentAsync(clusterName, args);

There's no need to call CreateAsync because that method has a totally different purpose: It's for adding another virtual machine to an existing deployment. 无需调用CreateAsync,因为该方法的用途完全不同:它用于向现有部署中添加另一个虚拟机。 The naming is simply misleading. 命名只是误导。

Working with the Azure SDK for .Net so far has been an excerise in frustration due to the lack of documentation and samples. 由于缺少文档和示例,到目前为止,使用适用于.Net的Azure SDK令人沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM