Class: OpenNebula::VirtualMachine

Inherits:
PoolElement show all
Defined in:
lib/opennebula/virtual_machine.rb

Defined Under Namespace

Modules: Driver

Constant Summary collapse

VM_METHODS =

Constants and Class Methods

{
    :info           => "vm.info",
    :allocate       => "vm.allocate",
    :action         => "vm.action",
    :migrate        => "vm.migrate",
    :deploy         => "vm.deploy",
    :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",
    :recover        => "vm.recover",
    :disksaveas     => "vm.disksaveas",
    :disksnapshotcreate => "vm.disksnapshotcreate",
    :disksnapshotrevert => "vm.disksnapshotrevert",
    :disksnapshotdelete => "vm.disksnapshotdelete",
    :disksnapshotrename => "vm.disksnapshotrename",
    :diskresize     => "vm.diskresize",
    :updateconf     => "vm.updateconf",
    :lock     => "vm.lock",
    :unlock     => "vm.unlock"
}
VM_STATE =
%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED
POWEROFF UNDEPLOYED CLONING CLONING_FAILURE}
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
    HOTPLUG_PROLOG_POWEROFF
    HOTPLUG_EPILOG_POWEROFF
    BOOT_MIGRATE
    BOOT_FAILURE
    BOOT_MIGRATE_FAILURE
    PROLOG_MIGRATE_FAILURE
    PROLOG_FAILURE
    EPILOG_FAILURE
    EPILOG_STOP_FAILURE
    EPILOG_UNDEPLOY_FAILURE
    PROLOG_MIGRATE_POWEROFF
    PROLOG_MIGRATE_POWEROFF_FAILURE
    PROLOG_MIGRATE_SUSPEND
    PROLOG_MIGRATE_SUSPEND_FAILURE
    BOOT_UNDEPLOY_FAILURE
    BOOT_STOPPED_FAILURE
    PROLOG_RESUME_FAILURE
    PROLOG_UNDEPLOY_FAILURE
    DISK_SNAPSHOT_POWEROFF
    DISK_SNAPSHOT_REVERT_POWEROFF
    DISK_SNAPSHOT_DELETE_POWEROFF
    DISK_SNAPSHOT_SUSPENDED
    DISK_SNAPSHOT_REVERT_SUSPENDED
    DISK_SNAPSHOT_DELETE_SUSPENDED
    DISK_SNAPSHOT
    DISK_SNAPSHOT_REVERT
    DISK_SNAPSHOT_DELETE
    PROLOG_MIGRATE_UNKNOWN
    PROLOG_MIGRATE_UNKNOWN_FAILURE
    DISK_RESIZE
    DISK_RESIZE_POWEROFF
    DISK_RESIZE_UNDEPLOYED
}
SHORT_VM_STATES =
{
    "INIT"              => "init",
    "PENDING"           => "pend",
    "HOLD"              => "hold",
    "ACTIVE"            => "actv",
    "STOPPED"           => "stop",
    "SUSPENDED"         => "susp",
    "DONE"              => "done",
    "FAILED"            => "fail",
    "POWEROFF"          => "poff",
    "UNDEPLOYED"        => "unde",
    "CLONING"           => "clon",
    "CLONING_FAILURE"   => "fail"
}
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",
    "HOTPLUG_PROLOG_POWEROFF"   => "hotp",
    "HOTPLUG_EPILOG_POWEROFF"   => "hotp",
    "BOOT_MIGRATE"              => "boot",
    "BOOT_FAILURE"              => "fail",
    "BOOT_MIGRATE_FAILURE"      => "fail",
    "PROLOG_MIGRATE_FAILURE"    => "fail",
    "PROLOG_FAILURE"            => "fail",
    "EPILOG_FAILURE"            => "fail",
    "EPILOG_STOP_FAILURE"       => "fail",
    "EPILOG_UNDEPLOY_FAILURE"   => "fail",
    "PROLOG_MIGRATE_POWEROFF"   => "migr",
    "PROLOG_MIGRATE_POWEROFF_FAILURE"   => "fail",
    "PROLOG_MIGRATE_SUSPEND"            => "migr",
    "PROLOG_MIGRATE_SUSPEND_FAILURE"    => "fail",
    "BOOT_UNDEPLOY_FAILURE"     => "fail",
    "BOOT_STOPPED_FAILURE"      => "fail",
    "PROLOG_RESUME_FAILURE"     => "fail",
    "PROLOG_UNDEPLOY_FAILURE"   => "fail",
    "DISK_SNAPSHOT_POWEROFF"        => "snap",
    "DISK_SNAPSHOT_REVERT_POWEROFF" => "snap",
    "DISK_SNAPSHOT_DELETE_POWEROFF" => "snap",
    "DISK_SNAPSHOT_SUSPENDED"       => "snap",
    "DISK_SNAPSHOT_REVERT_SUSPENDED"=> "snap",
    "DISK_SNAPSHOT_DELETE_SUSPENDED"=> "snap",
    "DISK_SNAPSHOT"        => "snap",
    "DISK_SNAPSHOT_DELETE" => "snap",
    "PROLOG_MIGRATE_UNKNOWN" => "migr",
    "PROLOG_MIGRATE_UNKNOWN_FAILURE" => "fail",
    "DISK_RESIZE"            => "drsz",
    "DISK_RESIZE_POWEROFF"   => "drsz",
    "DISK_RESIZE_UNDEPLOYED" => "drsz"
}
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 disk-attach disk-detach nic-attach nic-detach
disk-snapshot-create disk-snapshot-delete terminate terminate-hard
disk-resize deploy chown chmod updateconf rename resize update
snapshot-resize snapshot-delete snapshot-revert disk-saveas
disk-snapshot-revert recover retry monitor disk-snapshot-rename
alias-attach alias-detach poweroff-migrate poweroff-hard-migrate
}
EXTERNAL_IP_ATTRS =
[
    'GUEST_IP',
    'AWS_IP_ADDRESS',
    'AWS_PUBLIC_IP_ADDRESS',
    'AWS_PRIVATE_IP_ADDRESS',
    'AZ_IPADDRESS',
    'SL_PRIMARYIPADDRESS'
]
REMOVE_VNET_ATTRS =

Clones the VM’s source Template, replacing the disks with live snapshots of the current disks. The VM capacity and NICs are also preserved

persistent, false make them non-persistent

Returns:

  • (Integer, OpenNebula::Error)

    the new Template ID in case of success, error otherwise

%w{AR_ID BRIDGE CLUSTER_ID IP MAC TARGET NIC_ID
NETWORK_ID VN_MAD SECURITY_GROUPS VLAN_ID}
REMOVE_IMAGE_ATTRS =
%w{DEV_PREFIX SOURCE ORIGINAL_SIZE SIZE
DISK_SNAPSHOT_TOTAL_SIZE DRIVER IMAGE_STATE SAVE CLONE READONLY
PERSISTENT TARGET ALLOW_ORPHANS CLONE_TARGET CLUSTER_ID DATASTORE
DATASTORE_ID DISK_ID DISK_TYPE IMAGE_ID IMAGE IMAGE_UNAME IMAGE_UID
LN_TARGET TM_MAD TYPE OPENNEBULA_MANAGED}

Class Method Summary collapse

Instance Method Summary collapse

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, #retrieve_xmlelements, #set_content, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

#initialize(xml, client) ⇒ VirtualMachine

Class constructor



271
272
273
# File 'lib/opennebula/virtual_machine.rb', line 271

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)


256
257
258
259
260
261
262
263
264
# File 'lib/opennebula/virtual_machine.rb', line 256

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



266
267
268
# File 'lib/opennebula/virtual_machine.rb', line 266

def VirtualMachine.get_history_action(action)
    return HISTORY_ACTION[action.to_i]
end

Instance Method Details

#allocate(description, hold = false) ⇒ nil, OpenNebula::Error

Allocates a new VirtualMachine in OpenNebula

Parameters:

  • description (String)

    A string containing the template of the VirtualMachine.

  • hold (true, false) (defaults to: false)

    false to create the VM in pending state, true to create it on hold

Returns:



295
296
297
# File 'lib/opennebula/virtual_machine.rb', line 295

def allocate(description, hold=false)
    super(VM_METHODS[:allocate], description, hold)
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

Returns:



549
550
551
552
553
# File 'lib/opennebula/virtual_machine.rb', line 549

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.

Parameters:

  • octet (String)

    Permissions octed , e.g. 640

Returns:



540
541
542
# File 'lib/opennebula/virtual_machine.rb', line 540

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



531
532
533
# File 'lib/opennebula/virtual_machine.rb', line 531

def chown(uid, gid)
    super(VM_METHODS[:chown], uid, gid)
end

#delete(recreate = false) ⇒ Object

Deletes a VM from the pool



695
696
697
698
699
700
701
# File 'lib/opennebula/virtual_machine.rb', line 695

def delete(recreate=false)
    if recreate
        recover(4)
    else
        recover(3)
    end
end

#deploy(host_id, enforce = false, ds_id = -1,, extra_template = "") ⇒ nil, OpenNebula::Error

Initiates the instance of the VM on the target host.

Parameters:

  • host_id (Interger)

    The host id (hid) of the target host where the VM will be instantiated.

  • enforce (true|false) (defaults to: 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

  • ds_id (Integer) (defaults to: -1,)

    The System Datastore where to deploy the VM. To use the default, set it to -1

Returns:



347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/opennebula/virtual_machine.rb', line 347

def deploy(host_id, enforce=false, ds_id=-1, extra_template="")
    enforce ||= false
    ds_id ||= -1
    extra_template ||= ""

    self.info

    return call(VM_METHODS[:deploy],
                @pe_id,
                host_id.to_i,
                enforce,
                ds_id.to_i,
                extra_template)
end

#deploy_idObject

Returns the deploy_id of the VirtualMachine (numeric value)



769
770
771
# File 'lib/opennebula/virtual_machine.rb', line 769

def deploy_id
    self['DEPLOY_ID']
end

#disk_attach(disk_template) ⇒ nil, OpenNebula::Error Also known as: attachdisk

Attaches a disk to a running VM

Parameters:

  • disk_template (String)

    Template containing a DISK element

Returns:



414
415
416
# File 'lib/opennebula/virtual_machine.rb', line 414

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

Parameters:

  • disk_id (Integer)

    Id of the disk to be detached

Returns:



425
426
427
# File 'lib/opennebula/virtual_machine.rb', line 425

def disk_detach(disk_id)
    return call(VM_METHODS[:detach], @pe_id, disk_id)
end

#disk_resize(disk_id, size) ⇒ nil, OpenNebula::Error

Changes the size of a disk

Parameters:

  • disk_id (Integer)

    Id of the disk

  • size (Integer)

    new size in MiB

Returns:



679
680
681
# File 'lib/opennebula/virtual_machine.rb', line 679

def disk_resize(disk_id, size)
    return call(VM_METHODS[:diskresize], @pe_id, disk_id, size.to_s)
end

#disk_saveas(disk_id, image_name, image_type = "", snap_id = -1)) ⇒ Integer, OpenNebula::Error

Set the specified vm’s disk to be saved as a new image

current disk image state

Parameters:

  • disk_id (Integer)

    ID of the disk to be saved

  • image_name (String)

    Name for the new image where the disk will be saved

  • image_type (String) (defaults to: "")

    Type of the new image. Set to empty string to use the default type

  • snap_id (Integer) (defaults to: -1))

    ID of the snapshot to save, -1 to use the

Returns:

  • (Integer, OpenNebula::Error)

    the new Image ID in case of success, error otherwise



500
501
502
503
504
505
506
507
508
509
510
# File 'lib/opennebula/virtual_machine.rb', line 500

def disk_saveas(disk_id, image_name, image_type="", snap_id=-1)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VM_METHODS[:disksaveas],
                      @pe_id,
                      disk_id,
                      image_name,
                      image_type,
                      snap_id)
    return rc
end

#disk_snapshot_create(disk_id, name) ⇒ Integer, OpenNebula::Error

Takes a new snapshot of a disk

Parameters:

  • disk_id (Integer)

    Id of the disk

  • name (String)

    description for the snapshot

Returns:



635
636
637
# File 'lib/opennebula/virtual_machine.rb', line 635

def disk_snapshot_create(disk_id, name)
  return call(VM_METHODS[:disksnapshotcreate], @pe_id, disk_id, name)
end

#disk_snapshot_delete(disk_id, snap_id) ⇒ nil, OpenNebula::Error

Deletes a disk snapshot

Parameters:

  • disk_id (Integer)

    Id of the disk

  • snap_id (Integer)

    Id of the snapshot

Returns:



657
658
659
# File 'lib/opennebula/virtual_machine.rb', line 657

def disk_snapshot_delete(disk_id, snap_id)
  return call(VM_METHODS[:disksnapshotdelete], @pe_id, disk_id, snap_id)
end

#disk_snapshot_rename(disk_id, snap_id, new_name) ⇒ nil, OpenNebula::Error

Renames a disk snapshot

Parameters:

  • disk_id (Integer)

    Id of the disk

  • snap_id (Integer)

    Id of the snapshot

  • new_name (String)

    New name for the snapshot

Returns:



669
670
671
# File 'lib/opennebula/virtual_machine.rb', line 669

def disk_snapshot_rename(disk_id, snap_id, new_name)
    return call(VM_METHODS[:disksnapshotrename], @pe_id, disk_id, snap_id, new_name)
end

#disk_snapshot_revert(disk_id, snap_id) ⇒ nil, OpenNebula::Error

Reverts disk state to a previously taken snapshot

Parameters:

  • disk_id (Integer)

    Id of the disk

  • snap_id (Integer)

    Id of the snapshot

Returns:



646
647
648
# File 'lib/opennebula/virtual_machine.rb', line 646

def disk_snapshot_revert(disk_id, snap_id)
  return call(VM_METHODS[:disksnapshotrevert], @pe_id, disk_id, snap_id)
end

#gidObject

Returns the group identifier

return

Integer the element’s group ID



764
765
766
# File 'lib/opennebula/virtual_machine.rb', line 764

def gid
    self['GID'].to_i
end

#holdObject

Sets a VM to hold state, scheduler will not deploy it



385
386
387
# File 'lib/opennebula/virtual_machine.rb', line 385

def hold
    action('hold')
end

#infoObject Also known as: info!

Retrieves the information of the given VirtualMachine.



280
281
282
# File 'lib/opennebula/virtual_machine.rb', line 280

def info()
    super(VM_METHODS[:info], 'VM')
end

#lcm_stateObject

Returns the LCM state of the VirtualMachine (numeric value)



742
743
744
# File 'lib/opennebula/virtual_machine.rb', line 742

def lcm_state
    self['LCM_STATE'].to_i
end

#lcm_state_strObject

Returns the LCM state of the VirtualMachine (string value)



747
748
749
# File 'lib/opennebula/virtual_machine.rb', line 747

def lcm_state_str
    LCM_STATE[lcm_state]
end

#live_migrate(host_id, enforce = false) ⇒ Object

Deprecated.

use #migrate instead



484
485
486
# File 'lib/opennebula/virtual_machine.rb', line 484

def live_migrate(host_id, enforce=false)
    migrate(host_id, true, enforce)
end

#lock(level) ⇒ Object

Lock a VM



718
719
720
# File 'lib/opennebula/virtual_machine.rb', line 718

def lock(level)
    return call(VM_METHODS[:lock], @pe_id, level)
end

#migrate(host_id, live = false, enforce = false, ds_id = -1,, mtype = 0) ⇒ nil, OpenNebula::Error

Moves a running VM to the specified host. With live=true the migration is done withdout downtime.

Parameters:

  • host_id (Interger)

    The host id (hid) of the target host where the VM will be migrated.

  • live (true|false) (defaults to: false)

    If true the migration is done without downtime. Defaults to false

  • enforce (true|false) (defaults to: 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

  • ds_id (Integer) (defaults to: -1,)

    The System Datastore where to migrate the VM. To use the current one, set it to -1

  • mtype (Integer) (defaults to: 0)

    How to perform the cold migration:

    • 0: save - restore,

    • 1: power off - boot

    • 2: power off hard - boot

Returns:



478
479
480
481
# File 'lib/opennebula/virtual_machine.rb', line 478

def migrate(host_id, live=false, enforce=false, ds_id=-1, mtype=0)
    call(VM_METHODS[:migrate], @pe_id, host_id.to_i, live==true,
        enforce, ds_id.to_i, mtype)
end

#monitoring(xpath_expressions) ⇒ Hash<String, Array<Array<int>>>, OpenNebula::Error

Retrieves this VM’s monitoring data from OpenNebula

Examples:

vm.monitoring( ['MONITORING/CPU', 'MONITORING/NETTX'] )

{
 "MONITORING/CPU"=>[["1435085098", "47"], ["1435085253", "5"],
   ["1435085410", "48"], ["1435085566", "3"], ["1435088136", "2"]],
 "MONITORING/NETTX"=>[["1435085098", "0"], ["1435085253", "50"],
   ["1435085410", "50"], ["1435085566", "50"], ["1435085723", "50"]]
}

Parameters:

  • xpath_expressions (Array<String>)

    Elements to retrieve.

Returns:

  • (Hash<String, Array<Array<int>>>, OpenNebula::Error)

    Hash with the requested xpath expressions, and an Array of ‘timestamp, value’.



572
573
574
575
# File 'lib/opennebula/virtual_machine.rb', line 572

def monitoring(xpath_expressions)
    return super(VM_METHODS[:monitoring], 'VM',
        'LAST_POLL', xpath_expressions)
end

#monitoring_xmlString

Retrieves this VM’s monitoring data from OpenNebula, in XML

Returns:

  • (String)

    VM monitoring data, in XML



580
581
582
583
584
# File 'lib/opennebula/virtual_machine.rb', line 580

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

Parameters:

  • nic_template (String)

    Template containing a NIC element

Returns:



436
437
438
# File 'lib/opennebula/virtual_machine.rb', line 436

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

Parameters:

  • nic_id (Integer)

    Id of the NIC to be detached

Returns:



445
446
447
# File 'lib/opennebula/virtual_machine.rb', line 445

def nic_detach(nic_id)
    return call(VM_METHODS[:detachnic], @pe_id, nic_id)
end

#poweroff(hard = false) ⇒ Object

Powers off a running VM



375
376
377
# File 'lib/opennebula/virtual_machine.rb', line 375

def poweroff(hard=false)
    action(hard ? 'poweroff-hard' : 'poweroff')
end

#reboot(hard = false) ⇒ Object

Reboots an already deployed VM



380
381
382
# File 'lib/opennebula/virtual_machine.rb', line 380

def reboot(hard=false)
    action(hard ? 'reboot-hard' : 'reboot')
end

#recover(result) ⇒ nil, OpenNebula::Error

Recovers an ACTIVE VM

retry (2), delete (3), delete-recreate (4), delete-db (5)

Parameters:

  • result (Integer)

    Recover with failure (0), success (1),

  • result (info)

    Additional information needed to recover the VM

Returns:



690
691
692
# File 'lib/opennebula/virtual_machine.rb', line 690

def recover(result)
    return call(VM_METHODS[:recover], @pe_id, result)
end

#releaseObject

Releases a VM from hold state



390
391
392
# File 'lib/opennebula/virtual_machine.rb', line 390

def release
    action('release')
end

#rename(name) ⇒ nil, OpenNebula::Error

Renames this VM

Parameters:

  • name (String)

    New name for the VM.

Returns:



592
593
594
# File 'lib/opennebula/virtual_machine.rb', line 592

def rename(name)
    return call(VM_METHODS[:rename], @pe_id, name)
end

#replace(opts = {}) ⇒ Object



331
332
333
# File 'lib/opennebula/virtual_machine.rb', line 331

def replace(opts = {})
    super(opts, "USER_TEMPLATE")
end

#reschedObject

Sets the re-scheduling flag for the VM



450
451
452
# File 'lib/opennebula/virtual_machine.rb', line 450

def resched
    action('resched')
end

#resize(capacity_template, enforce) ⇒ nil, OpenNebula::Error

Resize the VM

Parameters:

  • 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

  • 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

Returns:



523
524
525
# File 'lib/opennebula/virtual_machine.rb', line 523

def resize(capacity_template, enforce)
    return call(VM_METHODS[:resize], @pe_id, capacity_template, enforce)
end

#resumeObject

Resumes the execution of a saved VM



405
406
407
# File 'lib/opennebula/virtual_machine.rb', line 405

def resume
    action('resume')
end

#save_as_template(name, description, persistent = nil) ⇒ Object



791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/opennebula/virtual_machine.rb', line 791

def save_as_template(name,description, persistent=nil)
    img_ids = []
    new_tid = nil
    begin
        rc = info()
        raise if OpenNebula.is_error?(rc)

        tid = self['TEMPLATE/TEMPLATE_ID']
        if tid.nil? || tid.empty?
            rc = Error.new('VM has no template to be saved')
            raise
        end

        if state_str() != "POWEROFF"
            rc = Error.new("VM state must be POWEROFF, "<<
                "current state is #{state_str()}, #{lcm_state_str()}")
            raise
        end

        # Clone the source template
        rc = OpenNebula::Template.new_with_id(tid, @client).clone(name)
        raise if OpenNebula.is_error?(rc)

        new_tid = rc

        # Replace the original template's capacity with the actual VM values
        replace = ""
        if !description.nil?
            replace << "DESCRIPTION = #{description}\n"
        end
        cpu = self['TEMPLATE/CPU']
        if !cpu.nil? && !cpu.empty?
            replace << "CPU = #{cpu}\n"
        end

        vcpu = self['TEMPLATE/VCPU']
        if !vcpu.nil? && !vcpu.empty?
            replace << "VCPU = #{vcpu}\n"
        end

        mem = self['TEMPLATE/MEMORY']
        if !mem.nil? && !mem.empty?
            replace << "MEMORY = #{mem}\n"
        end

        self.each('TEMPLATE/DISK') do |disk|
            # While the previous snapshot is still in progress, we wait
            # indefinitely
            rc = info()
            raise if OpenNebula.is_error?(rc)

            steps = 0
            while lcm_state_str() == "HOTPLUG_SAVEAS_POWEROFF"
                if steps < 30
                    sleep 1
                else
                    sleep 15
                end

                rc = info()
                raise if OpenNebula.is_error?(rc)

                steps += 1
            end

            # If the VM is not busy with a previous disk snapshot, we wait
            # but this time with a timeout
            rc = wait_state("POWEROFF")
            raise if OpenNebula.is_error?(rc)

            disk_id = disk["DISK_ID"]
            if disk_id.nil? || disk_id.empty?
                rc = Error.new('The DISK_ID is missing from the VM template')
                raise
            end

            image_id           = disk["IMAGE_ID"]
            opennebula_managed = disk["OPENNEBULA_MANAGED"]
            type               = disk["TYPE"]

            REMOVE_IMAGE_ATTRS.each do |attr|
                disk.delete_element(attr)
            end

            if !image_id.nil? && !image_id.empty?
                if type == 'CDROM'
                    replace << "DISK = [ IMAGE_ID = #{image_id}"
                    if opennebula_managed
                        replace << ", OPENNEBULA_MANAGED=#{opennebula_managed}"
                    end
                    replace << " ]\n"
                else
                    rc = disk_saveas(disk_id.to_i,"#{name}-disk-#{disk_id}","",-1)

                    raise if OpenNebula.is_error?(rc)

                    if persistent == true
                        OpenNebula::Image.new_with_id(rc.to_i, @client).persistent()
                    end

                    img_ids << rc.to_i

                    disk_template = disk.template_like_str(".").tr("\n", ",\n")

                    if disk_template.empty?
                        replace << "DISK = [ IMAGE_ID = #{rc} ] \n"
                    else
                        replace << "DISK = [ IMAGE_ID = #{rc}, " <<
                            disk_template << " ] \n"
                    end
                end
            else
                # Volatile disks cannot be saved, so the definition is copied
                replace << self.template_like_str(
                    "TEMPLATE", true, "DISK[DISK_ID=#{disk_id}]") << "\n"
            end
        end

        self.each('TEMPLATE/NIC') do |nic|
            nic_id = nic["NIC_ID"]

            if nic_id.nil? || nic_id.empty?
                rc = Error.new('The NIC_ID is missing from the VM template')
                raise
            end

            REMOVE_VNET_ATTRS.each do |attr|
                nic.delete_element(attr)
            end

            replace << "NIC = [ " << nic.template_like_str(".").tr("\n", ",\n") << " ] \n"
        end

        # Required by the Sunstone Cloud View
        replace << "SAVED_TEMPLATE_ID = #{tid}\n"

        new_tmpl = OpenNebula::Template.new_with_id(new_tid, @client)

        rc = new_tmpl.update(replace, true)
        raise if OpenNebula.is_error?(rc)

        return new_tid

    rescue
        # Rollback. Delete the template and the images created
        if !new_tid.nil?
            new_tmpl = OpenNebula::Template.new_with_id(new_tid, @client)
            new_tmpl.delete()
        end

        img_ids.each do |id|
            img = OpenNebula::Image.new_with_id(id, @client)
            img.delete()
        end

        return rc
    end
end

#snapshot_create(name = "") ⇒ Integer, OpenNebula::Error

Creates a new VM snapshot

Parameters:

  • name (String) (defaults to: "")

    Name for the snapshot.

Returns:

  • (Integer, OpenNebula::Error)

    The new snaphost ID in case of success, Error otherwise



602
603
604
605
606
607
# File 'lib/opennebula/virtual_machine.rb', line 602

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

Parameters:

  • snap_id (Integer)

    Id of the snapshot

Returns:



625
626
627
# File 'lib/opennebula/virtual_machine.rb', line 625

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

Parameters:

  • snap_id (Integer)

    Id of the snapshot

Returns:



615
616
617
# File 'lib/opennebula/virtual_machine.rb', line 615

def snapshot_revert(snap_id)
    return call(VM_METHODS[:snapshotrevert], @pe_id, snap_id)
end

#stateObject

Returns the VM state of the VirtualMachine (numeric value)



732
733
734
# File 'lib/opennebula/virtual_machine.rb', line 732

def state
    self['STATE'].to_i
end

#state_strObject

Returns the VM state of the VirtualMachine (string value)



737
738
739
# File 'lib/opennebula/virtual_machine.rb', line 737

def state_str
    VM_STATE[state]
end

#statusObject

Returns the short status string for the VirtualMachine



752
753
754
755
756
757
758
759
760
# File 'lib/opennebula/virtual_machine.rb', line 752

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

#stopObject

Stops a running VM



395
396
397
# File 'lib/opennebula/virtual_machine.rb', line 395

def stop
    action('stop')
end

#suspendObject

Saves a running VM



400
401
402
# File 'lib/opennebula/virtual_machine.rb', line 400

def suspend
    action('suspend')
end

#terminate(hard = false) ⇒ Object Also known as: shutdown

Shutdowns an already deployed VM



363
364
365
# File 'lib/opennebula/virtual_machine.rb', line 363

def terminate(hard=false)
    action(hard ? 'terminate-hard' : 'terminate')
end

#undeploy(hard = false) ⇒ Object

Shuts down an already deployed VM, saving its state in the system DS



370
371
372
# File 'lib/opennebula/virtual_machine.rb', line 370

def undeploy(hard=false)
    action(hard ? 'undeploy-hard' : 'undeploy')
end

#unlockObject

Unlock a VM



723
724
725
# File 'lib/opennebula/virtual_machine.rb', line 723

def unlock()
    return call(VM_METHODS[:unlock], @pe_id)
end

#unreschedObject

Unsets the re-scheduling flag for the VM



455
456
457
# File 'lib/opennebula/virtual_machine.rb', line 455

def unresched
    action('unresched')
end

#update(new_template = nil, append = false) ⇒ nil, OpenNebula::Error

Replaces the template contents

Parameters:

  • new_template (String) (defaults to: nil)

    New template contents

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



307
308
309
# File 'lib/opennebula/virtual_machine.rb', line 307

def update(new_template=nil, append=false)
    super(VM_METHODS[:update], new_template, append ? 1 : 0)
end

#updateconf(new_conf) ⇒ nil, OpenNebula::Error

will replace the existing ones or delete it if empty. Attributes that

can be updated are: INPUT/{TYPE, BUS}; RAW/{TYPE, DATA, DATA_VMX},
OS/{BOOT, BOOTLOADER, ARCH, MACHINE, KERNEL, INITRD},
FEATURES/{ACPI, APIC, PAE, LOCALTIME, HYPERV, GUEST_AGENT},
and GRAPHICS/{TYPE, LISTEN, PASSWD, KEYMAP}

Returns:



713
714
715
# File 'lib/opennebula/virtual_machine.rb', line 713

def updateconf(new_conf)
    return call(VM_METHODS[:updateconf], @pe_id, new_conf)
end

#user_template_str(indent = true) ⇒ String

Returns the <USER_TEMPLATE> element in text form

Parameters:

  • indent (true, false) (defaults to: true)

    indents the resulting string, defaults to true

Returns:

  • (String)

    The USER_TEMPLATE



316
317
318
# File 'lib/opennebula/virtual_machine.rb', line 316

def user_template_str(indent=true)
    template_like_str('USER_TEMPLATE', indent)
end

#user_template_xmlString

Returns the <USER_TEMPLATE> element in XML form

Returns:

  • (String)

    The USER_TEMPLATE



323
324
325
326
327
328
329
# File 'lib/opennebula/virtual_machine.rb', line 323

def user_template_xml
    if NOKOGIRI
        @xml.xpath('USER_TEMPLATE').to_s
    else
        @xml.elements['USER_TEMPLATE'].to_s
    end
end

#wait_state(state, timeout = 10) ⇒ Object



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'lib/opennebula/virtual_machine.rb', line 950

def wait_state(state, timeout=10)
    vm_state = ""
    lcm_state = ""

    timeout.times do
        rc = info()
        return rc if OpenNebula.is_error?(rc)

        vm_state = state_str()
        lcm_state = lcm_state_str()

        if vm_state == state
            return true
        end

        sleep 1
    end

    return Error.new("Timeout expired for state #{state}. "<<
        "VM is in state #{vm_state}, #{lcm_state}")
end