class OpenNebula::VirtualMachine
Constants
- HISTORY_ACTION
- LCM_STATE
- MIGRATE_REASON
- SHORT_LCM_STATES
- SHORT_MIGRATE_REASON
- SHORT_VM_STATES
- VM_METHODS
Constants and Class Methods
- VM_STATE
Public Class Methods
Creates a VirtualMachine description with
just its identifier this method should be used to create plain VirtualMachine objects. id
the
id of the vm
Example:
vnet = VirtualMachine.new(VirtualMachine.build_xml(3),rpc_client)
# File lib/opennebula/virtual_machine.rb, line 128 def VirtualMachine.build_xml(pe_id=nil) if pe_id vm_xml = "<VM><ID>#{pe_id}</ID></VM>" else vm_xml = "<VM></VM>" end XMLElement.build_xml(vm_xml, 'VM') end
# File lib/opennebula/virtual_machine.rb, line 145 def VirtualMachine.get_history_action(action) return HISTORY_ACTION[action.to_i] end
# File lib/opennebula/virtual_machine.rb, line 138 def VirtualMachine.get_reason(reason) reason=MIGRATE_REASON[reason.to_i] reason_str=SHORT_MIGRATE_REASON[reason] reason_str end
Class constructor
# File lib/opennebula/virtual_machine.rb, line 150 def initialize(xml, client) super(xml,client) end
Public Instance Methods
Allocates a new VirtualMachine in OpenNebula
@param description [String] A string containing the template of
the VirtualMachine.
@param hold [true,false] false to create the VM in pending state,
true to create it on hold
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 174 def allocate(description, hold=false) super(VM_METHODS[:allocate], description, hold) end
Forces a re-deployment of a VM in UNKNOWN or BOOT state
# File lib/opennebula/virtual_machine.rb, line 343 def boot action('boot') end
@deprecated use {#shutdown}
# File lib/opennebula/virtual_machine.rb, line 259 def cancel shutdown(true) end
Changes the permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 458 def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) super(VM_METHODS[:chmod], owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) end
Changes the permissions.
@param octet [String] Permissions octed , e.g. 640 @return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 449 def chmod_octet(octet) super(VM_METHODS[:chmod], octet) end
Changes the owner/group
- uid
-
Integer the new owner id. Set to -1 to leave the current one
- gid
-
Integer the new group id. Set to -1 to leave the current one
- return
-
nil in case of success or an Error object
# File lib/opennebula/virtual_machine.rb, line 440 def chown(uid, gid) super(VM_METHODS[:chown], uid, gid) end
Deletes a VM from the pool
# File lib/opennebula/virtual_machine.rb, line 329 def delete(recreate=false) if recreate action('delete-recreate') else action('delete') end end
Initiates the instance of the VM on the target host.
@param host_id [Interger] The host id (hid) of the target host where
the VM will be instantiated.
@param enforce [true|false] If it is set to true, the host capacity
will be checked, and the deployment will fail if the host is overcommited. Defaults to false
@param ds_id [Integer] The System Datastore where to deploy the VM. To
use the default, set it to -1
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 223 def deploy(host_id, enforce=false, ds_id=-1) enforce ||= false ds_id ||= -1 return call(VM_METHODS[:deploy], @pe_id, host_id.to_i, enforce, ds_id.to_i) end
Returns the #deploy_id of the VirtualMachine (numeric value)
# File lib/opennebula/virtual_machine.rb, line 594 def deploy_id self['DEPLOY_ID'] end
Attaches a disk to a running VM
@param disk_template [String] Template containing a DISK element @return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 293 def disk_attach(disk_template) return call(VM_METHODS[:attach], @pe_id, disk_template) end
Detaches a disk from a running VM
@param disk_id [Integer] Id of the disk to be detached @return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 304 def disk_detach(disk_id) return call(VM_METHODS[:detach], @pe_id, disk_id) end
Set the specified vm's disk to be saved in a new image when the VirtualMachine shutdowns
@param disk_id [Integer] ID of the disk to be saved @param image_name [String] Name for the new image where the
disk will be saved
@param image_type [String] Type of the new image. Set to empty string
to use the default type
@param hot [true|false] True to save the disk immediately, false will
perform the operation when the VM shuts down
@param do_template [true|false] True to clone also the VM originating template and replace the disk with the saved image
@return [Integer, OpenNebula::Error] the new Image ID in case of
success, error otherwise
# File lib/opennebula/virtual_machine.rb, line 402 def disk_snapshot(disk_id, image_name, image_type="", hot=false, do_template=false) return Error.new('ID not defined') if !@pe_id rc = @client.call(VM_METHODS[:savedisk], @pe_id, disk_id, image_name, image_type, hot, do_template) return rc end
@deprecated use {#delete} instead
# File lib/opennebula/virtual_machine.rb, line 338 def finalize(recreate=false) delete(recreate) end
Returns the group identifier
- return
-
Integer the element's group ID
# File lib/opennebula/virtual_machine.rb, line 589 def gid self['GID'].to_i end
Sets a VM to hold state, scheduler will not deploy it
# File lib/opennebula/virtual_machine.rb, line 264 def hold action('hold') end
Retrieves the information of the given VirtualMachine.
# File lib/opennebula/virtual_machine.rb, line 159 def info() super(VM_METHODS[:info], 'VM') end
Returns the LCM state of the VirtualMachine (numeric value)
# File lib/opennebula/virtual_machine.rb, line 567 def lcm_state self['LCM_STATE'].to_i end
Returns the LCM state of the VirtualMachine (string value)
# File lib/opennebula/virtual_machine.rb, line 572 def lcm_state_str LCM_STATE[lcm_state] end
@deprecated use {#migrate} instead
# File lib/opennebula/virtual_machine.rb, line 383 def live_migrate(host_id, enforce=false) migrate(host_id, true, enforce) end
Moves a running VM to the specified host. With live=true the migration is done withdout downtime.
@param host_id [Interger] The host id (hid) of the target host where
the VM will be migrated.
@param live [true|false] If true the migration is done without
downtime. Defaults to false
@param enforce [true|false] If it is set to true, the host capacity
will be checked, and the deployment will fail if the host is overcommited. Defaults to false
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 377 def migrate(host_id, live=false, enforce=false) call(VM_METHODS[:migrate], @pe_id, host_id.to_i, live==true, enforce) end
Retrieves this VM's monitoring data from OpenNebula
@param [Array<String>] xpath_expressions Elements to retrieve.
@return [Hash<String, Array<Array<int>>>, OpenNebula::Error] Hash with
the requested xpath expressions, and an Array of 'timestamp, value'.
@example
vm.monitoring( ['CPU', 'NET_TX', 'TEMPLATE/CUSTOM_PROBE'] ) { "NET_TX" => [["1337264510", "210"], ["1337264553", "220"], ["1337264584", "230"]], "TEMPLATE/CUSTOM_PROBE" => [], "CPU" => [["1337264510", "0"], ["1337264553", "0"], ["1337264584", "0"]] }
# File lib/opennebula/virtual_machine.rb, line 485 def monitoring(xpath_expressions) return super(VM_METHODS[:monitoring], 'VM', 'LAST_POLL', xpath_expressions) end
Retrieves this VM's monitoring data from OpenNebula, in XML
@return [String] VM monitoring data, in XML
# File lib/opennebula/virtual_machine.rb, line 493 def monitoring_xml() return Error.new('ID not defined') if !@pe_id return @client.call(VM_METHODS[:monitoring], @pe_id) end
Attaches a NIC to a running VM
@param nic_template [String] Template containing a NIC element @return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 315 def nic_attach(nic_template) return call(VM_METHODS[:attachnic], @pe_id, nic_template) end
Detaches a NIC from a running VM
@param nic_id [Integer] Id of the NIC to be detached @return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 324 def nic_detach(nic_id) return call(VM_METHODS[:detachnic], @pe_id, nic_id) end
Powers off a running VM
# File lib/opennebula/virtual_machine.rb, line 244 def poweroff(hard=false) action(hard ? 'poweroff-hard' : 'poweroff') end
Reboots an already deployed VM
# File lib/opennebula/virtual_machine.rb, line 249 def reboot(hard=false) action(hard ? 'reboot-hard' : 'reboot') end
Recovers an ACTIVE VM
@param result [Boolean] Recover with success (true) or failure (false) @param result [info] Additional information needed to recover the VM @return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 548 def recover(result) return call(VM_METHODS[:recover], @pe_id, result) end
Releases a VM from hold state
# File lib/opennebula/virtual_machine.rb, line 269 def release action('release') end
Renames this VM
@param name [String] New name for the VM.
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 505 def rename(name) return call(VM_METHODS[:rename], @pe_id, name) end
Sets the re-scheduling flag for the VM
# File lib/opennebula/virtual_machine.rb, line 355 def resched action('resched') end
@deprecated use {#reboot}
# File lib/opennebula/virtual_machine.rb, line 254 def reset reboot(true) end
Resize the VM
@param capacity_template [String] Template containing the new capacity
elements CPU, VCPU, MEMORY. If one of them is not present, or its value is 0, it will not be resized
@param enforce [true|false] If it is set to true, the host capacity
will be checked. This will only affect oneadmin requests, regular users resize requests will always be enforced
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 432 def resize(capacity_template, enforce) return call(VM_METHODS[:resize], @pe_id, capacity_template, enforce) end
@deprecated use {#delete} instead
# File lib/opennebula/virtual_machine.rb, line 350 def resubmit action('delete-recreate') end
Resumes the execution of a saved VM
# File lib/opennebula/virtual_machine.rb, line 284 def resume action('resume') end
@deprecated use {#disk_snapshot}
# File lib/opennebula/virtual_machine.rb, line 417 def save_as(disk_id, image_name, image_type="", hot=false) return disk_snapshot(disk_id, image_name, image_type, hot) end
Shutdowns an already deployed VM
# File lib/opennebula/virtual_machine.rb, line 234 def shutdown(hard=false) action(hard ? 'shutdown-hard' : 'shutdown') end
Creates a new VM snapshot
@param name [String] Name for the snapshot.
@return [Integer, OpenNebula::Error] The new snaphost ID in case
of success, Error otherwise
# File lib/opennebula/virtual_machine.rb, line 515 def snapshot_create(name="") return Error.new('ID not defined') if !@pe_id name ||= "" return @client.call(VM_METHODS[:snapshotcreate], @pe_id, name) end
Deletes a VM snapshot
@param snap_id [Integer] Id of the snapshot
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 538 def snapshot_delete(snap_id) return call(VM_METHODS[:snapshotdelete], @pe_id, snap_id) end
Reverts to a snapshot
@param snap_id [Integer] Id of the snapshot
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 528 def snapshot_revert(snap_id) return call(VM_METHODS[:snapshotrevert], @pe_id, snap_id) end
Returns the VM state of the VirtualMachine (numeric value)
# File lib/opennebula/virtual_machine.rb, line 557 def state self['STATE'].to_i end
Returns the VM state of the VirtualMachine (string value)
# File lib/opennebula/virtual_machine.rb, line 562 def state_str VM_STATE[state] end
Returns the short status string for the VirtualMachine
# File lib/opennebula/virtual_machine.rb, line 577 def status short_state_str=SHORT_VM_STATES[state_str] if short_state_str=="actv" short_state_str=SHORT_LCM_STATES[lcm_state_str] end short_state_str end
Stops a running VM
# File lib/opennebula/virtual_machine.rb, line 274 def stop action('stop') end
Saves a running VM
# File lib/opennebula/virtual_machine.rb, line 279 def suspend action('suspend') end
Shuts down an already deployed VM, saving its state in the system DS
# File lib/opennebula/virtual_machine.rb, line 239 def undeploy(hard=false) action(hard ? 'undeploy-hard' : 'undeploy') end
Unsets the re-scheduling flag for the VM
# File lib/opennebula/virtual_machine.rb, line 360 def unresched action('unresched') end
Replaces the template contents
@param new_template [String] New template contents @param append [true, false] True to append new attributes instead of
replace the whole template
@return [nil, OpenNebula::Error] nil in case of success, Error
otherwise
# File lib/opennebula/virtual_machine.rb, line 186 def update(new_template=nil, append=false) super(VM_METHODS[:update], new_template, append ? 1 : 0) end
Returns the <USER_TEMPLATE> element in text form
@param indent [true,false] indents the resulting string, defaults to true
@return [String] The USER_TEMPLATE
# File lib/opennebula/virtual_machine.rb, line 195 def user_template_str(indent=true) template_like_str('USER_TEMPLATE', indent) end
Returns the <USER_TEMPLATE> element in XML form
@return [String] The USER_TEMPLATE
# File lib/opennebula/virtual_machine.rb, line 202 def user_template_xml if NOKOGIRI @xml.xpath('USER_TEMPLATE').to_s else @xml.elements['USER_TEMPLATE'].to_s end end
Private Instance Methods
# File lib/opennebula/virtual_machine.rb, line 599 def action(name) return Error.new('ID not defined') if !@pe_id rc = @client.call(VM_METHODS[:action], name, @pe_id) rc = nil if !OpenNebula.is_error?(rc) return rc end