Class: OpenNebula::VirtualMachine

Inherits:
PoolElement show all
Defined in:
OpenNebula/VirtualMachine.rb

Constant Summary

VM_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"
}
VM_STATE =
%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED 
POWEROFF}
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 UNKNOWN HOTPLUG SHUTDOWN_POWEROFF
BOOT_UNKNOWN BOOT_POWEROFF BOOT_SUSPENDED BOOT_STOPPED}
SHORT_VM_STATES =
{
    "INIT"      => "init",
    "PENDING"   => "pend",
    "HOLD"      => "hold",
    "ACTIVE"    => "actv",
    "STOPPED"   => "stop",
    "SUSPENDED" => "susp",
    "DONE"      => "done",
    "FAILED"    => "fail",
    "POWEROFF"  => "poff"
}
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"       => "clea",
    "UNKNOWN"       => "unkn",
    "HOTPLUG"       => "hotp",
    "SHUTDOWN_POWEROFF" => "shut",
    "BOOT_UNKNOWN"  => "boot",
    "BOOT_POWEROFF" => "boot",
    "BOOT_SUSPENDED" => "boot",
    "BOOT_STOPPED"  => "boot"
}
MIGRATE_REASON =
%w{NONE ERROR STOP_RESUME USER CANCEL}
SHORT_MIGRATE_REASON =
{
    "NONE"          => "none",
    "ERROR"         => "erro",
    "STOP_RESUME"   => "stop",
    "USER"          => "user",
    "CANCEL"        => "canc"
}

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



121
122
123
# File 'OpenNebula/VirtualMachine.rb', line 121

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)


103
104
105
106
107
108
109
110
111
# File 'OpenNebula/VirtualMachine.rb', line 103

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_reason(reason)



113
114
115
116
117
118
# File 'OpenNebula/VirtualMachine.rb', line 113

def VirtualMachine.get_reason(reason)
    reason=MIGRATE_REASON[reason.to_i]
    reason_str=SHORT_MIGRATE_REASON[reason]

    reason_str
end

Instance Method Details

- (Object) allocate(description)

Allocates a new VirtualMachine in OpenNebula

description A string containing the template of the VirtualMachine.



137
138
139
# File 'OpenNebula/VirtualMachine.rb', line 137

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

- (Object) attachdisk(disk)

Attaches a disk to a running VM



205
206
207
208
209
210
211
212
# File 'OpenNebula/VirtualMachine.rb', line 205

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

    rc = @client.call(VM_METHODS[:attach], @pe_id, disk)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

- (Object) cancel

Cancels a running VM



175
176
177
# File 'OpenNebula/VirtualMachine.rb', line 175

def cancel
    action('cancel')
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:



314
315
316
317
318
# File 'OpenNebula/VirtualMachine.rb', line 314

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:



305
306
307
# File 'OpenNebula/VirtualMachine.rb', line 305

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



296
297
298
# File 'OpenNebula/VirtualMachine.rb', line 296

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

- (Object) deploy(host_id)

Initiates the instance of the VM on the target host.

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



145
146
147
148
149
150
151
152
# File 'OpenNebula/VirtualMachine.rb', line 145

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

    rc = @client.call(VM_METHODS[:deploy], @pe_id, host_id.to_i)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

- (Object) detachdisk(disk)

Detaches a disk from a running VM



215
216
217
218
219
220
221
222
# File 'OpenNebula/VirtualMachine.rb', line 215

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

    rc = @client.call(VM_METHODS[:detach], @pe_id, disk)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

- (Object) finalize

Deletes a VM from the pool



225
226
227
# File 'OpenNebula/VirtualMachine.rb', line 225

def finalize
    action('finalize')
end

- (Object) gid

Returns the group identifier

return

Integer the element's group ID



392
393
394
# File 'OpenNebula/VirtualMachine.rb', line 392

def gid
    self['GID'].to_i
end

- (Object) hold

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



180
181
182
# File 'OpenNebula/VirtualMachine.rb', line 180

def hold
    action('hold')
end

- (Object) info

Retrieves the information of the given VirtualMachine.



130
131
132
# File 'OpenNebula/VirtualMachine.rb', line 130

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

- (Object) lcm_state

Returns the LCM state of the VirtualMachine (numeric value)



370
371
372
# File 'OpenNebula/VirtualMachine.rb', line 370

def lcm_state
    self['LCM_STATE'].to_i
end

- (Object) lcm_state_str

Returns the LCM state of the VirtualMachine (string value)



375
376
377
# File 'OpenNebula/VirtualMachine.rb', line 375

def lcm_state_str
    LCM_STATE[lcm_state]
end

- (Object) live_migrate(host_id)

Migrates a running VM to another host without downtime



260
261
262
263
264
265
266
267
# File 'OpenNebula/VirtualMachine.rb', line 260

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

    rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, true)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

- (Object) migrate(host_id)

Saves a running VM and starts it again in the specified host



250
251
252
253
254
255
256
257
# File 'OpenNebula/VirtualMachine.rb', line 250

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

    rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, false)
    rc = nil if !OpenNebula.is_error?(rc)

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



341
342
343
344
# File 'OpenNebula/VirtualMachine.rb', line 341

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



349
350
351
352
353
# File 'OpenNebula/VirtualMachine.rb', line 349

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

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

- (Object) poweroff

Powers off a running VM



160
161
162
# File 'OpenNebula/VirtualMachine.rb', line 160

def poweroff
    action('poweroff')
end

- (Object) reboot

Reboots an already deployed VM



165
166
167
# File 'OpenNebula/VirtualMachine.rb', line 165

def reboot
    action('reboot')
end

- (Object) release

Releases a VM from hold state



185
186
187
# File 'OpenNebula/VirtualMachine.rb', line 185

def release
    action('release')
end

- (Object) resched

Sets the re-scheduling flag for the VM



240
241
242
# File 'OpenNebula/VirtualMachine.rb', line 240

def resched
    action('resched')
end

- (Object) reset

Resets an already deployed VM



170
171
172
# File 'OpenNebula/VirtualMachine.rb', line 170

def reset
    action('reset')
end

- (Object) restart

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



230
231
232
# File 'OpenNebula/VirtualMachine.rb', line 230

def restart
    action('restart')
end

- (Object) resubmit

Resubmits a VM to PENDING state



235
236
237
# File 'OpenNebula/VirtualMachine.rb', line 235

def resubmit
    action('resubmit')
end

- (Object) resume

Resumes the execution of a saved VM



200
201
202
# File 'OpenNebula/VirtualMachine.rb', line 200

def resume
    action('resume')
end

- (Integer, OpenNebula::Error) save_as(disk_id, image_name, image_type = "")

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

Returns:

  • (Integer, OpenNebula::Error)

    the new Image ID in case of success, error otherwise



280
281
282
283
284
285
286
287
288
289
290
# File 'OpenNebula/VirtualMachine.rb', line 280

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

    rc = @client.call(VM_METHODS[:savedisk],
                      @pe_id,
                      disk_id,
                      image_name,
                      image_type)

    return rc
end

- (Object) shutdown

Shutdowns an already deployed VM



155
156
157
# File 'OpenNebula/VirtualMachine.rb', line 155

def shutdown
    action('shutdown')
end

- (Object) state

Returns the VM state of the VirtualMachine (numeric value)



360
361
362
# File 'OpenNebula/VirtualMachine.rb', line 360

def state
    self['STATE'].to_i
end

- (Object) state_str

Returns the VM state of the VirtualMachine (string value)



365
366
367
# File 'OpenNebula/VirtualMachine.rb', line 365

def state_str
    VM_STATE[state]
end

- (Object) status

Returns the short status string for the VirtualMachine



380
381
382
383
384
385
386
387
388
# File 'OpenNebula/VirtualMachine.rb', line 380

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



190
191
192
# File 'OpenNebula/VirtualMachine.rb', line 190

def stop
    action('stop')
end

- (Object) suspend

Saves a running VM



195
196
197
# File 'OpenNebula/VirtualMachine.rb', line 195

def suspend
    action('suspend')
end

- (Object) unresched

Unsets the re-scheduling flag for the VM



245
246
247
# File 'OpenNebula/VirtualMachine.rb', line 245

def unresched
    action('unresched')
end