Class: OpenNebula::VirtualMachine

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

Constant Summary

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)

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

Constructor Details

- (VirtualMachine) initialize(xml, client)

Class constructor



151
152
153
# File 'opennebula/virtual_machine.rb', line 151

def initialize(xml, client)
    super(xml,client)
end

Class Method Details

+ (Object) build_xml(pe_id = nil)

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 '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

+ (Object) get_history_action(action)



146
147
148
# File 'opennebula/virtual_machine.rb', line 146

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

+ (Object) get_reason(reason)



139
140
141
142
143
144
# File '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

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

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:



175
176
177
# File 'opennebula/virtual_machine.rb', line 175

def allocate(description, hold=false)
    super(VM_METHODS[:allocate], description, hold)
end

- (Object) boot Also known as: restart

Forces a re-deployment of a VM in UNKNOWN or BOOT state



336
337
338
# File 'opennebula/virtual_machine.rb', line 336

def boot
    action('boot')
end

- (Object) cancel

Deprecated.

use #shutdown



252
253
254
# File 'opennebula/virtual_machine.rb', line 252

def cancel
    shutdown(true)
end

- (nil, OpenNebula::Error) chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a)

Changes the permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change

Returns:



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

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

- (nil, OpenNebula::Error) chmod_octet(octet)

Changes the permissions.

Parameters:

  • octet (String)

    Permissions octed , e.g. 640

Returns:



439
440
441
# File 'opennebula/virtual_machine.rb', line 439

def chmod_octet(octet)
    super(VM_METHODS[:chmod], octet)
end

- (Object) chown(uid, gid)

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



430
431
432
# File 'opennebula/virtual_machine.rb', line 430

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

- (Object) delete(recreate = false)

Deletes a VM from the pool



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

def delete(recreate=false)
    if recreate
        action('delete-recreate')
    else
        action('delete')
    end
end

- (nil, OpenNebula::Error) deploy(host_id, enforce = false)

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

Returns:



222
223
224
# File 'opennebula/virtual_machine.rb', line 222

def deploy(host_id, enforce=false)
    return call(VM_METHODS[:deploy], @pe_id, host_id.to_i, enforce)
end

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

Attaches a disk to a running VM

Parameters:

  • disk_template (String)

    Template containing a DISK element

Returns:



286
287
288
# File 'opennebula/virtual_machine.rb', line 286

def disk_attach(disk_template)
    return call(VM_METHODS[:attach], @pe_id, disk_template)
end

- (nil, OpenNebula::Error) disk_detach(disk_id) Also known as: detachdisk

Detaches a disk from a running VM

Parameters:

  • disk_id (Integer)

    Id of the disk to be detached

Returns:



297
298
299
# File 'opennebula/virtual_machine.rb', line 297

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

- (Integer, OpenNebula::Error) disk_snapshot(disk_id, image_name, image_type = "", hot = false)

Set the specified vm's disk to be saved in a new image when the VirtualMachine shutdowns

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

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

    True to save the disk immediately, false will perform the operation when the VM shuts down

Returns:

  • (Integer, OpenNebula::Error)

    the new Image ID in case of success, error otherwise



393
394
395
396
397
398
399
400
401
402
403
404
# File 'opennebula/virtual_machine.rb', line 393

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

- (Object) finalize(recreate = false)

Deprecated.

use #delete instead



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

def finalize(recreate=false)
    delete(recreate)
end

- (Object) gid

Returns the group identifier

return

Integer the element's group ID



579
580
581
# File 'opennebula/virtual_machine.rb', line 579

def gid
    self['GID'].to_i
end

- (Object) hold

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



257
258
259
# File 'opennebula/virtual_machine.rb', line 257

def hold
    action('hold')
end

- (Object) info Also known as: info!

Retrieves the information of the given VirtualMachine.



160
161
162
# File 'opennebula/virtual_machine.rb', line 160

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

- (Object) lcm_state

Returns the LCM state of the VirtualMachine (numeric value)



557
558
559
# File 'opennebula/virtual_machine.rb', line 557

def lcm_state
    self['LCM_STATE'].to_i
end

- (Object) lcm_state_str

Returns the LCM state of the VirtualMachine (string value)



562
563
564
# File 'opennebula/virtual_machine.rb', line 562

def lcm_state_str
    LCM_STATE[lcm_state]
end

- (Object) live_migrate(host_id, enforce = false)

Deprecated.

use #migrate instead



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

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

- (nil, OpenNebula::Error) migrate(host_id, live = false, enforce = false)

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

Returns:



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

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

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

Retrieves this VM's monitoring data from OpenNebula

Examples:

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"]]
}

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



475
476
477
478
# File 'opennebula/virtual_machine.rb', line 475

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

- (String) monitoring_xml

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

Returns:

  • (String)

    VM monitoring data, in XML



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

def monitoring_xml()
    return Error.new('ID not defined') if !@pe_id

    return @client.call(VM_METHODS[:monitoring], @pe_id)
end

- (nil, OpenNebula::Error) nic_attach(nic_template)

Attaches a NIC to a running VM

Parameters:

  • nic_template (String)

    Template containing a NIC element

Returns:



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

def nic_attach(nic_template)
    return call(VM_METHODS[:attachnic], @pe_id, nic_template)
end

- (nil, OpenNebula::Error) nic_detach(nic_id)

Detaches a NIC from a running VM

Parameters:

  • nic_id (Integer)

    Id of the NIC to be detached

Returns:



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

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

- (Object) poweroff(hard = false)

Powers off a running VM



237
238
239
# File 'opennebula/virtual_machine.rb', line 237

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

- (Object) reboot(hard = false)

Reboots an already deployed VM



242
243
244
# File 'opennebula/virtual_machine.rb', line 242

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

- (nil, OpenNebula::Error) recover(result)

Recovers an ACTIVE VM

Parameters:

  • result (Boolean)

    Recover with success (true) or failure (false)

  • result (info)

    Additional information needed to recover the VM

Returns:



538
539
540
# File 'opennebula/virtual_machine.rb', line 538

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

- (Object) release

Releases a VM from hold state



262
263
264
# File 'opennebula/virtual_machine.rb', line 262

def release
    action('release')
end

- (nil, OpenNebula::Error) rename(name)

Renames this VM

Parameters:

  • name (String)

    New name for the VM.

Returns:



495
496
497
# File 'opennebula/virtual_machine.rb', line 495

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

- (Object) resched

Sets the re-scheduling flag for the VM



348
349
350
# File 'opennebula/virtual_machine.rb', line 348

def resched
    action('resched')
end

- (Object) reset

Deprecated.

use #reboot



247
248
249
# File 'opennebula/virtual_machine.rb', line 247

def reset
    reboot(true)
end

- (nil, OpenNebula::Error) resize(capacity_template, enforce)

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:



422
423
424
# File 'opennebula/virtual_machine.rb', line 422

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

- (Object) resubmit

Deprecated.

use #delete instead



343
344
345
# File 'opennebula/virtual_machine.rb', line 343

def resubmit
    action('delete-recreate')
end

- (Object) resume

Resumes the execution of a saved VM



277
278
279
# File 'opennebula/virtual_machine.rb', line 277

def resume
    action('resume')
end

- (Object) save_as(disk_id, image_name, image_type = "", hot = false)

Deprecated.


407
408
409
# File 'opennebula/virtual_machine.rb', line 407

def save_as(disk_id, image_name, image_type="", hot=false)
    return disk_snapshot(disk_id, image_name, image_type, hot)
end

- (Object) shutdown(hard = false)

Shutdowns an already deployed VM



227
228
229
# File 'opennebula/virtual_machine.rb', line 227

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

- (Integer, OpenNebula::Error) snapshot_create(name = "")

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



505
506
507
508
509
510
# File 'opennebula/virtual_machine.rb', line 505

def snapshot_create(name="")
    return Error.new('ID not defined') if !@pe_id

    name ||= ""
    return @client.call(VM_METHODS[:snapshotcreate], @pe_id, name)
end

- (nil, OpenNebula::Error) snapshot_delete(snap_id)

Deletes a VM snapshot

Parameters:

  • snap_id (Integer)

    Id of the snapshot

Returns:



528
529
530
# File 'opennebula/virtual_machine.rb', line 528

def snapshot_delete(snap_id)
    return call(VM_METHODS[:snapshotdelete], @pe_id, snap_id)
end

- (nil, OpenNebula::Error) snapshot_revert(snap_id)

Reverts to a snapshot

Parameters:

  • snap_id (Integer)

    Id of the snapshot

Returns:



518
519
520
# File 'opennebula/virtual_machine.rb', line 518

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

- (Object) state

Returns the VM state of the VirtualMachine (numeric value)



547
548
549
# File 'opennebula/virtual_machine.rb', line 547

def state
    self['STATE'].to_i
end

- (Object) state_str

Returns the VM state of the VirtualMachine (string value)



552
553
554
# File 'opennebula/virtual_machine.rb', line 552

def state_str
    VM_STATE[state]
end

- (Object) status

Returns the short status string for the VirtualMachine



567
568
569
570
571
572
573
574
575
# File 'opennebula/virtual_machine.rb', line 567

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

- (Object) stop

Stops a running VM



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

def stop
    action('stop')
end

- (Object) suspend

Saves a running VM



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

def suspend
    action('suspend')
end

- (Object) undeploy(hard = false)

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



232
233
234
# File 'opennebula/virtual_machine.rb', line 232

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

- (Object) unresched

Unsets the re-scheduling flag for the VM



353
354
355
# File 'opennebula/virtual_machine.rb', line 353

def unresched
    action('unresched')
end

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

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:



187
188
189
# File 'opennebula/virtual_machine.rb', line 187

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

- (String) user_template_str(indent = true)

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



196
197
198
# File 'opennebula/virtual_machine.rb', line 196

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

- (String) user_template_xml

Returns the <USER_TEMPLATE> element in XML form

Returns:

  • (String)

    The USER_TEMPLATE



203
204
205
206
207
208
209
# File '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