Class: OpenNebula::VirtualMachine
- Inherits:
-
PoolElement
- Object
- XMLElement
- PoolElement
- OpenNebula::VirtualMachine
- Defined in:
- lib/opennebula/virtual_machine.rb
Constant Summary collapse
- VM_METHODS =
Constants and Class Methods
{ :info => "vm.info", :allocate => "vm.allocate", :action => "vm.action", :migrate => "vm.migrate", :deploy => "vm.deploy", :savedisk => "vm.savedisk", :chown => "vm.chown", :chmod => "vm.chmod", :monitoring => "vm.monitoring", :attach => "vm.attach", :detach => "vm.detach", :rename => "vm.rename", :update => "vm.update", :resize => "vm.resize", :snapshotcreate => "vm.snapshotcreate", :snapshotrevert => "vm.snapshotrevert", :snapshotdelete => "vm.snapshotdelete", :attachnic => "vm.attachnic", :detachnic => "vm.detachnic", :resize => "vm.resize", :recover => "vm.recover" }
- VM_STATE =
%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED POWEROFF UNDEPLOYED}
- LCM_STATE =
%w{LCM_INIT PROLOG BOOT RUNNING MIGRATE SAVE_STOP SAVE_SUSPEND SAVE_MIGRATE PROLOG_MIGRATE PROLOG_RESUME EPILOG_STOP EPILOG SHUTDOWN CANCEL FAILURE CLEANUP_RESUBMIT UNKNOWN HOTPLUG SHUTDOWN_POWEROFF BOOT_UNKNOWN BOOT_POWEROFF BOOT_SUSPENDED BOOT_STOPPED CLEANUP_DELETE HOTPLUG_SNAPSHOT HOTPLUG_NIC HOTPLUG_SAVEAS HOTPLUG_SAVEAS_POWEROFF HOTPLUG_SAVEAS_SUSPENDED SHUTDOWN_UNDEPLOY EPILOG_UNDEPLOY PROLOG_UNDEPLOY BOOT_UNDEPLOY}
- SHORT_VM_STATES =
{ "INIT" => "init", "PENDING" => "pend", "HOLD" => "hold", "ACTIVE" => "actv", "STOPPED" => "stop", "SUSPENDED" => "susp", "DONE" => "done", "FAILED" => "fail", "POWEROFF" => "poff", "UNDEPLOYED"=> "unde" }
- SHORT_LCM_STATES =
{ "PROLOG" => "prol", "BOOT" => "boot", "RUNNING" => "runn", "MIGRATE" => "migr", "SAVE_STOP" => "save", "SAVE_SUSPEND" => "save", "SAVE_MIGRATE" => "save", "PROLOG_MIGRATE" => "migr", "PROLOG_RESUME" => "prol", "EPILOG_STOP" => "epil", "EPILOG" => "epil", "SHUTDOWN" => "shut", "CANCEL" => "shut", "FAILURE" => "fail", "CLEANUP_RESUBMIT" => "clea", "UNKNOWN" => "unkn", "HOTPLUG" => "hotp", "SHUTDOWN_POWEROFF" => "shut", "BOOT_UNKNOWN" => "boot", "BOOT_POWEROFF" => "boot", "BOOT_SUSPENDED" => "boot", "BOOT_STOPPED" => "boot", "CLEANUP_DELETE" => "clea", "HOTPLUG_SNAPSHOT" => "snap", "HOTPLUG_NIC" => "hotp", "HOTPLUG_SAVEAS" => "hotp", "HOTPLUG_SAVEAS_POWEROFF" => "hotp", "HOTPLUG_SAVEAS_SUSPENDED" => "hotp", "SHUTDOWN_UNDEPLOY" => "shut", "EPILOG_UNDEPLOY" => "epil", "PROLOG_UNDEPLOY" => "prol", "BOOT_UNDEPLOY" => "boot" }
- MIGRATE_REASON =
%w{NONE ERROR USER}
- SHORT_MIGRATE_REASON =
{ "NONE" => "none", "ERROR" => "erro", "USER" => "user" }
- HISTORY_ACTION =
%w{none migrate live-migrate shutdown shutdown-hard undeploy undeploy-hard hold release stop suspend resume boot delete delete-recreate reboot reboot-hard resched unresched poweroff poweroff-hard}
Class Method Summary collapse
-
.build_xml(pe_id = nil) ⇒ Object
Creates a VirtualMachine description with just its identifier this method should be used to create plain VirtualMachine objects.
- .get_history_action(action) ⇒ Object
- .get_reason(reason) ⇒ Object
Instance Method Summary collapse
-
#allocate(description, hold = false) ⇒ nil, OpenNebula::Error
Allocates a new VirtualMachine in OpenNebula.
-
#boot ⇒ Object
(also: #restart)
Forces a re-deployment of a VM in UNKNOWN or BOOT state.
-
#cancel ⇒ Object
deprecated
Deprecated.
use #shutdown
-
#chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) ⇒ nil, OpenNebula::Error
Changes the permissions.
-
#chmod_octet(octet) ⇒ nil, OpenNebula::Error
Changes the permissions.
-
#chown(uid, gid) ⇒ Object
- Changes the owner/group uid
-
Integer the new owner id.
-
#delete(recreate = false) ⇒ Object
Deletes a VM from the pool.
-
#deploy(host_id, enforce = false, ds_id = -1)) ⇒ nil, OpenNebula::Error
Initiates the instance of the VM on the target host.
-
#disk_attach(disk_template) ⇒ nil, OpenNebula::Error
(also: #attachdisk)
Attaches a disk to a running VM.
-
#disk_detach(disk_id) ⇒ nil, OpenNebula::Error
(also: #detachdisk)
Detaches a disk from a running VM.
-
#disk_snapshot(disk_id, image_name, image_type = "", hot = false) ⇒ Integer, OpenNebula::Error
Set the specified vm’s disk to be saved in a new image when the VirtualMachine shutdowns.
-
#finalize(recreate = false) ⇒ Object
deprecated
Deprecated.
use #delete instead
-
#gid ⇒ Object
Returns the group identifier [return] Integer the element’s group ID.
-
#hold ⇒ Object
Sets a VM to hold state, scheduler will not deploy it.
-
#info ⇒ Object
(also: #info!)
Retrieves the information of the given VirtualMachine.
-
#initialize(xml, client) ⇒ VirtualMachine
constructor
Class constructor.
-
#lcm_state ⇒ Object
Returns the LCM state of the VirtualMachine (numeric value).
-
#lcm_state_str ⇒ Object
Returns the LCM state of the VirtualMachine (string value).
-
#live_migrate(host_id, enforce = false) ⇒ Object
deprecated
Deprecated.
use #migrate instead
-
#migrate(host_id, live = false, enforce = false) ⇒ nil, OpenNebula::Error
Moves a running VM to the specified host.
-
#monitoring(xpath_expressions) ⇒ Hash<String, Array<Array<int>>>, OpenNebula::Error
Retrieves this VM’s monitoring data from OpenNebula.
-
#monitoring_xml ⇒ String
Retrieves this VM’s monitoring data from OpenNebula, in XML.
-
#nic_attach(nic_template) ⇒ nil, OpenNebula::Error
Attaches a NIC to a running VM.
-
#nic_detach(nic_id) ⇒ nil, OpenNebula::Error
Detaches a NIC from a running VM.
-
#poweroff(hard = false) ⇒ Object
Powers off a running VM.
-
#reboot(hard = false) ⇒ Object
Reboots an already deployed VM.
-
#recover(result) ⇒ nil, OpenNebula::Error
Recovers an ACTIVE VM.
-
#release ⇒ Object
Releases a VM from hold state.
-
#rename(name) ⇒ nil, OpenNebula::Error
Renames this VM.
-
#resched ⇒ Object
Sets the re-scheduling flag for the VM.
-
#reset ⇒ Object
deprecated
Deprecated.
use #reboot
-
#resize(capacity_template, enforce) ⇒ nil, OpenNebula::Error
Resize the VM.
-
#resubmit ⇒ Object
deprecated
Deprecated.
use #delete instead
-
#resume ⇒ Object
Resumes the execution of a saved VM.
-
#save_as(disk_id, image_name, image_type = "", hot = false) ⇒ Object
deprecated
Deprecated.
use #disk_snapshot
-
#shutdown(hard = false) ⇒ Object
Shutdowns an already deployed VM.
-
#snapshot_create(name = "") ⇒ Integer, OpenNebula::Error
Creates a new VM snapshot.
-
#snapshot_delete(snap_id) ⇒ nil, OpenNebula::Error
Deletes a VM snapshot.
-
#snapshot_revert(snap_id) ⇒ nil, OpenNebula::Error
Reverts to a snapshot.
-
#state ⇒ Object
Returns the VM state of the VirtualMachine (numeric value).
-
#state_str ⇒ Object
Returns the VM state of the VirtualMachine (string value).
-
#status ⇒ Object
Returns the short status string for the VirtualMachine.
-
#stop ⇒ Object
Stops a running VM.
-
#suspend ⇒ Object
Saves a running VM.
-
#undeploy(hard = false) ⇒ Object
Shuts down an already deployed VM, saving its state in the system DS.
-
#unresched ⇒ Object
Unsets the re-scheduling flag for the VM.
-
#update(new_template = nil, append = false) ⇒ nil, OpenNebula::Error
Replaces the template contents.
-
#user_template_str(indent = true) ⇒ String
Returns the <USER_TEMPLATE> element in text form.
-
#user_template_xml ⇒ String
Returns the <USER_TEMPLATE> element in XML form.
Methods inherited from PoolElement
#id, #name, new_with_id, #to_str
Methods inherited from XMLElement
#[], #add_element, #attr, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml
Constructor Details
#initialize(xml, client) ⇒ VirtualMachine
Class constructor
151 152 153 |
# File 'lib/opennebula/virtual_machine.rb', line 151 def initialize(xml, client) super(xml,client) end |
Class Method Details
.build_xml(pe_id = nil) ⇒ Object
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)
129 130 131 132 133 134 135 136 137 |
# File 'lib/opennebula/virtual_machine.rb', line 129 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 |
.get_history_action(action) ⇒ Object
146 147 148 |
# File 'lib/opennebula/virtual_machine.rb', line 146 def VirtualMachine.get_history_action(action) return HISTORY_ACTION[action.to_i] end |
.get_reason(reason) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/opennebula/virtual_machine.rb', line 139 def VirtualMachine.get_reason(reason) reason=MIGRATE_REASON[reason.to_i] reason_str=SHORT_MIGRATE_REASON[reason] reason_str end |
Instance Method Details
#allocate(description, hold = false) ⇒ nil, OpenNebula::Error
Allocates a new VirtualMachine in OpenNebula
175 176 177 |
# File 'lib/opennebula/virtual_machine.rb', line 175 def allocate(description, hold=false) super(VM_METHODS[:allocate], description, hold) end |
#boot ⇒ Object Also known as: restart
Forces a re-deployment of a VM in UNKNOWN or BOOT state
340 341 342 |
# File 'lib/opennebula/virtual_machine.rb', line 340 def boot action('boot') end |
#cancel ⇒ Object
use #shutdown
256 257 258 |
# File 'lib/opennebula/virtual_machine.rb', line 256 def cancel shutdown(true) end |
#chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) ⇒ nil, OpenNebula::Error
Changes the permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
452 453 454 455 456 |
# File 'lib/opennebula/virtual_machine.rb', line 452 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 |
#chmod_octet(octet) ⇒ nil, OpenNebula::Error
Changes the permissions.
443 444 445 |
# File 'lib/opennebula/virtual_machine.rb', line 443 def chmod_octet(octet) super(VM_METHODS[:chmod], octet) end |
#chown(uid, gid) ⇒ Object
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
434 435 436 |
# File 'lib/opennebula/virtual_machine.rb', line 434 def chown(uid, gid) super(VM_METHODS[:chown], uid, gid) end |
#delete(recreate = false) ⇒ Object
Deletes a VM from the pool
326 327 328 329 330 331 332 |
# File 'lib/opennebula/virtual_machine.rb', line 326 def delete(recreate=false) if recreate action('delete-recreate') else action('delete') end end |
#deploy(host_id, enforce = false, ds_id = -1)) ⇒ nil, OpenNebula::Error
Initiates the instance of the VM on the target host.
224 225 226 227 228 |
# File 'lib/opennebula/virtual_machine.rb', line 224 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 |
#disk_attach(disk_template) ⇒ nil, OpenNebula::Error Also known as: attachdisk
Attaches a disk to a running VM
290 291 292 |
# File 'lib/opennebula/virtual_machine.rb', line 290 def disk_attach(disk_template) return call(VM_METHODS[:attach], @pe_id, disk_template) end |
#disk_detach(disk_id) ⇒ nil, OpenNebula::Error Also known as: detachdisk
Detaches a disk from a running VM
301 302 303 |
# File 'lib/opennebula/virtual_machine.rb', line 301 def disk_detach(disk_id) return call(VM_METHODS[:detach], @pe_id, disk_id) end |
#disk_snapshot(disk_id, image_name, image_type = "", hot = false) ⇒ Integer, OpenNebula::Error
Set the specified vm’s disk to be saved in a new image when the VirtualMachine shutdowns
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/opennebula/virtual_machine.rb', line 397 def disk_snapshot(disk_id, image_name, image_type="", hot=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) return rc end |
#finalize(recreate = false) ⇒ Object
use #delete instead
335 336 337 |
# File 'lib/opennebula/virtual_machine.rb', line 335 def finalize(recreate=false) delete(recreate) end |
#gid ⇒ Object
Returns the group identifier
- return
-
Integer the element’s group ID
583 584 585 |
# File 'lib/opennebula/virtual_machine.rb', line 583 def gid self['GID'].to_i end |
#hold ⇒ Object
Sets a VM to hold state, scheduler will not deploy it
261 262 263 |
# File 'lib/opennebula/virtual_machine.rb', line 261 def hold action('hold') end |
#info ⇒ Object Also known as: info!
Retrieves the information of the given VirtualMachine.
160 161 162 |
# File 'lib/opennebula/virtual_machine.rb', line 160 def info() super(VM_METHODS[:info], 'VM') end |
#lcm_state ⇒ Object
Returns the LCM state of the VirtualMachine (numeric value)
561 562 563 |
# File 'lib/opennebula/virtual_machine.rb', line 561 def lcm_state self['LCM_STATE'].to_i end |
#lcm_state_str ⇒ Object
Returns the LCM state of the VirtualMachine (string value)
566 567 568 |
# File 'lib/opennebula/virtual_machine.rb', line 566 def lcm_state_str LCM_STATE[lcm_state] end |
#live_migrate(host_id, enforce = false) ⇒ Object
use #migrate instead
380 381 382 |
# File 'lib/opennebula/virtual_machine.rb', line 380 def live_migrate(host_id, enforce=false) migrate(host_id, true, enforce) end |
#migrate(host_id, live = false, enforce = false) ⇒ nil, OpenNebula::Error
Moves a running VM to the specified host. With live=true the migration is done withdout downtime.
374 375 376 377 |
# File 'lib/opennebula/virtual_machine.rb', line 374 def migrate(host_id, live=false, enforce=false) call(VM_METHODS[:migrate], @pe_id, host_id.to_i, live==true, enforce) end |
#monitoring(xpath_expressions) ⇒ Hash<String, Array<Array<int>>>, OpenNebula::Error
Retrieves this VM’s monitoring data from OpenNebula
479 480 481 482 |
# File 'lib/opennebula/virtual_machine.rb', line 479 def monitoring(xpath_expressions) return super(VM_METHODS[:monitoring], 'VM', 'LAST_POLL', xpath_expressions) end |
#monitoring_xml ⇒ String
Retrieves this VM’s monitoring data from OpenNebula, in XML
487 488 489 490 491 |
# File 'lib/opennebula/virtual_machine.rb', line 487 def monitoring_xml() return Error.new('ID not defined') if !@pe_id return @client.call(VM_METHODS[:monitoring], @pe_id) end |
#nic_attach(nic_template) ⇒ nil, OpenNebula::Error
Attaches a NIC to a running VM
312 313 314 |
# File 'lib/opennebula/virtual_machine.rb', line 312 def nic_attach(nic_template) return call(VM_METHODS[:attachnic], @pe_id, nic_template) end |
#nic_detach(nic_id) ⇒ nil, OpenNebula::Error
Detaches a NIC from a running VM
321 322 323 |
# File 'lib/opennebula/virtual_machine.rb', line 321 def nic_detach(nic_id) return call(VM_METHODS[:detachnic], @pe_id, nic_id) end |
#poweroff(hard = false) ⇒ Object
Powers off a running VM
241 242 243 |
# File 'lib/opennebula/virtual_machine.rb', line 241 def poweroff(hard=false) action(hard ? 'poweroff-hard' : 'poweroff') end |
#reboot(hard = false) ⇒ Object
Reboots an already deployed VM
246 247 248 |
# File 'lib/opennebula/virtual_machine.rb', line 246 def reboot(hard=false) action(hard ? 'reboot-hard' : 'reboot') end |
#recover(result) ⇒ nil, OpenNebula::Error
Recovers an ACTIVE VM
542 543 544 |
# File 'lib/opennebula/virtual_machine.rb', line 542 def recover(result) return call(VM_METHODS[:recover], @pe_id, result) end |
#release ⇒ Object
Releases a VM from hold state
266 267 268 |
# File 'lib/opennebula/virtual_machine.rb', line 266 def release action('release') end |
#rename(name) ⇒ nil, OpenNebula::Error
Renames this VM
499 500 501 |
# File 'lib/opennebula/virtual_machine.rb', line 499 def rename(name) return call(VM_METHODS[:rename], @pe_id, name) end |
#resched ⇒ Object
Sets the re-scheduling flag for the VM
352 353 354 |
# File 'lib/opennebula/virtual_machine.rb', line 352 def resched action('resched') end |
#reset ⇒ Object
use #reboot
251 252 253 |
# File 'lib/opennebula/virtual_machine.rb', line 251 def reset reboot(true) end |
#resize(capacity_template, enforce) ⇒ nil, OpenNebula::Error
Resize the VM
426 427 428 |
# File 'lib/opennebula/virtual_machine.rb', line 426 def resize(capacity_template, enforce) return call(VM_METHODS[:resize], @pe_id, capacity_template, enforce) end |
#resubmit ⇒ Object
use #delete instead
347 348 349 |
# File 'lib/opennebula/virtual_machine.rb', line 347 def resubmit action('delete-recreate') end |
#resume ⇒ Object
Resumes the execution of a saved VM
281 282 283 |
# File 'lib/opennebula/virtual_machine.rb', line 281 def resume action('resume') end |
#save_as(disk_id, image_name, image_type = "", hot = false) ⇒ Object
use #disk_snapshot
411 412 413 |
# File 'lib/opennebula/virtual_machine.rb', line 411 def save_as(disk_id, image_name, image_type="", hot=false) return disk_snapshot(disk_id, image_name, image_type, hot) end |
#shutdown(hard = false) ⇒ Object
Shutdowns an already deployed VM
231 232 233 |
# File 'lib/opennebula/virtual_machine.rb', line 231 def shutdown(hard=false) action(hard ? 'shutdown-hard' : 'shutdown') end |
#snapshot_create(name = "") ⇒ Integer, OpenNebula::Error
Creates a new VM snapshot
509 510 511 512 513 514 |
# File 'lib/opennebula/virtual_machine.rb', line 509 def snapshot_create(name="") return Error.new('ID not defined') if !@pe_id name ||= "" return @client.call(VM_METHODS[:snapshotcreate], @pe_id, name) end |
#snapshot_delete(snap_id) ⇒ nil, OpenNebula::Error
Deletes a VM snapshot
532 533 534 |
# File 'lib/opennebula/virtual_machine.rb', line 532 def snapshot_delete(snap_id) return call(VM_METHODS[:snapshotdelete], @pe_id, snap_id) end |
#snapshot_revert(snap_id) ⇒ nil, OpenNebula::Error
Reverts to a snapshot
522 523 524 |
# File 'lib/opennebula/virtual_machine.rb', line 522 def snapshot_revert(snap_id) return call(VM_METHODS[:snapshotrevert], @pe_id, snap_id) end |
#state ⇒ Object
Returns the VM state of the VirtualMachine (numeric value)
551 552 553 |
# File 'lib/opennebula/virtual_machine.rb', line 551 def state self['STATE'].to_i end |
#state_str ⇒ Object
Returns the VM state of the VirtualMachine (string value)
556 557 558 |
# File 'lib/opennebula/virtual_machine.rb', line 556 def state_str VM_STATE[state] end |
#status ⇒ Object
Returns the short status string for the VirtualMachine
571 572 573 574 575 576 577 578 579 |
# File 'lib/opennebula/virtual_machine.rb', line 571 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 |
#stop ⇒ Object
Stops a running VM
271 272 273 |
# File 'lib/opennebula/virtual_machine.rb', line 271 def stop action('stop') end |
#suspend ⇒ Object
Saves a running VM
276 277 278 |
# File 'lib/opennebula/virtual_machine.rb', line 276 def suspend action('suspend') end |
#undeploy(hard = false) ⇒ Object
Shuts down an already deployed VM, saving its state in the system DS
236 237 238 |
# File 'lib/opennebula/virtual_machine.rb', line 236 def undeploy(hard=false) action(hard ? 'undeploy-hard' : 'undeploy') end |
#unresched ⇒ Object
Unsets the re-scheduling flag for the VM
357 358 359 |
# File 'lib/opennebula/virtual_machine.rb', line 357 def unresched action('unresched') end |
#update(new_template = nil, append = false) ⇒ nil, OpenNebula::Error
Replaces the template contents
187 188 189 |
# File 'lib/opennebula/virtual_machine.rb', line 187 def update(new_template=nil, append=false) super(VM_METHODS[:update], new_template, append ? 1 : 0) end |
#user_template_str(indent = true) ⇒ String
Returns the <USER_TEMPLATE> element in text form
196 197 198 |
# File 'lib/opennebula/virtual_machine.rb', line 196 def user_template_str(indent=true) template_like_str('USER_TEMPLATE', indent) end |
#user_template_xml ⇒ String
Returns the <USER_TEMPLATE> element in XML form
203 204 205 206 207 208 209 |
# File 'lib/opennebula/virtual_machine.rb', line 203 def user_template_xml if NOKOGIRI @xml.xpath('TEMPLATE').to_s else @xml.elements['TEMPLATE'].to_s end end |