Class: Azure::Armrest::VirtualMachineManager
- Inherits:
-
ArmrestManager
- Object
- ArmrestManager
- Azure::Armrest::VirtualMachineManager
- Defined in:
- lib/azure/armrest/virtual_machine_manager.rb
Overview
Base class for managing virtual machines
Direct Known Subclasses
Constant Summary
Constants inherited from ArmrestManager
Instance Attribute Summary collapse
-
#provider ⇒ Object
The provider used in requests when gathering VM information.
Attributes inherited from ArmrestManager
#accept, #api_version, #base_url, #content_type, #grant_type, #resource_group, #subscription_id, #token
Instance Method Summary collapse
- #add_network_profile(vms) ⇒ Object
-
#capture(vmname, action = 'capture') ⇒ Object
Captures the
vmname
and associated disks into a reusable CSM template. -
#create(options = {}) ⇒ Object
(also: #update)
Creates a new virtual machine (or updates an existing one).
-
#deallocate(vmname, group = vmname) ⇒ Object
Stop the VM
vmname
ingroup
and deallocate the tenant in Fabric. -
#delete(vmname, group = vmname) ⇒ Object
Deletes the
vmname
ingroup
that you specify. -
#generalize(vmname, group = vmname) ⇒ Object
Sets the OSState for the
vmname
ingroup
to ‘Generalized’. -
#get(vmname, group = vmname, model_view = true) ⇒ Object
Retrieves the settings of the VM named
vmname
in resource groupgroup
, which will default to the same as the name of the VM. -
#get_instance_view(vmname, group = vmname) ⇒ Object
Convenient wrapper around the get method that retrieves the instance view for
vmname
in resource_groupgroup
. -
#get_model_view(vmname, group = vmname) ⇒ Object
Convenient wrapper around the get method that retrieves the model view for
vmname
in resource_groupgroup
. - #get_nic_profile(nic) ⇒ Object
-
#initialize(options = {}) ⇒ VirtualMachineManager
constructor
Create and return a new VirtualMachineManager (VMM) instance.
-
#list(group = nil) ⇒ Object
(also: #get_vms)
Returns a list of available virtual machines for the given subscription for the provided
group
, or all resource groups if none is provided. -
#restart(vmname, group = vmname) ⇒ Object
Restart the VM
vmname
for the givengroup
, which will default to the same as the vmname. -
#series(location) ⇒ Object
(also: #sizes)
Return a list of available VM series (aka sizes, flavors, etc), such as “Basic_A1”, though information is included as well.
-
#start(vmname, group = vmname) ⇒ Object
Start the VM
vmname
for the givengroup
, which will default to the same as the vmname. -
#stop(vmname, group = vmname) ⇒ Object
Stop the VM
vmname
for the givengroup
gracefully.
Methods inherited from ArmrestManager
configure, #get_token, #locations, #provider_info, #providers, #resource_group_info, #resource_groups, #resources, #subscription_info, #subscriptions, #tags, #tenants
Constructor Details
#initialize(options = {}) ⇒ VirtualMachineManager
Create and return a new VirtualMachineManager (VMM) instance. Most methods for a VMM instance will return one or more VirtualMachine instances.
This subclass accepts the additional :provider option as well. The default is ‘Microsoft.ClassicCompute’. You may need to set this to ‘Microsoft.Compute’ for your purposes.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 19 def initialize( = {}) super @provider = [:provider] || 'Microsoft.Compute' # Typically only empty in testing. unless @@providers.empty? @api_version = @@providers[@provider]['virtualMachines']['api_version'] end end |
Instance Attribute Details
#provider ⇒ Object
The provider used in requests when gathering VM information.
9 10 11 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 9 def provider @provider end |
Instance Method Details
#add_network_profile(vms) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 104 def add_network_profile(vms) vms.each { |vm| vm['properties']['networkProfile']['networkInterfaces'].each { |net| get_nic_profile(net) } } end |
#capture(vmname, action = 'capture') ⇒ Object
Captures the vmname
and associated disks into a reusable CSM template. – POST
138 139 140 141 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 138 def capture(vmname, action = 'capture') uri = @uri + "/#{vmname}/#{action}?api-version=#{api_version}" uri end |
#create(options = {}) ⇒ Object Also known as: update
Creates a new virtual machine (or updates an existing one). Pass a hash of options to configure the VM as you see fit. Some options are mandatory. The following are a list of possible options:
-
:name Required. The name of the virtual machine. The name must be unique within the availability set that it belongs to.
-
:location Required. The location where the VM should be created, e.g. “West US”.
-
:tags Optional. Specifies an identifier for the availability set.
-
:hardwareprofile Required. Contains a collection of hardware settings for the VM.
-
:vmsize Required. Specifies the size of the virtual machine. Possible sizes are Standard_A0..Standard_A4.
-
-
:osprofile Required. Contains a collection of settings for the OS configuration which must contain all of the following:
-
:computername
-
:adminusername
-
:adminpassword
-
:username
-
:password
-
-
:storageprofile Required. Contains a collection of settings for storage and disk settings for the VM. You must specify an :osdisk and :name. The :datadisks setting is optional.
-
:osdisk Required. Contains a collection of settings for the operating system disk.
-
:name
-
:ostype
-
:caching
-
:image
-
:vhd
-
-
:datadisks Optional. Contains a collection of settings for data disks.
-
:name
-
:image
-
:vhd
-
:lun
-
:caching
-
-
:name Required. Specifies the name of the disk.
-
For clarity, we recommend using the update method for existing VM’s.
Example:
vmm = VirtualMachineManager.new(x, y, z)
vm = vmm.create(
:name => 'test1',
:location => 'West US',
:hardwareprofile => {:vmsize => 'Standard_A0'},
:osprofile => {
:computername => 'some_name',
:adminusername => 'admin_user',
:adminpassword => 'adminxxxxxx',
:username => 'some_user',
:password => 'userpassxxxxxx',
},
:storageprofile => {
:osdisk => {
:ostype => 'Windows',
:caching => 'Read'
}
}
)
– PUT operation
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 228 def create( = {}) #name = options.fetch(:name) #location = options.fetch(:location) #tags = option[:tags] vmsize = .fetch(:vmsize) unless VALID_VM_SIZES.include?(vmsize) raise ArgumentError, "Invalid vmsize '#{vmsize}'" end end |
#deallocate(vmname, group = vmname) ⇒ Object
Stop the VM vmname
in group
and deallocate the tenant in Fabric.
243 244 245 246 247 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 243 def deallocate(vmname, group = vmname) url = build_url(group, vmname, 'deallocate') response = rest_post(url) response.return! end |
#delete(vmname, group = vmname) ⇒ Object
Deletes the vmname
in group
that you specify. Note that associated disks are not deleted.
252 253 254 255 256 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 252 def delete(vmname, group = vmname) url = build_url(group, vmname) response = rest_delete(url) response.return! end |
#generalize(vmname, group = vmname) ⇒ Object
Sets the OSState for the vmname
in group
to ‘Generalized’.
260 261 262 263 264 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 260 def generalize(vmname, group = vmname) url = build_url(group, vmname, 'generalize') response = rest_post(url) response.return! end |
#get(vmname, group = vmname, model_view = true) ⇒ Object
Retrieves the settings of the VM named vmname
in resource group group
, which will default to the same as the name of the VM.
By default this method will retrieve the model view. If the model_view
parameter is false, it will retrieve an instance view. The difference is in the details of the information retrieved.
273 274 275 276 277 278 279 280 281 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 273 def get(vmname, group = vmname, model_view = true) if model_view url = build_url(group, vmname) else url = build_url(group, vmname, 'instanceView') end JSON.parse(rest_get(url)) end |
#get_instance_view(vmname, group = vmname) ⇒ Object
Convenient wrapper around the get method that retrieves the instance view for vmname
in resource_group group
.
293 294 295 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 293 def get_instance_view(vmname, group = vmname) get(vmname, group, false) end |
#get_model_view(vmname, group = vmname) ⇒ Object
Convenient wrapper around the get method that retrieves the model view for vmname
in resource_group group
.
286 287 288 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 286 def get_model_view(vmname, group = vmname) get(vmname, group, true) end |
#get_nic_profile(nic) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 112 def get_nic_profile(nic) url = File.join( Azure::Armrest::RESOURCE, nic['id'], "?api-version=#{api_version}" ) nic['properties'] = JSON.parse(rest_get(url))['properties']['ipConfigurations'] nic['properties'].each do |n| next if n['properties']['publicIPAddress'].nil? # public IP is a URI so we need to make another rest call to get it. url = File.join( Azure::Armrest::RESOURCE, n['properties']['publicIPAddress']['id'], "?api-version=#{api_version}" ) public_ip = JSON.parse(rest_get(url))['properties']['ipAddress'] n['properties']['publicIPAddress'] = public_ip end end |
#list(group = nil) ⇒ Object Also known as: get_vms
Returns a list of available virtual machines for the given subscription for the provided group
, or all resource groups if none is provided.
Examples:
# Get VM's for all resource groups
vmm.list
# Get VM's only for a specific group
vmm.list('some_group')
– The specific hashes we can grab are: p JSON.parse(resp.body)[0][“instanceView”] p JSON.parse(resp.body)[0][“hardwareProfile”] p JSON.parse(resp.body)[0][“storageProfile”]
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 75 def list(group = nil) array = [] if group url = build_url(group) array << JSON.parse(rest_get(url))['value'] else threads = [] mutex = Mutex.new resource_groups.each do |group| url = build_url(group['name']) threads << Thread.new(url) do |thread_url| response = rest_get(thread_url) result = JSON.parse(response)['value'] mutex.synchronize{ array << result if result } end end threads.each(&:join) array = array.flatten end add_network_profile(array) end |
#restart(vmname, group = vmname) ⇒ Object
Restart the VM vmname
for the given group
, which will default to the same as the vmname.
This is an asynchronous operation that returns a response object which you can inspect, such as response.code or response.headers.
303 304 305 306 307 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 303 def restart(vmname, group = vmname) url = build_url(group, vmname, 'restart') response = rest_post(url) response.return! end |
#series(location) ⇒ Object Also known as: sizes
Return a list of available VM series (aka sizes, flavors, etc), such as “Basic_A1”, though information is included as well.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 42 def series(location) unless @@providers[@provider] && @@providers[@provider]['locations/vmSizes'] raise ArgumentError, "Invalid provider '#{provider}'" end version = @@providers[@provider]['locations/vmSizes']['api_version'] url = url_with_api_version( version, @base_url, 'subscriptions', subscription_id, 'providers', provider, 'locations', location, 'vmSizes' ) JSON.parse(rest_get(url))['value'] end |
#start(vmname, group = vmname) ⇒ Object
Start the VM vmname
for the given group
, which will default to the same as the vmname.
This is an asynchronous operation that returns a response object which you can inspect, such as response.code or response.headers.
315 316 317 318 319 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 315 def start(vmname, group = vmname) url = build_url(group, vmname, 'start') response = rest_post(url) response.return! end |
#stop(vmname, group = vmname) ⇒ Object
Stop the VM vmname
for the given group
gracefully. However, a forced shutdown will occur after 15 minutes.
This is an asynchronous operation that returns a response object which you can inspect, such as response.code or response.headers.
327 328 329 330 331 |
# File 'lib/azure/armrest/virtual_machine_manager.rb', line 327 def stop(vmname, group = vmname) url = build_url(group, vmname, 'powerOff') response = rest_post(url) response.return! end |