Skip to content

Provisioning a VM from a Snapshot

Daniel Berger edited this page Dec 18, 2015 · 13 revisions

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://miqazuredan12006.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.

vhd_uri = "http://miqazuredan12006.blob.core.windows.net/your_company/testprov1_" + SecureRandom.uuid + ".vhd"

# We also recommend that you use a common, easily recognizable container name for storing all
# of your custom provisioning.

# 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.

# One thing you will need first is a NIC. You can either create a new one or
# use an existing one. For this example, we'll just use an existing one:

vms = Azure::Armrest::VirtualMachineService.new(conf)
nis = Azure::Armrest::Network::NetworkInterfaceService.new(conf)

nic = nis.get('some_nic', 'your_group')

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       => blob_properties.x_ms_meta_microsoftazurecompute_ostype,
        :image        => { :uri => src_uri }, # source
        :vhd          => { :uri => vhd_uri }  # target
      }
    },
    :networkProfile => {
      :networkInterfaces => [{:id => nic.id}]
    }
  }
}

vm.create('testprov1', 'your_group', options)