-
Notifications
You must be signed in to change notification settings - Fork 36
Provisioning a VM from a Snapshot
There are two ways to provision a VM. One is to attach an existing VHD. The other, and more likely approach, is to copy a VM image. Below is a step by step guide to the second approach using The azure-armrest gem.
require 'azure-armest' # This library
require 'securerandom' # In the Ruby stdlib
First, you must know where the image is that you want to copy. The StorageAccountService class may help you here for dynamic lookup.
src_uri = "https://somestorage123.blob.core.windows.net/system/Microsoft.Compute/Images/some-container/dan-img-osDisk.714a9ffb-55c8-4e31-951f-d26307b5afa4.vhd"
Second, you must provide a target location. This is a storage account URI, followed by a container name, followed by the name of the image you want to create. We follow MS practices here and add a GUID to the image name to ensure uniqueness.
We also recommend that you use a common, easily recognizable container name for storing all of your custom provisioning.
vhd_uri = "http://somestorage123.blob.core.windows.net/your_company/testprov1_" + SecureRandom.uuid + ".vhd"
One thing you must have first is a NIC that you will attach to the VM. You can either create a new one or use an existing one. For this example, we'll just use an existing one. While we're at it, we'll also need a VirtualMachineService instance.
vms = Azure::Armrest::VirtualMachineService.new(conf)
nis = Azure::Armrest::Network::NetworkInterfaceService.new(conf)
nic = nis.get('some_nic', 'your_group')
With that set, we need to set our options. For all available options, please see the online REST documentation for Azure. Here we provide enough to at least give you a general idea how things should work.
options =
{
:name => 'testprov1',
:location => 'centralus',
:properties => {
:hardwareProfile => { :vmSize => 'Standard_A0' },
:osProfile => {
:adminUserName => 'your_username',
:adminPassword => 'your_password',
:computerName => 'whatever_you_want'
},
:storageProfile => {
:osDisk => {
:createOption => 'FromImage',
:caching => 'ReadWrite',
:name => 'whatever_' + SecureRandom.uuid + '.vhd',
:osType => 'Windows',
:image => { :uri => src_uri }, # source
:vhd => { :uri => vhd_uri } # target
}
},
:networkProfile => {
:networkInterfaces => [{:id => nic.id}]
}
}
}
vm.create('testprov1', 'your_group', options)
The name and location are mandatory, as is the information under :osProfile, and the :networkProfile. Here we used the NIC that we retrieved earlier. As a best practice, the :name should match the first part of the target VHD name so that it's easy to identify which VM is associated with which VHD.
The possibilities for the :vmSize option under :hardwareProfile can be found using the VirtualMachineService#series method. Or, you can just lookup the information online to find out what is currently available from MS. Here we use 'Standard_A0', which is the smallest configuration possible.
Under the :osDisk parameter, the :createOption of "FromImage" let's Azure know that you're creating a VM from an image. If you were attaching an existing VHD to a VM, you would use "Attach" here instead (and other options would be different). The :image option is the source image, and the :vhd option is the target image that will be generated.
Note that, at the time of this writing, the :osType can only be "Windows" or "Linux". Hopefully you know the OS type in advance, but if you don't, it can be determined dynamically. See below.
blob_properties.x_ms_meta_microsoftazurecompute_ostype